diff --git a/mesh_bot.py b/mesh_bot.py index fbd3e1d..c89c1b9 100755 --- a/mesh_bot.py +++ b/mesh_bot.py @@ -143,6 +143,11 @@ def auto_response(message, snr, rssi, hop, message_from_id, channel_number): return bot_response def onReceive(packet, interface): + # extract interface from interface object + rxInterface = interface.__dict__.get('devPath') + if rxInterface == port1: + rxNode = 1 + # receive a packet and process it, main instruction loop # print the packet for debugging @@ -272,7 +277,7 @@ def start_rx(): def exit_handler(): # Close the interface and save the BBS messages print("\nSystem: Closing Autoresponder") - interface.close() + interface1.close() print("System: Interface Closed") print("Saving BBS Messages") save_bbsdb() diff --git a/modules/settings.py b/modules/settings.py index 26f3d17..b8498f3 100644 --- a/modules/settings.py +++ b/modules/settings.py @@ -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 diff --git a/modules/system.py b/modules/system.py index 0107591..3b257fd 100644 --- a/modules/system.py +++ b/modules/system.py @@ -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 :) diff --git a/pong_bot.py b/pong_bot.py index bad2441..7b5efac 100755 --- a/pong_bot.py +++ b/pong_bot.py @@ -53,6 +53,14 @@ def auto_response(message, snr, rssi, hop, message_from_id, channel_number): return bot_response def onReceive(packet, interface): + # extract interface from interface object + rxInterface = interface.__dict__.get('devPath') + if rxInterface == port1: + rxNode = 1 + else: + rxNode = 0 + print(f"{log_timestamp()} System: Error, received packet on unknown interface: {rxInterface}") + # receive a packet and process it, main instruction loop # print the packet for debugging @@ -105,7 +113,7 @@ def onReceive(packet, interface): if message_string == help_message or message_string == welcome_message or "CMD?:" in message_string: # ignore help and welcome messages - print(f"{log_timestamp()} Got Own Welcome/Help header. From: {get_name_from_number(message_from_id)}") + print(f"{log_timestamp()} Got Own Welcome/Help header. Device:{rxNode} From: {get_name_from_number(message_from_id)}") return # If the packet is a DM (Direct Message) respond to it, otherwise validate its a message for us on the channel @@ -114,20 +122,20 @@ def onReceive(packet, interface): # check if the message contains a trap word, DMs are always responded to if messageTrap(message_string): - print(f"{log_timestamp()} Received DM: {message_string} on Channel: {channel_number} From: {get_name_from_number(message_from_id)}") + print(f"{log_timestamp()} Received DM: {message_string} on Device:{rxNode} Channel: {channel_number} From: {get_name_from_number(message_from_id)}") # respond with DM - send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number), channel_number, message_from_id) + send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number, rxNode), channel_number, message_from_id) else: # respond with welcome message on DM - print(f"{log_timestamp()} Ignoring DM: {message_string} From: {get_name_from_number(message_from_id)}") + print(f"{log_timestamp()} Ignoring DM: {message_string} on Device:{rxNode} From: {get_name_from_number(message_from_id)}") send_message(welcome_message, channel_number, message_from_id) else: # message is on a channel if messageTrap(message_string): - print(f"{log_timestamp()} Received On Channel {channel_number}: {message_string} From: {get_name_from_number(message_from_id)}") + print(f"{log_timestamp()} Received On Device:{rxNode} Channel {channel_number}: {message_string} From: {get_name_from_number(message_from_id)}") if RESPOND_BY_DM_ONLY: # respond to channel message via direct message - send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number), channel_number, message_from_id) + send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number, rxNode), channel_number, message_from_id) else: # or respond to channel message on the channel itself if channel_number == DEFAULT_CHANNEL: @@ -135,26 +143,25 @@ def onReceive(packet, interface): print(f"{log_timestamp()} System: Warning spamming default channel not allowed. sending DM to {get_name_from_number(message_from_id)}") # respond to channel message via direct message - send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number), channel_number, message_from_id) + send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number, rxNode), channel_number, message_from_id) else: # respond to channel message on the channel itself - send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number), channel_number) + send_message(auto_response(message_string, snr, rssi, hop, message_from_id, channel_number, rxNode), channel_number) else: # add the message to the message history but limit timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") if len(msg_history) < storeFlimit: - msg_history.append((get_name_from_number(message_from_id), message_string, channel_number, timestamp)) + msg_history.append((get_name_from_number(message_from_id), message_string, channel_number, timestamp, rxNode)) else: msg_history.pop(0) - msg_history.append((get_name_from_number(message_from_id), message_string, channel_number, timestamp)) + msg_history.append((get_name_from_number(message_from_id), message_string, channel_number, timestamp, rxNode)) - print(f"{log_timestamp()} System: Ignoring incoming channel {channel_number}: {message_string} From: {get_name_from_number(message_from_id)}") + print(f"{log_timestamp()} System: Ignoring incoming Device:{rxNode} Channel:{channel_number} Message: {message_string} From: {get_name_from_number(message_from_id)}") except KeyError as e: print(f"System: Error processing packet: {e}") print(packet) # print the packet for debugging print("END of packet \n") - def exit_handler(): # Close the interface and save the BBS messages print("\nSystem: Closing Autoresponder")