-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from VIP-LES/101-health-response-format
101 health response format
- Loading branch information
Showing
4 changed files
with
80 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]', | ||
|