Enhanced error handling

This commit is contained in:
Chad Attermann 2024-03-06 14:52:04 -07:00
parent ebd77a6c7d
commit 2c67ae9d8b
2 changed files with 82 additions and 51 deletions

View File

@ -1,24 +1,29 @@
from .Interface import Interface from .Interface import Interface
import meshtastic
import meshtastic.serial_interface
from pubsub import pub from pubsub import pub
import time import time
import sys import sys
import RNS import RNS
try:
import meshtastic
import meshtastic.serial_interface
class MeshtasticInterface(Interface): class MeshtasticInterface(Interface):
@staticmethod @staticmethod
def onReceive(packet, interface): def onReceive(packet, interface):
#print(f"Received: {packet}")
if packet['decoded']['portnum'] == "PRIVATE_APP": if packet['decoded']['portnum'] == "PRIVATE_APP":
interface.callback.processIncoming(packet['decoded']['payload']) interface.callback.processIncoming(packet['decoded']['payload'])
def __init__(self, owner, name, port=None, channel=0): def __init__(self, owner, name, config):
super().__init__() super().__init__()
self.port = config["port"] if "port" in config else None
RNS.log("MeshtasticInterface: port="+str(self.port), RNS.LOG_DEBUG)
self.channel = int(config["channel"]) if "channel" in config else 0
RNS.log("MeshtasticInterface: channel="+str(self.channel), RNS.LOG_DEBUG)
self.rxb = 0 self.rxb = 0
self.txb = 0 self.txb = 0
@ -28,20 +33,32 @@ class MeshtasticInterface(Interface):
self.OUT = False self.OUT = False
self.owner = owner self.owner = owner
self.name = name self.name = name
self.port = port
self.channel = channel
self.online = False self.online = False
# static default long-fast bitrate # static default long-fast bitrate
self.bitrate = 5469 self.bitrate = 5469
self.bitrate_kbps = 5.47 self.bitrate_kbps = 5.47
self.interface = None
try:
RNS.log("Initializing Meshtastic interface...", RNS.LOG_DEBUG) RNS.log("Initializing Meshtastic interface...", RNS.LOG_DEBUG)
self.interface = meshtastic.serial_interface.SerialInterface(devPath=port) # avoid api forcing app to terminate when no devices are found
if self.port == None:
ports = meshtastic.util.findPorts(True)
if len(ports) == 0:
RNS.log("Failed to initialize Meshtastic interface! No devices found", RNS.LOG_ERROR)
return
self.interface = meshtastic.serial_interface.SerialInterface(devPath=self.port)
self.interface.callback = self self.interface.callback = self
RNS.log("Subscribing to Meshtastic events", RNS.LOG_DEBUG) RNS.log("Subscribing to Meshtastic events", RNS.LOG_DEBUG)
pub.subscribe(self.onReceive, "meshtastic.receive.data") pub.subscribe(self.onReceive, "meshtastic.receive.data")
except Exception as e:
RNS.log("Failed to initialize Meshtastic interface! "+str(e), RNS.LOG_ERROR)
self.interface = None
return
RNS.log("Initialized Meshtastic interface!", RNS.LOG_DEBUG) RNS.log("Initialized Meshtastic interface!", RNS.LOG_DEBUG)
def processIncoming(self, data): def processIncoming(self, data):
@ -50,16 +67,34 @@ class MeshtasticInterface(Interface):
self.owner.inbound(data, self) self.owner.inbound(data, self)
def processOutgoing(self, data): def processOutgoing(self, data):
if self.interface == None:
return
RNS.log("Sending Meshtastic packet "+RNS.prettyhexrep(data), RNS.LOG_DEBUG) RNS.log("Sending Meshtastic packet "+RNS.prettyhexrep(data), RNS.LOG_DEBUG)
try: try:
#self.interface.sendData(data) #self.interface.sendData(data)
self.interface.sendData(data, portNum=256, channelIndex=self.channel) self.interface.sendData(data, portNum=256, channelIndex=self.channel)
self.txb += len(data) self.txb += len(data)
except Exception as e: except Exception as e:
RNS.log("Could not transmit on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR) RNS.log("Could not transmit on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
def __str__(self):
if self.port == None:
return "MeshtasticInterface["+self.name+"]"
return "MeshtasticInterface["+self.name+"/"+self.port+"]"
except ImportError:
class MeshtasticInterface(Interface):
def __init__(self, owner, name, port=None, channel=0):
super().__init__()
RNS.log("Failed to initialize Meshtastic interface! Meshtastic API is not installed", RNS.LOG_ERROR)
#raise OSError("Meshtastic API is not installed")
def processOutgoing(self, data):
return
def __str__(self): def __str__(self):
if self.port == None: if self.port == None:
return "MeshtasticInterface["+self.name+"]" return "MeshtasticInterface["+self.name+"]"

View File

@ -935,14 +935,10 @@ class Reticulum:
if c["type"] == "MeshtasticInterface": if c["type"] == "MeshtasticInterface":
port = c["port"] if "port" in c else None
channel = int(c["channel"]) if "channel" in c else 0
interface = MeshtasticInterface.MeshtasticInterface( interface = MeshtasticInterface.MeshtasticInterface(
RNS.Transport, RNS.Transport,
name, name,
port, c
channel
) )
if "outgoing" in c and c.as_bool("outgoing") == False: if "outgoing" in c and c.as_bool("outgoing") == False: