From b65b0905e734ba8f1a6d23b72e5717e8016a4b42 Mon Sep 17 00:00:00 2001 From: Marat Fayzullin Date: Fri, 2 Aug 2024 19:28:45 -0400 Subject: [PATCH] Reporting client connections and chat messages via MQTT. --- owrx/client.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/owrx/client.py b/owrx/client.py index 7ffc6d02..d46bfb66 100644 --- a/owrx/client.py +++ b/owrx/client.py @@ -53,6 +53,7 @@ class ClientRegistry(object): raise TooManyClientsException() self.clients.append(client) self.broadcast() + self.reportClient(client, True) def clientCount(self): return len(self.clients) @@ -65,12 +66,33 @@ class ClientRegistry(object): except ValueError: pass self.broadcast() + self.reportClient(client, False) def _checkClientCount(self, new_count): for client in self.clients[new_count:]: logger.debug("closing one connection...") client.close() + # Report client coming or leaving + def reportClient(self, client, connected: bool): + ReportingEngine.getSharedInstance().spot({ + "mode" : "CLIENT", + "timestamp" : round(datetime.now().timestamp() * 1000), + "ip" : self.getIp(client.conn.handler), + "state" : "CONNECTED" if connected else "DISCONNECTED" + }) + + # Report chat message from a client + def reportChatMessage(self, client, text: str): + name = self.chat[client]["name"] if client in self.chat else "???" + ReportingEngine.getSharedInstance().spot({ + "mode" : "CHAT", + "timestamp" : round(datetime.now().timestamp() * 1000), + "ip" : self.getIp(client.conn.handler), + "name" : name, + "message" : text + }) + # Broadcast chat message to all connected clients. def broadcastChatMessage(self, client, text: str, name: str = None): # If chat disabled, ignore messages @@ -105,18 +127,13 @@ class ClientRegistry(object): self.chat[client] = { "name": name, "color": color } 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 for c in self.clients: c.write_chat_message(name, text, color) + # Report message + self.reportChatMessage(client, text) + # Broadcast administrative message to all connected clients. def broadcastAdminMessage(self, text: str): for c in self.clients: