11import asyncio
22import logging
3+ from enum import Enum
4+ import json
35from .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
510logger = 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+
751class 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 :])
0 commit comments