-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGMA2.py
239 lines (166 loc) · 6.49 KB
/
GMA2.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
from enum import Enum
from datetime import datetime
import json
from GMA2Executor import GMA2Executor
from GMA2Executor import MSCCommand
from GMA2State import GMA2State
class MSCCommandFormat(Enum):
GeneralLighting = 1
MovingLights = 2
All = 127
class GMA2():
## Tested
def __init__(self, logger):
self.deviceID = '00'
self.deviceState = GMA2State()
self.logger = logger
pass
def processHexArray(self, hexArray):
hexArray = [x.lower() for x in hexArray]
if not self.validateHexArray(hexArray): return False
self.deviceID = hexArray[2]
commandFormat = MSCCommandFormat(int(hexArray[4],16))
if int(hexArray[5],16) in MSCCommand._value2member_map_:
commandType = MSCCommand(int(hexArray[5],16))
else:
return False
data = hexArray[6:-1]
return self.processData(commandType, data)
# Tested
def validateHexArray(self, hexArray):
hexArray = [x.lower() for x in hexArray]
# Something about the length
# Assume minimum length of array is 8, with 1 octet for data
if (len(hexArray) < 8): return False
# First octet must be 'F0'
if (hexArray[0] != 'f0'): return False
# Second octet must be '7F'
if (hexArray[1] != '7f'): return False
# Fourth octet must be '02'
if (hexArray[3] != '02'): return False
# Fifth octet is the command format
# Sixth octed is the command type
# Data validation
# Last byte must be 'F7'
if (hexArray[-1] != 'f7'): return False
return True
def processData(self, commandType, data):
match commandType:
case MSCCommand.Go:
return self.processGo(data)
case MSCCommand.Stop:
self.logger.warn("Stop command - not implemented")
return
case MSCCommand.Resume:
self.logger.warn("Resume command - not implemented")
pass
case MSCCommand.Timed_Go:
self.logger.warn("Timed Go command - not implemented")
pass
case MSCCommand.Set:
return self.processSet(data)
pass
case MSCCommand.Fire:
self.logger.warn("Fire command - not implemented")
pass
case MSCCommand.Go_Off:
self.logger.warn("Go Off command - not implemented")
pass
return False
def processSet(self, data):
if len(data) != 4: return False
fader = -1
page = int(data[1], 16)
executor = int(data[0], 16) + 1
coarse = int(data[3], 16)
fine = int(data[2], 16)
fader = (coarse + fine) / 128.0
gma2Executor = GMA2Executor(executor)
gma2Executor.fader = fader
gma2Executor.lastCommand = MSCCommand.Set
self.deviceState.updateExecutor(page, gma2Executor)
return True
def processTimedGo(self, data):
return False
return True
def processGo(self, data):
# Data will either be 6 or 10 octets long for a valid go
# TODO check this assumption is valid
if (len(data) != 9) and (len(data) != 11): return False
cue = -1
page = -1
executor = -1
if len(data) == 6:
cue = self.getCueNumber(data)
page, executor = "0"
else:
if (data.count('00') == 0): return False
separatorIndex = data.index('00')
cue = self.getCueNumber(data[0:separatorIndex])
page, executor = self.getExecutorNumber(data[separatorIndex+1:])
if (page == -1 or executor == -1): return False
gma2Executor = GMA2Executor(executor)
gma2Executor.cue = cue
gma2Executor.lastCommand = MSCCommand.Go
self.deviceState.updateExecutor(page, gma2Executor)
# self.deviceState.update({str(page): {str(executor): {"currentCue": cue, "lastUpdate": datetime.now()}}})
return True
def getCueNumber(self, cueData):
# Confirm that the list contains a valid separator
if (cueData.count('2e') == 0): return -1
separatorIndex = cueData.index('2e')
# Separate the cue data list, then remove the first character, and map the strings to ints
integerList = self.removeFirstChar(cueData[:separatorIndex])
decimalList = self.removeFirstChar(cueData[separatorIndex+1:])
cueString = self.abListToNumericString(integerList, decimalList)
cueFloat = float(cueString)
return cueFloat
def getExecutorNumber(self, execData):
# Confirm that the list contains a valid separator
validSeparatorFound = False
separatorString = ''
if (execData.count('2e') != 0):
validSeparatorFound = True
separatorString = '2e'
if (execData.count('20') != 0):
validSeparatorFound = True
separatorString = '20'
if not validSeparatorFound: return -1
separatorIndex = execData.index(separatorString)
execList = self.removeFirstChar(execData[:separatorIndex])
pageList = self.removeFirstChar(execData[separatorIndex+1:])
execString = self.listToString(execList)
pageString = self.listToString(pageList)
execNum = int(execString)
pageNum = int(pageString)
return pageNum, execNum
# Tested
def removeFirstChar(self, lst):
charLst = lst
for idX, x in enumerate(lst):
lstX = list(x)
if len(lstX) == 1:
charLst[idX] = lstX[0]
else:
try:
charLst[idX] = lstX[1:][0]
except IndexError:
self.logger.error("Index Error in remove_first_char")
return charLst
# So simple, not testing
def listToString(self, aList):
tempString = ""
for a in aList:
tempString += str(a)
return tempString
# Tested
def abListToNumericString(self, aList, bList):
tempString = ""
if len(aList) == 0: tempString += "0"
for a in aList:
tempString += str(a)
tempString += "."
if len(bList) == 0: tempString += "0"
for b in bList:
tempString += str(b)
return tempString