Skip to content

Commit

Permalink
Merge pull request #105 from VIP-LES/101-health-response-format
Browse files Browse the repository at this point in the history
101 health response format
  • Loading branch information
panyab authored Mar 6, 2024
2 parents 67e89b3 + 5249daf commit b3ea193
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
2 changes: 1 addition & 1 deletion EosLib/format/__init__.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions EosLib/format/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ class Type(IntEnum):
SCIENCE_DATA = 13
DRIVER_HEALTH_REPORT = 14
FIELDMILL = 15
HEALTH_RESPONSE = 17
ERROR = 255
77 changes: 77 additions & 0 deletions EosLib/format/formats/health/health_response.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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='[email protected]',
Expand Down

0 comments on commit b3ea193

Please sign in to comment.