use pydantic instead of serde in Python
This commit is contained in:
20
pytmtc/opssat_tmtc/camera.py
Normal file
20
pytmtc/opssat_tmtc/camera.py
Normal file
@ -0,0 +1,20 @@
|
||||
import enum
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class ActionId(enum.IntEnum):
|
||||
DEFAULT_SINGLE = 1
|
||||
BALANCED_SINGLE = 2
|
||||
DEFAULT_SINGLE_FLATSAT = 3
|
||||
BALANCED_SNGLE_FLATSAT = 4
|
||||
CUSTOM_PARAMS = 5
|
||||
|
||||
|
||||
class CameraParameters(BaseModel):
|
||||
R: int
|
||||
G: int
|
||||
B: int
|
||||
N: int
|
||||
P: bool
|
||||
E: int
|
||||
W: int
|
@ -1,17 +0,0 @@
|
||||
import struct
|
||||
from serde import Model, fields
|
||||
|
||||
from opssat_tmtc.common import EXPERIMENT_APID, UniqueId, make_unique_id
|
||||
|
||||
|
||||
class CameraParameters(Model):
|
||||
R: fields.Int()
|
||||
G: fields.Int()
|
||||
B: fields.Int()
|
||||
N: fields.Int()
|
||||
P: fields.Bool()
|
||||
E: fields.Int()
|
||||
W: fields.Int()
|
||||
|
||||
def serialize_for_uplink(self) -> bytearray:
|
||||
return self.to_json().encode("utf-8")
|
@ -24,7 +24,6 @@ class UniqueId(enum.IntEnum):
|
||||
|
||||
|
||||
class EventSeverity(enum.IntEnum):
|
||||
|
||||
INFO = 0
|
||||
LOW = 1
|
||||
MEDIUM = 2
|
||||
|
@ -37,7 +37,6 @@ def create_set_mode_cmd(
|
||||
|
||||
|
||||
def create_cmd_definition_tree() -> CmdTreeNode:
|
||||
|
||||
root_node = CmdTreeNode.root_node()
|
||||
|
||||
hk_node = CmdTreeNode("hk", "Housekeeping Node", hide_children_for_print=True)
|
||||
|
@ -14,12 +14,15 @@ authors = [
|
||||
]
|
||||
dependencies = [
|
||||
"tmtccmd==8.0.0rc.2",
|
||||
"serde==0.9.0"
|
||||
"serde==0.9.0",
|
||||
"pydantic==2.7.1"
|
||||
]
|
||||
|
||||
[tool.setuptools.packages]
|
||||
find = {}
|
||||
|
||||
[tool.ruff]
|
||||
extend-exclude = ["archive"]
|
||||
[tool.ruff.lint]
|
||||
ignore = ["E501"]
|
||||
[tool.ruff.lint.extend-per-file-ignores]
|
||||
|
0
pytmtc/tests/__init__.py
Normal file
0
pytmtc/tests/__init__.py
Normal file
27
pytmtc/tests/test_cam.py
Normal file
27
pytmtc/tests/test_cam.py
Normal file
@ -0,0 +1,27 @@
|
||||
from unittest import TestCase
|
||||
from opssat_tmtc.camera_params import CameraParameters
|
||||
|
||||
|
||||
TEST_CAM_PARAMS = CameraParameters(R=8, G=8, B=8, N=1, P=True, E=200, W=1000)
|
||||
EXPECTED_JSON = '{"R":8,"G":8,"B":8,"N":1,"P":true,"E":200,"W":1000}'
|
||||
|
||||
|
||||
class TestCamInterface(TestCase):
|
||||
def test_serialization_to_dict(self):
|
||||
model = TEST_CAM_PARAMS.model_dump()
|
||||
self.assertEqual(model["R"], 8)
|
||||
self.assertEqual(model["G"], 8)
|
||||
self.assertEqual(model["B"], 8)
|
||||
self.assertEqual(model["N"], 1)
|
||||
self.assertEqual(model["P"], True)
|
||||
self.assertEqual(model["E"], 200)
|
||||
self.assertEqual(model["W"], 1000)
|
||||
|
||||
def test_serialization_to_json(self):
|
||||
json = TEST_CAM_PARAMS.model_dump_json()
|
||||
self.assertEqual(json, EXPECTED_JSON)
|
||||
print(json)
|
||||
|
||||
def test_deserialization(self):
|
||||
model_deserialized = CameraParameters.model_validate_json(EXPECTED_JSON)
|
||||
self.assertEqual(TEST_CAM_PARAMS, model_deserialized)
|
@ -1,20 +0,0 @@
|
||||
import struct
|
||||
from opssat_tmtc.camera_params import CameraParameters
|
||||
from opssat_tmtc.common import make_unique_id, EXPERIMENT_APID
|
||||
|
||||
|
||||
def test_serde_serialization():
|
||||
# Example serializatn
|
||||
data = bytearray(make_unique_id(EXPERIMENT_APID))
|
||||
params = CameraParameters(8, 8, 8, 1, True, 200, 1000)
|
||||
serialized = params.to_json().encode("utf-8")
|
||||
byte_string = bytearray(struct.pack("!{}s".format(len(serialized)), serialized))
|
||||
print(byte_string)
|
||||
print(params.serialize_for_uplink())
|
||||
data.extend(params.serialize_for_uplink())
|
||||
print(data)
|
||||
|
||||
# Example deserialization
|
||||
data = '{"R": 100, "G": 150, "B": 200, "N": 3, "P": true, "E": 10, "W": 20}'
|
||||
deserialized_params = CameraParameters.from_json(data)
|
||||
print(deserialized_params)
|
Reference in New Issue
Block a user