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
|
|
|
|
|
|
|
from exporter.registry import ProcessorRegistry
|
|
|
|
|
|
|
|
|
|
|
|
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-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 08:05:47 -07:00
|
|
|
source_client_details = self._get_client_details(mesh_packet['from'])
|
|
|
|
if os.getenv('MESH_HIDE_SOURCE_DATA', 'false') == 'true':
|
|
|
|
source_client_details = {
|
|
|
|
'id': source_client_details['id'],
|
|
|
|
'short_name': 'Hidden',
|
|
|
|
'long_name': 'Hidden',
|
|
|
|
}
|
|
|
|
destination_client_details = self._get_client_details(mesh_packet['to'])
|
|
|
|
if os.getenv('MESH_HIDE_DESTINATION_DATA', 'false') == 'true':
|
|
|
|
destination_client_details = {
|
|
|
|
'id': destination_client_details['id'],
|
|
|
|
'short_name': 'Hidden',
|
|
|
|
'long_name': 'Hidden',
|
|
|
|
}
|
|
|
|
|
|
|
|
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 08:05:47 -07:00
|
|
|
source_id=source_client_details['id'],
|
|
|
|
source_short_name=source_client_details['short_name'],
|
|
|
|
source_long_name=source_client_details['long_name'],
|
|
|
|
|
|
|
|
destination_id=destination_client_details['id'],
|
|
|
|
destination_short_name=destination_client_details['short_name'],
|
|
|
|
destination_long_name=destination_client_details['long_name'],
|
|
|
|
|
|
|
|
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)
|
|
|
|
processor.process(payload)
|
2024-06-23 13:00:33 -07:00
|
|
|
|
2024-06-24 08:05:47 -07:00
|
|
|
def _get_client_details(self, id: str):
|
|
|
|
details = self.redis_client.hgetall(f"node:{id}")
|
2024-06-23 13:00:33 -07:00
|
|
|
if details:
|
|
|
|
return details
|
|
|
|
|
|
|
|
return {
|
2024-06-24 08:05:47 -07:00
|
|
|
'id': id,
|
2024-06-23 13:00:33 -07:00
|
|
|
'short_name': 'Unknown',
|
|
|
|
'long_name': 'Unknown',
|
|
|
|
}
|