Compare commits

...

18 Commits

Author SHA1 Message Date
pdxlocations
08f19e653a add note in draw_node_list 2025-02-09 20:46:55 -08:00
pdxlocations
6e05446d6a Merge branch 'main' into fix-startup-error 2025-02-09 06:45:53 -08:00
pdxlocations
a0cb4f9480 sir locks a lilttle less 2025-02-08 21:07:54 -08:00
pdxlocations
89d5890338 sir locks a lot 2025-02-08 21:04:51 -08:00
pdxlocations
a645e41146 lock it down 2025-02-08 18:07:12 -08:00
pdxlocations
aa736472cc fix conflict 2025-02-08 18:02:49 -08:00
pdxlocations
4dc1b4b791 try a threading lock 2025-02-08 18:02:25 -08:00
pdxlocations
41050577aa Merge branch 'main' into fix-startup-error 2025-02-08 15:57:46 -08:00
pdxlocations
d9c249af56 db snapshot 2025-02-08 14:21:44 -08:00
pdxlocations
ad64004e79 back up 2025-02-08 13:25:27 -08:00
pdxlocations
d9a84c4b29 more global 2025-02-08 13:19:48 -08:00
pdxlocations
6cf46a202a grasping at straws 2025-02-08 11:48:07 -08:00
pdxlocations
351f4d7f8f less is more 2025-02-08 08:38:35 -08:00
pdxlocations
5f0277c460 refactor 2025-02-08 08:29:57 -08:00
pdxlocations
f34db01a78 typo 2025-02-08 08:04:52 -08:00
pdxlocations
463b655684 more checks 2025-02-08 08:03:59 -08:00
pdxlocations
14aa5c00a3 none isn't better than nothing 2025-02-08 08:00:20 -08:00
pdxlocations
a201bcccb2 more excuses 2025-02-08 07:57:09 -08:00
5 changed files with 94 additions and 84 deletions

View File

@@ -144,6 +144,7 @@ def load_messages_from_db():
def init_nodedb(): def init_nodedb():
"""Initialize the node database and update it with nodes from the interface.""" """Initialize the node database and update it with nodes from the interface."""
try: try:
if not globals.interface.nodes: if not globals.interface.nodes:
return # No nodes to initialize return # No nodes to initialize

View File

@@ -10,3 +10,4 @@ selected_channel = 0
selected_message = 0 selected_message = 0
selected_node = 0 selected_node = 0
current_window = 0 current_window = 0
lock = None

22
main.py
View File

@@ -11,6 +11,7 @@ from pubsub import pub
import os import os
import logging import logging
import traceback import traceback
import threading
from utilities.arg_parser import setup_parser from utilities.arg_parser import setup_parser
from utilities.interfaces import initialize_interface from utilities.interfaces import initialize_interface
@@ -38,6 +39,8 @@ logging.basicConfig(
format="%(asctime)s - %(levelname)s - %(message)s" format="%(asctime)s - %(levelname)s - %(message)s"
) )
globals.lock = threading.Lock()
def main(stdscr): def main(stdscr):
try: try:
draw_splash(stdscr) draw_splash(stdscr)
@@ -45,15 +48,16 @@ def main(stdscr):
args = parser.parse_args() args = parser.parse_args()
logging.info("Initializing interface %s", args) logging.info("Initializing interface %s", args)
globals.interface = initialize_interface(args) with globals.lock:
logging.info("Interface initialized") globals.interface = initialize_interface(args)
globals.myNodeNum = get_nodeNum() logging.info("Interface initialized")
globals.channel_list = get_channels() globals.myNodeNum = get_nodeNum()
globals.node_list = get_node_list() globals.channel_list = get_channels()
pub.subscribe(on_receive, 'meshtastic.receive') globals.node_list = get_node_list()
init_nodedb() pub.subscribe(on_receive, 'meshtastic.receive')
load_messages_from_db() init_nodedb()
logging.info("Starting main UI") load_messages_from_db()
logging.info("Starting main UI")
main_ui(stdscr) main_ui(stdscr)
except Exception as e: except Exception as e:
logging.error("An error occurred: %s", e) logging.error("An error occurred: %s", e)

View File

