mirror of
https://github.com/craigerl/aprsd.git
synced 2026-01-20 20:46:28 -05:00
The seen_list, tracker and aprsdthreadlist wasn't serializing it's data when requested during stats() time..
64 lines
2.1 KiB
Python
64 lines
2.1 KiB
Python
import datetime
|
|
import logging
|
|
import threading
|
|
|
|
from oslo_config import cfg
|
|
|
|
from aprsd.packets import core
|
|
from aprsd.utils import objectstore
|
|
|
|
CONF = cfg.CONF
|
|
LOG = logging.getLogger('APRSD')
|
|
|
|
|
|
class SeenList(objectstore.ObjectStoreMixin):
|
|
"""Global callsign seen list."""
|
|
|
|
_instance = None
|
|
data: dict = {}
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
if cls._instance is None:
|
|
cls._instance = super().__new__(cls)
|
|
cls._instance.lock = threading.RLock()
|
|
cls._instance.data = {}
|
|
return cls._instance
|
|
|
|
def stats(self, serializable=False):
|
|
"""Return the stats for the PacketTrack class."""
|
|
with self.lock:
|
|
if serializable:
|
|
# Convert datetime objects to strings for JSON serialization
|
|
serializable_data = {}
|
|
for callsign, data in self.data.items():
|
|
serializable_data[callsign] = data.copy()
|
|
if 'last' in serializable_data[callsign] and isinstance(
|
|
serializable_data[callsign]['last'], datetime.datetime
|
|
):
|
|
serializable_data[callsign]['last'] = serializable_data[
|
|
callsign
|
|
]['last'].isoformat()
|
|
return serializable_data
|
|
else:
|
|
return self.data
|
|
|
|
def rx(self, packet: type[core.Packet]):
|
|
"""When we get a packet from the network, update the seen list."""
|
|
with self.lock:
|
|
callsign = None
|
|
if packet.from_call:
|
|
callsign = packet.from_call
|
|
else:
|
|
LOG.warning(f"Can't find FROM in packet {packet}")
|
|
return
|
|
if callsign not in self.data:
|
|
self.data[callsign] = {
|
|
'last': None,
|
|
'count': 0,
|
|
}
|
|
self.data[callsign]['last'] = datetime.datetime.now()
|
|
self.data[callsign]['count'] += 1
|
|
|
|
def tx(self, packet: type[core.Packet]):
|
|
"""We don't care about TX packets."""
|