1
|
|
|
"""Parse incoming BLE Lego messages from hubs |
2
|
|
|
|
3
|
|
|
Each hub has one of these objects to control access to the underlying BLE library notification thread. |
4
|
|
|
Communication back into the hub (running in python async-land) is through a :class:`curio.UniversalQueue` |
5
|
|
|
object. |
6
|
|
|
|
7
|
|
|
Todo: |
8
|
|
|
* The message parsers need to handle detaching of peripherals |
9
|
|
|
""" |
10
|
|
|
import struct, logging |
11
|
|
|
from .const import DEVICES |
12
|
|
|
from .messages import Message, UnknownMessageError |
13
|
|
|
|
14
|
|
|
logger = logging.getLogger(__name__) |
15
|
|
|
|
16
|
|
|
class MessageDispatch: |
17
|
|
|
"""Parse messages (bytearray) |
18
|
|
|
|
19
|
|
|
Once the :meth:`parse` method is called, the message header will be parsed, and based on the msg_type |
20
|
|
|
byte, the processing of the message body will be dispatched to the `parse` method of the matching Message body parser. |
21
|
|
|
Message body parsers are subclasses of :class:`bricknil.messages.Message`, and will call back |
22
|
|
|
to the `message*` methods below. This object will then send a message to the connected :class:`bricknil.hub.Hub` |
23
|
|
|
object. |
24
|
|
|
""" |
25
|
|
|
def __init__(self, hub): |
26
|
|
|
""" |
27
|
|
|
Args: |
28
|
|
|
hub (:class:`bricknil.hub.Hub`) : The hub that will be sending messages |
29
|
|
|
|
30
|
|
|
Attributes: |
31
|
|
|
port_info (dict): A mirror copy of the :py:attr:`bricknil.hub.Hub.port_info` object. This object is sent every time |
32
|
|
|
an update on the port meta data is made. |
33
|
|
|
""" |
34
|
|
|
self.hub = hub |
35
|
|
|
self.port_info = {} |
36
|
|
|
|
37
|
|
|
def parse(self, msg:bytearray): |
38
|
|
|
"""Parse the header of the message and dispatch message body processing |
39
|
|
|
|
40
|
|
|
`l` is only used to build up a log message to display during operation, telling |
41
|
|
|
the user what kind of message was received and how it was parsed. If the message |
42
|
|
|
cannot be parsed, then `l` contains the remaining unparsed raw message that was received from the |
43
|
|
|
hardware ble device. |
44
|
|
|
""" |
45
|
|
|
msg_bytes = list(msg) |
46
|
|
|
msg_bytes = msg_bytes[2:] # Skip the first two bytes (msg length and hub id (always 0) ) |
47
|
|
|
|
48
|
|
|
msg_type = msg_bytes.pop(0) |
49
|
|
|
l = [] # keep track of the parsed return message |
50
|
|
|
try: |
51
|
|
|
if msg_type in Message.parsers: |
52
|
|
|
Message.parsers[msg_type].parse(msg_bytes, l, self) |
53
|
|
|
else: |
54
|
|
|
raise UnknownMessageError |
55
|
|
|
except UnknownMessageError: |
56
|
|
|
l.append(self._parse_msg_bytes(msg)) |
57
|
|
|
|
58
|
|
|
return ' '.join([str(x) for x in l]) |
59
|
|
|
|
60
|
|
|
def _parse_msg_bytes(self, msg_bytes): |
61
|
|
|
hex_bytes = ':'.join(hex(c) for c in msg_bytes) |
62
|
|
|
return hex_bytes |
63
|
|
|
|
64
|
|
|
def message_update_value_to_peripheral(self, port, value): |
65
|
|
|
"""Called whenever a peripheral on the hub reports a change in its sensed value |
66
|
|
|
""" |
67
|
|
|
self.hub.peripheral_queue.put( ('value_change', (port, value)) ) |
68
|
|
|
|
69
|
|
|
def message_port_info_to_peripheral(self, port, message): |
70
|
|
|
"""Called whenever a peripheral needs to update its meta-data |
71
|
|
|
""" |
72
|
|
|
self.hub.peripheral_queue.put( ('update_port', (port, self.port_info[port])) ) |
73
|
|
|
self.hub.peripheral_queue.put( (message, port) ) |
74
|
|
|
|
75
|
|
|
def message_attach_to_hub(self, device_name, port): |
76
|
|
|
"""Called whenever a peripheral is attached to the hub |
77
|
|
|
""" |
78
|
|
|
# Now, we should activate updates from this sensor |
79
|
|
|
self.hub.peripheral_queue.put( ('attach', (port, device_name)) ) |
80
|
|
|
|
81
|
|
|
# Send a message to update the information on this port |
82
|
|
|
self.hub.peripheral_queue.put( ('update_port', (port, self.port_info[port])) ) |
83
|
|
|
|
84
|
|
|
# Send a message saying this port is detected, in case the hub |
85
|
|
|
# wants to query for more properties. (Since an attach message |
86
|
|
|
# doesn't do anything if the user hasn't @attach'ed a peripheral to it) |
87
|
|
|
self.hub.peripheral_queue.put( ('port_detected', port)) |
88
|
|
|
|
89
|
|
|
|