2024-06-24 08:05:47 -07:00
|
|
|
import os
|
|
|
|
|
2024-06-23 13:00:33 -07:00
|
|
|
import redis
|
2024-06-23 12:15:31 -07:00
|
|
|
from meshtastic.mesh_pb2 import MeshPacket
|
2024-06-23 13:00:33 -07:00
|
|
|
from prometheus_client import CollectorRegistry, Counter
|
2024-06-23 12:15:31 -07:00
|
|
|
|
2024-06-24 11:44:10 -07:00
|
|
|
from exporter.registry import ProcessorRegistry, ClientDetails
|
2024-06-23 12:15:31 -07:00
|
|
|
|
|
|
|
|
|
|
|
class MessageProcessor:
|
2024-06-23 13:00:33 -07:00
|
|
|
def __init__(self, registry: CollectorRegistry, redis_client: redis.Redis):
|
2024-06-23 12:15:31 -07:00
|
|
|
self.registry = registry
|
2024-06-23 13:00:33 -07:00
|
|
|
self.redis_client = redis_client
|
|
|
|
self.counter = Counter('mesh_packets', 'Number of mesh packets processed',
|
2024-06-24 08:05:47 -07:00
|
|
|
[
|
|
|
|
'source_id', 'source_short_name', 'source_long_name',
|
|
|
|
'destination_id', 'destination_short_name', 'destination_long_name',
|
|
|
|
'portnum',
|
|
|
|
'rx_time', 'rx_snr', 'hop_limit', 'want_ack', 'via_mqtt', 'hop_start'
|
|
|
|
],
|
2024-06-23 13:00:33 -07:00
|
|
|
registry=self.registry)
|
2024-06-24 11:44:10 -07:00
|
|
|
self.processor_registry = ProcessorRegistry()
|
2024-06-23 12:15:31 -07:00
|
|
|
|
|
|
|
def process(self, mesh_packet: MeshPacket):
|
2024-06-24 08:05:47 -07:00
|
|
|
port_num = int(mesh_packet.decoded.portnum)
|
2024-06-23 12:15:31 -07:00
|
|
|
payload = mesh_packet.decoded.payload
|
|
|
|
|
2024-06-24 11:44:10 -07:00
|
|
|
source_client_details = self._get_client_details(getattr(mesh_packet, 'from'))
|
2024-06-24 08:05:47 -07:00
|
|
|
if os.getenv('MESH_HIDE_SOURCE_DATA', 'false') == 'true':
|
2024-06-24 11:44:10 -07:00
|
|
|
source_client_details = ClientDetails(node_id=source_client_details['id'], short_name='Hidden',
|
|
|
|
long_name='Hidden')
|
|
|
|
|
|
|
|
destination_client_details = self._get_client_details(getattr(mesh_packet, 'to'))
|
2024-06-24 08:05:47 -07:00
|
|
|
if os.getenv('MESH_HIDE_DESTINATION_DATA', 'false') == 'true':
|
2024-06-24 11:44:10 -07:00
|
|
|
destination_client_details = ClientDetails(node_id=destination_client_details['id'], short_name='Hidden',
|
|
|
|
long_name='Hidden')
|
2024-06-24 08:05:47 -07:00
|
|
|
|
|
|
|
if port_num in map(int, os.getenv('FILTERED_PORTS', '1').split(',')): # Filter out ports
|
|
|
|
return None # Ignore this packet
|
|
|
|
|
2024-06-23 13:00:33 -07:00
|
|
|
self.counter.labels(
|
2024-06-24 11:44:10 -07:00
|
|
|
source_id=source_client_details.node_id,
|
|
|
|
source_short_name=source_client_details.short_name,
|
|
|
|
source_long_name=source_client_details.long_name,
|
2024-06-24 08:05:47 -07:00
|
|
|
|
2024-06-24 11:44:10 -07:00
|
|
|
destination_id=destination_client_details.node_id,
|
|
|
|
destination_short_name=destination_client_details.short_name,
|
|
|
|
destination_long_name=destination_client_details.long_name,
|
2024-06-24 08:05:47 -07:00
|
|
|
|
|
|
|
rx_time=mesh_packet.rx_time,
|
|
|
|
rx_snr=mesh_packet.rx_snr,
|
|
|
|
hop_limit=mesh_packet.hop_limit,
|
|
|
|
want_ack=mesh_packet.want_ack,
|
|
|
|
via_mqtt=mesh_packet.via_mqtt,
|
|
|
|
hop_start=mesh_packet.hop_start,
|
2024-06-23 13:00:33 -07:00
|
|
|
portnum=port_num
|
|
|
|
).inc()
|
|
|
|
|
2024-06-24 08:05:47 -07:00
|
|
|
processor = ProcessorRegistry.get_processor(port_num)(self.registry, self.redis_client)
|
2024-06-24 11:44:10 -07:00
|
|
|
processor.process(payload, client_details=source_client_details)
|
2024-06-23 13:00:33 -07:00
|
|
|
|
2024-06-24 11:44:10 -07:00
|
|
|
def _get_client_details(self, node_id: str) -> ClientDetails:
|
|
|
|
details = self.redis_client.hgetall(f"node:{node_id}")
|
2024-06-23 13:00:33 -07:00
|
|
|
if details:
|
2024-06-24 11:44:10 -07:00
|
|
|
return ClientDetails(node_id=node_id, short_name=details['short_name'], long_name=details['long_name'])
|
2024-06-23 13:00:33 -07:00
|
|
|
|
2024-06-24 11:44:10 -07:00
|
|
|
return ClientDetails(node_id=node_id, short_name='Unknown', long_name='Unknown')
|