-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathLWRPClient.py
executable file
·264 lines (199 loc) · 8.86 KB
/
LWRPClient.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
"""LWRP Client. An Open-Source Python Client for the Axia Livewire Routing Protocol."""
import time
from LWRPClientComms import LWRPClientComms
__author__ = "Anthony Eden"
__copyright__ = "Copyright 2015-2018, Anthony Eden / Media Realm"
__credits__ = ["Anthony Eden"]
__license__ = "GPL"
__version__ = "0.6"
class LWRPClient():
"""Provides a friendly API for the Livewire Routing Protocol."""
def __init__(self, host, port):
"""Init LWRP connection."""
# This is our access to the LWRP
self.LWRP = None
# This variable gets given the callback data, ready to be processed by a waiting function
self.waitingForCallback = False
self.callbackData = None
self.LWRP = LWRPClientComms(host, port)
self.LWRP.start()
def stop(self):
"""Close LWRP connection."""
self.LWRP.stop()
def waitForCallback(self, timeout=5):
"""Wait for data to be returned from the Comms class."""
# The time we need to stop waiting for data and return it
waitTimeout = time.time() + timeout
while True:
if self.waitingForCallback is False or waitTimeout <= time.time():
returnData = self.callbackData
self.callbackData = None
return returnData
else:
time.sleep(0.1)
def genericCallback(self, data):
"""Generic callback receiving function."""
self.callbackData = data
self.waitingForCallback = False
def login(self, password=None):
"""Login to the device/server. Required for non-info commands."""
if password is not None:
self.LWRP.sendCommand("LOGIN " + password)
else:
self.LWRP.sendCommand("LOGIN")
def errorSub(self, callback):
"""Subscribe to error messages."""
self.LWRP.addSubscription("ERROR", callback, False)
def deviceData(self):
"""Get core data about the device/server."""
self.LWRP.addSubscription("DEVICE", self.genericCallback, 1)
self.LWRP.sendCommand("VER")
self.waitingForCallback = True
return self.waitForCallback()
def networkData(self):
"""Get networking data about the device/server."""
self.LWRP.addSubscription("NETWORK", self.genericCallback, 1)
self.LWRP.sendCommand("IP")
self.waitingForCallback = True
data1 = self.waitForCallback()
# Some extra data is available via the 'SET' command. Find this and append it to the NETWORK data.
self.LWRP.addSubscription("SET", self.genericCallback, 1)
self.LWRP.sendCommand("SET")
self.waitingForCallback = True
data2 = self.waitForCallback()
data1[0]['attributes'].update(data2[0]['attributes'])
return data1
def sourceData(self):
"""Get current audio source data."""
self.LWRP.addSubscription("SOURCE", self.genericCallback, 1)
self.LWRP.sendCommand("SRC")
self.waitingForCallback = True
return self.waitForCallback()
def sourceDataSub(self, callback):
"""Subscribe to audio source data updates."""
self.LWRP.addSubscription("SOURCE", callback, False)
self.LWRP.sendCommand("SRC")
def destinationData(self):
"""Get current audio destination data."""
self.LWRP.addSubscription("DESTINATION", self.genericCallback, 1)
self.LWRP.sendCommand("DST")
self.waitingForCallback = True
return self.waitForCallback()
def destinationDataSub(self, callback):
"""Subscribe to audio destination data updates."""
self.LWRP.addSubscription("DESTINATION", callback, False)
self.LWRP.sendCommand("DST")
def meterData(self):
"""Get the current audio level meter data."""
self.LWRP.addSubscription("METER", self.genericCallback, 1)
self.LWRP.sendCommand("MTR")
self.waitingForCallback = True
return self.waitForCallback()
def setSource(self, chnum, multicast_addr):
"""Set the source address for a specified channel"""
self.LWRP.sendCommand("SRC " + str(chnum) + " RTPA:" + str(multicast_addr))
def setDestination(self, chnum, multicast_addr):
"""Set the output address for a specified channel"""
self.LWRP.sendCommand("DST " + str(chnum) + " ADDR:" + str(multicast_addr))
def setSilenceThreshold(self, io, chnum, threshold, timems):
"""Set a silence threshold and time for a specific I/O channel."""
if io == "in":
ioch = "ICH"
elif io == "out":
ioch = "OCH"
else:
raise ValueError("IO Direction set incorrectly. Use 'in' or 'out'.")
chnum = str(int(chnum))
threshold = str(int(threshold))
timems = str(int(timems))
self.LWRP.addSubscription("LEVEL_ALERT", self.genericCallback, 1)
self.LWRP.sendCommand("LVL " + ioch + " " + chnum + " LOW.LEVEL:" + threshold + " LOW.TIME:" + timems)
self.waitingForCallback = True
return self.waitForCallback()
def setClippingThreshold(self, io, chnum, threshold, timems):
"""Set a clipping threshold and time for a specific I/O channel."""
if io == "in":
ioch = "ICH"
elif io == "out":
ioch = "OCH"
else:
raise ValueError("IO Direction set incorrectly. Use 'in' or 'out'.")
chnum = str(int(chnum))
threshold = str(int(threshold))
timems = str(int(timems))
self.LWRP.addSubscription("LEVEL_ALERT", self.genericCallback, 1)
self.LWRP.sendCommand("LVL " + ioch + " " + chnum + " CLIP.LEVEL:" + threshold + " CLIP.TIME:" + timems)
self.waitingForCallback = True
return self.waitForCallback()
def levelAlertSub(self, callback):
"""Subscribe to Level Alerts (Silence & Clipping detection)."""
self.LWRP.addSubscription("LEVEL_ALERT", callback, False)
def GPIData(self):
"""Get current GPI state data."""
self.LWRP.addSubscription("GPI", self.genericCallback, 1)
self.LWRP.sendCommand("ADD GPI")
self.waitingForCallback = True
return self.waitForCallback()
def GPIDataSub(self, callback):
"""Subscribe to GPI data updates."""
self.LWRP.addSubscription("GPI", callback, False)
self.LWRP.sendCommand("ADD GPI")
def GPOData(self):
"""Get current GPO state data."""
self.LWRP.addSubscription("GPO", self.genericCallback, 1)
self.LWRP.sendCommand("ADD GPO")
self.waitingForCallback = True
return self.waitForCallback()
def GPODataSub(self, callback):
"""Subscribe to GPO data updates."""
self.LWRP.addSubscription("GPO", callback, False)
self.LWRP.sendCommand("ADD GPO")
def setGPO(self, chnum, pin, state, type = "GPO"):
"""Set the GPO pin state for a specific channel."""
chnum = str(int(chnum))
pinstr = ""
if state == "low":
state = "l"
elif state == "high":
state = "h"
else:
raise ValueError("Incorrect pin state specified")
# Build the pin state string (e.g. xxlxx will make pin 3 low)
for i in range(1, 6):
if i == pin:
pinstr += state
else:
pinstr += "x"
self.LWRP.sendCommand(type + " " + chnum + " " + pinstr)
def setGPI(self, chnum, pin, state):
"""Set the GPI pin state for a specific channel."""
self.setGPO(chnum, pin, state, "GPI")
def setGPIText(self, chnum, commandText):
"""Set the GPI text command for a specific channel."""
chnum = str(chnum)
commandText = str(commandText).replace('"', '\"')[:128]
self.LWRP.sendCommand("GPI " + chnum + " CMD:\"" + commandText + "\"")
def setGPOText(self, chnum, commandText):
"""Set the GPO text command for a specific channel."""
chnum = str(chnum)
commandText = str(commandText).replace('"', '\"')[:128]
self.LWRP.sendCommand("GPO " + chnum + " CMD:\"" + commandText + "\"")
def matrixSub(self, callback):
"""Subscribe to matrix changes."""
self.LWRP.addSubscription("MATRIX", callback, False)
self.LWRP.sendCommand("MIX")
def matrixSet(self, dstchnum, srcchnum, srclevel):
"""Sets a matrix mix point for a specific destination channel."""
chnum = str(int(dstchnum))
if srclevel != "-":
srclevel = str(int(srclevel))
if isinstance(srcchnum, list):
changes = ""
for ch in srcchnum:
changes += str(int(ch)) + ":" + srclevel + " "
else:
changes = str(int(srcchnum)) + ":" + srclevel
self.LWRP.sendCommand("MIX " + chnum + " " + changes)
def matrixRelease(self, dstchnum, srcchnum):
""" Releases a matrix mix point. """
self.matrixSet(dstchnum, srcchnum, "-")