mirror of
https://github.com/pdxlocations/contact.git
synced 2026-03-28 17:12:35 +01:00
Compare commits
8 Commits
fix-startu
...
reinitiali
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
088d13c88b | ||
|
|
c83ccea4ef | ||
|
|
d7f0bee54c | ||
|
|
fb60773ae6 | ||
|
|
47ab0a5b9a | ||
|
|
989c3cf44e | ||
|
|
71aeae4f92 | ||
|
|
34cd21b323 |
@@ -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
|
||||||
|
|||||||
@@ -9,4 +9,5 @@ myNodeNum = 0
|
|||||||
selected_channel = 0
|
selected_channel = 0
|
||||||
selected_message = 0
|
selected_message = 0
|
||||||
selected_node = 0
|
selected_node = 0
|
||||||
current_window = 0
|
current_window = 0
|
||||||
|
lock = None
|
||||||
33
main.py
33
main.py
@@ -3,7 +3,7 @@
|
|||||||
'''
|
'''
|
||||||
Contact - A Console UI for Meshtastic by http://github.com/pdxlocations
|
Contact - A Console UI for Meshtastic by http://github.com/pdxlocations
|
||||||
Powered by Meshtastic.org
|
Powered by Meshtastic.org
|
||||||
V 1.2.0
|
V 1.2.1
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import curses
|
import curses
|
||||||
@@ -11,12 +11,15 @@ 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
|
||||||
from message_handlers.rx_handler import on_receive
|
from message_handlers.rx_handler import on_receive
|
||||||
from ui.curses_ui import main_ui, draw_splash
|
from ui.curses_ui import main_ui, draw_splash
|
||||||
|
from input_handlers import get_list_input
|
||||||
from utilities.utils import get_channels, get_node_list, get_nodeNum
|
from utilities.utils import get_channels, get_node_list, get_nodeNum
|
||||||
|
from settings import set_region
|
||||||
from db_handler import init_nodedb, load_messages_from_db
|
from db_handler import init_nodedb, load_messages_from_db
|
||||||
import default_config as config
|
import default_config as config
|
||||||
import globals
|
import globals
|
||||||
@@ -38,6 +41,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 +50,23 @@ 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()
|
if globals.interface.localNode.localConfig.lora.region == 0:
|
||||||
globals.channel_list = get_channels()
|
confirmation = get_list_input("Your region is UNSET. Set it now?", "Yes", ["Yes", "No"])
|
||||||
globals.node_list = get_node_list()
|
if confirmation == "Yes":
|
||||||
pub.subscribe(on_receive, 'meshtastic.receive')
|
set_region()
|
||||||
init_nodedb()
|
globals.interface.close()
|
||||||
load_messages_from_db()
|
globals.interface = initialize_interface(args)
|
||||||
logging.info("Starting main UI")
|
logging.info("Interface initialized")
|
||||||
|
globals.myNodeNum = get_nodeNum()
|
||||||
|
globals.channel_list = get_channels()
|
||||||
|
globals.node_list = get_node_list()
|
||||||
|
pub.subscribe(on_receive, 'meshtastic.receive')
|
||||||
|
init_nodedb()
|
||||||
|
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)
|
||||||
|
|||||||
@@ -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:
|
|
||||||
draw_packetlog_win()
|
if globals.display_log:
|
||||||
try:
|
draw_packetlog_win()
|
||||||
if 'decoded' not in packet:
|
try:
|
||||||
return
|
if 'decoded' not in packet:
|
||||||
|
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}")
|
||||||
|
|||||||
20
settings.py
20
settings.py
@@ -9,6 +9,7 @@ from ui.menus import generate_menu_from_protobuf
|
|||||||
from ui.colors import setup_colors, get_color
|
from ui.colors import setup_colors, get_color
|
||||||
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
|
||||||
|
from ui.dialog import dialog
|
||||||
from user_config import json_editor
|
from user_config import json_editor
|
||||||
import globals
|
import globals
|
||||||
|
|
||||||
@@ -348,6 +349,25 @@ def settings_menu(stdscr, interface):
|
|||||||
menu_win.refresh()
|
menu_win.refresh()
|
||||||
break
|
break
|
||||||
|
|
||||||
|
def set_region():
|
||||||
|
node = globals.interface.getNode('^local')
|
||||||
|
device_config = node.localConfig
|
||||||
|
lora_descriptor = device_config.lora.DESCRIPTOR
|
||||||
|
|
||||||
|
# Get the enum mapping of region names to their numerical values
|
||||||
|
region_enum = lora_descriptor.fields_by_name["region"].enum_type
|
||||||
|
region_name_to_number = {v.name: v.number for v in region_enum.values}
|
||||||
|
|
||||||
|
regions = list(region_name_to_number.keys())
|
||||||
|
|
||||||
|
new_region_name = get_list_input('Select your region:', 'UNSET', regions)
|
||||||
|
|
||||||
|
# Convert region name to corresponding enum number
|
||||||
|
new_region_number = region_name_to_number.get(new_region_name, 0) # Default to 0 if not found
|
||||||
|
|
||||||
|
node.localConfig.lora.region = new_region_number
|
||||||
|
node.writeConfig("lora")
|
||||||
|
|
||||||
|
|
||||||
def main(stdscr):
|
def main(stdscr):
|
||||||
logging.basicConfig( # Run `tail -f client.log` in another terminal to view live
|
logging.basicConfig( # Run `tail -f client.log` in another terminal to view live
|
||||||
|
|||||||
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user