ソースを参照

add cd recording status websocket

Noah Vogt 1 日 前
コミット
d154350e7e

+ 83 - 0
cd_recording_status_websocket.py

@@ -0,0 +1,83 @@
+#!/usr/bin/env python3
+
+import asyncio
+import json
+from os import path
+
+import time
+import websockets
+
+from utils import expand_dir, get_current_yyyy_mm_dd_date, log
+import config as const
+from input import get_cachefile_content
+from recording import (
+    is_valid_cd_record_checkfile,
+    get_is_recording_active,
+    get_cd_count,
+    get_cd_marker_count,
+    get_full_rec_time,
+    get_track_rec_time,
+)
+
+# Simulated CD status (you can replace this with real logic)
+cd_number = 3
+track_number = 7
+start_time = time.time()
+track_start = time.time() - 37  # Simulate track started 37s ago
+
+
+def get_inactive_recording_status() -> dict:
+    return {
+        "recording": False,
+        "cd": 0,
+        "track": 0,
+        "cd_time": "00:00",
+        "track_time": "00:00",
+    }
+
+
+async def status_sender(websocket):
+    while True:
+        if path.isfile(expand_dir(const.CD_RECORD_CACHEFILE)):
+            cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
+            if is_valid_cd_record_checkfile(
+                cachefile_content, get_current_yyyy_mm_dd_date()
+            ):
+                is_recording_active = get_is_recording_active(cachefile_content)
+                status = {
+                    "recording": is_recording_active,
+                    "cd": get_cd_count(is_recording_active, cachefile_content),
+                    "track": get_cd_count(
+                        is_recording_active, cachefile_content
+                    ),
+                    "cd_time": get_full_rec_time(
+                        is_recording_active, cachefile_content
+                    ),
+                    "track_time": get_track_rec_time(
+                        is_recording_active, cachefile_content
+                    ),
+                }
+            else:
+                status = get_inactive_recording_status()
+        else:
+            status = get_inactive_recording_status()
+        await websocket.send(json.dumps(status))
+        await asyncio.sleep(0.3)
+
+
+async def handler(websocket):
+    log(f"Client connected: {websocket.remote_address}")
+    try:
+        await status_sender(websocket)
+    except websockets.exceptions.ConnectionClosed:
+        log("Client disconnected")
+
+
+async def main():
+    log("Starting CD Rec status WebSocket server on ws://localhost:8765")
+    async with websockets.serve(handler, "localhost", 8765):
+        await asyncio.Future()  # Run forever
+
+
+if __name__ == "__main__":
+    asyncio.run(main())

+ 8 - 1
recording/__init__.py

@@ -26,4 +26,11 @@ from .verify import (
 )
 from .cd import mark_end_of_recording, burn_cds_of_day
 from .gui import choose_sermon_day, choose_cd_day
-from .status_display import cd_recording_status_webserver
+from .status_display import (
+    cd_recording_status_webserver,
+    get_is_recording_active,
+    get_cd_count,
+    get_cd_marker_count,
+    get_track_rec_time,
+    get_full_rec_time,
+)

+ 5 - 1
recording/status_display.py

@@ -24,6 +24,10 @@ from .verify import ongoing_cd_recording_detected, calc_cuesheet_timestamp
 cd_recording_status_webserver = Flask(__name__)
 
 
+def get_is_recording_active(cachefile_content: list) -> bool:
+    return ongoing_cd_recording_detected(cachefile_content)
+
+
 def get_cd_marker_count(active_recording: bool, cachefile_content: list) -> int:
     if not active_recording:
         return 0
@@ -58,10 +62,10 @@ def get_track_rec_time(active_recording: bool, cachefile_content: list) -> str:
 
 @cd_recording_status_webserver.route("/")
 def index():
-    recording_active = ongoing_cd_recording_detected()
     cachefile_content = get_cachefile_content(
         const.CD_RECORD_CACHEFILE, suppress_error=True
     )
+    recording_active = get_is_recording_active(cachefile_content)
     cd_marker_count = get_cd_marker_count(recording_active, cachefile_content)
     cd_count = get_cd_count(recording_active, cachefile_content)
     cd_rec_time = get_full_rec_time(recording_active, cachefile_content)

+ 10 - 10
recording/verify.py

@@ -60,21 +60,21 @@ def is_valid_cd_record_checkfile(
     )
 
 
-def ongoing_cd_recording_detected() -> bool:
-    if path.isfile(expand_dir(const.CD_RECORD_CACHEFILE)):
-        cachefile_content = get_cachefile_content(const.CD_RECORD_CACHEFILE)
-        if is_valid_cd_record_checkfile(
-            cachefile_content, get_current_yyyy_mm_dd_date()
+def ongoing_cd_recording_detected(cachefile_content: list) -> bool:
+    if is_valid_cd_record_checkfile(
+        cachefile_content, get_current_yyyy_mm_dd_date()
+    ):
+        if cachefile_content[1].strip() != "9001" and pid_exists(
+            int(cachefile_content[2].strip())
         ):
-            if cachefile_content[1].strip() != "9001" and pid_exists(
-                int(cachefile_content[2].strip())
-            ):
-                return True
+            return True
     return False
 
 
 def make_sure_there_is_no_ongoing_cd_recording() -> None:
-    if ongoing_cd_recording_detected():
+    if ongoing_cd_recording_detected(
+        get_cachefile_content(const.CD_RECORD_CACHEFILE)
+    ):
         InfoMsgBox(
             QMessageBox.Critical,
             "Error",

+ 1 - 0
requirements-unix.txt

@@ -8,3 +8,4 @@ pycdio
 soundfile
 Flask
 psutil
+websockets

+ 1 - 0
requirements-win32.txt

@@ -8,3 +8,4 @@ wmi
 soundfile
 Flask
 psutil
+websockets