system-status/system-status.py

154 lines
3.7 KiB
Python
Raw Permalink Normal View History

2021-06-17 10:07:58 -04:00
import psutil
2021-06-16 19:36:25 -04:00
import sys
2021-06-16 19:26:51 -04:00
from gpiozero import Button
2021-06-17 15:04:54 -04:00
from signal import signal, SIGTERM
2021-06-16 19:26:51 -04:00
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 14:08:58 -04:00
DISPLAY_UPDATE_IN_SECONDS = 2
2021-06-17 11:19:01 -04:00
2021-06-17 14:08:58 -04:00
BLACK = [0, 0, 0 ]
RED = [64, 0, 0 ]
BLUE = [0, 16, 32]
ORANGE = [64, 16, 0 ]
PINK = [64, 0, 16]
2021-06-17 11:19:01 -04:00
TEMP_COLOR = RED
CPU_COLOR = BLUE
2021-06-17 15:04:54 -04:00
NETWORK_COLOR = PINK
2021-06-17 11:19:01 -04:00
2021-06-18 09:52:28 -04:00
MIN_TEMP = 40
2021-06-17 17:00:40 -04:00
MAX_TEMP = 85
2021-06-16 19:26:51 -04:00
class SystemStatus:
2021-06-17 14:08:58 -04:00
2021-06-16 19:26:51 -04:00
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 11:19:01 -04:00
2021-06-17 15:04:54 -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
2021-06-17 15:04:54 -04:00
self.networkEndColumn = self.width
2021-06-16 19:26:51 -04:00
2021-06-17 15:04:54 -04:00
self.start()
2021-06-17 11:19:01 -04:00
def terminate(self):
2021-06-17 14:08:58 -04:00
self.stop()
2021-06-17 11:19:01 -04:00
for _ in range(3):
sleep(0.25)
2021-06-17 14:08:58 -04:00
self.hat.set_all(*RED)
2021-06-17 11:19:01 -04:00
self.hat.show()
sleep(0.25)
2021-06-17 14:08:58 -04:00
self.clear()
2021-06-17 11:19:01 -04:00
2021-06-17 15:04:54 -04:00
def start(self):
self.isRunning = True
self.clear()
2021-06-17 14:08:58 -04:00
def stop(self):
self.isRunning = False
self.clear()
def clear(self):
self.hat.set_all(*BLACK)
2021-06-16 19:26:51 -04:00
self.hat.show()
2021-06-17 14:08:58 -04:00
def display(self):
if self.isRunning:
self.displayTemp()
self.displayCpu()
self.displayNetwork()
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))
2021-06-19 08:09:37 -04:00
return max(1, round(self.height * percent))
2021-06-17 09:39:49 -04:00
def getCpuValue(self):
2021-06-19 08:09:37 -04:00
percent = psutil.cpu_percent() / 100
return round(self.height * percent)
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:26:51 -04:00
2021-06-17 15:04:54 -04:00
def main():
systemStatus = SystemStatus(UnicornHATMini())
powerButton = Button(BUTTON_A, hold_time=POWER_BUTTON_HOLD_TIME_IN_SECONDS)
signal(SIGTERM, cleanExit(systemStatus, powerButton))
powerButton.when_held = poweroff(systemStatus)
while True:
systemStatus.display()
sleep(DISPLAY_UPDATE_IN_SECONDS)
2021-06-16 19:36:25 -04:00
def cleanExit(systemStatus, button):
2021-06-17 15:04:54 -04:00
def _cleanExit_(signum, frame):
2021-06-16 19:36:25 -04:00
button.close()
2021-06-17 14:08:58 -04:00
systemStatus.stop()
2021-06-16 19:36:25 -04:00
sys.exit(0)
2021-06-16 19:26:51 -04:00
2021-06-17 15:04:54 -04:00
return _cleanExit_
2021-06-16 19:36:25 -04:00
2021-06-17 09:09:56 -04:00
def poweroff(systemStatus):
2021-06-17 15:04:54 -04:00
def _poweroff_():
2021-06-16 19:36:25 -04:00
systemStatus.terminate()
check_call(['sudo', 'poweroff'])
2021-06-17 15:04:54 -04:00
return _poweroff_
2021-06-16 19:26:51 -04:00
if __name__ == '__main__':
main()