Skip to content
This repository was archived by the owner on Apr 10, 2025. It is now read-only.
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions control/planktoscopehat/planktoscope/camera/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,11 @@ def run(self) -> None:
streaming_thread.start()

loguru.logger.info("Starting the MQTT backend...")
# TODO(ethanjli): expose the camera settings over "camera/settings" instead! This requires
# removing the "settings" action from the "imager/image" route which is a breaking change
# to the MQTT API, so we'll do this later.
mqtt = messaging.MQTT_Client(topic="imager/image", name="imager_camera_client")
# TODO(ethanjli): allow an MQTT client to trigger this broadcast with an MQTT command. This
# requires modifying the MQTT API (by adding a new route), and we'll want to make the
# Node-RED dashboard query that route at startup, so we'll do this later.
mqtt.client.publish("status/imager", json.dumps({"camera_name": self._camera.camera_name}))
mqtt = messaging.MQTT_Client(topic="camera/info", name="imager_camera_client")
mqtt.client.publish(
"status/camera/info",
json.dumps({"status": "success", "camera_name": self._camera.camera_name}),
)

try:
while not self._stop_event_loop.is_set():
Expand All @@ -107,8 +104,19 @@ def run(self) -> None:
if (message := mqtt.msg) is None:
continue
self._receive_message(message)
if (status_update := mqtt.read_message()) is not None:
mqtt.client.publish("status/imager", status_update)
if message["topic"] == "camera/info" and message["payload"].get("action") == "get":
camera_name = (
self._camera.camera_name
if self._camera is not None
else "Not recognized"
)
response_payload = json.dumps(
{"status": "success", "camera_name": camera_name}
)
mqtt.client.publish("status/camera/info", response_payload)
loguru.logger.info(
f"Published camera name '{camera_name}' to topic 'status/camera/info'"
)
finally:
loguru.logger.info("Stopping the MQTT API...")
mqtt.shutdown()
Expand Down