diff --git a/db_handler.py b/db_handler.py index 9567bb6..5da7bb9 100644 --- a/db_handler.py +++ b/db_handler.py @@ -76,7 +76,7 @@ def load_messages_from_db(): # Add messages to globals.all_messages in tuple format for user_id, message in db_messages: if user_id == str(get_nodeNum()): - formatted_message = (f"{globals.message_prefix} Sent: ", message) + formatted_message = (f"{globals.sent_message_prefix}: ", message) else: formatted_message = (f"{globals.message_prefix} {get_name_from_number(int(user_id), 'short')}: ", message) diff --git a/globals.py b/globals.py index a3b1fe4..b6786cb 100644 --- a/globals.py +++ b/globals.py @@ -11,5 +11,5 @@ interface = None display_log = False db_file_path = "client.db" message_prefix = ">>" -sent_message_prefix =">> Sent:" -notification_symbol = "*" \ No newline at end of file +sent_message_prefix = message_prefix + " Sent" +notification_symbol = "*" diff --git a/message_handlers/rx_handler.py b/message_handlers/rx_handler.py index e07d77f..0326701 100644 --- a/message_handlers/rx_handler.py +++ b/message_handlers/rx_handler.py @@ -5,7 +5,7 @@ from ui.curses_ui import draw_packetlog_win, draw_node_list, draw_messages_windo from db_handler import save_message_to_db, maybe_store_nodeinfo_in_db -def on_receive(packet): +def on_receive(packet, interface): global nodes_win # update packet log diff --git a/message_handlers/tx_handler.py b/message_handlers/tx_handler.py index 241589b..74f7300 100644 --- a/message_handlers/tx_handler.py +++ b/message_handlers/tx_handler.py @@ -3,30 +3,59 @@ from db_handler import save_message_to_db from utilities.utils import get_nodeNum import globals +ack_naks = {} + +# Note "onAckNak" has special meaning to the API, thus the nonstandard naming convention +# See https://github.com/meshtastic/python/blob/master/meshtastic/mesh_interface.py#L462 +def onAckNak(packet): + from ui.curses_ui import draw_messages_window + request = packet['decoded']['requestId'] + if(request not in ack_naks): + return + + acknak = ack_naks.pop(request) + message = globals.all_messages[acknak['channel']][acknak['messageIndex']][1] + + confirm_string = " " + if(packet['decoded']['routing']['errorReason'] == "NONE"): + if(packet['from'] == get_nodeNum()): # Ack "from" ourself means implicit ACK + confirm_string = "[◌]" + else: + confirm_string = "[✓]" + else: + confirm_string = "[x]" + + globals.all_messages[acknak['channel']][acknak['messageIndex']] = (globals.sent_message_prefix + confirm_string + ": ", message) + + draw_messages_window() def send_message(message, destination=BROADCAST_NUM, channel=0): myid = get_nodeNum() send_on_channel = 0 - if isinstance(globals.channel_list[channel], int): + channel_id = globals.channel_list[channel] + if isinstance(channel_id, int): send_on_channel = 0 - destination = globals.channel_list[channel] - elif isinstance(globals.channel_list[channel], str): + destination = channel_id + elif isinstance(channel_id, str): send_on_channel = channel - globals.interface.sendText( + sent_message_data = globals.interface.sendText( text=message, destinationId=destination, - wantAck=False, + wantAck=True, wantResponse=False, - onResponse=None, + onResponse=onAckNak, channelIndex=send_on_channel, ) # Add sent message to the messages dictionary - if globals.channel_list[channel] in globals.all_messages: - globals.all_messages[globals.channel_list[channel]].append((globals.sent_message_prefix + " ", message)) + if channel_id in globals.all_messages: + globals.all_messages[channel_id].append((globals.sent_message_prefix + "[…]: ", message)) else: - globals.all_messages[globals.channel_list[channel]] = [(globals.sent_message_prefix + " ", message)] + globals.all_messages[channel_id] = [(globals.sent_message_prefix + "[…]: ", message)] - save_message_to_db(globals.channel_list[channel], myid, message) \ No newline at end of file + ack_naks[sent_message_data.id] = {'channel' : channel_id, 'messageIndex' : len(globals.all_messages[channel_id]) - 1 } + + + save_message_to_db(channel_id, myid, message)