From 26eb6ea44bf93abe10effa1765b2f94a989b111c Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Thu, 17 Jun 2021 15:04:54 -0400 Subject: [PATCH] Clean up code --- system-status.py | 58 +++++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/system-status.py b/system-status.py index 1a7282d..a8da57a 100644 --- a/system-status.py +++ b/system-status.py @@ -1,9 +1,8 @@ import psutil import sys -import signal from gpiozero import Button -from signal import pause +from signal import signal, SIGTERM from subprocess import check_call from time import sleep from unicornhatmini import UnicornHATMini, BUTTON_A @@ -19,7 +18,7 @@ PINK = [64, 0, 16] TEMP_COLOR = RED CPU_COLOR = BLUE -NETWORK_COLOR = ORANGE +NETWORK_COLOR = PINK MIN_TEMP = 40 MAX_TEMP = 80 @@ -32,16 +31,15 @@ class SystemStatus: self.hat.set_brightness(0.1) self.hat.set_rotation(270) self.width, self.height = self.hat.get_shape() - self.isRunning = True - self.tempStartColumn = 0 - self.tempEndColumn = int(self.width / 2) - self.cpuStartColumn = int(self.width / 2) - self.cpuEndColumn = self.width - 1 + self.tempStartColumn = 0 + self.tempEndColumn = int(self.width / 2) + self.cpuStartColumn = int(self.width / 2) + self.cpuEndColumn = self.width - 1 self.networkStartColumn = self.width - 1 - self.networkEndColumn = self.width + self.networkEndColumn = self.width - self.clear() + self.start() def terminate(self): self.stop() @@ -53,6 +51,10 @@ class SystemStatus: sleep(0.25) self.clear() + def start(self): + self.isRunning = True + self.clear() + def stop(self): self.isRunning = False self.clear() @@ -116,27 +118,10 @@ class SystemStatus: return min(self.height, len(remoteConnections)) -def cleanExit(systemStatus, button): - def exit(signum, frame): - button.close() - systemStatus.stop() - sys.exit(0) - - return exit - - -def poweroff(systemStatus): - def terminate(): - systemStatus.terminate() - check_call(['sudo', 'poweroff']) - - return terminate - - def main(): systemStatus = SystemStatus(UnicornHATMini()) powerButton = Button(BUTTON_A, hold_time=POWER_BUTTON_HOLD_TIME_IN_SECONDS) - signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton)) + signal(SIGTERM, cleanExit(systemStatus, powerButton)) powerButton.when_held = poweroff(systemStatus) @@ -145,5 +130,22 @@ def main(): sleep(DISPLAY_UPDATE_IN_SECONDS) +def cleanExit(systemStatus, button): + def _cleanExit_(signum, frame): + button.close() + systemStatus.stop() + sys.exit(0) + + return _cleanExit_ + + +def poweroff(systemStatus): + def _poweroff_(): + systemStatus.terminate() + check_call(['sudo', 'poweroff']) + + return _poweroff_ + + if __name__ == '__main__': main()