diff --git a/.gitignore b/.gitignore index 808386f..ec7276f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .venv/ -__pycache__/ \ No newline at end of file +__pycache__/ +client.db diff --git a/database.py b/database.py new file mode 100644 index 0000000..25f69fb --- /dev/null +++ b/database.py @@ -0,0 +1,69 @@ +import sqlite3 +import globals + +nodeinfo_table = "nodeinfo" + +def initialize_database(): + conn = sqlite3.connect(globals.db_file_path) + db_cursor = conn.cursor() + # Create the nodeinfo table for storing nodeinfos + query = f'CREATE TABLE IF NOT EXISTS {nodeinfo_table} (user_id TEXT, long_name TEXT, short_name TEXT)' + db_cursor.execute(query) + + # Create the messages table for storing messages + table_name = globals.channel_list[globals.selected_channel] + "_messages" + query = f'CREATE TABLE IF NOT EXISTS {table_name} (user_id TEXT,message_text TEXT)' + db_cursor.execute(query) + +def save_message_to_db(channel, user_id, message_text): + + conn = sqlite3.connect(globals.db_file_path) + table_name = f"{channel}_messages" + db_cursor = conn.cursor() + query = f''' + INSERT INTO {table_name} (user_id, message_text) + VALUES (?, ?) + ''' + db_cursor.execute(query, (user_id, message_text)) + conn.commit() + conn.close() + +def maybe_store_nodeinfo_in_db(packet): + """Save nodeinfo unless that record is already there.""" + + try: + with sqlite3.connect(globals.db_file_path) as db_connection: + db_cursor = db_connection.cursor() + + # Check if a record with the same user_id already exists + existing_record = db_cursor.execute(f'SELECT * FROM {nodeinfo_table} WHERE user_id=?', (packet['from'],)).fetchone() + + if existing_record is None: + + # No existing record, insert the new record + db_cursor.execute(f''' + INSERT INTO {nodeinfo_table} (user_id, long_name, short_name) + VALUES (?, ?, ?) + ''', (packet['from'], packet['decoded']['user']['longName'], packet['decoded']['user']['shortName'])) + db_connection.commit() + + else: + # Check if long_name or short_name is different, update if necessary + if existing_record[1] != packet['decoded']['user']['longName'] or existing_record[2] != packet['decoded']['user']['shortName']: + + db_cursor.execute(f''' + UPDATE {nodeinfo_table} + SET long_name=?, short_name=? + WHERE user_id=? + ''', (packet['longName'], packet['shortName'], packet['from'])) + db_connection.commit() + + # Fetch the updated record + updated_record = db_cursor.execute(f'SELECT * FROM {nodeinfo_table} WHERE user_id=?', (packet['from'],)).fetchone() + + + except sqlite3.Error as e: + print(f"SQLite error in maybe_store_nodeinfo_in_db: {e}") + + finally: + db_connection.close() diff --git a/globals.py b/globals.py index 900ae5d..139b97e 100644 --- a/globals.py +++ b/globals.py @@ -6,4 +6,5 @@ selected_channel = 0 selected_node = 0 direct_message = False interface = None -display_log = False \ No newline at end of file +display_log = False +db_file_path = "client.db" \ No newline at end of file diff --git a/main.py b/main.py index 91b45c2..0a4ab58 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,7 @@ from utilities.interfaces import initialize_interface from message_handlers.rx_handler import on_receive from ui.curses_ui import main_ui from utilities.utils import get_channels +from database import initialize_database import globals if __name__ == "__main__": @@ -21,5 +22,6 @@ if __name__ == "__main__": args = parser.parse_args() globals.interface = initialize_interface(args) globals.channel_list = get_channels() + initialize_database() pub.subscribe(on_receive, 'meshtastic.receive') curses.wrapper(main_ui) \ No newline at end of file diff --git a/message_handlers/rx_handler.py b/message_handlers/rx_handler.py index 75739e8..a629d55 100644 --- a/message_handlers/rx_handler.py +++ b/message_handlers/rx_handler.py @@ -2,9 +2,12 @@ from meshtastic import BROADCAST_NUM from utilities.utils import get_node_list, decimal_to_hex, get_nodeNum import globals from ui.curses_ui import update_packetlog_win, draw_node_list, update_messages_window, draw_channel_list, add_notification +from database import save_message_to_db, maybe_store_nodeinfo_in_db + def on_receive(packet): + # update packet log globals.packet_buffer.append(packet) if len(globals.packet_buffer) > 20: @@ -17,6 +20,7 @@ def on_receive(packet): if 'decoded' in packet and packet['decoded']['portnum'] == 'NODEINFO_APP': get_node_list() draw_node_list() + maybe_store_nodeinfo_in_db(packet) elif 'decoded' in packet and packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP': message_bytes = packet['decoded']['payload'] @@ -56,6 +60,8 @@ def on_receive(packet): draw_channel_list() update_messages_window() + save_message_to_db(globals.channel_list[channel_number], message_from_id, message_string) + except KeyError as e: print(f"Error processing packet: {e}")