From 0480c2ddd0390c5b6a0c29c81b85b740c69c4f90 Mon Sep 17 00:00:00 2001 From: sophie Date: Fri, 2 Aug 2024 18:08:48 +0300 Subject: [PATCH] first commit --- main.py | 41 +++++++++++++++++++++++++++++++++++++++++ main.qml | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 main.py create mode 100644 main.qml diff --git a/main.py b/main.py new file mode 100644 index 0000000..538a477 --- /dev/null +++ b/main.py @@ -0,0 +1,41 @@ + +import asyncio, time, json, discordrpc, socket +from websockets.server import serve + +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): + 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 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() + +asyncio.run(main()) \ No newline at end of file diff --git a/main.qml b/main.qml new file mode 100644 index 0000000..203cbd5 --- /dev/null +++ b/main.qml @@ -0,0 +1,47 @@ +import QOwnNotesTypes 1.0 +import QtQml 2.0 +import QtWebSockets 1.1 + +Script { + property string scriptDirPath + property QtObject timer + property QtObject websocket + + websocket: WebSocket { + id: socket + + url: "ws://localhost:52121" + onTextMessageReceived: { + script.log("Received message: " + message); + } + onStatusChanged: { + if (socket.status == WebSocket.Error) + script.log("Error: " + socket.errorString); + else if (socket.status == WebSocket.Open) + script.log("Open!"); + else if (socket.status == WebSocket.Closed) + script.log("Socket closed"); + } + active: true + } + + function init() { + script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath); + } + + timer: Timer { + interval: 1000 + running: true + repeat: true + onTriggered: { + var note = script.currentNote(); + + websocket.sendTextMessage(JSON.stringify({ + name: note.name, + cursorPosition: script.noteTextEditCursorPosition(), + + })) + } + } + +}