Moved main.py to serve.py, implemented modularization and added control module

This commit is contained in:
Malasaur 2025-08-30 13:43:21 +02:00
parent ad6190b46a
commit e185786959
6 changed files with 138 additions and 21 deletions

View file

@ -2,6 +2,7 @@ from typing import Callable, List, Optional
from requests import post as _post
from sseclient import SSEClient
from threading import Thread
from inspect import stack
from time import sleep
from json import loads
from uuid import uuid4
@ -31,7 +32,8 @@ def listGroups() -> List[libcommon.Group]:
def onMessage(group: Optional[libcommon.Group] = None) -> Callable:
def decorator(func: Callable) -> Callable:
libcommon.signalMessageBinds.append((group, func))
module = stack()[1].filename.split("/")[-1][:-3]
libcommon.signalMessageBinds[module].append((group, func))
return func
return decorator
@ -44,9 +46,13 @@ def main():
sleep(5)
for msg in SSEClient(url+"/api/v1/events"):
data = loads(msg.data)
try:
data = loads(msg.data)
except:
data = {}
envelope = data.get("envelope", None)
if envelope:
dataMessage = envelope.get("dataMessage", None)
if dataMessage:
groupInfo = dataMessage.get("groupInfo", None)
@ -55,10 +61,27 @@ def main():
body = dataMessage["message"]
groupId = groupInfo["groupId"]
if body is not None:
for group, func in libcommon.signalMessageBinds:
if group is None or group.id == groupId:
func(name, body)
break
for module in libcommon.loadedMods:
if module in libcommon.signalMessageBinds:
for group, func in libcommon.signalMessageBinds[module]:
if group is None or group.id == groupId:
func(name, body)
syncMessage = envelope.get("syncMessage", None)
if syncMessage:
sentMessage = syncMessage.get("sentMessage", None)
if sentMessage:
groupInfo = sentMessage.get("groupInfo", None)
if groupInfo:
name = envelope["sourceName"]
body = sentMessage["message"]
groupId = groupInfo["groupId"]
if body is not None:
for module in libcommon.loadedMods:
if module in libcommon.signalMessageBinds:
for group, func in libcommon.signalMessageBinds[module]:
if group is None or group.id == groupId:
func(name, body)
if __name__ == "__main__":