Skip to content

Commit

Permalink
Group publishing mqtt state under setting status led
Browse files Browse the repository at this point in the history
  • Loading branch information
WillB97 authored Jul 11, 2024
1 parent f5668b1 commit 1027f7f
Showing 1 changed file with 20 additions and 40 deletions.
60 changes: 20 additions & 40 deletions runusb/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ def set_status(self, value: LedStatus) -> None:
GPIO.output(self.LEDs.STATUS_RED, GPIO.HIGH if value.value[0] else GPIO.LOW)
GPIO.output(self.LEDs.STATUS_GREEN, GPIO.HIGH if value.value[1] else GPIO.LOW)
GPIO.output(self.LEDs.STATUS_BLUE, GPIO.HIGH if value.value[2] else GPIO.LOW)

status_str = {
LedStatus.NoUSB: "no USB",
LedStatus.Running: "running",
LedStatus.Killed: "killed",
LedStatus.Finished: "finished",
LedStatus.Crashed: "crashed",
}

# Also send the status over MQTT
if MQTT_CLIENT is not None:
MQTT_CLIENT.publish(
f'{MQTT_TOPIC_ROOT}/state',
json.dumps({"state": status_str.get(value)}),
qos=1,
retain=True,
)


LED_CONTROLLER = LEDController()
Expand Down Expand Up @@ -218,13 +235,6 @@ def __init__(self, mountpoint_path: str) -> None:
self._setup_logging(mountpoint_path)
LED_CONTROLLER.set_code(True)
LED_CONTROLLER.set_status(LedStatus.Running)
if MQTT_CLIENT is not None:
MQTT_CLIENT.publish(
f'{MQTT_TOPIC_ROOT}/state',
'{"state": "running"}',
qos=1,
retain=True,
)

env = dict(os.environ)
env["SBOT_METADATA_PATH"] = MOUNTPOINT_DIR
Expand Down Expand Up @@ -258,22 +268,13 @@ def cleanup(self) -> None:
except subprocess.TimeoutExpired:
# The process did not exit after 5 seconds, so kill it.
self._send_signal(signal.SIGKILL)
self._set_leds()

def close(self) -> None:
self.cleanup()
LED_CONTROLLER.set_status(LedStatus.NoUSB)
LED_CONTROLLER.set_code(False)
USERCODE_LOGGER.removeHandler(self.handler)

if MQTT_CLIENT is not None:
MQTT_CLIENT.publish(
f'{MQTT_TOPIC_ROOT}/state',
'{"state": "no USB"}',
qos=1,
retain=True,
)

def _send_signal(self, sig: int) -> None:
if self.process.poll() is not None:
# Process has already exited, so the kill() call would fail.
Expand All @@ -285,18 +286,10 @@ def _watch_process(self) -> None:
self.process.wait()
if self.process.returncode != 0:
USERCODE_LOGGER.warning(f"Process exited with code {self.process.returncode}")
new_state = 'crashed'
LED_CONTROLLER.set_status(LedStatus.Crashed)
else:
USERCODE_LOGGER.info("Your code finished successfully.")
new_state = 'finished'

if MQTT_CLIENT is not None:
MQTT_CLIENT.publish(
f'{MQTT_TOPIC_ROOT}/state',
json.dumps({"state": new_state}),
qos=1,
retain=True,
)
LED_CONTROLLER.set_status(LedStatus.Finished)

process_lifetime = time.time() - self.process_start_time

Expand Down Expand Up @@ -334,12 +327,6 @@ def _log_output(self, pipe: IO[str]) -> None:
USERCODE_LOGGER.log(USERCODE_LEVEL, line.rstrip('\n'))
LOGGER.info('Process output finished')

def _set_leds(self) -> None:
if self.process.returncode == 0:
LED_CONTROLLER.set_status(LedStatus.Finished)
else:
LED_CONTROLLER.set_status(LedStatus.Crashed)

def _rotate_old_logs(self, log_dir: str) -> None:
"""
Add a suffix to the old log file, if it exists.
Expand Down Expand Up @@ -509,14 +496,7 @@ def main():
registry = AutorunProcessRegistry()

LED_CONTROLLER.mark_start()

if MQTT_CLIENT is not None:
MQTT_CLIENT.publish(
f'{MQTT_TOPIC_ROOT}/state',
'{"state": "no USB"}',
qos=1,
retain=True,
)
LED_CONTROLLER.set_status(LedStatus.NoUSB)

# Initial pass (in case an autorun FS is already mounted)
registry.update_filesystems(fstab_reader.read())
Expand Down

0 comments on commit 1027f7f

Please sign in to comment.