From e69efc8c25f3e27ec025a7fccbe3fce0d4bb3b57 Mon Sep 17 00:00:00 2001 From: jthomas462 Date: Wed, 28 Feb 2024 17:56:06 -0500 Subject: [PATCH 1/2] health query format created --- EosLib/format/formats/health/health_query.py | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 EosLib/format/formats/health/health_query.py diff --git a/EosLib/format/formats/health/health_query.py b/EosLib/format/formats/health/health_query.py new file mode 100644 index 0000000..53d9a11 --- /dev/null +++ b/EosLib/format/formats/health/health_query.py @@ -0,0 +1,67 @@ +from abc import abstractmethod +from dataclasses import dataclass +from enum import IntEnum, unique +from typing_extensions import Self +import struct + +from EosLib.device import Device +from EosLib.format.base_format import BaseFormat +from EosLib.format.definitions import Type + + +@dataclass +class HealthQuery(BaseFormat): + + def __init__(self, device_id: Device, device: IntEnum, requested_fields_bitmask: int): + self.device_id = device_id + self.device = device + self.requested_fields_bitmask = requested_fields_bitmask + self.valid = self.get_validity() + + @staticmethod + def get_format_string(self) -> str: + return "!" \ + "B" \ + "B" \ + "B" + + @staticmethod + def _i_promise_all_abstract_methods_are_implemented() -> bool: + """ This method exists because if you use a dataclass-based format ABC gets mad because it can't figure out + that the dataclass implements __init__, __eq__, etc. + + + :return: True if isabstract check should be bypassed, otherwise False + """ + return True + + @staticmethod + @abstractmethod + def get_format_type() -> Type: + return Type.HEALTH_QUERY + # raise NotImplementedError + + @abstractmethod + def encode(self) -> bytes: + return struct.pack(self.get_format_string(), + self.device_id, + self.device, + self.requested_fields_bitmask + + ) + # raise NotImplementedError + + @classmethod + @abstractmethod + def decode(cls, data: bytes) -> Self: + unpacked_data = struct.unpack(cls.get_format_string(), data) + return HealthQuery(unpacked_data[0], unpacked_data[1], unpacked_data[2]) + # raise NotImplementedError + + @abstractmethod + def get_validity(self) -> bool: + return ( + 0 <= self.device_id <= len(Device) + and 0 <= self.requested_fields_bitmask <= 255 + ) + # raise NotImplementedError From 2177ea1cb44a91e1910564f1f4074ae8b1d42e8b Mon Sep 17 00:00:00 2001 From: jthomas462 Date: Thu, 29 Feb 2024 18:49:18 -0500 Subject: [PATCH 2/2] Updated health query format with updated decisions, added to definitions, and added to init under formats --- EosLib/format/__init__.py | 2 +- EosLib/format/definitions.py | 1 + EosLib/format/formats/health/health_query.py | 20 ++++++++++---------- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/EosLib/format/__init__.py b/EosLib/format/__init__.py index 9370def..816638e 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_query from EosLib.format.definitions import Type as Type diff --git a/EosLib/format/definitions.py b/EosLib/format/definitions.py index f415f32..d4f2c29 100644 --- a/EosLib/format/definitions.py +++ b/EosLib/format/definitions.py @@ -18,4 +18,5 @@ class Type(IntEnum): E_FIELD = 12 SCIENCE_DATA = 13 DRIVER_HEALTH_REPORT = 14 + HEALTH_QUERY = 16 ERROR = 255 diff --git a/EosLib/format/formats/health/health_query.py b/EosLib/format/formats/health/health_query.py index 53d9a11..a2d2104 100644 --- a/EosLib/format/formats/health/health_query.py +++ b/EosLib/format/formats/health/health_query.py @@ -8,20 +8,22 @@ from EosLib.format.base_format import BaseFormat from EosLib.format.definitions import Type - +@unique +class QueryType(IntEnum): + DEVICE = 0 + DEVICE_HISTORY = 1 + LOGS = 2 @dataclass class HealthQuery(BaseFormat): - def __init__(self, device_id: Device, device: IntEnum, requested_fields_bitmask: int): + def __init__(self, device_id: Device, query_type: QueryType): self.device_id = device_id - self.device = device - self.requested_fields_bitmask = requested_fields_bitmask + self.query_type = query_type self.valid = self.get_validity() @staticmethod def get_format_string(self) -> str: return "!" \ - "B" \ "B" \ "B" @@ -45,9 +47,7 @@ def get_format_type() -> Type: def encode(self) -> bytes: return struct.pack(self.get_format_string(), self.device_id, - self.device, - self.requested_fields_bitmask - + self.query_type ) # raise NotImplementedError @@ -55,13 +55,13 @@ def encode(self) -> bytes: @abstractmethod def decode(cls, data: bytes) -> Self: unpacked_data = struct.unpack(cls.get_format_string(), data) - return HealthQuery(unpacked_data[0], unpacked_data[1], unpacked_data[2]) + return HealthQuery(unpacked_data[0], unpacked_data[1]) # raise NotImplementedError @abstractmethod def get_validity(self) -> bool: return ( 0 <= self.device_id <= len(Device) - and 0 <= self.requested_fields_bitmask <= 255 + and 0 <= self.query_type <= len(QueryType) ) # raise NotImplementedError