diff --git a/globals.py b/globals.py index 72ab0ad..bb97fd7 100644 --- a/globals.py +++ b/globals.py @@ -9,4 +9,5 @@ selected_channel = 0 selected_message = 0 selected_node = 0 -current_window = 0 \ No newline at end of file +current_window = 0 +connected = False \ No newline at end of file diff --git a/main.py b/main.py index 469fa1c..715743e 100644 --- a/main.py +++ b/main.py @@ -14,6 +14,7 @@ from utilities.arg_parser import setup_parser from utilities.interfaces import initialize_interface +from ui.curses_ui import on_connection from message_handlers.rx_handler import on_receive from ui.curses_ui import main_ui, draw_splash from utilities.utils import get_channels, get_node_list, get_nodeNum @@ -47,6 +48,7 @@ def main(stdscr): globals.channel_list = get_channels() globals.node_list = get_node_list() pub.subscribe(on_receive, 'meshtastic.receive') + pub.subscribe(on_connection, "meshtastic.connection") init_nodedb() load_messages_from_db() logging.info("Starting main UI") diff --git a/message_handlers/rx_handler.py b/message_handlers/rx_handler.py index 4e0e311..4bb505c 100644 --- a/message_handlers/rx_handler.py +++ b/message_handlers/rx_handler.py @@ -1,5 +1,6 @@ import logging import time +from datetime import datetime from utilities.utils import get_node_list from ui.curses_ui import draw_packetlog_win, draw_node_list, draw_messages_window, draw_channel_list, add_notification from db_handler import save_message_to_db, maybe_store_nodeinfo_in_db, get_name_from_database @@ -7,8 +8,6 @@ import globals -from datetime import datetime - def on_receive(packet, interface): # Update packet log diff --git a/ui/curses_ui.py b/ui/curses_ui.py index ace7f2a..0abbd58 100644 --- a/ui/curses_ui.py +++ b/ui/curses_ui.py @@ -1,5 +1,7 @@ import curses import textwrap +import logging +from pubsub import pub from utilities.utils import get_channels, get_time_ago from settings import settings_menu from message_handlers.tx_handler import send_message, send_traceroute @@ -9,6 +11,31 @@ import ui.dialog import globals +def on_connection(interface, topic=pub.AUTO_TOPIC) -> None: + """Callback invoked when we connect/disconnect from a radio.""" + connection_status = topic.getName() + + if connection_status == "meshtastic.connection.established": + globals.connected = True + elif connection_status == "meshtastic.connection.lost": + globals.connected = False + + logging.info(f"Connection changed: {connection_status}") + draw_connection_indicator() + +def draw_connection_indicator(): + """Draw a connection indicator (green dot if connected, red if disconnected) in the input window.""" + + height, width = entry_win.getmaxyx() + + indicator_x = width - 3 + indicator_y = 1 + + entry_win.addstr(indicator_y, indicator_x, "✔" if globals.connected else "✖", get_color("input", bold=True)) + curses.curs_set(0) + entry_win.refresh() + + def draw_node_details(): node = None try: @@ -447,7 +474,8 @@ def main_ui(stdscr): handle_resize(stdscr, True) while True: - draw_text_field(entry_win, f"Input: {input_text[-(stdscr.getmaxyx()[1] - 10):]}", get_color("input")) + draw_connection_indicator() + draw_text_field(entry_win, f"Input: {input_text[-(stdscr.getmaxyx()[1] - 12):]}", get_color("input")) # Get user input from entry window char = entry_win.get_wch()