|
| 1 | +import asyncio |
| 2 | +from typing import Callable, Awaitable, Any |
| 3 | +from .connection import Connection |
| 4 | + |
| 5 | + |
| 6 | +class DeviceManager: |
| 7 | + """ |
| 8 | + A class to manage created Devices |
| 9 | +
|
| 10 | + Instance attributes: |
| 11 | + devices: dict that maps deviceid->Device |
| 12 | + """ |
| 13 | + |
| 14 | + def __init__(self): |
| 15 | + self.devices: dict[str, Device] = {} |
| 16 | + |
| 17 | + def __getitem__(self, device_id: str) -> "Device": |
| 18 | + """ |
| 19 | + Returns a device from a device id |
| 20 | +
|
| 21 | + :param device_id: device id |
| 22 | + :return: Device with associated device id |
| 23 | + """ |
| 24 | + return self.devices[device_id] |
| 25 | + |
| 26 | + def __contains__(self, device_id): |
| 27 | + """ |
| 28 | + Returns whether device id is created |
| 29 | +
|
| 30 | + :param device_id: device id |
| 31 | + :return: If device id is created |
| 32 | + """ |
| 33 | + return device_id in self.devices |
| 34 | + |
| 35 | + def create_device(self, device_id: str, connection: Connection | None) -> "Device": |
| 36 | + """ |
| 37 | + Creates a device if not already created, otherwise return the already created one |
| 38 | +
|
| 39 | + :param device_id: ID of the device we connect to |
| 40 | + :param connection: The connection that should be used for live_telemetry |
| 41 | + :return: Created device |
| 42 | + """ |
| 43 | + if device_id in self.devices: |
| 44 | + device = self.devices[device_id] |
| 45 | + else: |
| 46 | + device = Device(device_id=device_id, connection=connection) |
| 47 | + self.devices[device_id] = device |
| 48 | + |
| 49 | + return device |
| 50 | + |
| 51 | + def remove_device(self, device_id: str): |
| 52 | + """ |
| 53 | + Removes a device from our manager |
| 54 | +
|
| 55 | + :param device_id: Device Id to remove |
| 56 | + :return: |
| 57 | + """ |
| 58 | + if device_id in self.devices: |
| 59 | + device = self.devices[device_id] # noqa |
| 60 | + # TODO: figure out what to do here |
| 61 | + del self.devices[device_id] |
| 62 | + |
| 63 | + |
| 64 | +class Device: |
| 65 | + """ |
| 66 | + A class that represents a single device |
| 67 | +
|
| 68 | + Instance attributes: |
| 69 | + connection: connection that the device will read messages from |
| 70 | + device_id: device id of the device |
| 71 | + raw_bus: set of subscribed handlers that will be called when receiving a raw response |
| 72 | + parsed_bus: set of subscribed handlers that will be called after parsing a response |
| 73 | + """ |
| 74 | + |
| 75 | + def __init__(self, device_id: str, connection: Connection | None): |
| 76 | + self.connection = connection |
| 77 | + self.device_id = device_id |
| 78 | + |
| 79 | + # Bus that contains all functions that handle raw data |
| 80 | + self.raw_bus: set[Callable[[str], Awaitable[Any]]] = set() |
| 81 | + # Bus that contains all functions that handle parsed data |
| 82 | + self.parsed_bus: set[Callable[[str], Awaitable[Any]]] = set() |
| 83 | + |
| 84 | + def raw_live_telemetry(self, subscriber: Callable[[str], Awaitable]): |
| 85 | + """ |
| 86 | + Decorator that adds a function to our bus |
| 87 | + Throws an error if no connection was specified |
| 88 | +
|
| 89 | + :param subscriber: Function that subscribes to raw data |
| 90 | + :return: subscriber |
| 91 | + """ |
| 92 | + if not self.connection: |
| 93 | + raise RuntimeError( |
| 94 | + f"No connection was specified for device {self.device_id}" |
| 95 | + ) |
| 96 | + |
| 97 | + self.raw_bus.add(subscriber) |
| 98 | + return subscriber |
| 99 | + |
| 100 | + def live_telemetry(self, subscriber: Callable[[str], Awaitable]): |
| 101 | + """ |
| 102 | + Decorator that adds a function to our bus |
| 103 | + Throws an error if no connection was specified |
| 104 | +
|
| 105 | + :param subscriber: Function that subscribes to raw data |
| 106 | + :return: subscriber |
| 107 | + """ |
| 108 | + if not self.connection: |
| 109 | + raise RuntimeError( |
| 110 | + f"No connection was specified for device {self.device_id}" |
| 111 | + ) |
| 112 | + |
| 113 | + self.parsed_bus.add(subscriber) |
| 114 | + return subscriber |
| 115 | + |
| 116 | + async def _handle_raw(self, data: str): |
| 117 | + """ |
| 118 | + Internal method to parse raw data and send it to a different bus |
| 119 | + :param data: raw data that was received by the connection |
| 120 | + :return: None |
| 121 | + """ |
| 122 | + await asyncio.gather(*[s(data) for s in self.raw_bus]) |
| 123 | + # TODO: parse data |
| 124 | + await asyncio.gather(*[s(data) for s in self.parsed_bus]) |
| 125 | + |
| 126 | + async def start_live_telemetry(self): |
| 127 | + """ |
| 128 | + Starts the connection to the server to receive live telemetry for this particular device |
| 129 | + This function is called when we want to initialize a connection to a single device |
| 130 | +
|
| 131 | + :return: None |
| 132 | + """ |
| 133 | + if self.connection: |
| 134 | + await self.connection.connect(self._handle_raw) |
| 135 | + else: |
| 136 | + raise RuntimeError( |
| 137 | + f"No connection was specified for device {self.device_id}" |
| 138 | + ) |
0 commit comments