Skip to content

Commit

Permalink
refactor; unify emoji style; use recent python features
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Hensel committed Jul 5, 2024
1 parent b22cb74 commit 31da03f
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 333 deletions.
17 changes: 7 additions & 10 deletions src/meshgram.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import asyncio
from typing import Optional, List
from typing import Optional
from collections.abc import Sequence
from meshtastic_interface import MeshtasticInterface
from telegram_interface import TelegramInterface
from message_processor import MessageProcessor
Expand All @@ -14,7 +15,7 @@ def __init__(self, config: ConfigManager) -> None:
self.meshtastic: Optional[MeshtasticInterface] = None
self.telegram: Optional[TelegramInterface] = None
self.message_processor: Optional[MessageProcessor] = None
self.tasks: List[Task] = []
self.tasks: Sequence[Task] = ()

async def setup(self) -> None:
self.logger.info("Setting up meshgram...")
Expand All @@ -40,13 +41,12 @@ async def run(self) -> None:
return

self.logger.info("Meshgram is running ヽ(´▽`)/")
self.tasks = [
self.tasks = (
asyncio.create_task(self.message_processor.process_messages()),
asyncio.create_task(self.meshtastic.process_thread_safe_queue()),
asyncio.create_task(self.meshtastic.process_pending_messages()),
asyncio.create_task(self.telegram.start_polling()),
asyncio.create_task(self.message_processor.check_heartbeats())
]
)
try:
await asyncio.gather(*self.tasks)
except asyncio.CancelledError:
Expand All @@ -67,10 +67,7 @@ async def shutdown(self) -> None:
if self.telegram:
await self.telegram.close()
if self.message_processor:
if hasattr(self.message_processor, 'close'):
await self.message_processor.close()
else:
self.logger.warning("MessageProcessor does not have a close method.")
await self.message_processor.close()
self.logger.info("Meshgram shutdown complete.")

async def main() -> None:
Expand All @@ -97,4 +94,4 @@ async def main() -> None:
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nShutdown complete.")
print("\nShutdown complete.")
14 changes: 13 additions & 1 deletion src/meshtastic_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,19 @@ async def _fetch_node_info(self) -> None:
def on_meshtastic_message(self, packet, interface):
self.logger.info(f"Received message from Meshtastic: {packet}")
self.logger.debug(f"Message details - fromId: {packet.get('fromId')}, toId: {packet.get('toId')}, portnum: {packet.get('decoded', {}).get('portnum')}")
self.thread_safe_queue.put(packet)
if packet.get('decoded', {}).get('portnum') == 'ROUTING_APP':
self.handle_ack(packet)
else:
self.thread_safe_queue.put(packet)

def handle_ack(self, packet):
ack_data = {
'type': 'ack',
'from': packet.get('fromId'),
'to': packet.get('toId'),
'message_id': packet.get('id')
}
self.loop.call_soon_threadsafe(self.message_queue.put_nowait, ack_data)

def on_connection(self, interface, topic=pub.AUTO_TOPIC):
self.logger.info(f"Connected to Meshtastic interface: {interface}")
Expand Down
Loading

0 comments on commit 31da03f

Please sign in to comment.