refactorInterface

setting up for multiple node interfaces
This commit is contained in:
SpudGunMan
2024-07-24 15:32:51 -07:00
parent df74ddd4f6
commit b9232ee67f
4 changed files with 92 additions and 73 deletions
+4 -4
View File
@@ -22,10 +22,10 @@ if config.sections() == []:
print (f"System: Config file created, check {config_file} or review the config.template")
# config.ini variables
interface_type = config['interface'].get('type', 'serial')
port = config['interface'].get('port', '')
hostname = config['interface'].get('hostname', '')
mac = config['interface'].get('mac', '')
interface1_type = config['interface'].get('type', 'serial')
port1 = config['interface'].get('port', '')
hostname1 = config['interface'].get('hostname', '')
mac1 = config['interface'].get('mac', '')
msg_history = [] # message history for the store and forward feature
storeFlimit = config['general'].getint('StoreLimit', 3) # limit of messages to store for Store and Forward
+63 -56
View File
@@ -40,20 +40,20 @@ if store_forward_enabled:
trap_list = trap_list + ("messages",)
help_message = help_message + ", messages"
# Interface Configuration
if interface_type == 'serial':
interface = meshtastic.serial_interface.SerialInterface(port)
elif interface_type == 'tcp':
interface = meshtastic.tcp_interface.TCPInterface(hostname)
elif interface_type == 'ble':
interface = meshtastic.ble_interface.BLEInterface(mac)
# Interface1 Configuration
if interface1_type == 'serial':
interface1 = meshtastic.serial_interface.SerialInterface(port1)
elif interface1_type == 'tcp':
interface1 = meshtastic.tcp_interface.TCPInterface(hostname1)
elif interface1_type == 'ble':
interface1 = meshtastic.ble_interface.BLEInterface(mac1)
else:
print(f"System: Interface Type: {interface_type} not supported. Validate your config against config.template Exiting")
print(f"System: Interface Type: {interface1_type} not supported. Validate your config against config.template Exiting")
exit()
#Get the node number of the device, check if the device is connected
try:
myinfo = interface.getMyNodeInfo()
myinfo = interface1.getMyNodeInfo()
myNodeNum = myinfo['num']
except Exception as e:
print(f"System: Critical Error script abort. {e}")
@@ -68,38 +68,40 @@ def log_timestamp():
def decimal_to_hex(decimal_number):
return f"!{decimal_number:08x}"
def get_name_from_number(number, type='long'):
def get_name_from_number(number, type='long', node=1):
name = ""
for node in interface.nodes.values():
if number == node['num']:
if type == 'long':
name = node['user']['longName']
return name
elif type == 'short':
name = node['user']['shortName']
return name
if node == 1:
for node in interface1.nodes.values():
if number == node['num']:
if type == 'long':
name = node['user']['longName']
return name
elif type == 'short':
name = node['user']['shortName']
return name
else:
pass
else:
pass
else:
name = str(decimal_to_hex(number)) # If long name not found, use the ID as string
return name
name = str(decimal_to_hex(number)) # If long name not found, use the ID as string
return name
def get_node_list():
def get_node_list(node=1):
node_list = []
short_node_list = []
if interface.nodes:
for node in interface.nodes.values():
# ignore own
if node['num'] != myNodeNum:
node_name = get_name_from_number(node['num'])
snr = node.get('snr', 0)
if node == 1:
if interface1.nodes:
for node in interface1.nodes.values():
# ignore own
if node['num'] != myNodeNum:
node_name = get_name_from_number(node['num'])
snr = node.get('snr', 0)
# issue where lastHeard is not always present
last_heard = node.get('lastHeard', 0)
# make a list of nodes with last heard time and SNR
item = (node_name, last_heard, snr)
node_list.append(item)
# issue where lastHeard is not always present
last_heard = node.get('lastHeard', 0)
# make a list of nodes with last heard time and SNR
item = (node_name, last_heard, snr)
node_list.append(item)
node_list.sort(key=lambda x: x[1], reverse=True)
#print (f"Node List: {node_list[:5]}\n")
@@ -113,28 +115,29 @@ def get_node_list():
else:
return "Error Processing Node List"
def get_node_location(number):
def get_node_location(number, node=1):
# Get the location of a node by its number from nodeDB on device
latitude = 0
longitude = 0
position = [0,0]
if interface.nodes:
for node in interface.nodes.values():
if number == node['num']:
if 'position' in node:
latitude = node['position']['latitude']
longitude = node['position']['longitude']
print (f"System: location data for {number} is {latitude},{longitude}")
position = [latitude,longitude]
return position
else:
print (f"{log_timestamp()} System: No location data for {number}")
return position
else:
print (f"{log_timestamp()} System: No nodes found")
return position
if node == 1:
if interface1.nodes:
for node in interface1.nodes.values():
if number == node['num']:
if 'position' in node:
latitude = node['position']['latitude']
longitude = node['position']['longitude']
print (f"System: location data for {number} is {latitude},{longitude}")
position = [latitude,longitude]
return position
else:
print (f"{log_timestamp()} System: No location data for {number}")
return position
else:
print (f"{log_timestamp()} System: No nodes found")
return position
def send_message(message, ch, nodeid=0):
def send_message(message, ch, nodeid=0, node=1):
# if message over 160 characters, split it into multiple messages
if len(message) > 160:
print (f"{log_timestamp()} System: Splitting Message, Message Length: {len(message)}")
@@ -160,20 +163,24 @@ def send_message(message, ch, nodeid=0):
if nodeid == 0:
#Send to channel
print (f"{log_timestamp()} System: Sending Multi-Chunk: {m} To: Channel:{ch}")
interface.sendText(text=m, channelIndex=ch)
if node == 1:
interface1.sendText(text=m, channelIndex=ch)
else:
# Send to DM
print (f"{log_timestamp()} System: Sending Multi-Chunk: {m} To: {get_name_from_number(nodeid)}")
interface.sendText(text=m, channelIndex=ch, destinationId=nodeid)
if node == 1:
interface1.sendText(text=m, channelIndex=ch, destinationId=nodeid)
else: # message is less than 160 characters
if nodeid == 0:
# Send to channel
print (f"{log_timestamp()} System: Sending: {message} To: Channel:{ch}")
interface.sendText(text=message, channelIndex=ch)
if node == 1:
interface1.sendText(text=message, channelIndex=ch)
else:
# Send to DM
print (f"{log_timestamp()} System: Sending: {message} To: {get_name_from_number(nodeid)}")
interface.sendText(text=message, channelIndex=ch, destinationId=nodeid)
if node == 1:
interface1.sendText(text=message, channelIndex=ch, destinationId=nodeid)
def tell_joke():
# tell a dad joke, does it need an explanationn :)