wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import json
import logging
import queue
import threading
import time
from typing import Any
 
logger = logging.getLogger(__name__)
 
 
class WakewordEventBridge:
    def __init__(self) -> None:
        self._clients: list[queue.Queue[str]] = []
        self._clients_lock = threading.Lock()
        self._running = True
 
    @property
    def is_running(self) -> bool:
        return self._running
 
    def build_ready_message(self) -> str:
        return self.build_message(
            "bridge_connected",
            {"status": "ready"},
        )
 
    def publish_detected(self, wake_word: str) -> None:
        self.publish(
            "wake_word_detected",
            {
                "wake_word": wake_word,
                "timestamp": time.time(),
            },
        )
 
    def publish(self, event_type: str, payload: dict[str, Any] | None = None) -> None:
        if not self._running:
            return
 
        message = self.build_message(event_type, payload or {})
        with self._clients_lock:
            clients = list(self._clients)
 
        stale_clients: list[queue.Queue[str]] = []
        for client_queue in clients:
            try:
                client_queue.put_nowait(message)
            except queue.Full:
                stale_clients.append(client_queue)
 
        if stale_clients:
            with self._clients_lock:
                for client_queue in stale_clients:
                    if client_queue in self._clients:
                        self._clients.remove(client_queue)
 
    def add_client(self) -> queue.Queue[str]:
        client_queue: queue.Queue[str] = queue.Queue(maxsize=16)
        with self._clients_lock:
            self._clients.append(client_queue)
        return client_queue
 
    def build_message(
        self,
        event_type: str,
        payload: dict[str, Any] | None = None,
        request_id: str | None = None,
        success: bool = True,
        error: str | None = None,
    ) -> str:
        message: dict[str, Any] = {
            "type": event_type,
            "requestId": request_id,
            "success": success,
            "payload": payload or {},
        }
        if error:
            message["error"] = error
        return json.dumps(message, ensure_ascii=False)
 
    def remove_client(self, client_queue: queue.Queue[str]) -> None:
        with self._clients_lock:
            if client_queue in self._clients:
                self._clients.remove(client_queue)
 
    def publish_service_ready(self) -> None:
        self.publish("service_ready", {"status": "ready"})
 
    def publish_service_stopping(self) -> None:
        self.publish("service_stopping", {"status": "stopping"})
 
    def close(self) -> None:
        if not self._running:
            return
 
        self.publish_service_stopping()
        self._running = False
        with self._clients_lock:
            clients = list(self._clients)
            self._clients.clear()
 
        for client_queue in clients:
            try:
                client_queue.put_nowait("__bridge_closed__")
            except queue.Full:
                pass