From 9e907f6d7730294b0190f084ab5109abc09c8be1 Mon Sep 17 00:00:00 2001 From: sophie Date: Sat, 3 Aug 2024 04:06:32 +0300 Subject: [PATCH] everything got fixed ? --- main.py | 61 +++++++++++++++++++++++++++++--------------------------- main.qml | 7 +------ 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/main.py b/main.py index 538a477..789a700 100644 --- a/main.py +++ b/main.py @@ -6,36 +6,39 @@ def is_port_in_use(port: int) -> bool: with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: return s.connect_ex(('localhost', port)) == 0 - -if(is_port_in_use(52121)): - exit(1) - -rpc = discordrpc.RPC(app_id="863873014587326494") -rpc.set_activity(details="Not doing.. anything..", large_image="small") -lastMessage = 0; - -async def echo(websocket): +def main(): + if(is_port_in_use(52121)): + return + + rpc = discordrpc.RPC(app_id="863873014587326494") + rpc.set_activity(details="Not doing.. anything..", large_image="small") global lastMessage; - async for message in websocket: - data = json.loads(message); - rpc.set_activity(state=f"Cursor position: {data["cursorPosition"]}", details=f"{data["name"]}", large_image="small") + lastMessage = 0; + + async def echo(websocket): + global lastMessage; + async for message in websocket: + data = json.loads(message); + rpc.set_activity(state=f"Cursor position: {data["cursorPosition"]}", details=f"{data["name"]}", large_image="small") + + lastMessage = time.time() + + async def main2(): + print("started ws") + loop = asyncio.get_event_loop() - lastMessage = time.time() + async def heartbeatChecker(): + if lastMessage != 0: + if time.time()-lastMessage > 5: + exit(0) + loop.call_later(2, lambda: asyncio.ensure_future(heartbeatChecker())) -async def main(): - print("started ws") - - loop = asyncio.get_event_loop() - - async def heartbeatChecker(): - if lastMessage != 0: - if time.time()-lastMessage > 5: - exit(0) - loop.call_later(2, lambda: asyncio.ensure_future(heartbeatChecker())) - - await heartbeatChecker(); - - async with await serve(echo, "localhost", 52121): - await asyncio.Future() + await heartbeatChecker(); + + async with await serve(echo, "localhost", 52121): + await asyncio.Future() -asyncio.run(main()) \ No newline at end of file + asyncio.run(main2()) + +if __name__ == "__main__": + main() diff --git a/main.qml b/main.qml index 3c273df..814b2dd 100644 --- a/main.qml +++ b/main.qml @@ -3,15 +3,13 @@ import QtQml 2.0 import QtWebSockets 1.1 Script { - //socket.active - property string scriptDirPath property QtObject timer property QtObject delay property QtObject websocket function init() { - script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath); + script.startDetachedProcess("/usr/bin/python3", [scriptDirPath + "/main.py"]); } websocket: WebSocket { @@ -28,11 +26,9 @@ Script { script.log("Open!"); } else if (socket.status == WebSocket.Closed) { this.active = false; - delay.setTimeout(() => { socket.active = true; }, 2000); - script.log("Socket closed"); } } @@ -60,7 +56,6 @@ Script { running: true repeat: true onTriggered: { - script.log(websocket.active); if (websocket.active) { var note = script.currentNote(); websocket.sendTextMessage(JSON.stringify({