FreeDATA/modem/event_manager.py

72 lines
2.3 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)
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 = {
f"arq-transfer-{direction}": {
'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)
2023-12-25 12:26:51 +00:00
def send_arq_session_progress(self, outbound: bool, session_id, dxcall, received_bytes, total_bytes, state):
2023-12-19 14:35:07 +00:00
direction = 'outbound' if outbound else 'inbound'
event = {
f"arq-transfer-{direction}": {
'session_id': session_id,
'dxcall': dxcall,
'received_bytes': received_bytes,
'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)
2023-12-30 22:00:27 +00:00
def send_arq_session_finished(self, outbound: bool, session_id, dxcall, total_bytes, success: bool, state, data=False):
if data:
data = base64.b64encode(data).decode("UTF-8")
2023-12-19 14:35:07 +00:00
direction = 'outbound' if outbound else 'inbound'
event = {
f"arq-transfer-{direction}": {
'session_id': session_id,
'dxcall': dxcall,
'total_bytes': total_bytes,
'success': success,
2023-12-25 12:26:51 +00:00
'state': state,
2023-12-30 22:00:27 +00:00
'data': data
2023-12-19 14:35:07 +00:00
}
}
2023-12-30 22:00:27 +00:00
self.broadcast(event)