-
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.
Add abstract format classes and update Position
- Loading branch information
1 parent
75b33e8
commit a660524
Showing
5 changed files
with
168 additions
and
115 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from typing_extensions import Self | ||
|
||
import EosLib | ||
|
||
|
||
class BaseFormat(ABC): | ||
@abstractmethod | ||
def __init__(self): | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def encode(self) -> bytes: | ||
raise NotImplementedError | ||
|
||
def encode_for_transmit(self) -> bytes: | ||
return self.encode()[0:EosLib.packet.Packet.radio_body_max_bytes] | ||
|
||
@classmethod | ||
@abstractmethod | ||
def decode(cls, data: bytes | EosLib.packet.Packet) -> Self: | ||
raise NotImplementedError |
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,20 @@ | ||
from abc import ABC | ||
from abc import abstractmethod | ||
from typing_extensions import Self | ||
|
||
from EosLib.format.base_format import BaseFormat | ||
|
||
|
||
class CsvFormat(BaseFormat, ABC): | ||
@abstractmethod | ||
def get_csv_headers(self): | ||
raise NotImplementedError | ||
|
||
@abstractmethod | ||
def encode_to_csv(self) -> str: | ||
raise NotImplementedError | ||
|
||
@classmethod | ||
@abstractmethod | ||
def decode_from_csv(cls, csv: str) -> Self: | ||
raise NotImplementedError |
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 |
---|---|---|
|
@@ -6,3 +6,4 @@ pluggy==1.0.0 | |
pyparsing==3.0.9 | ||
pytest==7.2.2 | ||
tomli==2.0.1 | ||
typing-extensions==4.6.0 |
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,66 +1,44 @@ | ||
from datetime import datetime | ||
from EosLib.packet.packet import Packet | ||
import EosLib.packet.transmit_header | ||
import EosLib.packet.data_header | ||
from EosLib.format.position import Position, FlightState | ||
|
||
import EosLib | ||
from EosLib.packet import data_header, transmit_header | ||
from EosLib.device import Device | ||
from EosLib.packet import Packet | ||
from EosLib.packet.definitions import Type | ||
|
||
def test_position_bytes(): | ||
"""Tests encode and decode functions | ||
:returns: If encode and decode work | ||
:rtype: boolean | ||
""" | ||
current_time = datetime.now() | ||
new_data = Position() | ||
|
||
# new_data.set_validity() | ||
time = current_time | ||
encoded_new_data = new_data.encode_position(time.timestamp(), 23.0, 24.0, 25.0, 26.0, 5, | ||
FlightState.NOT_SET) | ||
|
||
decoded_new_data = Position.decode_position(encoded_new_data) | ||
|
||
assert time == decoded_new_data.timestamp | ||
assert 23.0 == decoded_new_data.latitude | ||
assert 24.0 == decoded_new_data.longitude | ||
assert 25.0 == decoded_new_data.altitude | ||
assert 26.0 == decoded_new_data.speed | ||
assert 5 == decoded_new_data.number_of_satellites | ||
assert FlightState.NOT_SET == decoded_new_data.flight_state | ||
assert decoded_new_data.valid | ||
|
||
|
||
def test_position_packet(): | ||
"""Tests encode and decode functions for packet input | ||
:returns: If encode and decode work | ||
:rtype: boolean | ||
""" | ||
|
||
position_data_header = EosLib.packet.data_header.DataHeader(Device.O3, Type.POSITION) | ||
position_transmit_header = EosLib.packet.transmit_header.TransmitHeader(2) | ||
|
||
current_time = datetime.now() | ||
new_data = Position() | ||
|
||
encoded_new_data = new_data.encode_position(current_time.timestamp(), 24.0, 25.0, 26.0, 27.0, 5, | ||
FlightState.NOT_SET) | ||
packet = Packet(encoded_new_data, position_data_header, position_transmit_header) | ||
|
||
decoded_new_data = Position.decode_position(packet) | ||
|
||
assert current_time == decoded_new_data.timestamp | ||
assert 24.0 == decoded_new_data.latitude | ||
assert 25.0 == decoded_new_data.longitude | ||
assert 26.0 == decoded_new_data.altitude | ||
assert 27.0 == decoded_new_data.speed | ||
assert 5 == decoded_new_data.number_of_satellites | ||
assert FlightState.NOT_SET == decoded_new_data.flight_state | ||
assert decoded_new_data.valid | ||
|
||
import datetime | ||
|
||
from EosLib.format.position import Position | ||
from EosLib.format.position import FlightState | ||
|
||
|
||
def get_good_position(): | ||
return Position(datetime.datetime.now(), | ||
33.7756, | ||
84.3963, | ||
974.5, | ||
70.2, | ||
5, | ||
FlightState.DESCENT) | ||
|
||
|
||
def test_encode_decode_bytes(): | ||
base_position = get_good_position() | ||
base_position_bytes = base_position.encode() | ||
new_position = Position.decode(base_position_bytes) | ||
valid_decode = base_position.gps_time == new_position.gps_time and \ | ||
base_position.latitude == new_position.latitude and \ | ||
base_position.longitude == new_position.longitude and \ | ||
base_position.altitude == new_position.altitude and \ | ||
base_position.speed == new_position.speed and \ | ||
base_position.number_of_satellites == new_position.number_of_satellites and\ | ||
base_position.flight_state == new_position.flight_state | ||
print("loading") | ||
assert valid_decode | ||
|
||
|
||
def test_encode_decode_csv(): | ||
base_position = get_good_position() | ||
base_position_csv = base_position.encode_to_csv() | ||
new_position = Position.decode_from_csv(base_position_csv) | ||
valid_decode = base_position.gps_time == new_position.gps_time and \ | ||
base_position.latitude == new_position.latitude and \ | ||
base_position.longitude == new_position.longitude and \ | ||
base_position.altitude == new_position.altitude and \ | ||
base_position.speed == new_position.speed and \ | ||
base_position.number_of_satellites == new_position.number_of_satellites and\ | ||
base_position.flight_state == new_position.flight_state | ||
print("loading") | ||
assert valid_decode |