swarmit 0.3.0__py3-none-any.whl → 0.4.5__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.
- swarmit-0.4.5.dist-info/METADATA +128 -0
- swarmit-0.4.5.dist-info/RECORD +12 -0
- testbed/cli/main.py +140 -170
- testbed/swarmit/__init__.py +1 -1
- testbed/swarmit/adapter.py +112 -64
- testbed/swarmit/controller.py +350 -306
- testbed/swarmit/protocol.py +38 -99
- swarmit-0.3.0.dist-info/METADATA +0 -101
- swarmit-0.3.0.dist-info/RECORD +0 -12
- {swarmit-0.3.0.dist-info → swarmit-0.4.5.dist-info}/WHEEL +0 -0
- {swarmit-0.3.0.dist-info → swarmit-0.4.5.dist-info}/entry_points.txt +0 -0
- {swarmit-0.3.0.dist-info → swarmit-0.4.5.dist-info}/licenses/AUTHORS +0 -0
- {swarmit-0.3.0.dist-info → swarmit-0.4.5.dist-info}/licenses/LICENSE +0 -0
testbed/swarmit/adapter.py
CHANGED
@@ -1,19 +1,27 @@
|
|
1
1
|
"""Module containing classes for interfacing with the DotBot gateway."""
|
2
2
|
|
3
|
-
import
|
3
|
+
import time
|
4
4
|
from abc import ABC, abstractmethod
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
10
18
|
|
11
19
|
|
12
20
|
class GatewayAdapterBase(ABC):
|
13
21
|
"""Base class for interface adapters."""
|
14
22
|
|
15
23
|
@abstractmethod
|
16
|
-
def init(self,
|
24
|
+
def init(self, on_frame_received: callable):
|
17
25
|
"""Initialize the interface."""
|
18
26
|
|
19
27
|
@abstractmethod
|
@@ -21,74 +29,114 @@ class GatewayAdapterBase(ABC):
|
|
21
29
|
"""Close the interface."""
|
22
30
|
|
23
31
|
@abstractmethod
|
24
|
-
def
|
25
|
-
"""Send
|
26
|
-
|
27
|
-
|
28
|
-
class
|
29
|
-
"""Class used to interface with
|
30
|
-
|
31
|
-
def
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
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)
|
44
60
|
)
|
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
61
|
|
49
|
-
def
|
50
|
-
|
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)
|
51
68
|
|
52
|
-
def
|
53
|
-
self.
|
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)
|
54
75
|
|
76
|
+
def close(self):
|
77
|
+
pass
|
55
78
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
self.port = port
|
62
|
-
self.client = None
|
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
|
+
)
|
63
84
|
|
64
|
-
def on_message(self, client, userdata, message):
|
65
|
-
self.on_data_received(base64.b64decode(message.payload))
|
66
85
|
|
67
|
-
|
68
|
-
|
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
|
+
)
|
69
120
|
|
70
|
-
def
|
71
|
-
|
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)
|
72
127
|
|
73
|
-
def init(self,
|
74
|
-
self.
|
75
|
-
self.
|
76
|
-
|
77
|
-
|
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()
|
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)
|
85
134
|
|
86
135
|
def close(self):
|
87
|
-
|
88
|
-
self.client.loop_stop()
|
136
|
+
pass
|
89
137
|
|
90
|
-
def
|
91
|
-
self.
|
92
|
-
|
93
|
-
|
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(),
|
94
142
|
)
|