diff --git a/modules/config.py b/modules/config.py index a30e303e..58fc8a4f 100644 --- a/modules/config.py +++ b/modules/config.py @@ -26,7 +26,7 @@ from logger import CustomRotatingFileHandler, app_logger from modules.helper.setting import Setting from modules.button_config import Button_Config -from modules.utils.cmd import exec_cmd, exec_cmd_return_value +from modules.utils.cmd import exec_cmd, exec_cmd_return_value, is_running_as_service from modules.utils.timer import Timer @@ -1003,10 +1003,17 @@ async def quit(self): self.loop.close() def poweroff(self): + # TODO + # should be replaced by quit() with power_off option + # keep the logic for now but remove the shutdown service eg: + # if we are running through a service, stop it and issue power-off command (on rasp-pi only) + + # this returns 0 if active + + if is_running_as_service(): + exec_cmd(["sudo", "systemctl", "stop", "pizero_bikecomputer"]) if self.G_IS_RASPI: - exec_cmd( - ["sudo", "systemctl", "start", "pizero_bikecomputer_shutdown.service"] - ) + exec_cmd(["sudo", "poweroff"]) def reboot(self): if self.G_IS_RASPI: diff --git a/modules/utils/cmd.py b/modules/utils/cmd.py index 335dfd29..c89aebd6 100644 --- a/modules/utils/cmd.py +++ b/modules/utils/cmd.py @@ -3,11 +3,12 @@ from logger import app_logger +# still return returncode def exec_cmd(cmd, cmd_print=True): if cmd_print: app_logger.info(cmd) try: - subprocess.run(cmd) + return subprocess.run(cmd).returncode except Exception: # noqa app_logger.exception(f"Failed executing {cmd}") @@ -25,3 +26,10 @@ def exec_cmd_return_value(cmd, cmd_print=True): return string except Exception: # noqa app_logger.exception(f"Failed executing {cmd}") + + +# TODO we might want to compare pid, because it might be running as a service AND manually +def is_running_as_service(): + return not exec_cmd( + ["sudo", "systemctl", "is-active", "--quiet", "pizero_bikecomputer"] + ) diff --git a/scripts/install/etc/systemd/system/pizero_bikecomputer_shutdown.service b/scripts/install/etc/systemd/system/pizero_bikecomputer_shutdown.service deleted file mode 100644 index ad3ab495..00000000 --- a/scripts/install/etc/systemd/system/pizero_bikecomputer_shutdown.service +++ /dev/null @@ -1,9 +0,0 @@ -[Unit] -Description = shutdown -DefaultDependencies = no - -[Service] -ExecStart = /usr/local/bin/pizero_bikecomputer_shutdown -Type = oneshot -RemainAfterExit = true - diff --git a/scripts/install/usr/local/bin/pizero_bikecomputer_shutdown b/scripts/install/usr/local/bin/pizero_bikecomputer_shutdown deleted file mode 100755 index 3923915b..00000000 --- a/scripts/install/usr/local/bin/pizero_bikecomputer_shutdown +++ /dev/null @@ -1,6 +0,0 @@ -#!/bin/sh - -/bin/systemctl stop pizero_bikecomputer.service - -/sbin/poweroff -