-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLWRPClientComms.py
executable file
·430 lines (347 loc) · 14.8 KB
/
LWRPClientComms.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
"""LWRP Client (Communication Class). An Open-Source Python Client for the Axia Livewire Routing Protocol."""
import socket
import time
import threading
__author__ = "Anthony Eden"
__copyright__ = "Copyright 2015-2018, Anthony Eden / Media Realm"
__credits__ = ["Anthony Eden"]
__license__ = "GPL"
__version__ = "0.6"
class LWRPClientComms(threading.Thread):
"""This class handles all the communications with the LWRP server."""
def __init__(self, host, port):
"""Create a socket connection to the LWRP server."""
# The handle for the socket connection to the LWRP server
self.sock = None
# A list of all commands to send to the LWRP server
self.sendQueue = []
# A list of data types to subscribe to (with callbacks)
self.dataSubscriptions = []
# Should we be shutting down this thread? Set via self.stop()
self._stop = False
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
self.sock.setblocking(0)
# Start the thread
threading.Thread.__init__(self)
def stop(self):
"""Attempt to close this thread."""
self._stop = True
def run(self):
"""Method keeps running forever, and handles all the communication with the open LWRP socket."""
while True:
# Try and receive data from the LWRP server
recvData = self.recvUntilNewline()
if recvData is not None:
self.processReceivedData(recvData)
# Check if we've got data to send back to the LWRP server
if len(self.sendQueue) > 0:
dataToSend = self.sendQueue[0]
while dataToSend:
sent = self.sock.send(dataToSend)
dataToSend = dataToSend[sent:]
# Once the message has been sent, take it out of the queue
self.sendQueue.pop(0)
if self._stop is True:
# End the thread
self.sock.close()
break
# Lower this number to receive data quicker
if len(self.sendQueue) == 0:
time.sleep(0.1)
def recvUntilNewline(self):
"""Receive data until we get to the end of a message (also accounts for BEGIN/END blocks)."""
totalData = ""
inBlock = False
while True:
try:
totalData += self.sock.recv(1024)
except:
pass
# Check if we're in a data block
if totalData[:5] == "BEGIN":
inBlock = True
# Check if the datablock is over
if "END" in totalData[-5:]:
return totalData
# If we're not in a datablock and a newline is found, return the data
if "\n" in totalData and inBlock is False:
return totalData
# We return 'None' if there's no data to return
if totalData == "":
return None
def processReceivedData(self, recvData):
"""Process the received data from the LWRP server. Attempts to parse it and trigger all the subscribed callbacks."""
# A dict with all the different message types we've received
messageTypes = {}
# Parse the data so it's in a usable format
# We receive a list in return (one per message - for blocks of data)
parsedData = self.parseMessage(recvData)
# Enumerate over all the messages
for dataIndex, data in enumerate(parsedData):
# Check if messageTypes already contains a list for this type.
# If not, create one
if parsedData[dataIndex]['type'] not in messageTypes:
messageTypes[parsedData[dataIndex]['type']] = []
# Add this message to the appropriate messageTypes list
messageTypes[parsedData[dataIndex]['type']].append(parsedData[dataIndex])
# Loop over every subscription
for subI, subX in enumerate(self.dataSubscriptions):
# If the subscribed command type matches the message's command type
if subX['commandType'] in messageTypes:
# Execute the callback!
subX['callback'](messageTypes[subX['commandType']])
# Check if we need to decrement the limit
if self.dataSubscriptions[subI]['limit'] is not False:
self.dataSubscriptions[subI]['limit'] = self.dataSubscriptions[subI]['limit'] - 1
# Check if we need to remove this subscription
if self.dataSubscriptions[subI]['limit'] <= 0 and self.dataSubscriptions[subI]['limit'] is not False:
self.dataSubscriptions.pop(subI)
def sendCommand(self, msg):
"""Buffer a command to send."""
self.sendQueue.append(msg + "\n")
def addSubscription(self, subType, callbackObj, limit=False, filters={}):
"""Add a subscription to the list of data subscriptions."""
self.dataSubscriptions.append({
"commandType": subType,
"callback": callbackObj,
"limit": limit
})
def splitSegments(self, string):
"""Attempt to parse all the segments provided in return data."""
segments = []
currentText = ""
inSubStr = False
string += " "
for char in string:
if char == " " and inSubStr is False:
# Finish the segment
segments.append(currentText)
currentText = ""
else:
# Continue the segment
if char == '"' and inSubStr is False:
inSubStr = True
elif char == '"' and inSubStr is True:
inSubStr = False
else:
currentText += char
return segments
def parseMessage(self, data):
"""Parse the messages and put them into a list of dictionaries."""
allData = []
for x in data.splitlines():
data = {}
if x[:3] == "VER":
segments = self.splitSegments(x[4:])
data['type'] = "DEVICE"
data["attributes"] = self.parseAttributes(segments)
elif x[:2] == "IP":
segments = self.splitSegments(x[3:])
data['type'] = "NETWORK"
data["attributes"] = self.parseAttributes(segments)
elif x[:3] == "SET":
segments = self.splitSegments(x[4:])
data['type'] = "SET"
data["attributes"] = self.parseAttributes(segments)
elif x[:3] == "SRC":
segments = self.splitSegments(x[4:])
data['type'] = "SOURCE"
data["num"] = segments[0]
data["attributes"] = self.parseAttributes(segments[1:])
elif x[:3] == "DST":
segments = self.splitSegments(x[4:])
data['type'] = "DESTINATION"
data["num"] = segments[0]
data["attributes"] = self.parseAttributes(segments[1:])
elif x[:3] == "MTR":
segments = self.splitSegments(x[4:])
data["type"] = "METER"
if segments[0] == "ICH":
data["io"] = "in"
elif segments[0] == "OCH":
data["io"] = "out"
else:
data["io"] = "unknown"
data["num"] = segments[1]
data["attributes"] = self.parseAttributes(segments[2:])
elif x[:3] == "LVL":
segments = self.splitSegments(x[4:])
data["type"] = "LEVEL_ALERT"
if segments[0] == "ICH":
data["io"] = "in"
elif segments[0] == "OCH":
data["io"] = "out"
else:
data["io"] = "unknown"
data["num"] = segments[1].split(".")[0]
data["side"] = segments[1].split(".")[1]
data["attributes"] = self.parseAttributes(segments[2:])
elif x[:3] == "GPI":
segments = self.splitSegments(x[4:])
data["type"] = "GPI"
data["num"] = segments[0]
if "CMD:" in x:
# We have a text command
data["attributes"] = self.parseAttributes(segments[1:])
else:
data["pin_states"] = self.parseGPIOStates(segments[1])
elif x[:3] == "GPO":
segments = self.splitSegments(x[4:])
data["type"] = "GPO"
data["num"] = segments[0]
if "CMD:" in x:
# We have a text command
data["attributes"] = self.parseAttributes(segments[1:])
else:
data["pin_states"] = self.parseGPIOStates(segments[1])
elif x[:3] == "MIX":
segments = self.splitSegments(x[4:])
data["type"] = "MATRIX"
data["dst"] = int(segments[0])
data["src"] = []
for point in segments[1:]:
point = point.split(":")
if len(point) >= 2 and point[0] != "" and point[1] != "-":
data["src"].append({
"num": int(point[0]),
"level": int(point[1]),
})
if x[:5] == "ERROR":
data['type'] = "ERROR"
data["message"] = x[6:]
elif x[:5] == "BEGIN":
pass
elif x[:3] == "END":
pass
if data != {}:
allData.append(data)
return allData
def parseAttributes(self, sections):
"""Parse all known attributes for a command and return in a dictionary."""
attrs = {}
for i, x in enumerate(sections):
if x[:4] == "PEEK":
# Peak level meters
# TODO: Convert to a proper format
attrs["PEAK_L"] = x[5:].split(":")[0]
attrs["PEAK_R"] = x[5:].split(":")[1]
elif x[:3] == "RMS":
# RMS level meters
# TODO: Convert to a proper format
attrs["RMS_L"] = x[4:].split(":")[0]
attrs["RMS_R"] = x[4:].split(":")[1]
elif x[:4] == "LWRP":
attrs["protocol_version"] = x[5:]
elif x[:4] == "DEVN":
attrs["device_name"] = x[5:]
elif x[:4] == "SYSV":
attrs["system_version"] = x[5:]
elif x[:4] == "NSRC":
# only parse source type if available
if '/' in x[5:]:
attrs["source_count"] = x[5:].split("/")[0]
attrs["source_type"] = x[5:].split("/")[1]
else:
attrs["source_count"] = x[5:]
attrs["source_type"] = ''
elif x[:4] == "NDST":
attrs["destination_count"] = x[5:]
elif x[:4] == "NGPI":
attrs["GPI_count"] = x[5:]
elif x[:4] == "NGPO":
attrs["GPO_count"] = x[5:]
elif x == "MIXCFG:1":
attrs["matrix_enabled"] = True
elif x == "MIXCFG:0":
attrs["matrix_enabled"] = False
elif x[:3] == "MIX":
attrs["matrix_channels"] = x[4:]
elif x[:7] == "address":
attrs["ip_address"] = sections[i + 1]
elif x[:7] == "netmask":
attrs["ip_netmask"] = sections[i + 1]
elif x[:7] == "gateway":
attrs["ip_gateway"] = sections[i + 1]
elif x[:8] == "hostname":
attrs["ip_hostname"] = sections[i + 1]
elif x[:4] == "ADIP":
attrs["advertisment_ipaddress"] = x[5:]
elif x[:10] == "IPCLK_ADDR":
attrs["clock_ipaddress"] = x[11:]
elif x[:10] == "NIC_IPADDR":
attrs["nic_ipaddress"] = x[11:]
elif x[:8] == "NIC_NAME":
attrs["nic_name"] = x[9:]
elif x[:4] == "PSNM":
attrs["name"] = x[5:]
elif x[:4] == "LWSE":
if x[5:] == "1":
attrs["livestream"] = True
else:
attrs["livestream"] = False
elif x[:4] == "LWSA":
attrs["livestream_destination"] = x[5:]
elif x[:4] == "RTPE":
if x[5:] == "1":
attrs["rtp"] = True
else:
attrs["rtp"] = False
elif x[:4] == "RTPA":
attrs["rtp_destination"] = x[5:]
elif x[:4] == "PSNM":
# Unknown attribute
attrs["_PSNM"] = x[5:]
elif x[:4] == "SHAB":
# Unknown attribute
attrs["_SHAB"] = x[5:]
elif x[:4] == "FASM":
# Unknown attribute
attrs["_FASM"] = x[5:]
elif x[:4] == "BSID":
# Unknown attribute
attrs["_BSID"] = x[5:]
elif x[:4] == "LPID":
# Unknown attribute
attrs["_LPID"] = x[5:]
elif x[:4] == "INGN":
# Unknown attribute
attrs["_INGN"] = x[5:]
elif x[:4] == "ADDR":
if x[5:12] == "0.0.0.0" or x[5:] == "":
attrs["address"] = None
elif " " in x[5:]:
# Sometimes other data is provides in this field after the actual address
# Discard that extra info and just return the address
attrs['address'] = x[5:].split(" ")[0]
else:
attrs["address"] = x[5:]
elif x[:4] == "NAME":
attrs["name"] = x[5:]
elif x[:4] == "CLIP":
attrs["clip"] = True
elif x[:7] == "NO-CLIP":
attrs["clip"] = False
elif x[:3] == "LOW":
attrs["silence"] = True
elif x[:6] == "NO-LOW":
attrs["silence"] = False
elif x[:3] == "CMD":
attrs["command_text"] = x[4:]
return attrs
def parseGPIOStates(self, states):
"""Turn the 'hlHLh' GPIO state strings into a dictionary."""
attrs = []
for x in states:
if x == "h":
data = {"state": "high", "changing": False}
elif x == "H":
data = {"state": "high", "changing": True}
elif x == "l":
data = {"state": "low", "changing": False}
elif x == "L":
data = {"state": "low", "changing": True}
else:
data = {}
attrs.append(data)
return attrs