Skip to content

Commit

Permalink
Make definitions available from EosLib. namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmholder committed Oct 31, 2022
1 parent a084a58 commit 217914d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions EosLib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from EosLib.packet.definitions import Type, Priority, Device, HeaderPreamble, RADIO_MAX_BYTES
16 changes: 5 additions & 11 deletions EosLib/packet/packet.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import struct

import EosLib
from EosLib.packet.transmit_header import TransmitHeader
from EosLib.packet.data_header import DataHeader
from EosLib.packet.definitions import HeaderPreamble, Priority, RADIO_MAX_BYTES
from EosLib.packet.exceptions import PacketFormatError


Expand Down Expand Up @@ -35,12 +35,12 @@ def validate_packet(self):
if self.body is None or len(self.body) == 0:
raise PacketFormatError("All packets must have a body")

if self.data_header.data_packet_priority != Priority.NO_TRANSMIT:
if self.data_header.data_packet_priority != EosLib.Priority.NO_TRANSMIT:
total_length = struct.calcsize(TransmitHeader.transmit_header_struct_format_string) + \
struct.calcsize(DataHeader.data_header_struct_format_string) + \
len(self.body)

if total_length > RADIO_MAX_BYTES:
if total_length > EosLib.RADIO_MAX_BYTES:
raise PacketFormatError("Packet is too large")

return True
Expand Down Expand Up @@ -72,13 +72,13 @@ def decode_packet(packet_bytes: bytes):
:return: The decoded Packet object
"""
decoded_packet = Packet()
if packet_bytes[0] == HeaderPreamble.TRANSMIT:
if packet_bytes[0] == EosLib.HeaderPreamble.TRANSMIT:
decoded_transmit_header = TransmitHeader.decode(
packet_bytes[0:struct.calcsize(TransmitHeader.transmit_header_struct_format_string)])
decoded_packet.transmit_header = decoded_transmit_header
packet_bytes = packet_bytes[struct.calcsize(TransmitHeader.transmit_header_struct_format_string):]

if packet_bytes[0] == HeaderPreamble.DATA:
if packet_bytes[0] == EosLib.HeaderPreamble.DATA:
decoded_data_header = DataHeader.decode(
packet_bytes[0:struct.calcsize(DataHeader.data_header_struct_format_string)])
decoded_packet.data_header = decoded_data_header
Expand All @@ -87,9 +87,3 @@ def decode_packet(packet_bytes: bytes):
decoded_packet.body = packet_bytes

return decoded_packet






10 changes: 5 additions & 5 deletions tests/packet/test_packet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
import EosLib.packet.definitions as definitions
import EosLib

from datetime import datetime
from EosLib.packet.packet import TransmitHeader, DataHeader, Packet, PacketFormatError
Expand All @@ -8,9 +8,9 @@

def get_valid_packet():
transmit_header = TransmitHeader(0, datetime.now())
data_header = DataHeader(definitions.Type.TELEMETRY,
definitions.Device.TEMPERATURE_HUMIDITY,
definitions.Priority.TELEMETRY,
data_header = DataHeader(EosLib.Type.TELEMETRY,
EosLib.Device.TEMPERATURE_HUMIDITY,
EosLib.Priority.TELEMETRY,
datetime.now())

return Packet(bytes("Hello World", 'utf-8'), data_header, transmit_header)
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_body_too_large():
def test_allow_large_body_no_transmit():
test_packet = get_valid_packet()
test_packet.body = bytearray(250)
test_packet.data_header.data_packet_priority = definitions.Priority.NO_TRANSMIT
test_packet.data_header.data_packet_priority = EosLib.Priority.NO_TRANSMIT

assert test_packet.encode_packet()

Expand Down

0 comments on commit 217914d

Please sign in to comment.