From c0ab3351c9f30257016f6291502fd208b698753c Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 13 Nov 2023 13:48:08 +0100 Subject: [PATCH] that's a lot of work.. --- eive_tmtc/tmtc/power/tm.py | 151 +++++++++++++++++++++++++------------ 1 file changed, 101 insertions(+), 50 deletions(-) diff --git a/eive_tmtc/tmtc/power/tm.py b/eive_tmtc/tmtc/power/tm.py index 650bea1..7226e8b 100644 --- a/eive_tmtc/tmtc/power/tm.py +++ b/eive_tmtc/tmtc/power/tm.py @@ -1,6 +1,9 @@ +import dataclasses +from datetime import datetime import struct +import logging import sqlite3 -from typing import List, Optional, Tuple +from typing import List, Tuple from uuid import UUID from eive_tmtc.tmtc.power.acu import acu_config_table_handler @@ -22,6 +25,8 @@ from eive_tmtc.config.object_ids import ( ACU_HANDLER_ID, ) +_LOGGER = logging.getLogger(__name__) + P60_INDEX_LIST = [ "ACU VCC", "PDU1 VCC", @@ -149,11 +154,23 @@ class DevicesInfoParser: return "Unknown Type" +@dataclasses.dataclass +class PduData: + boot_count: int + batt_mode: int + temperature: float + vcc: int + vbat: int + out_enables: List[bool] + voltages: List[int] + currents: List[int] + + def handle_pdu_data( hk_data: bytes, hk_packet: Service3FsfwTm, packet_uuid: UUID, - con: Optional[sqlite3.Connection], + con: sqlite3.Connection, pw: PrintWrapper, pdu_idx: int, set_id: int, @@ -230,58 +247,92 @@ def handle_pdu_data( f"Boot Count {boot_count} | Battery Mode {batt_mode} | " f"Temperature {temperature} | VCC {vcc} | VBAT {vbat}" ) - if con is not None: - packet_dt = hk_packet.pus_tm.time_provider.as_datetime() # type: ignore - cursor = con.cursor() - if pdu_idx == 1: - tbl_base_name = "Pdu1_" - channel_list = PDU1_CHANNELS_NAMES - else: - tbl_base_name = "Pdu2_" - channel_list = PDU2_CHANNELS_NAMES - cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS {tbl_base_name}BootCount( - packet_uuid TEXT PRIMARY KEY, - generation_time TEXT, - Count NUM - )""" + try: + handle_pdu_db_insertion( + con, + packet_uuid, + hk_packet.pus_tm.time_provider.as_datetime(), # type: ignore + pdu_idx, + PduData( + boot_count, + batt_mode, + temperature, + vcc, + vbat, + output_enb_list, + voltage_list, + current_list, + ), ) - cursor.execute( - f"INSERT INTO {tbl_base_name}BootCount VALUES(?, ?, ?)", - (str(packet_uuid), packet_dt, boot_count), - ) - - for idx, name in enumerate(channel_list): - words = name.split() - camel_case_name = "".join(word.capitalize() for word in words) - tbl_name = f"{tbl_base_name}{camel_case_name}" - print(f"creating table {tbl_name}") - cursor.execute( - f""" - CREATE TABLE IF NOT EXISTS {tbl_name}( - packet_uuid TEXT PRIMARY KEY, - generation_time TEXT, - out_enable NUM, - voltage NUM, - current NUM - )""" - ) - value_tuple = ( - str(packet_uuid), - packet_dt, - output_enb_list[idx], - voltage_list[idx], - current_list[idx], - ) - print(value_tuple) - cursor.execute( - f"INSERT INTO {tbl_name} VALUES(?, ?, ?, ?, ?)", value_tuple - ) - con.commit() + except sqlite3.OperationalError as e: + _LOGGER.warning(f"SQLite error {e}") pw.dlog(info) +def handle_pdu_db_insertion( + con: sqlite3.Connection, + packet_uuid: UUID, + packet_dt: datetime, + pdu_idx: int, + pdu_data: PduData, +): + cursor = con.cursor() + if pdu_idx == 1: + tbl_base_name = "pdu1" + channel_list = PDU1_CHANNELS_NAMES + else: + tbl_base_name = "pdu2" + channel_list = PDU2_CHANNELS_NAMES + cursor.execute( + f""" + CREATE TABLE IF NOT EXISTS {tbl_base_name}( + packet_uuid TEXT PRIMARY KEY, + generation_time TEXT, + boot_count NUM, + bat_mode NUM, + temp REAL, + vcc NUM, + vbat NUM + )""" + ) + cursor.execute( + f"INSERT INTO {tbl_base_name} VALUES(?, ?, ?, ?, ?, ?, ?)", + ( + str(packet_uuid), + packet_dt, + pdu_data.boot_count, + pdu_data.batt_mode, + pdu_data.temperature, + pdu_data.vcc, + pdu_data.vbat, + ), + ) + + for idx, name in enumerate(channel_list): + words = name.split() + camel_case_name = "_".join(word.lower() for word in words) + tbl_name = f"{tbl_base_name}_{camel_case_name}" + cursor.execute( + f""" + CREATE TABLE IF NOT EXISTS {tbl_name}( + packet_uuid TEXT PRIMARY KEY, + generation_time TEXT, + out_enable NUM, + voltage NUM, + current NUM + )""" + ) + value_tuple = ( + str(packet_uuid), + packet_dt, + pdu_data.out_enables[idx], + pdu_data.voltages[idx], + pdu_data.currents[idx], + ) + cursor.execute(f"INSERT INTO {tbl_name} VALUES(?, ?, ?, ?, ?)", value_tuple) + con.commit() + + def handle_p60_hk_data(pw: PrintWrapper, set_id: int, hk_data: bytes): if set_id == SetId.CORE: pw.dlog("Received P60 Core HK. Voltages in mV, currents in mA")