Skip to content

Commit 4467125

Browse files
committed
getting amm from sensors
1 parent de00634 commit 4467125

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/meshcore/binary_commands.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,53 @@
11
import asyncio
22
import logging
3+
from enum import Enum
4+
import json
35
from .events import Event, EventType
6+
from cayennelpp import LppFrame, LppData
7+
from cayennelpp.lpp_type import LppType
8+
from meshcore.lpp_json_encoder import lpp_json_encoder, my_lpp_types
49

510
logger = logging.getLogger("meshcore")
611

12+
class BinaryReqType(Enum):
13+
TELEMETRY = 3
14+
AMM = 4
15+
16+
def lpp_parse(buf):
17+
"""Parse a given byte string and return as a LppFrame object."""
18+
i = 0
19+
lpp_data_list = []
20+
while i < len(buf) and buf[i] != 0:
21+
lppdata = LppData.from_bytes(buf[i:])
22+
lpp_data_list.append(lppdata)
23+
i = i + len(lppdata)
24+
25+
return json.loads(json.dumps(LppFrame(lpp_data_list), default=lpp_json_encoder))
26+
27+
def lpp_parse_amm(buf):
28+
i = 0
29+
res = []
30+
while i < len(buf) and buf[i] != 0:
31+
chan = buf[i]
32+
i = i + 1
33+
type = buf[i]
34+
lpp_type = LppType.get_lpp_type(type)
35+
size = lpp_type.size
36+
i = i + 1
37+
min = lpp_type.decode(buf[i:i+size])
38+
i = i + size
39+
max = lpp_type.decode(buf[i:i+size])
40+
i = i + size
41+
avg = lpp_type.decode(buf[i:i+size])
42+
i = i + size
43+
res.append({"channel":chan,
44+
"type":my_lpp_types[type][0],
45+
"avg":avg[0],
46+
"min":min[0],
47+
"max":max[0],
48+
})
49+
return res
50+
751
class BinaryCommandHandler :
852
""" Helper functions to handle binary requests through binary commands """
953
def __init__ (self, c):
@@ -28,3 +72,24 @@ async def req_binary (self, contact, request) :
2872
return None
2973
else:
3074
return res2.payload
75+
76+
async def req_telemetry (self, contact) :
77+
code = BinaryReqType.TELEMETRY
78+
req = code.to_bytes(1, 'little', signed=False)
79+
res = await self.req_binary(contact, req)
80+
if (res is None) :
81+
return None
82+
else:
83+
return lpp_parse(bytes.fromhex(res["data"]))
84+
85+
async def req_amm (self, contact, start, end) :
86+
code = 4
87+
req = code.to_bytes(1, 'little', signed=False)\
88+
+ start.to_bytes(4, 'little', signed = False)\
89+
+ end.to_bytes(4, 'little', signed=False)\
90+
+ b"\0\0"
91+
res = await self.req_binary(contact, req)
92+
if (res is None) :
93+
return None
94+
else:
95+
return lpp_parse_amm(bytes.fromhex(res["data"])[4:])

src/meshcore/reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import logging
33
import asyncio
44
import json
5-
from cayennelpp import LppFrame, LppData
65
from typing import Any, Optional, Dict
76
from .events import Event, EventType, EventDispatcher
87
from .packets import PacketType
8+
from cayennelpp import LppFrame, LppData
99
from meshcore.lpp_json_encoder import lpp_json_encoder
1010

1111
logger = logging.getLogger("meshcore")

0 commit comments

Comments
 (0)