Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MQTT Handling for Retrieving and Publishing Camera Name #44

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
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.
melissadjadoun marked this conversation as resolved.
Show resolved Hide resolved
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")
melissadjadoun marked this conversation as resolved.
Show resolved Hide resolved
mqtt.client.publish(
"status/camera/info",
json.dumps({"status": "success", "camera_name": self._camera.camera_name}),
)
melissadjadoun marked this conversation as resolved.
Show resolved Hide resolved

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"
melissadjadoun marked this conversation as resolved.
Show resolved Hide resolved
)
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'"
)
ethanjli marked this conversation as resolved.
Show resolved Hide resolved
finally:
loguru.logger.info("Stopping the MQTT API...")
mqtt.shutdown()
Expand Down