FreeDATA/modem/event_manager.py

101 lines
3.1 KiB
Python
Raw Normal View History

2023-12-30 22:00:27 +00:00
import base64
import json
2023-11-30 07:44:18 +00:00
import structlog
class EventManager:
def __init__(self, queues):
self.queues = queues
2023-12-19 14:35:07 +00:00
self.logger = structlog.get_logger('Event Manager')
2023-11-30 21:50:10 +00:00
self.lastpttstate = False
def broadcast(self, data):
for q in self.queues:
2023-12-19 14:35:07 +00:00
self.logger.debug(f"Event: ", ev=data)
2024-03-15 12:28:21 +00:00
if q.qsize() > 10:
q.queue.clear()
q.put(data)
def send_ptt_change(self, on:bool = False):
2023-11-30 21:50:10 +00:00
if (on == self.lastpttstate):
return
self.lastpttstate= on
self.broadcast({"ptt": bool(on)})
2023-11-12 22:22:53 +00:00
def send_scatter_change(self, data):
2023-12-01 02:12:04 +00:00
self.broadcast({"scatter": json.dumps(data)})
2023-11-13 17:11:55 +00:00
def send_buffer_overflow(self, data):
self.broadcast({"buffer-overflow": str(data)})
2023-11-20 08:00:07 +00:00
def send_custom_event(self, **event_data):
self.broadcast(event_data)
2023-12-19 14:35:07 +00:00
2023-12-25 12:26:51 +00:00
def send_arq_session_new(self, outbound: bool, session_id, dxcall, total_bytes, state):
2023-12-19 14:35:07 +00:00
direction = 'outbound' if outbound else 'inbound'
event = {
"type": "arq",
f"arq-transfer-{direction}": {
2023-12-19 14:35:07 +00:00
'session_id': session_id,
'dxcall': dxcall,
'total_bytes': total_bytes,
2023-12-25 12:26:51 +00:00
'state': state,
2023-12-19 14:35:07 +00:00
}
}
self.broadcast(event)
2024-02-24 20:49:53 +00:00
def send_arq_session_progress(self, outbound: bool, session_id, dxcall, received_bytes, total_bytes, state, statistics=None):
if statistics is None:
statistics = {}
2023-12-19 14:35:07 +00:00
direction = 'outbound' if outbound else 'inbound'
event = {
"type": "arq",
f"arq-transfer-{direction}": {
2023-12-19 14:35:07 +00:00
'session_id': session_id,
'dxcall': dxcall,
'received_bytes': received_bytes,
'total_bytes': total_bytes,
2023-12-25 12:26:51 +00:00
'state': state,
2024-02-24 20:49:53 +00:00
'statistics': statistics,
2023-12-19 14:35:07 +00:00
}
}
self.broadcast(event)
def send_arq_session_finished(self, outbound: bool, session_id, dxcall, success: bool, state: bool, data=False, statistics=None):
if statistics is None:
statistics = {}
2023-12-30 22:00:27 +00:00
if data:
data = base64.b64encode(data).decode("UTF-8")
2023-12-19 14:35:07 +00:00
direction = 'outbound' if outbound else 'inbound'
event = {
"type" : "arq",
f"arq-transfer-{direction}": {
2023-12-19 14:35:07 +00:00
'session_id': session_id,
'dxcall': dxcall,
'statistics': statistics,
'success': bool(success),
2023-12-25 12:26:51 +00:00
'state': state,
2024-01-05 15:25:34 +00:00
'data': data
2023-12-19 14:35:07 +00:00
}
}
2023-12-30 22:00:27 +00:00
self.broadcast(event)
def modem_started(self):
event = {"modem": "started"}
self.broadcast(event)
def modem_restarted(self):
event = {"modem": "restarted"}
self.broadcast(event)
def modem_stopped(self):
event = {"modem": "stopped"}
self.broadcast(event)
def modem_failed(self):
event = {"modem": "failed"}
2024-01-27 11:07:07 +00:00
self.broadcast(event)
def freedata_message_db_change(self):
self.broadcast({"message-db": "changed"})