formating lpp

This commit is contained in:
Florent
2025-07-15 18:03:01 +02:00
parent 44671259b5
commit f190b60426
2 changed files with 31 additions and 26 deletions

View File

@@ -5,13 +5,13 @@ import json
from .events import Event, EventType from .events import Event, EventType
from cayennelpp import LppFrame, LppData from cayennelpp import LppFrame, LppData
from cayennelpp.lpp_type import LppType from cayennelpp.lpp_type import LppType
from meshcore.lpp_json_encoder import lpp_json_encoder, my_lpp_types from meshcore.lpp_json_encoder import lpp_json_encoder, my_lpp_types, lpp_format_val
logger = logging.getLogger("meshcore") logger = logging.getLogger("meshcore")
class BinaryReqType(Enum): class BinaryReqType(Enum):
TELEMETRY = 3 TELEMETRY = 3
AMM = 4 MMA = 4
def lpp_parse(buf): def lpp_parse(buf):
"""Parse a given byte string and return as a LppFrame object.""" """Parse a given byte string and return as a LppFrame object."""
@@ -24,7 +24,7 @@ def lpp_parse(buf):
return json.loads(json.dumps(LppFrame(lpp_data_list), default=lpp_json_encoder)) return json.loads(json.dumps(LppFrame(lpp_data_list), default=lpp_json_encoder))
def lpp_parse_amm(buf): def lpp_parse_mma (buf):
i = 0 i = 0
res = [] res = []
while i < len(buf) and buf[i] != 0: while i < len(buf) and buf[i] != 0:
@@ -34,17 +34,17 @@ def lpp_parse_amm(buf):
lpp_type = LppType.get_lpp_type(type) lpp_type = LppType.get_lpp_type(type)
size = lpp_type.size size = lpp_type.size
i = i + 1 i = i + 1
min = lpp_type.decode(buf[i:i+size]) min = lpp_format_val(lpp_type, lpp_type.decode(buf[i:i+size]))
i = i + size i = i + size
max = lpp_type.decode(buf[i:i+size]) max = lpp_format_val(lpp_type, lpp_type.decode(buf[i:i+size]))
i = i + size i = i + size
avg = lpp_type.decode(buf[i:i+size]) avg = lpp_format_val(lpp_type, lpp_type.decode(buf[i:i+size]))
i = i + size i = i + size
res.append({"channel":chan, res.append({"channel":chan,
"type":my_lpp_types[type][0], "type":my_lpp_types[type][0],
"avg":avg[0], "min":min,
"min":min[0], "max":max,
"max":max[0], "avg":avg,
}) })
return res return res
@@ -74,7 +74,7 @@ class BinaryCommandHandler :
return res2.payload return res2.payload
async def req_telemetry (self, contact) : async def req_telemetry (self, contact) :
code = BinaryReqType.TELEMETRY code = BinaryReqType.TELEMETRY.value
req = code.to_bytes(1, 'little', signed=False) req = code.to_bytes(1, 'little', signed=False)
res = await self.req_binary(contact, req) res = await self.req_binary(contact, req)
if (res is None) : if (res is None) :
@@ -82,8 +82,8 @@ class BinaryCommandHandler :
else: else:
return lpp_parse(bytes.fromhex(res["data"])) return lpp_parse(bytes.fromhex(res["data"]))
async def req_amm (self, contact, start, end) : async def req_mma (self, contact, start, end) :
code = 4 code = BinaryReqType.MMA.value
req = code.to_bytes(1, 'little', signed=False)\ req = code.to_bytes(1, 'little', signed=False)\
+ start.to_bytes(4, 'little', signed = False)\ + start.to_bytes(4, 'little', signed = False)\
+ end.to_bytes(4, 'little', signed=False)\ + end.to_bytes(4, 'little', signed=False)\
@@ -92,4 +92,4 @@ class BinaryCommandHandler :
if (res is None) : if (res is None) :
return None return None
else: else:
return lpp_parse_amm(bytes.fromhex(res["data"])[4:]) return lpp_parse_mma(bytes.fromhex(res["data"])[4:])

View File

@@ -33,6 +33,20 @@ my_lpp_types = {
142: ('switch', []), 142: ('switch', []),
} }
def lpp_format_val(type, val):
if my_lpp_types[type.type][1] is None :
return val
if len(my_lpp_types[type.type][1]) == 0 :
return val[0]
val_dict = {}
i = 0
for t in my_lpp_types[type.type][1] :
val_dict[t] = val[i]
i = i + 1
return val_dict
def lpp_json_encoder (obj, types = my_lpp_types) : def lpp_json_encoder (obj, types = my_lpp_types) :
"""Encode LppType, LppData, and LppFrame to JSON.""" """Encode LppType, LppData, and LppFrame to JSON."""
if isinstance(obj, LppFrame): if isinstance(obj, LppFrame):
@@ -40,17 +54,8 @@ def lpp_json_encoder (obj, types = my_lpp_types) :
if isinstance(obj, LppType): if isinstance(obj, LppType):
return my_lpp_types[obj.type][0] return my_lpp_types[obj.type][0]
if isinstance(obj, LppData): if isinstance(obj, LppData):
d = {"channel" : obj.channel, "type" : obj.type} return {"channel" : obj.channel,
if my_lpp_types[obj.type.type][1] is None : "type" : obj.type,
d["value"] = obj.value "value" : lpp_format_val(obj.type, obj.value)
elif len(my_lpp_types[obj.type.type][1]) == 0 : }
d["value"] = obj.value[0]
else :
val_dict = {}
i = 0
for t in my_lpp_types[obj.type.type][1] :
val_dict[t] = obj.value[i]
i = i + 1
d["value"] = val_dict
return d
raise TypeError(repr(obj) + " is not JSON serialized") raise TypeError(repr(obj) + " is not JSON serialized")