From 67fc7661e09ae370fd88ccf34fc621ca62e4bded Mon Sep 17 00:00:00 2001 From: Ion Li Date: Tue, 27 Feb 2024 14:19:38 -0500 Subject: [PATCH 1/4] created initial health response format class and added 2 driver to Type ENUM --- EosLib/format/definitions.py | 2 + .../format/formats/health/health-response.py | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 EosLib/format/formats/health/health-response.py diff --git a/EosLib/format/definitions.py b/EosLib/format/definitions.py index f415f32..af2bf99 100644 --- a/EosLib/format/definitions.py +++ b/EosLib/format/definitions.py @@ -18,4 +18,6 @@ class Type(IntEnum): E_FIELD = 12 SCIENCE_DATA = 13 DRIVER_HEALTH_REPORT = 14 + HEALTH_QUERY = 15 + HEALTH_RESPONSE = 16 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..7467bf9 --- /dev/null +++ b/EosLib/format/formats/health/health-response.py @@ -0,0 +1,59 @@ +import struct + +from typing_extensions import Self + +from EosLib.device import Device +from EosLib.format.definitions import Type +from EosLib.format.base_format import BaseFormat + + +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" \ + "s" + + def __init__(self, device_id: Device, response_type: int, num_entries: int, entries: list[str]): + 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.num_entries == other.num_entries and \ + self.entries == other.entries and \ + self.valid == other.get_validity() + + def get_validity(self) -> bool: + if self.device_id < 0 or self.device_id >= len(Device): + return False + elif self.num_entries != len(self.entries): + return False + else: + return True + + 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) + return HealthResponse(unpacked_data[0], unpacked_data[1], unpacked_data[2], unpacked_data[3]) + + def to_terminal_output_string(self) -> str: + return f"{self.device_id} has {self.num_entries} entries" # not sure what the output should be or if necessary From 66dc440b5300d341b7ee2066f64cac5b7baf8ed5 Mon Sep 17 00:00:00 2001 From: Ion Li Date: Wed, 28 Feb 2024 18:07:58 -0500 Subject: [PATCH 2/4] modified format string and properly decode struct --- .../format/formats/health/health-response.py | 37 ++++++++++++++----- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/EosLib/format/formats/health/health-response.py b/EosLib/format/formats/health/health-response.py index 7467bf9..b53fe29 100644 --- a/EosLib/format/formats/health/health-response.py +++ b/EosLib/format/formats/health/health-response.py @@ -1,4 +1,5 @@ import struct +from enum import unique, IntEnum from typing_extensions import Self @@ -7,21 +8,27 @@ 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: + def get_format_string(self) -> str: # Struct format is: device_id, response_type, num_entries, entries[] return "!" \ "B" \ "B" \ "B" \ - "s" + "Bd"*len(self.entries) - def __init__(self, device_id: Device, response_type: int, num_entries: int, entries: list[str]): + 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 @@ -36,24 +43,34 @@ def __eq__(self, other): self.valid == other.get_validity() def get_validity(self) -> bool: + all_valid = True if self.device_id < 0 or self.device_id >= len(Device): - return False + all_valid = False elif self.num_entries != len(self.entries): - return False - else: - return True + 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) + *self.entries) @classmethod def decode(cls, data: bytes) -> Self: - unpacked_data = struct.unpack(cls.get_format_string(), data) - return HealthResponse(unpacked_data[0], unpacked_data[1], unpacked_data[2], unpacked_data[3]) + 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(device_id, response_type, num_entries, entries) def to_terminal_output_string(self) -> str: return f"{self.device_id} has {self.num_entries} entries" # not sure what the output should be or if necessary From 6b05f674709010b768f36511f52521bb8594f2d2 Mon Sep 17 00:00:00 2001 From: Ion Li Date: Thu, 29 Feb 2024 18:50:09 -0500 Subject: [PATCH 3/4] added health_response to init --- EosLib/format/__init__.py | 2 +- EosLib/format/definitions.py | 4 +- ...{health-response.py => health_response.py} | 45 ++++++++++--------- 3 files changed, 26 insertions(+), 25 deletions(-) rename EosLib/format/formats/health/{health-response.py => health_response.py} (52%) diff --git a/EosLib/format/__init__.py b/EosLib/format/__init__.py index 9370def..533df26 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 -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 af2bf99..394348a 100644 --- a/EosLib/format/definitions.py +++ b/EosLib/format/definitions.py @@ -18,6 +18,6 @@ class Type(IntEnum): E_FIELD = 12 SCIENCE_DATA = 13 DRIVER_HEALTH_REPORT = 14 - HEALTH_QUERY = 15 - HEALTH_RESPONSE = 16 + 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 similarity index 52% rename from EosLib/format/formats/health/health-response.py rename to EosLib/format/formats/health/health_response.py index b53fe29..7e5e9f2 100644 --- a/EosLib/format/formats/health/health-response.py +++ b/EosLib/format/formats/health/health_response.py @@ -20,34 +20,34 @@ def get_format_type() -> Type: return Type.HEALTH_RESPONSE @staticmethod - def get_format_string(self) -> str: + def get_format_string() -> str: # Struct format is: device_id, response_type, num_entries, entries[] return "!" \ "B" \ - "B" \ - "B" \ - "Bd"*len(self.entries) + "B" + # "B" + # "Bd"*len(self.entries) - def __init__(self, device_id: Device, response_type: ResponseType, num_entries: int, entries: list[tuple[int, float]]): + 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.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.num_entries == other.num_entries and \ - self.entries == other.entries 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 + # elif self.num_entries != len(self.entries): + # all_valid = False try: ResponseType(self.response_type) except ValueError: @@ -57,20 +57,21 @@ def get_validity(self) -> bool: def encode(self) -> bytes: return struct.pack(self.get_format_string(), self.device_id, - self.response_type, - self.num_entries, - *self.entries) + self.response_type) # self.num_entries, *self.entries @classmethod def decode(cls, data: bytes) -> Self: - 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]) + unpacked_data = struct.unpack(cls.get_format_string(), data) - 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(device_id, response_type, num_entries, entries) + # 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} has {self.num_entries} entries" # not sure what the output should be or if necessary + return f"{self.device_id} was requested" # not sure what the output should be or if necessary From 322cd114b2b6bc5c27159c3b42de51cd83c7dcad Mon Sep 17 00:00:00 2001 From: Ion Li Date: Wed, 6 Mar 2024 12:44:06 -0500 Subject: [PATCH 4/4] bumped version to 4.7.0 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index e4c0a7b..d6a2885 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import find_packages setup(name='EosLib', - version='4.5.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',