Skip to content

Commit

Permalink
Updated swarm and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis-chambers committed Jun 7, 2024
1 parent bad0c25 commit 4f19146
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 224 deletions.
15 changes: 10 additions & 5 deletions src/iotdevicesimulator/devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,22 @@ def _format_payload(self, payload: dict):
"signature": 111111,
"environment": {
"station_name": self.device_id,
"table_name": self.topic_suffix,
"table_name": "no table",
"model": self.device_type,
"os_version": "Not a real OS",
"prog_name": "test",
},
}

time = payload["DATE_TIME"]
if isinstance(payload, dict):
time = payload["DATE_TIME"]

payload.pop("DATE_TIME")
payload.pop("DATE_TIME")

f_payload["data"] = {"time": time, "vals": list(payload.values())}
f_payload["fields"] = [{"name": key} for key in payload.keys()]
elif hasattr(payload, "__iter__"):
f_payload["fields"] = ["_" + i for i in range(len(payload))]
f_payload["data"] = {"vals": payload}

f_payload["data"] = {"time": time, "vals": list(payload.values())}
f_payload["fields"] = [{"name": key} for key in payload.keys()]
return f_payload
39 changes: 20 additions & 19 deletions src/iotdevicesimulator/swarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@


class Swarm:
"""Object for creating a swarm of COSMOS site devices.
This object instantiates a group of sensor devices that submit data from the
COSMOS database and then wait for a specified time. When run unrestricted, this
can simulate the full COSMOS network in real time.
"""Manages a swarm of IoT devices and runs the main loop
of all devices. Can receive any number or combination of devices.
"""

name: str
Expand All @@ -30,31 +28,32 @@ def __len__(self):
return len(self.devices)

def __repr__(self):
return f"{self.__class__.__name__}({self.devices}, name={self.name})"

def __str__(self):
topic_prefix = (
"" if self.topic_prefix is None else f', topic_prefix="{self.topic_prefix}"'
name_arg = (
f', name="{self.name}"'
if not self.name.startswith("unnamed-swarm-")
else ""
)
return (
f'CosmosSwarm({self.query.__class__.__name__}.{self.query.name}, name="{self.name}"'
f"{topic_prefix}, sleep_time={self.sleep_time}, max_cycles={self.max_cycles}, delay_start={self.delay_start})"
devices_arg = (
self.devices[0].__repr__()
if len(self.devices) == 1
else self.devices.__repr__()
)
return f"{self.__class__.__name__}({devices_arg}{name_arg})"

def __init__(
self,
devices: List[BaseDevice],
name: str | None = None,
) -> None:
"""Factory method for initialising the class.
"""Initializes the class.
Args:
devices: A list of devices to swarmify.
name: Name / ID given to swarm.
"""

if not hasattr(devices, "__iter__"):
devices = set(devices)
devices = [devices]

if not all([isinstance(device, BaseDevice) for device in devices]):
raise TypeError(
Expand All @@ -66,17 +65,19 @@ def __init__(
if name is not None:
self.name = str(name)
else:
self.name = f"swarm-{uuid.uuid4()}"
self.name = f"unnamed-swarm-{uuid.uuid4()}"

self._instance_logger = logger.getChild(self.name)
self._instance_logger = logger.getChild(
f"{self.__class__.__name__}.{self.name}"
)

async def run(self) -> None:
"""Main function for running the swarm. Sends the query
and message connection object. Runs until all sites reach
their maximum cycle. If no maximum, it runs forever.
their maximum cycle. If any site has no maximum, it runs forever.
"""

self._instance_logger.info(f"Running main loop: swarm-{self.name}")
self._instance_logger.info("Running main loop.")
await asyncio.gather(*[device.run() for device in self.devices])

self._instance_logger.info(f"Terminated: swarm-{self.name}")
self._instance_logger.info("Terminated.")
Loading

0 comments on commit 4f19146

Please sign in to comment.