Skip to content

Commit

Permalink
fix: some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewthetechie committed Mar 23, 2024
1 parent 48bdc53 commit 1e96541
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
15 changes: 10 additions & 5 deletions aprs_backend/aprs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ class APRSBackend(ErrBot):
def __init__(self, config):
log.debug("Initied")

self._errbot_config = config

aprs_config = {"host": "rotate.aprs.net", "port": 14580}
aprs_config.update(config.BOT_IDENTITY)

sender_config = check_sender_config(config)
self._sender_config = check_sender_config(config)

if "callsign" not in aprs_config:
log.fatal("No callsign in bot identity")
Expand Down Expand Up @@ -67,11 +69,14 @@ def __init__(self, config):
port=aprs_config["port"],
)
self.threads = {}
super().__init__(config)

def setup_threads(self) -> None:
self.threads["rx"] = ErrbotRXThread(
packet_queue=self._rx_queue, client=self._aprs_client
)
self.threads["sender"] = ErrbotAPRSSender(
client=self._aprs_client, config=sender_config
client=self._aprs_client, config=self._sender_config
)
self.threads["processor"] = PacketProcessorThread(
callsign=self.callsign,
Expand All @@ -88,22 +93,21 @@ def __init__(self, config):
seen_list=self.seen_list,
thread_list=ErrbotAPRSDThreadList(),
)
if str(getattr(config, "APRS_BEACON_ENABLED", False)).lower() in [
if str(getattr(self._errbot_config, "APRS_BEACON_ENABLED", False)).lower() in [
"true",
"t",
"y",
"yes",
"1",
]:
try:
beacon_kwargs = check_beacon_config(config)
beacon_kwargs = check_beacon_config(self._errbot_config)
self.treads["beacon"] = BeaconSendThread(
**beacon_kwargs, callsign=self.callsign
)
except ValueError as exc:
log.error(exc)
log.error("Beaconing disabled due to config error")
super().__init__(config)

def build_reply(
self, msg: Message, text: str, private: bool = False, threaded: bool = False
Expand Down Expand Up @@ -148,6 +152,7 @@ def serve_once(self):
log.debug("APRS backend started")
self.connect_callback()
try:
self.setup_threads()
for key, thread in self.threads.items():
log.debug("Starting %s thread", key)
thread.start()
Expand Down
5 changes: 4 additions & 1 deletion aprs_backend/packets/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ def get_last_tx(self):
@property
def secs_since_last_rx(self) -> float:
now = datetime.now()
return (now - self.get_last_rx()).total_seconds()
if self.get_last_rx() is not None:
return (now - self.get_last_rx()).total_seconds()
return 0.0


@property
def secs_since_last_tx(self) -> float:
Expand Down
8 changes: 4 additions & 4 deletions aprs_backend/threads/keep_alive.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ def loop(self):
if self._loop_counter % self._keep_alive_freq == 0:
uptime = datetime.now() - self.started_time
curr_mem, peak_mem = tracemalloc.get_traced_memory()
stats = f"Uptime {strfdelta(uptime)} RX: {self.packet_list.total_rx()} "
stats = f"Uptime {strfdelta(uptime)} RX: {self._packet_list.total_rx()} "
stats += (
f"TX: {self.packet_list.total_tx()} Tracked: {len(self.packet_tracker)}"
f"TX: {self._packet_list.total_tx()} Tracked: {len(self._packet_tracker)}"
)
stats += (
f"Threads: {len(self._thread_list)} Mem: {curr_mem} PeakMem: {peak_mem}"
)
log.info(stats)
thread_out = []
thread_info = {}
for thread in self.thread_list.threads_list:
for thread in self._thread_list.threads_list:
alive = thread.is_alive()
age = thread.loop_age()
key = thread.__class__.__name__
Expand All @@ -78,7 +78,7 @@ def loop(self):
else:
if (
self._packet_list.secs_since_last_rx > self._aprs_keep_alive_seconds
and self._packet_list > self._aprs_keep_alive_seconds
and self._packet_list
):
log.error(
"No keepalive from aprs in %d seconds. Resetting connection",
Expand Down
16 changes: 8 additions & 8 deletions aprs_backend/threads/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,19 +108,19 @@ def process_packet(self, packet):
log.debug(f"Packet processing complete for pkt '{packet.key}'")
return False

def process_our_message_packet(self, packet: core.Packet):
def process_our_message_packet(self, packet: core.MessagePacket):
"""Process a MessagePacket destined for us.
Convert to an APRSMessage and call the backend_callback to send
the message through errbot plugins
"""
log.debug(packet)
try:
text = packet["message_text"]
sender = packet["from"]
msg_number = int(packet.get("msgNo", "0"))
path = packet["path"]
via = packet["via"]
except KeyError as exc:
text = packet.message_text
sender = packet.from_call
msg_number = packet.msgNo
path = packet.path
via = packet.via
except AttributeError as exc:
log.error("malformed packet, missing key %s", exc)
return

Expand All @@ -130,7 +130,7 @@ def process_our_message_packet(self, packet: core.Packet):
"msg_number": msg_number,
"via": via,
"path": path,
"raw": packet["raw"],
"raw": packet.raw,
"packet": packet,
},
)
Expand Down
6 changes: 3 additions & 3 deletions aprs_backend/threads/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ def send(self, packet: core.Packet, direct: bool = False) -> None:

def _send_packet(self, packet: core.Packet, direct=False):
if not direct:
thread = SendPacketThread(packet=packet)
thread = SendPacketThread(packet=packet, aprs_sender=self)
thread.start()
else:
self._send_direct(packet)

def _send_ack(self, packet: core.AckPacket, direct=False):
if not direct:
thread = SendAckThread(packet=packet)
thread = SendAckThread(packet=packet, aprs_sender=self)
thread.start()
else:
self._send_direct(packet)
Expand Down Expand Up @@ -200,7 +200,7 @@ def loop(self):
# no attempt time, so lets send it, and start
# tracking the time.
packet.last_send_time = int(round(time.time()))
self.aprs_sender.send(packet, direct=True)
self.sender.send(packet, direct=True)
packet.send_count += 1

time.sleep(1)
Expand Down

0 comments on commit 1e96541

Please sign in to comment.