system-status/system-status.py

131 lines
3.3 KiB
Python
Raw Normal View History

2021-06-17 10:07:58 -04:00
import psutil
2021-06-16 19:36:25 -04:00
import sys
import signal
2021-06-16 19:26:51 -04:00
from gpiozero import Button
from signal import pause
from subprocess import check_call
from time import sleep
2021-06-17 09:09:56 -04:00
from unicornhatmini import UnicornHATMini, BUTTON_A
POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1
2021-06-17 09:39:49 -04:00
TEMP_COLOR = [64, 0, 0]
CPU_COLOR = [0, 16, 32]
2021-06-17 10:46:40 -04:00
NETWORK_COLOR = [64, 16, 0]
2021-06-17 10:07:58 -04:00
MAX_TEMP = 80
MIN_TEMP = 40
2021-06-17 10:14:56 -04:00
BLACK = [0, 0, 0]
2021-06-16 19:26:51 -04:00
class SystemStatus:
def __init__(self, hat):
self.hat = hat
self.hat.set_brightness(0.1)
2021-06-17 09:39:49 -04:00
self.hat.set_rotation(270)
2021-06-17 09:09:56 -04:00
self.width, self.height = self.hat.get_shape()
2021-06-17 09:39:49 -04:00
self.tempStartColumn = 0
self.tempEndColumn = int(self.width / 2)
self.cpuStartColumn = int(self.width / 2)
self.cpuEndColumn = self.width - 1
2021-06-17 10:46:40 -04:00
self.networkStartColumn = self.width - 1
self.networkEndColumn = self.width
2021-06-16 19:26:51 -04:00
def display(self):
2021-06-17 09:39:49 -04:00
self.displayTemp()
self.displayCpu()
2021-06-17 10:46:40 -04:00
self.displayNetwork()
2021-06-16 19:26:51 -04:00
self.hat.show()
2021-06-17 09:39:49 -04:00
def displayTemp(self):
self.displayField(
start=self.tempStartColumn,
end=self.tempEndColumn,
value=self.getTempValue(),
color=TEMP_COLOR
)
def displayCpu(self):
self.displayField(
start=self.cpuStartColumn,
end=self.cpuEndColumn,
value=self.getCpuValue(),
color=CPU_COLOR
)
2021-06-17 10:46:40 -04:00
def displayNetwork(self):
self.displayField(
start=self.networkStartColumn,
end=self.networkEndColumn,
value=self.getNetworkValue(),
color=NETWORK_COLOR
)
2021-06-17 09:39:49 -04:00
def displayField(self, start, end, value, color):
for x in range(start, end):
for y in range(value):
self.hat.set_pixel(x, y, *color)
2021-06-17 10:14:56 -04:00
for y in range(value, self.height):
2021-06-17 10:15:40 -04:00
self.hat.set_pixel(x, y, *BLACK)
2021-06-17 09:39:49 -04:00
def getTempValue(self):
2021-06-17 10:07:58 -04:00
temp = psutil.sensors_temperatures()['cpu_thermal'][0].current
adjusted = max(0, temp - MIN_TEMP)
percent = min(1, adjusted / (MAX_TEMP - MIN_TEMP))
return int(self.height * percent)
2021-06-17 09:39:49 -04:00
def getCpuValue(self):
2021-06-17 10:07:58 -04:00
return int(self.height * (psutil.cpu_percent() / 100))
2021-06-17 09:39:49 -04:00
2021-06-17 10:46:40 -04:00
def getNetworkValue(self):
connections = psutil.net_connections(kind='inet')
remoteConnections = [c for c in connections if c.raddr]
return min(self.height, len(remoteConnections))
2021-06-16 19:36:25 -04:00
def exit(self):
self.hat.set_all(0, 0, 0)
self.hat.show()
2021-06-16 19:26:51 -04:00
def terminate(self):
2021-06-16 20:06:13 -04:00
for _ in range(3):
2021-06-17 09:09:56 -04:00
self.hat.set_all(0, 0, 0)
2021-06-16 20:06:13 -04:00
self.hat.show()
sleep(0.25)
2021-06-17 09:09:56 -04:00
self.hat.set_all(255, 127, 0)
2021-06-16 20:06:13 -04:00
self.hat.show()
sleep(0.25)
2021-06-16 19:36:25 -04:00
2021-06-16 19:26:51 -04:00
2021-06-16 19:36:25 -04:00
def cleanExit(systemStatus, button):
def exit(signum, frame):
button.close()
systemStatus.exit()
sys.exit(0)
2021-06-16 19:26:51 -04:00
2021-06-16 19:36:25 -04:00
return exit
2021-06-17 09:09:56 -04:00
def poweroff(systemStatus):
2021-06-16 19:36:25 -04:00
def terminate():
systemStatus.terminate()
check_call(['sudo', 'poweroff'])
return terminate
2021-06-16 19:26:51 -04:00
def main():
systemStatus = SystemStatus(UnicornHATMini())
2021-06-17 09:09:56 -04:00
powerButton = Button(BUTTON_A, hold_time=POWER_BUTTON_HOLD_TIME_IN_SECONDS)
2021-06-16 19:36:25 -04:00
signal.signal(signal.SIGTERM, cleanExit(systemStatus, powerButton))
2021-06-17 09:09:56 -04:00
powerButton.when_held = poweroff(systemStatus)
2021-06-16 19:36:25 -04:00
2021-06-17 09:09:56 -04:00
while True:
systemStatus.display()
sleep(2)
2021-06-16 19:26:51 -04:00
if __name__ == '__main__':
main()