swarmit 0.2.0__py3-none-any.whl → 0.4.4__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.4.4"
@@ -0,0 +1,142 @@
1
+ """Module containing classes for interfacing with the DotBot gateway."""
2
+
3
+ import time
4
+ from abc import ABC, abstractmethod
5
+
6
+ from dotbot.protocol import (
7
+ Packet,
8
+ Payload,
9
+ ProtocolPayloadParserException,
10
+ )
11
+ from marilib.communication_adapter import MQTTAdapter as MarilibMQTTAdapter
12
+ from marilib.communication_adapter import SerialAdapter as MarilibSerialAdapter
13
+ from marilib.mari_protocol import Frame as MariFrame
14
+ from marilib.marilib_cloud import MarilibCloud
15
+ from marilib.marilib_edge import MarilibEdge
16
+ from marilib.model import EdgeEvent, MariNode
17
+ from rich import print
18
+
19
+
20
+ class GatewayAdapterBase(ABC):
21
+ """Base class for interface adapters."""
22
+
23
+ @abstractmethod
24
+ def init(self, on_frame_received: callable):
25
+ """Initialize the interface."""
26
+
27
+ @abstractmethod
28
+ def close(self):
29
+ """Close the interface."""
30
+
31
+ @abstractmethod
32
+ def send_payload(self, destination: int, payload: Payload):
33
+ """Send payload to the interface."""
34
+
35
+
36
+ class MarilibEdgeAdapter(GatewayAdapterBase):
37
+ """Class used to interface with Marilib."""
38
+
39
+ def on_event(self, event: EdgeEvent, event_data: MariNode | MariFrame):
40
+ if event == EdgeEvent.NODE_JOINED:
41
+ if self.verbose:
42
+ print("[green]Node joined:[/]", event_data)
43
+ elif event == EdgeEvent.NODE_LEFT:
44
+ if self.verbose:
45
+ print("[orange]Node left:[/]", event_data)
46
+ elif event == EdgeEvent.NODE_DATA:
47
+ try:
48
+ packet = Packet.from_bytes(event_data.payload)
49
+ except (ValueError, ProtocolPayloadParserException) as exc:
50
+ print(f"[red]Error parsing packet: {exc}[/]")
51
+ return
52
+ if not hasattr(self, "on_frame_received"):
53
+ return
54
+ self.on_frame_received(event_data.header, packet)
55
+
56
+ def __init__(self, port: str, baudrate: int, verbose: bool = False):
57
+ self.verbose = verbose
58
+ self.mari = MarilibEdge(
59
+ self.on_event, MarilibSerialAdapter(port, baudrate)
60
+ )
61
+
62
+ def _busy_wait(self, timeout: int):
63
+ """Wait for the condition to be met."""
64
+ while timeout > 0:
65
+ self.mari.update()
66
+ timeout -= 0.1
67
+ time.sleep(0.1)
68
+
69
+ def init(self, on_frame_received: callable):
70
+ self.on_frame_received = on_frame_received
71
+ if self.verbose:
72
+ self._busy_wait(3)
73
+ print("[yellow]Mari nodes available:[/]")
74
+ print(self.mari.nodes)
75
+
76
+ def close(self):
77
+ pass
78
+
79
+ def send_payload(self, destination: int, payload: Payload):
80
+ self.mari.send_frame(
81
+ dst=destination,
82
+ payload=Packet.from_payload(payload).to_bytes(),
83
+ )
84
+
85
+
86
+ class MarilibCloudAdapter(GatewayAdapterBase):
87
+ """Class used to interface with Marilib."""
88
+
89
+ def on_event(self, event: EdgeEvent, event_data: MariNode | MariFrame):
90
+ if event == EdgeEvent.NODE_JOINED:
91
+ if self.verbose:
92
+ print("[green]Node joined:[/]", event_data)
93
+ elif event == EdgeEvent.NODE_LEFT:
94
+ if self.verbose:
95
+ print("[orange]Node left:[/]", event_data)
96
+ elif event == EdgeEvent.NODE_DATA:
97
+ try:
98
+ packet = Packet.from_bytes(event_data.payload)
99
+ except (ValueError, ProtocolPayloadParserException) as exc:
100
+ print(f"[red]Error parsing packet: {exc}[/]")
101
+ return
102
+ if not hasattr(self, "on_frame_received"):
103
+ return
104
+ self.on_frame_received(event_data.header, packet)
105
+
106
+ def __init__(
107
+ self,
108
+ host: str,
109
+ port: int,
110
+ use_tls: bool,
111
+ network_id: int,
112
+ verbose: bool = False,
113
+ ):
114
+ self.verbose = verbose
115
+ self.mari = MarilibCloud(
116
+ self.on_event,
117
+ MarilibMQTTAdapter(host, port, use_tls=use_tls, is_edge=False),
118
+ network_id,
119
+ )
120
+
121
+ def _busy_wait(self, timeout):
122
+ """Wait for the condition to be met."""
123
+ while timeout > 0:
124
+ self.mari.update()
125
+ timeout -= 0.1
126
+ time.sleep(0.1)
127
+
128
+ def init(self, on_frame_received: callable):
129
+ self.on_frame_received = on_frame_received
130
+ if self.verbose:
131
+ self._busy_wait(3)
132
+ print("[yellow]Mari nodes available:[/]")
133
+ print(self.mari.nodes)
134
+
135
+ def close(self):
136
+ pass
137
+
138
+ def send_payload(self, destination: int, payload: Payload):
139
+ self.mari.send_frame(
140
+ dst=destination,
141
+ payload=Packet.from_payload(payload).to_bytes(),
142
+ )