diff --git a/EosLib/format/__init__.py b/EosLib/format/__init__.py index abfd2c8..d74bf64 100644 --- a/EosLib/format/__init__.py +++ b/EosLib/format/__init__.py @@ -1,4 +1,4 @@ from EosLib.format.formats import telemetry_data, position, empty_format, cutdown, ping_format, valve, e_field, \ science_data, field_mill -from EosLib.format.formats.health import driver_health_report +from EosLib.format.formats.health import driver_health_report, health_response from EosLib.format.definitions import Type as Type diff --git a/EosLib/format/definitions.py b/EosLib/format/definitions.py index a7bb2b2..394348a 100644 --- a/EosLib/format/definitions.py +++ b/EosLib/format/definitions.py @@ -19,4 +19,5 @@ class Type(IntEnum): SCIENCE_DATA = 13 DRIVER_HEALTH_REPORT = 14 FIELDMILL = 15 + HEALTH_RESPONSE = 17 ERROR = 255 diff --git a/EosLib/format/formats/health/health_response.py b/EosLib/format/formats/health/health_response.py new file mode 100644 index 0000000..7e5e9f2 --- /dev/null +++ b/EosLib/format/formats/health/health_response.py @@ -0,0 +1,77 @@ +import struct +from enum import unique, IntEnum + +from typing_extensions import Self + +from EosLib.device import Device +from EosLib.format.definitions import Type +from EosLib.format.base_format import BaseFormat + + +@unique +class ResponseType(IntEnum): + DEVICE = 0 + HISTORY = 1 + + +class HealthResponse(BaseFormat): + @staticmethod + def get_format_type() -> Type: + return Type.HEALTH_RESPONSE + + @staticmethod + def get_format_string() -> str: + # Struct format is: device_id, response_type, num_entries, entries[] + return "!" \ + "B" \ + "B" + # "B" + # "Bd"*len(self.entries) + + def __init__(self, device_id: Device, response_type: ResponseType): # num_entries: int, entries: list[tuple[int, float]] + self.device_id = device_id + self.response_type = response_type + # self.num_entries = num_entries + # self.entries = entries + self.valid = self.get_validity() + + def __eq__(self, other): + return self.device_id == other.device_id and \ + self.response_type == other.response_type and \ + self.valid == other.get_validity() + # self.num_entries == other.num_entries and \ + # self.entries == other.entries and \ + + def get_validity(self) -> bool: + all_valid = True + if self.device_id < 0 or self.device_id >= len(Device): + all_valid = False + # elif self.num_entries != len(self.entries): + # all_valid = False + try: + ResponseType(self.response_type) + except ValueError: + all_valid = False + return all_valid + + def encode(self) -> bytes: + return struct.pack(self.get_format_string(), + self.device_id, + self.response_type) # self.num_entries, *self.entries + + @classmethod + def decode(cls, data: bytes) -> Self: + unpacked_data = struct.unpack(cls.get_format_string(), data) + + # this is code for device_id, response_type, num_entries, and entries[] + # constant_part_format_string = "!BBB" + # offset = struct.calcsize(constant_part_format_string) + # device_id, response_type, num_entries = struct.unpack(constant_part_format_string, data[:offset]) + # + # entries_format_string = "Bd"*num_entries + # entries_as_tuple = struct.unpack(entries_format_string, data[offset:]) + # entries = [(entries_as_tuple[i], entries_as_tuple[i + 1]) for i in range(0, len(entries_as_tuple), 2)] + return HealthResponse(unpacked_data[0], unpacked_data[1]) + + def to_terminal_output_string(self) -> str: + return f"{self.device_id} was requested" # not sure what the output should be or if necessary diff --git a/setup.py b/setup.py index a72df49..d6a2885 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import find_packages setup(name='EosLib', - version='4.6.0', + version='4.7.0', description='Library of shared code between EosPayload and EosGround', author='Lightning From The Edge of Space', author_email='thomasmholder@gmail.com',