From 0f876b2d675a1b90b52274dbff3c8ce568be8375 Mon Sep 17 00:00:00 2001 From: Hercules Konsoulas Date: Thu, 18 Jan 2024 04:44:36 +0200 Subject: [PATCH] Added some error handling for debug purposes and fixed a copilot typo --- .../dropstar_gui/telecoms/rxsm_receiver.py | 30 ++++++++++--------- .../dropstar_gui/telecoms/telecom_util.py | 12 +++++--- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Ground Station/dropstar_gui/telecoms/rxsm_receiver.py b/Ground Station/dropstar_gui/telecoms/rxsm_receiver.py index efa6842..ed9c17e 100644 --- a/Ground Station/dropstar_gui/telecoms/rxsm_receiver.py +++ b/Ground Station/dropstar_gui/telecoms/rxsm_receiver.py @@ -24,7 +24,10 @@ def receive_data(): data = connection.readline() if data: logging.info(f"Received data: {data}") - insert_data_in_db(deserialize_data(data)) + try: + insert_data_in_db(deserialize_data(data)) + except Exception as e: + logging.error(f"Error in inserting data: {e}") data_has_been_received = True else: @@ -58,8 +61,12 @@ def omit_values_with_endline(x): return x[:-1] return x - deserialized_data = map(omit_values_with_endline, deserialized_data) - deserialized_data = map(omit_none_values, deserialized_data) + try: + deserialized_data = map(omit_values_with_endline, deserialized_data) + deserialized_data = map(omit_none_values, deserialized_data) + except Exception as e: + logging.error(f"Error in deserializing data: {e}") + raise e return tuple(deserialized_data) @@ -72,8 +79,12 @@ def insert_data_in_db(data: tuple): """ with sql.connect('GS_data.db', timeout=10) as db: cursor = db.cursor() - previous_row = get_previous_row_values(cursor) - coalesced_data = coalesce_data(data, previous_row) + try: + previous_row = get_previous_row_values(cursor) + coalesced_data = coalesce_data(data, previous_row) + except Exception as e: + logging.error(f"Error in coalescing data: {e}") + raise e cursor.execute(''' INSERT INTO GS_DATA ( time, @@ -122,12 +133,3 @@ def create_db(): ''') db.commit() logging.info('Created table GS_data') - - -''' --- This is the script to get the latest data from the database - SELECT TIME, MOTOR_SPEED, SOUND_CARD_STATUS, CAMERA_STATUS, TEMP_1, TEMP_2, TEMP_3, LO_SIGNAL, SOE_SIGNAL, SODS_SIGNAL, ERROR_CODE - FROM GS_DATA - ORDER BY TIME DESC - LIMIT 1; -''' diff --git a/Ground Station/dropstar_gui/telecoms/telecom_util.py b/Ground Station/dropstar_gui/telecoms/telecom_util.py index 42fc4a8..7bfc817 100644 --- a/Ground Station/dropstar_gui/telecoms/telecom_util.py +++ b/Ground Station/dropstar_gui/telecoms/telecom_util.py @@ -2,12 +2,12 @@ from typing import Any, Tuple -def coalesce_data(data: Tuple, previous_non_null_values: Tuple) -> Tuple: +def coalesce_data(data: Tuple, previous_row_values: Tuple) -> Tuple: """Coalesces the data with the previous non-null values. Args: data (Tuple): The data to be coalesced. - previous_non_null_values (Tuple): The previous non-null values. + previous_row_values (Tuple): The previous non-null values. Returns: Tuple: The coalesced data. @@ -15,7 +15,11 @@ def coalesce_data(data: Tuple, previous_non_null_values: Tuple) -> Tuple: def coalesce(x, y): return x if x is not None else y - return tuple(map(coalesce, data, previous_non_null_values)) + if len(previous_row_values) != len(data): + raise ValueError( + f"The length of the previous row values ({len(previous_row_values)}) is different from the length of the data ({len(data)}).") + + return tuple(map(coalesce, data, previous_row_values)) def get_previous_row_values(cursor: sql.Cursor) -> Tuple: @@ -29,7 +33,7 @@ def get_previous_row_values(cursor: sql.Cursor) -> Tuple: """ cursor.execute(''' SELECT * FROM GS_DATA - ORDER BY ID DESC + ORDER BY time DESC LIMIT 1 ''') previous_row = cursor.fetchone()