|
|
|
@ -18,8 +18,7 @@ class MultiNode:
|
|
|
|
|
def __init__(self):
|
|
|
|
|
with open("Config.toml", "r") as config_file:
|
|
|
|
|
config = toml.loads(config_file.read())
|
|
|
|
|
|
|
|
|
|
print(config)
|
|
|
|
|
|
|
|
|
|
self.server_address = config["server"]["address"]
|
|
|
|
|
self.server_secret_key = config["server"]["secret_key"]
|
|
|
|
|
# How often to send the time
|
|
|
|
@ -27,6 +26,10 @@ class MultiNode:
|
|
|
|
|
self.devices = {}
|
|
|
|
|
self.last_time_message = time.time()
|
|
|
|
|
|
|
|
|
|
for node_id_str in config["node"]:
|
|
|
|
|
node_id = int(node_id_str, 16)
|
|
|
|
|
self.devices[node_id] = config["node"][node_id_str]
|
|
|
|
|
|
|
|
|
|
self.lora = LoRa(
|
|
|
|
|
0, # SPI channel
|
|
|
|
|
25, # Interrupt pin
|
|
|
|
@ -83,15 +86,30 @@ class MultiNode:
|
|
|
|
|
print(f"Invalid length! Expected {length + 9 + HASH_LENGTH} actual {len(payload.message)}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
hash_function = blake2s(key=0x0.to_bytes(8, "little"), digest_size=8)
|
|
|
|
|
if not tx_id in self.devices:
|
|
|
|
|
with open("Config.toml", "r") as config_file:
|
|
|
|
|
config = toml.loads(config_file.read())
|
|
|
|
|
for node_id_str in config["node"]:
|
|
|
|
|
node_id = int(node_id_str, 16)
|
|
|
|
|
if not node_id in self.devices:
|
|
|
|
|
self.devices[node_id] = config["node"][node_id_str]
|
|
|
|
|
|
|
|
|
|
if not tx_id in self.devices:
|
|
|
|
|
print(f"Error: Unrecognized device with ID {tx_id}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
hash_function = blake2s(key=self.devices[tx_id]["secret_key"].to_bytes(8, "little"), digest_size=8)
|
|
|
|
|
hash_function.update(payload.message[:-HASH_LENGTH])
|
|
|
|
|
|
|
|
|
|
if hash_function.digest() != data_hash:
|
|
|
|
|
print(f"Hash doesn't match! Expected {hash_function.digest()} got {data_hash}")
|
|
|
|
|
return
|
|
|
|
|
# self.devices[tx_id] = {}
|
|
|
|
|
|
|
|
|
|
if not tx_id in self.devices:
|
|
|
|
|
self.devices[tx_id] = {}
|
|
|
|
|
if "last_message_id" in self.devices[tx_id]:
|
|
|
|
|
if msg_id <= self.devices[tx_id]["last_message_id"]:
|
|
|
|
|
print(f'Error, expected an ID larger than {self.devices[tx_id]["last_message_id"]} but got {msg_id}')
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.devices[tx_id]["last_message_id"] = msg_id
|
|
|
|
|
self.decode_packet(self.devices[tx_id], data)
|
|
|
|
@ -122,19 +140,20 @@ class MultiNode:
|
|
|
|
|
def print_data(self):
|
|
|
|
|
for device_id in self.devices:
|
|
|
|
|
device = self.devices[device_id]
|
|
|
|
|
print(f'Node {device_id} @ ID {device["last_message_id"]}:')
|
|
|
|
|
|
|
|
|
|
if "status" in device:
|
|
|
|
|
print(f'\t{device["status"]["battery"]:.3f} V {device["status"]["temperature"]:.1f} °C')
|
|
|
|
|
|
|
|
|
|
if "sensors" in device:
|
|
|
|
|
for sensor in device["sensors"]:
|
|
|
|
|
if sensor["value"] == math.nan:
|
|
|
|
|
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: ERROR')
|
|
|
|
|
else:
|
|
|
|
|
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: {sensor["value"]:.4f} {self.sensor_type_table[sensor["type"]]}')
|
|
|
|
|
|
|
|
|
|
print()
|
|
|
|
|
if "last_message_id" in device:
|
|
|
|
|
print(f'Node {device_id} @ ID {device["last_message_id"]}:')
|
|
|
|
|
|
|
|
|
|
if "status" in device:
|
|
|
|
|
print(f'\t{device["status"]["battery"]:.3f} V {device["status"]["temperature"]:.1f} °C')
|
|
|
|
|
|
|
|
|
|
if "sensors" in device:
|
|
|
|
|
for sensor in device["sensors"]:
|
|
|
|
|
if sensor["value"] == math.nan:
|
|
|
|
|
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: ERROR')
|
|
|
|
|
else:
|
|
|
|
|
print(f'\tCH {sensor["channel"]} on Pin {sensor["pin"]}: {sensor["value"]:.4f} {self.sensor_type_table[sensor["type"]]}')
|
|
|
|
|
|
|
|
|
|
print()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|