-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
161 lines (127 loc) · 5.1 KB
/
client.py
File metadata and controls
161 lines (127 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#
# Copyright (c) 2021, Motion Workshop
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
import asyncio
import struct
import xml.etree.ElementTree
#
# Message format is an unsigned integer in network order that defines the
# length of the payload followed by the binary payload. The data stream is
# a sequence of messages.
# [N] [N bytes]
# [unsigned] [bytes...]
#
class Stream:
def __init__(self, reader, writer):
self.__reader = reader
self.__writer = writer
self.__name_map = None
async def read_message(self, timeout=None):
message = await read_message(self.__reader, timeout)
if is_metadata(message):
self.__name_map = parse_metadata(message)
message = await read_message(self.__reader, timeout)
return message
async def write_message(self, timeout=None):
return await write_message(self.__writer, timeout)
def get_name_map(self):
return self.__name_map
async def open_connection(host='127.0.0.1', port=32076):
reader, writer = await asyncio.open_connection(host=host, port=32076)
message = await read_message(reader, 1)
if not is_metadata(message):
raise RuntimeError('unknown data stream format')
return Stream(reader, writer)
async def read_message(reader, timeout=None):
"""Read one message from the Shadow data stream
Keyword arguments:
reader -- asyncio.StreamReader connected the Shadow data stream
timeout -- wait for timeout seconds for the message to arrive, set to None
to block until the stream read completes
Returns bytes object
"""
header = await asyncio.wait_for(reader.readexactly(4), timeout)
length = int.from_bytes(header, byteorder='big')
return await asyncio.wait_for(reader.readexactly(length), timeout)
async def write_message(writer, message, timeout=None):
"""Write one message to the Shadow data stream
writer -- asyncio.StreamWriter connected the Shadow data stream
message -- bytes to write to the stream
timeout -- wait for timeout seconds for the message to send, set to None to
block until the stream write completes
"""
length = len(message)
header = int(length).to_bytes(4, byteorder='big')
writer.write(header)
writer.write(message)
await asyncio.wait_for(writer.drain(), timeout)
def is_metadata(message):
"""Returns true iff the Shadow data stream message contains metadata rather
than a measurement sample
"""
return message.startswith(b'<?xml')
def parse_metadata(message):
#
# Convert a list of channel names to a map from integer key to string name.
#
# Parse an XML message string from a MotionSDK.Client stream. The first
# message is always a flat list of node names and integer key pairs.
#
# parse_name_map('<node key="1" id="Name"/>...') -> {1: "Name", ...}
#
# Use the key to look up the node name for every sample of measurement data
# until we receive a new XML message.
#
tree = xml.etree.ElementTree.fromstring(message)
# <node key="N" id="Name"> ... </node>
items = tree.findall('node')
return {
int(item.get('key')): item.get('id')
for item in items
}
def unpack_sample(message):
"""Convert a binary message from the Shadow data stream into a Python dict
message -- bytes from the data stream
Returns dict object
"""
#
# Format of one sample from the configurable data service. All data is
# little endian and 4 bytes (unsigned, float C types).
#
# node =
# [key] [N] [N values]
# [unsigned] [unsigned] [float, ...]
#
# message = [node, ...]
#
sample = {}
i = 0
while i < len(message):
(key, length) = struct.unpack_from('<2I', message, i)
i += 2 * 4
sample[key] = struct.unpack_from(f'<{length}f', message, i)
i += length * 4
return sample