Clean up code

This commit is contained in:
Mike Cifelli 2021-06-17 15:04:54 -04:00
parent eb12d4444a
commit 26eb6ea44b
1 changed files with 30 additions and 28 deletions

View File

@ -1,9 +1,8 @@
import psutil import psutil
import sys import sys
import signal
from gpiozero import Button from gpiozero import Button
from signal import pause from signal import signal, SIGTERM
from subprocess import check_call from subprocess import check_call
from time import sleep from time import sleep
from unicornhatmini import UnicornHATMini, BUTTON_A from unicornhatmini import UnicornHATMini, BUTTON_A
@ -19,7 +18,7 @@ PINK = [64, 0, 16]
TEMP_COLOR = RED TEMP_COLOR = RED
CPU_COLOR = BLUE CPU_COLOR = BLUE
NETWORK_COLOR = ORANGE NETWORK_COLOR = PINK
MIN_TEMP = 40 MIN_TEMP = 40
MAX_TEMP = 80 MAX_TEMP = 80
@ -32,16 +31,15 @@ class SystemStatus:
self.hat.set_brightness(0.1) self.hat.set_brightness(0.1)
self.hat.set_rotation(270) self.hat.set_rotation(270)
self.width, self.height = self.hat.get_shape() self.width, self.height = self.hat.get_shape()
self.isRunning = True
self.tempStartColumn = 0 self.tempStartColumn = 0
self.tempEndColumn = int(self.width / 2) self.tempEndColumn = int(self.width / 2)
self.cpuStartColumn = int(self.width / 2) self.cpuStartColumn = int(self.width / 2)
self.cpuEndColumn = self.width - 1 self.cpuEndColumn = self.width - 1
self.networkStartColumn = self.width - 1 self.networkStartColumn = self.width - 1
self.networkEndColumn = self.width self.networkEndColumn = self.width
self.clear() self.start()
def terminate(self): def terminate(self):
self.stop() self.stop()
@ -53,6 +51,10 @@ class SystemStatus:
sleep(0.25) sleep(0.25)
self.clear() self.clear()
def start(self):
self.isRunning = True
self.clear()
def stop(self): def stop(self):
self.isRunning = False self.isRunning = False
self.clear() self.clear()
@ -116,27 +118,10 @@ class SystemStatus:
return min(self.height, len(remoteConnections)) 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(): def main():
systemStatus = SystemStatus(UnicornHATMini()) systemStatus = SystemStatus(UnicornHATMini())
powerButton = Button(BUTTON_A, hold_time=POWER_BUTTON_HOLD_TIME_IN_SECONDS) 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) powerButton.when_held = poweroff(systemStatus)
@ -145,5 +130,22 @@ def main():
sleep(DISPLAY_UPDATE_IN_SECONDS) 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__': if __name__ == '__main__':
main() main()