diff --git a/main.py b/main.py index f3e71e1..c177f59 100644 --- a/main.py +++ b/main.py @@ -1,3 +1,4 @@ +import logging import meshtastic import meshtastic.serial_interface import meshtastic.tcp_interface @@ -11,19 +12,39 @@ from pubsub import pub local_interface = None remote_interface = None +bridge_logger = logging.getLogger(name="meshtastic.bridge") + +BRIDGE_LOG = os.environ['BRIDGE_LOG'] if 'BRIDGE_LOG' in os.environ else 'INFO' +NODE_LOG = os.environ['NODE_LOG'] if 'NODE_LOG' in os.environ else 'INFO' + +if BRIDGE_LOG == 'DEBUG': + bridge_logger.setLevel(logging.DEBUG) +elif BRIDGE_LOG == 'INFO': + bridge_logger.setLevel(logging.INFO) + +if NODE_LOG == 'DEBUG': + logging.basicConfig(level=logging.DEBUG) +elif NODE_LOG == 'INFO': + logging.basicConfig(level=logging.INFO) local_node_addr = os.environ['LOCAL_NODE_ADDR'] if 'LOCAL_NODE_ADDR' in os.environ else None remote_node_addr = os.environ['REMOTE_NODE_ADDR'] if local_node_addr and '/' in local_node_addr: + bridge_logger.debug(f"Connecting to local node via serial port: {local_node_addr} ...") local_interface = meshtastic.serial_interface.SerialInterface(devPath=local_node_addr) -elif local_node_addr and '.' in local_node_addr: +elif local_node_addr: + bridge_logger.debug(f"Connecting to local node via TCP: {local_node_addr} ...") local_interface = meshtastic.tcp_interface.TCPInterface(hostname=local_node_addr) else: + bridge_logger.debug(f"Connecting to local node via serial port ...") local_interface = meshtastic.serial_interface.SerialInterface() -remote_interface = meshtastic.tcp_interface.TCPInterface(hostname=remote_node_addr) +bridge_logger.info(f"Connected to local node") +bridge_logger.debug(f"Connecting to remote node via TCP: {remote_node_addr} ...") +remote_interface = meshtastic.tcp_interface.TCPInterface(hostname=remote_node_addr) +bridge_logger.info(f"Connected to remote node") ourNode = local_interface.getNode('^local') SUPPORTED_MESSAGES = [ @@ -37,8 +58,10 @@ NODE_PROXY = {BROADCAST_ADDR: BROADCAST_ADDR} def onReceive(packet, interface): # called when a packet arrives + bridge_logger.debug(f"Packet received: {packet}") + if packet['decoded']['portnum'] not in SUPPORTED_MESSAGES: - print(f"Dropping {packet['decoded']['portnum']}") + bridge_logger.debug(f"Dropping {packet['decoded']['portnum']}") return message_source_position = None @@ -53,8 +76,12 @@ def onReceive(packet, interface): # called when a packet arrives current_local_position = (nodeInfo['position']['latitude'], nodeInfo['position']['longitude']) if message_source_position and current_local_position: + + distance_km = haversine(message_source_position, current_local_position) + # message originates from too far a distance - if SUPPORTED_BRIDGE_DISTANCE_KM > 0 and haversine(message_source_position, current_local_position) > SUPPORTED_BRIDGE_DISTANCE_KM: + if SUPPORTED_BRIDGE_DISTANCE_KM > 0 and distance_km > SUPPORTED_BRIDGE_DISTANCE_KM: + bridge_logger.debug(f"Packet from too far: {distance_km} > {SUPPORTED_BRIDGE_DISTANCE_KM}") return if 'to' in packet: @@ -74,7 +101,7 @@ def onReceive(packet, interface): # called when a packet arrives meshPacket.id = remote_interface._generatePacketId() if destinationId == BROADCAST_ADDR or destinationId in remote_interface.nodes: - print(f"Sending {meshPacket} to TCP server") + bridge_logger.debug(f"Sending packet {meshPacket.id} to TCP server") remote_interface._sendPacket(meshPacket=meshPacket, destinationId=destinationId) def onConnection(interface, topic=pub.AUTO_TOPIC): # called when we (re)connect to the radio