From 466afba00cddd1c0cc30a3ead202c1e29a472197 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Sun, 6 Oct 2024 13:14:22 -0400 Subject: [PATCH] Update relay storage considerations --- opensensor/collection_apis.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/opensensor/collection_apis.py b/opensensor/collection_apis.py index 8c64cde..9d55f16 100644 --- a/opensensor/collection_apis.py +++ b/opensensor/collection_apis.py @@ -140,6 +140,8 @@ def _record_data_to_ts_collection( if value is not None: if key == "unit": doc_to_insert[column_name + "_unit"] = value + elif key == "relays": + doc_to_insert[column_name] = json.dumps(value) else: doc_to_insert[column_name] = str(value) @@ -580,11 +582,14 @@ def sample_and_paginate_collection( relays = [] for item in raw_data: for relay in item["relays"]: - if isinstance(relay, str): - relay = json.loads(relay) - if isinstance(relay, list): - relay = relay[0] - relays.append(RelayStatus(**relay)) + try: + if isinstance(relay, str): + relay = json.loads(relay) + if isinstance(relay, list): + relay = relay[0] + relays.append(RelayStatus(**relay)) + except Exception: + pass # Ignore invalid relay data relay_board = RelayBoard(relays=relays) data.append(relay_board) else: