Skip to content

Commit 733007e

Browse files
committed
Publish robot USB status to MQTT
Follows the status LED
1 parent dc7f2e3 commit 733007e

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

runusb/__main__.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
# This will be populated if we have the config file
4949
# url format: mqtt[s]://[<username>[:<password>]@]<host>[:<port>]/<topic_root>
5050
MQTT_URL = None
51+
MQTT_TOPIC_ROOT = ''
52+
MQTT_CLIENT = None
5153
MQTT_CONFIG_FILE = '/etc/sbot/mqtt.conf'
5254

5355

@@ -216,6 +218,9 @@ def __init__(self, mountpoint_path: str) -> None:
216218
self._setup_logging(mountpoint_path)
217219
LED_CONTROLLER.set_code(True)
218220
LED_CONTROLLER.set_status(LedStatus.Running)
221+
if MQTT_CLIENT is not None:
222+
MQTT_CLIENT.publish(f'{MQTT_TOPIC_ROOT}/state', '{"state": "running"}', qos=1, retain=True)
223+
219224
env = dict(os.environ)
220225
env["SBOT_METADATA_PATH"] = MOUNTPOINT_DIR
221226
if MQTT_URL is not None:
@@ -256,6 +261,9 @@ def close(self) -> None:
256261
LED_CONTROLLER.set_code(False)
257262
USERCODE_LOGGER.removeHandler(self.handler)
258263

264+
if MQTT_CLIENT is not None:
265+
MQTT_CLIENT.publish(f'{MQTT_TOPIC_ROOT}/state', '{"state": "no USB"}', qos=1, retain=True)
266+
259267
def _send_signal(self, sig: int) -> None:
260268
if self.process.poll() is not None:
261269
# Process has already exited, so the kill() call would fail.
@@ -267,8 +275,13 @@ def _watch_process(self) -> None:
267275
self.process.wait()
268276
if self.process.returncode != 0:
269277
USERCODE_LOGGER.warning(f"Process exited with code {self.process.returncode}")
278+
new_state = 'crashed'
270279
else:
271280
USERCODE_LOGGER.info("Your code finished successfully.")
281+
new_state = 'finished'
282+
283+
if MQTT_CLIENT is not None:
284+
MQTT_CLIENT.publish(f'{MQTT_TOPIC_ROOT}/state', json.dumps({"state": new_state}), qos=1, retain=True)
272285

273286
process_lifetime = time.time() - self.process_start_time
274287

@@ -438,7 +451,7 @@ def read_mqtt_config_file() -> MQTTVariables | None:
438451

439452

440453
def setup_usercode_logging() -> None:
441-
global REL_TIME_FILTER
454+
global REL_TIME_FILTER, MQTT_CLIENT, MQTT_TOPIC_ROOT
442455
REL_TIME_FILTER = RelativeTimeFilter()
443456
USERCODE_LOGGER.addFilter(REL_TIME_FILTER)
444457
USERCODE_LOGGER.setLevel(logging.DEBUG)
@@ -459,6 +472,8 @@ def setup_usercode_logging() -> None:
459472
connected_callback=lambda: LED_CONTROLLER.set_wifi(True),
460473
disconnected_callback=lambda: LED_CONTROLLER.set_wifi(False),
461474
)
475+
MQTT_CLIENT = handler.mqtt
476+
MQTT_TOPIC_ROOT = mqtt_config.topic_prefix
462477

463478
handler.setLevel(logging.INFO)
464479
handler.setFormatter(TieredFormatter(
@@ -479,6 +494,10 @@ def main():
479494
registry = AutorunProcessRegistry()
480495

481496
LED_CONTROLLER.mark_start()
497+
498+
if MQTT_CLIENT is not None:
499+
MQTT_CLIENT.publish(f'{MQTT_TOPIC_ROOT}/state', '{"state": "no USB"}', qos=1, retain=True)
500+
482501
# Initial pass (in case an autorun FS is already mounted)
483502
registry.update_filesystems(fstab_reader.read())
484503

0 commit comments

Comments
 (0)