From 455c3b10dd804a46628a8f37471bd67ac848c6af Mon Sep 17 00:00:00 2001 From: Russell Schmidt Date: Wed, 15 Jan 2025 12:14:12 -0600 Subject: [PATCH] Sort nodes list by last heard --- globals.py | 3 ++- main.py | 5 +++-- message_handlers/rx_handler.py | 14 ++++++++++---- ui/curses_ui.py | 8 ++++---- utilities/utils.py | 10 ++++++---- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/globals.py b/globals.py index 24c9900..c0ac1fe 100644 --- a/globals.py +++ b/globals.py @@ -16,4 +16,5 @@ interface = None display_log = False message_prefix = ">>" sent_message_prefix = message_prefix + " Sent" -notification_symbol = "*" \ No newline at end of file +notification_symbol = "*" +node_list = [] diff --git a/main.py b/main.py index e3bbfdf..7d4c998 100644 --- a/main.py +++ b/main.py @@ -14,7 +14,7 @@ from utilities.arg_parser import setup_parser from utilities.interfaces import initialize_interface from message_handlers.rx_handler import on_receive from ui.curses_ui import main_ui, draw_splash -from utilities.utils import get_channels +from utilities.utils import get_channels, get_node_list from db_handler import init_nodedb, load_messages_from_db import globals @@ -30,10 +30,11 @@ def main(stdscr): globals.interface = initialize_interface(args) globals.channel_list = get_channels() pub.subscribe(on_receive, 'meshtastic.receive') + globals.node_list = get_node_list() init_nodedb() load_messages_from_db() main_ui(stdscr) if __name__ == "__main__": - curses.wrapper(main) \ No newline at end of file + curses.wrapper(main) diff --git a/message_handlers/rx_handler.py b/message_handlers/rx_handler.py index 0326701..dab6ca3 100644 --- a/message_handlers/rx_handler.py +++ b/message_handlers/rx_handler.py @@ -17,13 +17,19 @@ def on_receive(packet, interface): if globals.display_log: draw_packetlog_win() try: - if 'decoded' in packet and packet['decoded']['portnum'] == 'NODEINFO_APP': + if 'decoded' not in packet: + return + + # Assume any incoming packet could update the last seen time for a node, so we + # may need to reorder the list. This could probably be limited to specific packets. + globals.node_list = get_node_list() + draw_node_list() + + if packet['decoded']['portnum'] == 'NODEINFO_APP': if "user" in packet['decoded'] and "longName" in packet['decoded']["user"]: - get_node_list() - draw_node_list() maybe_store_nodeinfo_in_db(packet) - elif 'decoded' in packet and packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP': + elif packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP': message_bytes = packet['decoded']['payload'] message_string = message_bytes.decode('utf-8') if packet.get('channel'): diff --git a/ui/curses_ui.py b/ui/curses_ui.py index 0bd46f4..615cc8e 100644 --- a/ui/curses_ui.py +++ b/ui/curses_ui.py @@ -1,7 +1,7 @@ import curses import textwrap import globals -from utilities.utils import get_node_list, get_name_from_number, get_channels +from utilities.utils import get_name_from_number, get_channels from settings import settings from message_handlers.tx_handler import send_message @@ -133,7 +133,7 @@ def draw_node_list(): win_height = nodes_win.getmaxyx()[0] start_index = max(0, globals.selected_node - (win_height - 3)) # Calculate starting index based on selected node and window height - for i, node in enumerate(get_node_list()[start_index:], start=1): + for i, node in enumerate(globals.node_list[start_index:], start=1): if i < win_height - 1 : # Check if there is enough space in the window if globals.selected_node + 1 == start_index + i and globals.current_window == 2: nodes_win.addstr(i, 1, get_name_from_number(node, "long"), curses.color_pair(3)) @@ -169,7 +169,7 @@ def select_messages(direction): draw_messages_window() def select_nodes(direction): - node_list_length = len(get_node_list()) + node_list_length = len(globals.node_list) globals.selected_node += direction if globals.selected_node < 0: @@ -327,7 +327,7 @@ def main_ui(stdscr): elif char == curses.KEY_ENTER or char == 10 or char == 13: if globals.current_window == 2: - node_list = get_node_list() + node_list = globals.node_list if node_list[globals.selected_node] not in globals.channel_list: globals.channel_list.append(node_list[globals.selected_node]) globals.all_messages[node_list[globals.selected_node]] = [] diff --git a/utilities/utils.py b/utilities/utils.py index 77230fc..7dfcf00 100644 --- a/utilities/utils.py +++ b/utilities/utils.py @@ -34,11 +34,13 @@ def get_channels(): return globals.channel_list def get_node_list(): - node_list = [] if globals.interface.nodes: - for node in globals.interface.nodes.values(): - node_list.append(node['num']) - return node_list + sorted_nodes = sorted( + globals.interface.nodes.values(), + key = lambda node: (node['lastHeard'] if 'lastHeard' in node else 0), + reverse = True) + return [node['num'] for node in sorted_nodes] + return [] def get_nodeNum(): myinfo = globals.interface.getMyNodeInfo()