-
Notifications
You must be signed in to change notification settings - Fork 2
/
Telemetry.py
206 lines (152 loc) · 5.67 KB
/
Telemetry.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import time
import socket
import select
import struct
import pickle
import threading
import enum
import cv2
class DataType(enum.Enum):
TELEM_RGB_IMAGE = 0
TELEM_DEPTH_FRAME = 1
TELEM_POSITION = 2
TELEM_STATUS = 3
class airTelemetry():
def __init__(self, hostname=''):
self.telemInterface = tcpInterface(hostname=hostname)
def start(self):
self.telemThread = threading.Thread(target=self.telemInterface.startServer, name='Air_Telemetry')
self.telemThread.daemon = True
self.telemThread.start()
def stop(self):
self.telemInterface.close()
def connected(self):
if self.telemThread.isAlive() or not self.telemInterface.running:
return False
return True
def sendImage(self, name, img):
encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), 50]
data = cv2.imencode('.jpg', img, encode_param)[1]
self.sendData(name, data)
def sendData(self, name, data):
if not self.connected():
return
try:
self.telemInterface.sendData((name, data))
# Start listen for new connections if socket closed
except (BrokenPipeError, ConnectionResetError):
self.telemInterface.close()
self.start()
class tcpInterface():
_PORT = 50006
_MAX_READ_LEN = 60000
_TIMEOUT = 1
def __init__(self, hostname=''):
self.hostname = hostname
self.sockObj = None
self.conn = None
self.running = False
pass
def _createSocket(self):
self.sockObj = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sockObj.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sockObj.settimeout(2)
def startServer(self):
self.addr = (self.hostname, self._PORT)
self._createSocket()
self.sockObj.bind(self.addr)
self.sockObj.listen()
while True:
# Wait for incoming connection
try:
self.conn, addr = self.sockObj.accept()
print('Connected to: {}'.format(addr))
except (BlockingIOError, socket.timeout):
print('*** Waiting for connection ***')
time.sleep(1)
else:
self.running = True
return
def close(self):
if self.sockObj is not None:
self.sockObj.close()
if self.conn is not None:
self.conn.close()
print('*** Connection Closed ***')
def startClient(self):
self.addr = (self.hostname, self._PORT)
while True:
try:
self._createSocket()
self.sockObj.connect(self.addr)
except (ConnectionRefusedError, socket.timeout):
print('*** [61] Connection Refused ***')
time.sleep(1)
else:
self.running = True
print( '*** Telemetry Connected ***')
return
def sendData(self, data):
byteData = pickle.dumps(data, protocol=pickle.HIGHEST_PROTOCOL)
self.sendByteData(byteData)
def sendByteData(self, byteData):
try:
writable = select.select([], [self.conn], [], self._TIMEOUT)[1]
for conn in writable:
msg = b'$' + struct.pack('>I', len(byteData)) + b':' + byteData
conn.sendall(msg)
except ValueError as e:
if self.conn.fileno() == -1:
raise BrokenPipeError
else:
print(e)
except BrokenPipeError:
self.conn.close()
def readMsg(self):
try:
readable = select.select([self.sockObj], [], [], self._TIMEOUT)[0]
for conn in readable:
# Syncronize stream
msg = conn.recv(1)
while msg != b'$':
msg = conn.recv(1)
if msg == b'':
return None
# Get message length
msg_len = struct.unpack('>I', conn.recv(4))[0]
# Wait for all data to be returned
if conn.recv(1) == b':':
msg = b''
while len(msg) < msg_len:
bytesIn = msg_len - len(msg)
if bytesIn > self._MAX_READ_LEN:
bytesIn = self._MAX_READ_LEN
try:
msg += conn.recv(bytesIn)
except BlockingIOError:
time.sleep(0.1)
data = pickle.loads(msg)
return data
except (ValueError):
if self.sockObj.fileno() == -1:
raise BrokenPipeError
except (ConnectionResetError, socket.timeout):
print( '*** Socket Closed ***' )
self.close()
return None
#if __name__ == "__main__":
# import threading
# remoteTelem = airTelemetry()
# remoteTelem.start()
# while not remoteTelem.connected():
# time.sleep(1)
# testImage = cv2.imread('/Users/freddiesherratt/Desktop/ERL_SmartCities_2019/modules/test.jpg', 0)
# totalTime = 0
# loops = 5
# for i in range(loops):
# print(i+1)
# startTime = time.time()
# remoteTelem.sendImage(testImage)
# totalTime += time.time() - startTime
# print(totalTime/loops)
# remoteTelem.stop()