mirror of
https://github.com/meshcore-dev/meshcore_py.git
synced 2026-04-20 22:13:49 +00:00
formating lpp
This commit is contained in:
parent
44671259b5
commit
f190b60426
2 changed files with 31 additions and 26 deletions
|
|
@ -5,13 +5,13 @@ import json
|
|||
from .events import Event, EventType
|
||||
from cayennelpp import LppFrame, LppData
|
||||
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")
|
||||
|
||||
class BinaryReqType(Enum):
|
||||
TELEMETRY = 3
|
||||
AMM = 4
|
||||
MMA = 4
|
||||
|
||||
def lpp_parse(buf):
|
||||
"""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))
|
||||
|
||||
def lpp_parse_amm(buf):
|
||||
def lpp_parse_mma (buf):
|
||||
i = 0
|
||||
res = []
|
||||
while i < len(buf) and buf[i] != 0:
|
||||
|
|
@ -34,17 +34,17 @@ def lpp_parse_amm(buf):
|
|||
lpp_type = LppType.get_lpp_type(type)
|
||||
size = lpp_type.size
|
||||
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
|
||||
max = lpp_type.decode(buf[i:i+size])
|
||||
max = lpp_format_val(lpp_type, lpp_type.decode(buf[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
|
||||
res.append({"channel":chan,
|
||||
"type":my_lpp_types[type][0],
|
||||
"avg":avg[0],
|
||||
"min":min[0],
|
||||
"max":max[0],
|
||||
"min":min,
|
||||
"max":max,
|
||||
"avg":avg,
|
||||
})
|
||||
return res
|
||||
|
||||
|
|
@ -74,7 +74,7 @@ class BinaryCommandHandler :
|
|||
return res2.payload
|
||||
|
||||
async def req_telemetry (self, contact) :
|
||||
code = BinaryReqType.TELEMETRY
|
||||
code = BinaryReqType.TELEMETRY.value
|
||||
req = code.to_bytes(1, 'little', signed=False)
|
||||
res = await self.req_binary(contact, req)
|
||||
if (res is None) :
|
||||
|
|
@ -82,8 +82,8 @@ class BinaryCommandHandler :
|
|||
else:
|
||||
return lpp_parse(bytes.fromhex(res["data"]))
|
||||
|
||||
async def req_amm (self, contact, start, end) :
|
||||
code = 4
|
||||
async def req_mma (self, contact, start, end) :
|
||||
code = BinaryReqType.MMA.value
|
||||
req = code.to_bytes(1, 'little', signed=False)\
|
||||
+ start.to_bytes(4, 'little', signed = False)\
|
||||
+ end.to_bytes(4, 'little', signed=False)\
|
||||
|
|
@ -92,4 +92,4 @@ class BinaryCommandHandler :
|
|||
if (res is None) :
|
||||
return None
|
||||
else:
|
||||
return lpp_parse_amm(bytes.fromhex(res["data"])[4:])
|
||||
return lpp_parse_mma(bytes.fromhex(res["data"])[4:])
|
||||
|
|
|
|||
|
|
@ -33,6 +33,20 @@ my_lpp_types = {
|
|||
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) :
|
||||
"""Encode LppType, LppData, and LppFrame to JSON."""
|
||||
if isinstance(obj, LppFrame):
|
||||
|
|
@ -40,17 +54,8 @@ def lpp_json_encoder (obj, types = my_lpp_types) :
|
|||
if isinstance(obj, LppType):
|
||||
return my_lpp_types[obj.type][0]
|
||||
if isinstance(obj, LppData):
|
||||
d = {"channel" : obj.channel, "type" : obj.type}
|
||||
if my_lpp_types[obj.type.type][1] is None :
|
||||
d["value"] = 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
|
||||
return {"channel" : obj.channel,
|
||||
"type" : obj.type,
|
||||
"value" : lpp_format_val(obj.type, obj.value)
|
||||
}
|
||||
raise TypeError(repr(obj) + " is not JSON serialized")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue