-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathx0genericcmd.py
257 lines (189 loc) · 8.48 KB
/
x0genericcmd.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
##
## imports
##
from abc import ABC, abstractmethod
import time
import misc
##
## code
##
class X0GenericCmd(ABC):
"""The main Apex state object that does the magic"""
def __init__(self, inComm, useLog, timeoutConfig, closeOnComplete = True):
self.log = useLog
self.desired = None
self.comm = inComm
self.state = ''
self.timeout = 0
self.refAckOffset = self.makeAckOffset(timeoutConfig)
self.cmdRetries = self.makeCmdRetries(timeoutConfig)
self.checkwaitacktimeout = 0
self.opcmd = b''
self.chatty = False
self.target = None
self.closeOnComplete = closeOnComplete
self.log.debug(f'refAckOffset {self.refAckOffset}, cmdRetries {self.cmdRetries}')
@abstractmethod
def makeDesired(self, cmd, data):
""" Returns the internal encoding of the command and data
cmd: the requested command
data: the data releated to the command
"""
pass
@abstractmethod
def makeOpCmd(self):
"""Takes the self.desired parameter and makes a command that can be sent to the remote device
Sometimes this simply adds a CR or LF to the self.desired
Other times more complex headers, etc. will be needed
"""
pass
@abstractmethod
def makeAckOffset(self, timeoutConfig):
"""Return a timeout in seconds for how long we should wait before receiving an Ack
timeoutConfig: dictionary that can be used to retrieve the timeout
"""
pass
@abstractmethod
def makeCmdRetries(self, timeoutConfig):
"""Returns the number of times the command should be retried if a timeout occurs
timeoutConfig: dictionary that can be used to retrieve the value
"""
pass
@abstractmethod
def isMatchingAck(self, rxData, opcmd):
"""Takes a value retrieved from the devices and converts it into a value for comparision with the target
rxData: the data retrieved from the device
if the result of this function matches the value returned from makeOpCmd then this response is
the correct response
"""
pass
@abstractmethod
def makeCmdResult(self, rxData):
"""When a matching response is found this function will convert it into the values returned to the caller
rxData: the data received from the device
"""
pass
def action(self):
# this is where the work happens
if self.state == '':
self.log.debug(f'Inside state "{self.state}" {self.checkwaitacktimeout}')
self.opcmd = self.makeOpCmd()
self.state = 'waitACK'
self.log.debug(f'Setting timeout')
self.timeout = time.time() + self.refAckOffset
self.state = 'sendCmd'
## this is intentionally an if and not an elif
## we want the code above to potentially trigger this code below
if self.state == 'sendCmd':
self.log.debug(f'Inside state "{self.state}" {self.checkwaitacktimeout}')
self.log.debug(f'Sending command {self.opcmd}')
# send the command
ok = self.comm.send(self.opcmd)
self.log.debug(f'OK is {ok}')
if not ok:
# do not move to next state!
# basically we'll try again next time
self.log.debug(f'Could not send command')
if time.time() > self.timeout:
# oops. Start over
self.log.debug(f'Trying to send Timeout in {self.state}. {self.checkwaitacktimeout}. Starting over')
self.state = ''
self.checkwaitacktimeout += 1
if self.checkwaitacktimeout > self.cmdRetries:
# we just give up
self.log.warning(f'Giving up...')
self.desired = None
self.checkwaitacktimeout = 0
else:
# it was sent
self.state = 'waitACK'
# self.timeout = time.time() + self.refAckOffset
elif self.state == 'waitACK':
self.log.debug(f'Inside state "{self.state}" {self.checkwaitacktimeout}')
# quick test of whether socket is connected
ok = self.comm.send(b'')
if not ok:
# no socket even after send tried to create one
self.log.debug(f'connection lost and not ready yet. Restarting...')
# got nothing. Have we timed out?
if time.time() > self.timeout:
# oops. Start over
self.log.debug(f'Connection Loss and Timeout in {self.state}. {self.checkwaitacktimeout}. Starting over')
self.state = ''
self.checkwaitacktimeout += 1
if self.checkwaitacktimeout > self.cmdRetries:
# we just give up
self.log.warning(f'Giving up...')
self.desired = None
self.checkwaitacktimeout = 0
else:
self.state = 'sendCmd'
# sent ok
else:
keepGoingCount = 0
keepGoing = True
while keepGoing:
# see if there's a response
keepGoingCount += 1
self.log.debug(f'Performing a read with keepGoingCount {keepGoingCount}')
rxData = self.comm.read()
if rxData == b'':
keepGoing = False
self.log.debug(f'Got no data')
# got nothing. Have we timed out?
if time.time() > self.timeout:
# oops. Start over
self.log.debug(f'Timeout in {self.state}. {self.checkwaitacktimeout}. Starting over')
self.state = ''
self.checkwaitacktimeout += 1
if self.checkwaitacktimeout > self.cmdRetries:
# we just give up
self.log.warning(f'Giving up...')
self.desired = None
self.checkwaitacktimeout = 0
else:
self.log.debug(f'Got data {rxData}')
# we got something
self.checkwaitacktimeout = 0
if self.isMatchingAck(rxData,self.opcmd):
# got the ack
self.log.debug(f'Got the ACK {rxData}')
self.log.info(f'Successfully performed {rxData}')
keepGoing = False
self.state = ''
self.desired = None
result = self.makeCmdResult(rxData)
if self.closeOnComplete:
self.log.debug(f'closing connection because closeOnComplete is {self.closeOnComplete}')
self.comm.close()
return (True, result)
else:
# what happened?
self.log.warning(f'Did not receive expected results with {rxData}. Ignoring...')
else:
self.log.error('**** YIKES {self.state}')
if not self.desired:
# we are done
if self.closeOnComplete:
self.log.debug(f'closing connection because closeOnComplete is {self.closeOnComplete}')
self.comm.close()
# return that we are done with no result
return (True,None)
else:
# more to do
return (False,None)
def set(self, cmd:str, data: bytearray):
# print(f'cmd is {cmd}')
# print(f'data is {data}')
combined = self.makeDesired(cmd,data)
# combined = bytes(cmd,'utf-8') + b' ' + data
# print(f'combined {combined}')
if self.desired:
# same mode
self.log.warning(f'!!!! Already sending command {combined}')
else:
self.desired = combined
self.log.debug(f'Asked to send command {combined}')
# definitely need to restart the state machine
self.state = ''
self.action()