Skip to content

Commit

Permalink
Added some error handling for debug purposes and fixed a copilot typo
Browse files Browse the repository at this point in the history
  • Loading branch information
dyka3773 committed Jan 18, 2024
1 parent ac0e2e7 commit 0f876b2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
30 changes: 16 additions & 14 deletions Ground Station/dropstar_gui/telecoms/rxsm_receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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,
Expand Down Expand Up @@ -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;
'''
12 changes: 8 additions & 4 deletions Ground Station/dropstar_gui/telecoms/telecom_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
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.
"""
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:
Expand All @@ -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()
Expand Down

0 comments on commit 0f876b2

Please sign in to comment.