From 22f5c6110d59d94d72dce794ea95edf37f1358ce Mon Sep 17 00:00:00 2001 From: miro Date: Wed, 8 Jan 2025 20:46:24 +0000 Subject: [PATCH] fix: no auto connect update README and make http client work the same as websocket (no auto connect) --- .github/workflows/publish_stable.yml | 2 +- README.md | 2 ++ hivemind_bus_client/http_client.py | 10 ++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.github/workflows/publish_stable.yml b/.github/workflows/publish_stable.yml index 88caf05..43e1379 100644 --- a/.github/workflows/publish_stable.yml +++ b/.github/workflows/publish_stable.yml @@ -26,7 +26,7 @@ jobs: - name: Setup Python uses: actions/setup-python@v1 with: - python-version: 3.8 + python-version: "3.11" - name: Install Build Tools run: | python -m pip install build wheel diff --git a/README.md b/README.md index bef9060..7d108de 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ from hivemind_bus_client.http_client import HiveMindHTTPClient # not passing key etc so it uses identity file client = HiveMindHTTPClient(host="http://localhost", port=5679) +client.connect() # establish a secure end-to-end encrypted connection ``` via [hivemind-websocket-protocol](https://github.com/JarbasHiveMind/hivemind-websocket-protocol) @@ -25,6 +26,7 @@ from hivemind_bus_client.client import HiveMessageBusClient # not passing key etc so it uses identity file client = HiveMessageBusClient(host="ws://localhost", port=5678) +client.connect() # establish a secure end-to-end encrypted connection ``` ### Example: Simple Chat diff --git a/hivemind_bus_client/http_client.py b/hivemind_bus_client/http_client.py index 57e9cde..e903aca 100644 --- a/hivemind_bus_client/http_client.py +++ b/hivemind_bus_client/http_client.py @@ -76,10 +76,9 @@ def __init__(self, key: Optional[str] = None, self._handlers: Dict[str, List[Callable[[HiveMessage], None]]] = {} self._agent_handlers: Dict[str, List[Callable[[MycroftMessage], None]]] = {} self.start() - self.wait_for_handshake() + def wait_for_handshake(self, timeout=5): - self.connected.wait(timeout=timeout) self.handshake_event.wait(timeout=timeout) if not self.handshake_event.is_set(): self.protocol.start_handshake() @@ -239,8 +238,9 @@ def _handle_hive_protocol(self, message: HiveMessage): # main loop def run(self): self.stopped.clear() + # Connect to the server - self.connect() + self.connected.wait() # Retrieve messages until stop while not self.stopped.is_set(): @@ -358,7 +358,8 @@ def connect(self, bus=FakeBus(), protocol=None, site_id=None): self.protocol.bind(bus) url = f"{self.base_url}/connect" response = requests.post(url, params={"authorization": self.auth}) - self.connected.set() # TODO validate no error in post request + self.connected.set() + self.wait_for_handshake() return response.json() def disconnect(self) -> dict: @@ -419,6 +420,7 @@ def handle_receive_tts(self, bin_data: bytes, # not passing key etc so it uses identity file client = HiveMindHTTPClient(host="http://localhost", port=5679, bin_callbacks=BinaryDataHandler()) + client.connect() # send HiveMessages as usual client.emit(HiveMessage(HiveMessageType.BUS,