@@ -12,93 +12,94 @@ from datetime import datetime
def on_receive(packet, interface): def on_receive(packet, interface):
# Update packet log with globals.lock:
globals.packet_buffer.append(packet) # Update packet log
if len(globals.packet_buffer) > 20: globals.packet_buffer.append(packet)
# Trim buffer to 20 packets if len(globals.packet_buffer) > 20:
globals.packet_buffer = globals.packet_buffer[-20:] # Trim buffer to 20 packets
globals.packet_buffer = globals.packet_buffer[-20:]
if globals.display_log: if globals.display_log:
draw_packetlog_win() draw_packetlog_win()
try: try:
if 'decoded' not in packet: if 'decoded' not in packet:
return return
# Assume any incoming packet could update the last seen time for a node # Assume any incoming packet could update the last seen time for a node
changed = refresh_node_list() changed = refresh_node_list()
if(changed): if(changed):
draw_node_list() draw_node_list()
if packet['decoded']['portnum'] == 'NODEINFO_APP': if packet['decoded']['portnum'] == 'NODEINFO_APP':
if "user" in packet['decoded'] and "longName" in packet['decoded']["user"]: if "user" in packet['decoded'] and "longName" in packet['decoded']["user"]:
maybe_store_nodeinfo_in_db(packet) maybe_store_nodeinfo_in_db(packet)
elif packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP': elif packet['decoded']['portnum'] == 'TEXT_MESSAGE_APP':
message_bytes = packet['decoded']['payload'] message_bytes = packet['decoded']['payload']
message_string = message_bytes.decode('utf-8') message_string = message_bytes.decode('utf-8')
refresh_channels = False refresh_channels = False
refresh_messages = False refresh_messages = False
if packet.get('channel'): if packet.get('channel'):
channel_number = packet['channel'] channel_number = packet['channel']
else:
channel_number = 0
if packet['to'] == globals.myNodeNum:
if packet['from'] in globals.channel_list:
pass
else: else:
globals.channel_list.append(packet['from']) channel_number = 0
if(packet['from'] not in globals.all_messages):
globals.all_messages[packet['from']] = [] if packet['to'] == globals.myNodeNum:
update_node_info_in_db(packet['from'], chat_archived=False) if packet['from'] in globals.channel_list:
pass
else:
globals.channel_list.append(packet['from'])
if(packet['from'] not in globals.all_messages):
globals.all_messages[packet['from']] = []
update_node_info_in_db(packet['from'], chat_archived=False)
refresh_channels = True
channel_number = globals.channel_list.index(packet['from'])
if globals.channel_list[channel_number] != globals.channel_list[globals.selected_channel]:
add_notification(channel_number)
refresh_channels = True refresh_channels = True
else:
refresh_messages = True
channel_number = globals.channel_list.index(packet['from']) # Add received message to the messages list
message_from_id = packet['from']
message_from_string = get_name_from_database(message_from_id, type='short') + ":"
if globals.channel_list[channel_number] != globals.channel_list[globals.selected_channel]: if globals.channel_list[channel_number] not in globals.all_messages:
add_notification(channel_number) globals.all_messages[globals.channel_list[channel_number]] = []
refresh_channels = True
else:
refresh_messages = True
# Add received message to the messages list # Timestamp handling
message_from_id = packet['from'] current_timestamp = time.time()
message_from_string = get_name_from_database(message_from_id, type='short') + ":" current_hour = datetime.fromtimestamp(current_timestamp).strftime('%Y-%m-%d %H:00')
if globals.channel_list[channel_number] not in globals.all_messages: # Retrieve the last timestamp if available
globals.all_messages[globals.channel_list[channel_number]] = [] channel_messages = globals.all_messages[globals.channel_list[channel_number]]
if channel_messages:
# Timestamp handling # Check the last entry for a timestamp
current_timestamp = time.time() for entry in reversed(channel_messages):
current_hour = datetime.fromtimestamp(current_timestamp).strftime('%Y-%m-%d %H:00') if entry[0].startswith("--"):
last_hour = entry[0].strip("- ").strip()
# Retrieve the last timestamp if available break
channel_messages = globals.all_messages[globals.channel_list[channel_number]] else:
if channel_messages: last_hour = None
# Check the last entry for a timestamp
for entry in reversed(channel_messages):
if entry[0].startswith("--"):
last_hour = entry[0].strip("- ").strip()
break
else: else:
last_hour = None last_hour = None
else:
last_hour = None
# Add a new timestamp if it's a new hour # Add a new timestamp if it's a new hour
if last_hour != current_hour: if last_hour != current_hour:
globals.all_messages[globals.channel_list[channel_number]].append((f"-- {current_hour} --", "")) globals.all_messages[globals.channel_list[channel_number]].append((f"-- {current_hour} --", ""))
globals.all_messages[globals.channel_list[channel_number]].append((f"{config.message_prefix} {message_from_string} ", message_string)) globals.all_messages[globals.channel_list[channel_number]].append((f"{config.message_prefix} {message_from_string} ", message_string))
if refresh_channels: if refresh_channels:
draw_channel_list() draw_channel_list()
if refresh_messages: if refresh_messages:
draw_messages_window(True) draw_messages_window(True)
save_message_to_db(globals.channel_list[channel_number], message_from_id, message_string) save_message_to_db(globals.channel_list[channel_number], message_from_id, message_string)
except KeyError as e: except KeyError as e:
logging.error(f"Error processing packet: {e}") logging.error(f"Error processing packet: {e}")

View File

@@ -417,8 +417,11 @@ def draw_messages_window(scroll_to_bottom = False):
def draw_node_list(): def draw_node_list():
global nodes_pad global nodes_pad
if nodes_pad is None: # This didn't work, for some reason an error is thown on startup, so we just create the pad every time
nodes_pad = curses.newpad(1, 1) # if nodes_pad is None:
# nodes_pad = curses.newpad(1, 1)
nodes_pad = curses.newpad(1, 1)
try: try:
nodes_pad.erase() nodes_pad.erase()