diff --git a/fvhiot/parsers/sensornode.py b/fvhiot/parsers/sensornode.py index 0dbb198..ff02d76 100644 --- a/fvhiot/parsers/sensornode.py +++ b/fvhiot/parsers/sensornode.py @@ -60,9 +60,9 @@ def parse_sensornode(hex_str: str, port: int) -> dict: # Remove next chunk from hex payload chunk = hex_str[:s] hex_str = hex_str[s:] - if len(hex_str) == 0: - break if _id in [1]: # List contains fields not to parse + if len(hex_str) == 0: + break # Get and remove next id from hex payload _id = int(hex_str[:2], 16) hex_str = hex_str[2:] @@ -83,6 +83,9 @@ def convert_deg(b): elif _id in [43]: # temp & humidity data["temprh_temp"] = struct.unpack(" {}".format(s[0], s[1], json.dumps(create_datalines(s[0], s[1], now), indent=2))) except ValueError as err: print(f"Invalid payload + FPort '{s[0]}:{s[1]}' or payload size {len(s[0])}: {err}") + raise print(f"\nUsage: {sys.argv[0]} hex_payload port\n\n") @@ -141,5 +145,6 @@ def main(samples: list): ["ffffffffffff2b840846299108143414", 10], ["0d0016090028b30b143414", 21], ["041528c22ea00000000000d72bc22ea000000000001a30c22ea0000000000000", 2], + ["8c14280b09297808", 20], ] main(examples) diff --git a/tests/test_parsers.py b/tests/test_parsers.py index 700be61..416edbc 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -80,6 +80,7 @@ def test_lat_lon(self): d = sensornode.create_datalines("90e12357f20e0140010205", 10, TS) assert 60.1985024 == d[0]["data"]["lat"] assert 25.0763008 == d[0]["data"]["lon"] + assert 2 == len(d[0]["data"]) assert TS == d[0]["time"] def test_lat_lon_batt_temp_in(self): @@ -88,6 +89,8 @@ def test_lat_lon_batt_temp_in(self): assert 25.1148032 == d[0]["data"]["lon"] assert 4.756 == d[0]["data"]["batt"] assert 4.42 == d[0]["data"]["temp_in"] + assert 3.43 == d[0]["data"]["temp_out1"] + assert 5 == len(d[0]["data"]) assert TS == d[0]["time"] def test_temprh_temp_rh_temp_out1(self): @@ -95,6 +98,8 @@ def test_temprh_temp_rh_temp_out1(self): assert 21.8 == d[0]["data"]["temprh_temp"] assert 35.0 == d[0]["data"]["temprh_rh"] assert 21.93 == d[0]["data"]["temp_out1"] + assert 5.172 == d[0]["data"]["batt"] + assert 4 == len(d[0]["data"]) assert TS == d[0]["time"] def test_analog1_analog2_temp_in(self): @@ -102,8 +107,19 @@ def test_analog1_analog2_temp_in(self): assert 0.013 == d[0]["data"]["analog1"] assert 0.009 == d[0]["data"]["analog2"] assert 29.95 == d[0]["data"]["temp_in"] + assert 5.172 == d[0]["data"]["batt"] + assert 4 == len(d[0]["data"]) assert TS == d[0]["time"] + def test_batt_temp_in_temp_out1(self): + d = sensornode.create_datalines("8c14280b09297808", 20, TS) + assert 5.260 == d[0]["data"]["batt"] + assert 23.15 == d[0]["data"]["temp_in"] + assert 21.68 == d[0]["data"]["temp_out1"] + assert 3 == len(d[0]["data"]) + assert TS == d[0]["time"] + + def test_empty(self): d = sensornode.create_datalines("041528c22ea00000000000d72bc22ea000000000001a30c22ea0000000000000", 2, TS) assert d[0]["data"] == {}