-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcommandhandler.py
575 lines (423 loc) · 18 KB
/
commandhandler.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
"""
.. module:: commandhandler
:platform: Unix
:synopsis: Handles the communication to/from the Arduino Hardware.
.. moduleauthor:: Jonathan Grizou <Jonathan.Grizou@gla.ac.uk>
"""
import time
import serial
import socket
import threading
import logging
from typing import Callable, Dict, List, Union
from .exceptions import CMHandlerConfigurationError, CMTimeout, CMCommunicationError
# Default delimiter to separate commands
DEFAULT_DELIM = ','
# Default terminal character of a command
DEFAULT_TERM = ';'
# Default decimal
DEFAULT_CMD_DECIMAL = 2
# Default baudrate for communication
DEFAULT_BAUDRATE = 115200
# Default timeout time
DEFAULT_TIMEOUT = 0.01
class CommandHandler(object):
"""
Represents the Command Handler which will handle commands to/from the Arduino hardware.
Args:
delim: Delimiter of the command, default set to DEFAULT_DELIM(',')
term: Terminal character of the command, set to DEFAULT_TERM(';')
cmd_decimal: Decimal of the command, default set to DEFAULT_CMD_DECIMAL(2)
"""
@classmethod
def from_config(cls, config: Dict) -> 'CommandHandler':
"""
Obtains the details of the handler from a configuration.
Args:
cls: The instantiating class.
config: Dictionary containing the configuration details.
Returns:
CommandHandler: New CommandHandler object with details set from a configuration setup.
"""
return cls(**config)
def __init__(self, delim: str = DEFAULT_DELIM, term: str = DEFAULT_TERM, cmd_decimal: int = DEFAULT_CMD_DECIMAL,
**kwargs):
self.logger = logging.getLogger(__name__).getChild(self.__class__.__name__)
# Something descriptive to reference the handler in logs.
self.name = self.__class__.__name__
self.delim = delim # character separating args in a message
self.term = term # character ending a message
self.buffer = '' # string that will hold the received data
self.handlers: Dict[str, List[Callable]] = {}
self.relays: Dict[str, List[Callable]] = {}
self.default_handlers: List[Callable] = []
self.cmd_header = ''
self.cmd_decimal = cmd_decimal
def process_char(self, a_char: bytes) -> None:
"""
Processes a single character of a command and adds it to the receiving buffer.
If the terminator character is found, process the buffer.
Args:
a_char: The character to be processed
"""
if a_char:
decoded_char = a_char.decode(encoding="utf-8", errors="ignore")
if decoded_char == self.term:
self.handle(self.buffer)
self.buffer = ''
else:
self.buffer += decoded_char
def handle(self, cmd: str):
"""
Handles a full command to/from the Arduino hardware.
Args:
cmd: The command to be handled.
"""
cmd = cmd.strip().strip(self.term)
cmd_list = cmd.split(self.delim)
cmd_id = cmd_list[0]
self.logger.debug('Handling "{}"'.format(cmd),
extra={'cmd_list': cmd_list,
'cmd_id': cmd_id})
if cmd_id in self.handlers:
for clb in self.handlers[cmd_id]:
self.logger.debug('Found callback for "{}"'.format(cmd_id),
extra={'callback_function': clb})
clb(*cmd_list[1:]) # all args in the command are given to the callback function as arguments
elif cmd_id in self.relays:
for clb in self.relays[cmd_id]:
self.logger.debug('Found relay for "{}"'.format(cmd_id),
extra={'relay_function': clb})
clb(self.build_remaining(cmd_list))
else:
self.logger.debug('No callback assigned to "{}", defaulting'.format(cmd_id))
for clb in self.default_handlers:
clb(cmd) # give back what was received
def build_remaining(self, cmd_list: List[str]) -> str:
"""
Builds an Arduino command from a list of partial commands.
Args:
cmd_list: The list of command constituents.
"""
return self.delim.join(cmd_list[1:]) + self.term
def add_command(self, command_id: str, callback_function: Callable) -> None:
"""
Adds an Arduino command to the handler.
Args:
command_id: The ID of the command.
callback_function: A copy of the command to "callback".
"""
if command_id not in self.handlers:
self.handlers[command_id] = []
if callback_function not in self.handlers[command_id]:
self.handlers[command_id].append(callback_function)
def remove_command(self, command_id: str, callback_function: Callable) -> None:
"""
Removes an Arduino command from the Handler.
Args:
command_id: The ID of the command.
callback_function: A copy of the command to "callback".
"""
if command_id in self.handlers:
if callback_function in self.handlers[command_id]:
self.handlers[command_id].remove(callback_function)
def add_relay(self, command_id: str, callback_function: Callable) -> None:
"""
Adds a relay to the Handler.
Args:
command_id: The ID of the command.
callback_function: A copy of the command to "callback".
"""
if command_id not in self.relays:
self.relays[command_id] = []
if callback_function not in self.relays[command_id]:
self.relays[command_id].append(callback_function)
def remove_relay(self, command_id: str, callback_function: Callable) -> None:
"""
Removes a relay form the Handler.
Args:
command_id: The ID of the command.
callback_function: A copy of the command to "callback".
"""
if command_id in self.relays:
if callback_function in self.relays[command_id]:
self.relays[command_id].remove(callback_function)
def add_default_handler(self, callback_function: Callable) -> None:
"""
Adds a default handler to the device.
Args:
callback_function: A copy of the command to "callback".
"""
if callback_function not in self.default_handlers:
self.default_handlers.append(callback_function)
def remove_default_handler(self, callback_function: Callable) -> None:
"""
Removes a default handler from the device.
Args:
callback_function: A copy of the command to "callback".
"""
if callback_function in self.default_handlers:
self.default_handlers.remove(callback_function)
#
def set_command_header(self, cmd_header: str, add_delim: bool = True) -> None:
"""
Sets the header of the Arduino command.
Args:
cmd_header: The header of the command.
add_delim: Adds a delimiter to the command, default set to True.
"""
self.cmd_header = cmd_header
if add_delim:
self.cmd_header += self.delim
self.logger.debug('Set command header to "{}"'.format(self.cmd_header))
def set_command_decimal(self, cmd_decimal: int) -> None:
"""
Sets the decimal of the Arduino command.
Args:
cmd_decimal: The decimal of the command.
"""
self.cmd_decimal = cmd_decimal
self.logger.debug('Set decimal to "{}"'.format(self.cmd_decimal))
def forge_command(self, command_id: str, *args) -> str:
"""
Creates a full Arduino command.
Args:
command_id: The ID of the command.
*args: Variable length argument list.
"""
cmd = self.cmd_header
cmd += command_id
for arg in args:
cmd += self.delim
if type(arg) == float:
cmd += str(round(arg, self.cmd_decimal))
else:
cmd += str(arg)
cmd += self.term
self.logger.debug('Forged "{}"'.format(cmd),
extra={'command_id': command_id,
'arguments': args})
return cmd
class SerialCommandHandler(threading.Thread, CommandHandler):
"""
Represents the Command Handler which will handle commands to/from the Arduino hardware via Serial Communication.
Args:
port: The port to communicate over.
baudrate: The baudrate of the serial communication, default set to DEFAULT_BAUDRATE (115200)
timeout: The time to wait for timeout, default set to DEFAULT_TIMEOUT (0.01)
delim: The delimiting character of a command, default set to DEFAULT_DELIM (',')
term: The terminal character of a command, default set to DEFAULT_TERM (';')
cmd_decimal: The decimal of the command, default set to DEFAULT_CMD_DECIMAL (2)
"""
def __init__(self, port: str, baudrate: int = DEFAULT_BAUDRATE, timeout: float = DEFAULT_TIMEOUT,
delim: str = DEFAULT_DELIM, term: str = DEFAULT_TERM, cmd_decimal: int = DEFAULT_CMD_DECIMAL):
threading.Thread.__init__(self)
self.daemon = True
self.interrupted = threading.Lock()
self.logger = logging.getLogger(__name__).getChild(self.__class__.__name__)
CommandHandler.__init__(self, delim, term, cmd_decimal)
self.name = port
self.open(port, baudrate, timeout)
def open(self, port: str, baudrate: int, timeout: float) -> None:
"""
Opens the serial communication between the PC and Arduino board.
Args:
port: The port to communicate over.
baudrate: The baudrate of serial communication.
timeout: The time to wait for timeout.
"""
self.logger.debug('Opening port {}'.format(port),
extra={'port': port,
'baudrate': baudrate,
'timeout': timeout})
try:
self._serial = serial.Serial(port, baudrate, timeout=timeout)
except (serial.SerialException, TypeError, ValueError) as e:
raise CMHandlerConfigurationError(str(e))
def close(self) -> None:
"""
Closes the serial communication between the PC and Arduino board.
"""
if hasattr(self, "_serial"):
self._serial.close()
self.logger.debug('Closing port "{}"'.format(self._serial.port))
def __del__(self):
"""
Closes the serial communication between the PC and Arduino board.
"""
self.close()
def __exit__(self, exc_type, exc_value, traceback):
"""
Closes the serial communication between the PC and Arduino Board, due to an exception occurrence.
Args:
exc_type (str): The exception type.
exc_value: The value of the exception.
traceback (str): The position in the code where the exception occurred.
"""
self.close()
def stop(self) -> None:
"""
Releases the lock when signalled via an interrupt.
"""
self.interrupted.release()
def run(self) -> None:
"""
Starts the Handler processing commands.
"""
self.interrupted.acquire()
while self.interrupted.locked():
try:
self.process_serial(self._serial)
except (serial.SerialException, serial.SerialTimeoutException) as e:
raise CMTimeout(f"Error reading from serial port {self._serial.port}! {e}") from None
self.close()
def send(self, command_id: str, *arg) -> None:
"""
Sends a command over the serial communication.
Args:
command_id (str): The ID of the command.
*arg: Variable argument.
"""
self.write(self.forge_command(command_id, *arg))
def write(self, msg: str) -> None:
"""
Writes a message over the serial communication.
Args:
msg (str): The message to send.
"""
self.logger.debug('Sending "{}" on port "{}"'.format(msg, self._serial.port))
try:
self._serial.write(msg.encode())
except serial.SerialException as e:
raise CMCommunicationError(f"Error writing to serial port {self._serial.port}! {e}") from None
def process_serial(self, a_serial: serial.Serial) -> None:
"""
Processes the serial communication to obtain data to be processed.
Args:
a_serial: The serial to read from.
"""
self.process_char(a_serial.read(1))
def wait_until_running(self, sleep_time: float = 0.01) -> None:
"""
Waits until the current thread is running.
Args:
sleep_time (float): The time to wait for, default set to 0.01
"""
while not self.interrupted.locked():
time.sleep(sleep_time)
class TCPIPCommandHandler(threading.Thread, CommandHandler):
"""
Represents the Command Handler which will handle commands to/from the Arduino hardware via TCP/IP socket.
Args:
port: The TCP/UDP port to communicate over.
address: The IP address of the device.
protocol: Either tcp or udp, default set to TCP.
timeout: The time to wait for timeout, default set to DEFAULT_TIMEOUT (0.01)
delim: The delimiting character of a command, default set to DEFAULT_DELIM (',')
term: The terminal character of a command, default set to DEFAULT_TERM (';')
cmd_decimal: The decimal of the command, default set to DEFAULT_CMD_DECIMAL (2)
"""
def __init__(self, port: str, address: str, protocol: str = "TCP", timeout: float = DEFAULT_TIMEOUT,
delim: str = DEFAULT_DELIM, term: str = DEFAULT_TERM, cmd_decimal: int = DEFAULT_CMD_DECIMAL):
threading.Thread.__init__(self)
self.daemon = True
self.interrupted = threading.Event()
self.interrupted.clear()
self.logger = logging.getLogger(__name__).getChild(self.__class__.__name__)
CommandHandler.__init__(self, delim, term, cmd_decimal)
self.name = address + ":" + port
self._connection: socket.socket = None # type: ignore
self.open(port, address, protocol.upper(), timeout)
def open(self, port: str, address: str, protocol: str, timeout: float):
"""
Opens the TCP/IP communication between the PC and Arduino board.
Args:
port: The port to communicate over.
address: The IP address of the device.
protocol: Protocol to use - TCP or UDP
timeout: The time to wait for timeout.
"""
self.logger.debug('Opening connection to %s:%s (%s)', address, port, protocol)
try:
if protocol == "TCP":
self._connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
elif protocol == "UDP":
self._connection = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
else:
raise CMHandlerConfigurationError(f"Unknown transport layer protocol <{protocol}> provided!")
self._connection.connect((address, int(port)))
# Receive timeout in seconds.
# This has to be done after opening the connection to avoid connect() error on non-blocking socket
self._connection.settimeout(timeout)
except TimeoutError as e:
raise CMHandlerConfigurationError(f"Socket timeout! {e}")
except (OSError, TypeError, ValueError) as e:
raise CMHandlerConfigurationError(f"Can't open socket! {e}")
def close(self) -> None:
"""
Closes the communication between the PC and Arduino board.
"""
self._connection.close()
self.logger.debug("Connection closed.")
def __del__(self):
"""
Closes the serial communication between the PC and Arduino board.
"""
self.close()
def __exit__(self, exc_type, exc_value, traceback):
"""
Closes the serial communication between the PC and Arduino Board, due to an exception occurrence.
Args:
exc_type (str): The exception type.
exc_value: The value of the exception.
traceback (str): The position in the code where the exception occurred.
"""
self.close()
def stop(self) -> None:
"""
Releases the lock when signalled via an interrupt.
"""
self.interrupted.set()
def run(self) -> None:
"""
Starts the Handler processing commands.
"""
while not self.interrupted.is_set():
self.process_data()
self.close()
def send(self, command_id: str, *arg) -> None:
"""
Sends a command over the TCP/IP connection.
Args:
command_id: The ID of the command.
*arg: Variable argument.
"""
try:
self.write(self.forge_command(command_id, *arg))
except OSError as e:
raise CMCommunicationError(f"Error writing to socket! {e}") from None
def write(self, msg: str) -> None:
"""
Writes raw data into the socket.
Args:
msg: The message to send.
"""
self.logger.debug('Sending "%s" to "%s"', msg, self._connection.getpeername())
self._connection.send(msg.encode())
def process_data(self) -> None:
"""
Gets the data from socket to be processed.
"""
try:
self.process_char(self._connection.recv(1))
except socket.timeout:
pass
except OSError as e:
raise CMCommunicationError(f"Error reading from socket! {e}")
def wait_until_running(self) -> None:
"""
Waits until the current thread is running.
"""
self.interrupted.wait()
# Typing variable for either Serial or TCPIP CommandHandler
GenericCommandHandler = Union[SerialCommandHandler, TCPIPCommandHandler]