Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions swarmit/testbed/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,6 @@ def __init__(self, settings: ControllerSettings):
self.settings = settings
self._interface: GatewayAdapterBase = None
self.status_data: dict[str, NodeStatus] = {}
self.started_data: list[str] = []
self.stopped_data: list[str] = []
self.chunks: list[DataChunk] = []
self.start_ota_data: StartOtaData = StartOtaData()
self.transfer_data: dict[str, TransferDataStatus] = {}
Expand Down Expand Up @@ -350,9 +348,6 @@ def send_payload(self, destination: int, payload: Payload):

def on_frame_received(self, header, packet: Packet):
"""Handle the received frame."""
# if self.settings.verbose:
# print()
# print(Frame(header, packet))
device_addr = f"{header.source:08X}"
if packet.payload_type == PayloadType.SWARMIT_STATUS:
now = time.time()
Expand Down Expand Up @@ -431,50 +426,53 @@ def start(self, devices=None, timeout=COMMAND_TIMEOUT):
if devices is None:
devices = self.settings.devices or []
ready_devices = self.ready_devices
devices_to_start = (
ready_devices
if not devices
else [d for d in devices if d in ready_devices]
)
attempts = 0
while attempts < COMMAND_MAX_ATTEMPTS and not all(
self.status_data[addr].status == StatusType.Running
for addr in ready_devices
addr in self.status_data
and self.status_data[addr].status == StatusType.Running
for addr in devices_to_start
):
if not devices:
self._send_start(addr_to_hex(BROADCAST_ADDRESS))
else:
for device_addr in devices:
if device_addr not in ready_devices:
continue
for device_addr in devices_to_start:
self._send_start(device_addr)
attempts += 1
time.sleep(COMMAND_ATTEMPT_DELAY)
self._live_status(timeout, devices=ready_devices, message="to start")
self._live_status(
timeout, devices=devices_to_start, message="to start"
)

def stop(self, devices=None, timeout=COMMAND_TIMEOUT):
"""Stop the application."""
if devices is None:
devices = self.settings.devices or []
stoppable_devices = self.running_devices + self.resetting_devices
devices_to_stop = (
stoppable_devices
if not devices
else [d for d in devices if d in stoppable_devices]
)

attempts = 0
while attempts < COMMAND_MAX_ATTEMPTS and not all(
self.status_data[addr].status
in [StatusType.Stopping, StatusType.Bootloader]
for addr in stoppable_devices
for addr in devices_to_stop
):
if not devices:
self.send_payload(BROADCAST_ADDRESS, PayloadStop())
else:
for device_addr in devices:
if (
device_addr not in stoppable_devices
or self.status_data[device_addr].status
in [StatusType.Stopping, StatusType.Bootloader]
):
continue
for device_addr in devices_to_stop:
self.send_payload(int(device_addr, 16), PayloadStop())
attempts += 1
time.sleep(COMMAND_ATTEMPT_DELAY)
self._live_status(
timeout, devices=stoppable_devices, message="to stop"
)
self._live_status(timeout, devices=devices_to_stop, message="to stop")

def _send_reset(self, device_addr: int, location: ResetLocation):
payload = PayloadReset(
Expand Down
3 changes: 2 additions & 1 deletion swarmit/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def test_controller_start_unicast():
f"{node.address:08X}" for node in nodes
]

controller.status_data = {}
controller.start(devices=["00000001", "00000003"], timeout=0.1)
time.sleep(0.3)
assert nodes[0].status == StatusType.Running
Expand Down Expand Up @@ -232,7 +233,7 @@ def test_controller_status(capsys):
"swarmit.testbed.adapter.MarilibMQTTAdapter",
MarilibMQTTAdapterMock,
)
def test_controller_status_adpater_cloud(capsys):
def test_controller_status_adapter_cloud(capsys):
controller = Controller(
ControllerSettings(
adapter="cloud", network_id=42, adapter_wait_timeout=0.1
Expand Down