Reporting RX startup, client connections, and chat messages via MQTT.

This commit is contained in:
Marat Fayzullin 2024-08-02 19:28:45 -04:00
parent 2bdcdc034d
commit 3825d86732
2 changed files with 47 additions and 10 deletions

View File

@ -20,8 +20,10 @@ from owrx.reporting import ReportingEngine
from owrx.version import openwebrx_version from owrx.version import openwebrx_version
from owrx.audio.queue import DecoderQueue from owrx.audio.queue import DecoderQueue
from owrx.admin import add_admin_parser, run_admin_action from owrx.admin import add_admin_parser, run_admin_action
from owrx.reporting import ReportingEngine
from owrx.markers import Markers from owrx.markers import Markers
from owrx.gps import GpsUpdater from owrx.gps import GpsUpdater
from datetime import datetime
from pathlib import Path from pathlib import Path
import signal import signal
import argparse import argparse
@ -29,6 +31,7 @@ import socket
import ssl import ssl
import os.path import os.path
class ThreadedHttpServer(ThreadingMixIn, HTTPServer): class ThreadedHttpServer(ThreadingMixIn, HTTPServer):
def __init__(self, web_port, RequestHandlerClass, use_ipv6, bind_address=None): def __init__(self, web_port, RequestHandlerClass, use_ipv6, bind_address=None):
if bind_address is None: if bind_address is None:
@ -143,6 +146,14 @@ Support and info: https://groups.io/g/openwebrx
# Instantiate and refresh marker database # Instantiate and refresh marker database
Markers.start() Markers.start()
# Report receiver started
ReportingEngine.getSharedInstance().spot({
"mode" : "RX",
"timestamp" : round(datetime.now().timestamp() * 1000),
"version" : openwebrx_version,
"state" : "ReceiverStarted"
})
try: try:
# This is our HTTP server # This is our HTTP server
server = ThreadedHttpServer(coreConfig.get_web_port(), RequestHandler, coreConfig.get_web_ipv6(), coreConfig.get_web_bind_address()) server = ThreadedHttpServer(coreConfig.get_web_port(), RequestHandler, coreConfig.get_web_ipv6(), coreConfig.get_web_bind_address())
@ -170,7 +181,17 @@ Support and info: https://groups.io/g/openwebrx
GpsUpdater.stop() GpsUpdater.stop()
Services.stop() Services.stop()
SdrService.stopAllSources() SdrService.stopAllSources()
ReportingEngine.stopAll()
DecoderQueue.stopAll() DecoderQueue.stopAll()
# Report receiver stopped
ReportingEngine.getSharedInstance().spot({
"mode" : "RX",
"timestamp" : round(datetime.now().timestamp() * 1000),
"version" : openwebrx_version,
"state" : "ReceiverStopped"
})
# Done with reporting now
ReportingEngine.stopAll()
return 0 return 0

View File

@ -1,6 +1,5 @@
from owrx.config import Config from owrx.config import Config
from owrx.color import ColorCache from owrx.color import ColorCache
from owrx.reporting import ReportingEngine
from datetime import datetime, timedelta from datetime import datetime, timedelta
from ipaddress import ip_address from ipaddress import ip_address
import threading import threading
@ -53,11 +52,13 @@ class ClientRegistry(object):
raise TooManyClientsException() raise TooManyClientsException()
self.clients.append(client) self.clients.append(client)
self.broadcast() self.broadcast()
self.reportClient(client, { "state":"CONNECTED" })
def clientCount(self): def clientCount(self):
return len(self.clients) return len(self.clients)
def removeClient(self, client): def removeClient(self, client):
self.reportClient(client, { "state":"DISCONNECTED" })
try: try:
if client in self.chat: if client in self.chat:
del self.chat[client] del self.chat[client]
@ -71,6 +72,26 @@ class ClientRegistry(object):
logger.debug("closing one connection...") logger.debug("closing one connection...")
client.close() client.close()
# Report client events
def reportClient(self, client, data):
from owrx.reporting import ReportingEngine
data.update({
"mode" : "CLIENT",
"timestamp" : round(datetime.now().timestamp() * 1000),
"ip" : self.getIp(client.conn.handler),
"banned" : self.isBanned(client.conn.handler)
})
ReportingEngine.getSharedInstance().spot(data)
# Report chat message from a client
def reportChatMessage(self, client, text: str):
name = self.chat[client]["name"] if client in self.chat else "???"
self.reportClient(client, {
"state" : "CHAT",
"name" : name,
"message" : text
})
# Broadcast chat message to all connected clients. # Broadcast chat message to all connected clients.
def broadcastChatMessage(self, client, text: str, name: str = None): def broadcastChatMessage(self, client, text: str, name: str = None):
# If chat disabled, ignore messages # If chat disabled, ignore messages
@ -105,18 +126,13 @@ class ClientRegistry(object):
self.chat[client] = { "name": name, "color": color } self.chat[client] = { "name": name, "color": color }
self.chatCount = self.chatCount + 1 self.chatCount = self.chatCount + 1
# Report message
ReportingEngine.getSharedInstance().spot({
"mode" : "CHAT",
"timestamp" : round(datetime.now().timestamp() * 1000),
"name" : name,
"message" : text
})
# Broadcast message to all clients # Broadcast message to all clients
for c in self.clients: for c in self.clients:
c.write_chat_message(name, text, color) c.write_chat_message(name, text, color)
# Report message
self.reportChatMessage(client, text)
# Broadcast administrative message to all connected clients. # Broadcast administrative message to all connected clients.
def broadcastAdminMessage(self, text: str): def broadcastAdminMessage(self, text: str):
for c in self.clients: for c in self.clients: