swarmit 0.2.0__py3-none-any.whl → 0.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ __version__ = "0.3.0"
@@ -0,0 +1,94 @@
1
+ """Module containing classes for interfacing with the DotBot gateway."""
2
+
3
+ import base64
4
+ from abc import ABC, abstractmethod
5
+
6
+ import paho.mqtt.client as mqtt
7
+ from dotbot.hdlc import HDLCHandler, HDLCState, hdlc_encode
8
+ from dotbot.protocol import PROTOCOL_VERSION
9
+ from dotbot.serial_interface import SerialInterface
10
+
11
+
12
+ class GatewayAdapterBase(ABC):
13
+ """Base class for interface adapters."""
14
+
15
+ @abstractmethod
16
+ def init(self, on_data_received: callable):
17
+ """Initialize the interface."""
18
+
19
+ @abstractmethod
20
+ def close(self):
21
+ """Close the interface."""
22
+
23
+ @abstractmethod
24
+ def send_data(self, data):
25
+ """Send data to the interface."""
26
+
27
+
28
+ class SerialAdapter(GatewayAdapterBase):
29
+ """Class used to interface with the serial port."""
30
+
31
+ def __init__(self, port, baudrate):
32
+ self.port = port
33
+ self.baudrate = baudrate
34
+ self.hdlc_handler = HDLCHandler()
35
+
36
+ def on_byte_received(self, byte):
37
+ self.hdlc_handler.handle_byte(byte)
38
+ if self.hdlc_handler.state == HDLCState.READY:
39
+ self.on_data_received(self.hdlc_handler.payload)
40
+
41
+ def init(self, on_data_received: callable):
42
+ self.serial = SerialInterface(
43
+ self.port, self.baudrate, self.on_byte_received
44
+ )
45
+ self.on_data_received = on_data_received
46
+ # Just write a single byte to fake a DotBot gateway handshake
47
+ self.serial.write(int(PROTOCOL_VERSION).to_bytes(length=1))
48
+
49
+ def close(self):
50
+ self.serial.stop()
51
+
52
+ def send_data(self, data):
53
+ self.serial.write(hdlc_encode(data))
54
+
55
+
56
+ class MQTTAdapter(GatewayAdapterBase):
57
+ """Class used to interface with MQTT."""
58
+
59
+ def __init__(self, host, port):
60
+ self.host = host
61
+ self.port = port
62
+ self.client = None
63
+
64
+ def on_message(self, client, userdata, message):
65
+ self.on_data_received(base64.b64decode(message.payload))
66
+
67
+ def on_log(self, client, userdata, paho_log_level, messages):
68
+ print(messages)
69
+
70
+ def on_connect(self, client, userdata, flags, reason_code, properties):
71
+ self.client.subscribe("/pydotbot/edge_to_controller")
72
+
73
+ def init(self, on_data_received: callable):
74
+ self.on_data_received = on_data_received
75
+ self.client = mqtt.Client(
76
+ mqtt.CallbackAPIVersion.VERSION2,
77
+ protocol=mqtt.MQTTProtocolVersion.MQTTv5,
78
+ )
79
+ self.client.tls_set_context(context=None)
80
+ # self.client.on_log = self.on_log
81
+ self.client.on_connect = self.on_connect
82
+ self.client.on_message = self.on_message
83
+ self.client.connect(self.host, self.port, 60)
84
+ self.client.loop_start()
85
+
86
+ def close(self):
87
+ self.client.disconnect()
88
+ self.client.loop_stop()
89
+
90
+ def send_data(self, data):
91
+ self.client.publish(
92
+ "/pydotbot/controller_to_edge",
93
+ base64.b64encode(data).decode(),
94
+ )