From d896e5de11a7657042fab58254f4d63dd98d2673 Mon Sep 17 00:00:00 2001 From: Cecil Worsley Date: Sun, 14 Feb 2021 12:37:56 -0500 Subject: [PATCH] Add a parameter to soft_power_off that allows the user to check if there is a soft power off This way the user can first react to soft power off before the system goes down --- power_api/power_api.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/power_api/power_api.py b/power_api/power_api.py index 70f132e..86704eb 100644 --- a/power_api/power_api.py +++ b/power_api/power_api.py @@ -1080,7 +1080,7 @@ def get_rtc_time(self, format=Definition.TIME_FORMAT_EPOCH, timeout=RESPONSE_DEL return time - def soft_power_off(self, timeout=RESPONSE_DELAY): + def soft_power_off(self, check=False, timeout=RESPONSE_DELAY): """ Function for checking any soft power off request is exist. If any request exist, raspberry pi turns off by using "sudo shutdown" terminal @@ -1088,6 +1088,9 @@ def soft_power_off(self, timeout=RESPONSE_DELAY): Parameters ----------- + check : boolean (optional) + check if it is time to perform a soft power off (default is False) + timeout : int (optional) timeout while receiving the response (default is RESPONSE_DELAY) @@ -1101,7 +1104,6 @@ def soft_power_off(self, timeout=RESPONSE_DELAY): command.send_command() delay_ms(timeout) raw = command.receive_command(COMMAND_SIZE_FOR_UINT8) - result2 = 0 result = raw[PROTOCOL_HEADER_SIZE] if result == Definition.SET_OK: @@ -1112,8 +1114,9 @@ def soft_power_off(self, timeout=RESPONSE_DELAY): result2 = raw[PROTOCOL_HEADER_SIZE] if result2 == Definition.SET_OK: - print("Raspberry Pi will shutdown in 5 seconds!") - os.system("sleep 5 && sudo shutdown -h now") + if check is False: + print("Raspberry Pi will shutdown in 5 seconds!") + os.system("sleep 5 && sudo shutdown -h now") return result2 return Definition.SET_FAILED