140 lines
3.4 KiB
Python
140 lines
3.4 KiB
Python
import psutil
|
|
import sys
|
|
import signal
|
|
|
|
from gpiozero import Button
|
|
from signal import pause
|
|
from subprocess import check_call
|
|
from time import sleep
|
|
from unicornhatmini import UnicornHATMini, BUTTON_A
|
|
|
|
POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1
|
|
|
|
BLACK = [00, 00, 00]
|
|
RED = [64, 00, 00]
|
|
BLUE = [00, 16, 32]
|
|
ORANGE = [64, 16, 00]
|
|
PINK = [64, 00, 16]
|
|
|
|
TEMP_COLOR = RED
|
|
CPU_COLOR = BLUE
|
|
NETWORK_COLOR = ORANGE
|
|
|
|
MIN_TEMP = 40
|
|
MAX_TEMP = 80
|
|
|
|
|
|
class SystemStatus:
|
|
def __init__(self, hat):
|
|
self.hat = hat
|
|
self.hat.set_brightness(0.1)
|
|
self.hat.set_rotation(270)
|
|
self.width, self.height = self.hat.get_shape()
|
|
|
|
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.clear()
|
|
|
|
def clear(self):
|
|
self.hat.set_all(0, 0, 0)
|
|
self.hat.show()
|
|
|
|
def terminate(self):
|
|
for _ in range(3):
|
|
self.clear()
|
|
sleep(0.25)
|
|
self.hat.set_all(128, 0, 0)
|
|
self.hat.show()
|
|
sleep(0.25)
|
|
|
|
def display(self):
|
|
self.displayTemp()
|
|
self.displayCpu()
|
|
self.displayNetwork()
|
|
self.hat.show()
|
|
|
|
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
|
|
)
|
|
|
|
def displayNetwork(self):
|
|
self.displayField(
|
|
start=self.networkStartColumn,
|
|
end=self.networkEndColumn,
|
|
value=self.getNetworkValue(),
|
|
color=NETWORK_COLOR
|
|
)
|
|
|
|
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)
|
|
for y in range(value, self.height):
|
|
self.hat.set_pixel(x, y, *BLACK)
|
|
|
|
def getTempValue(self):
|
|
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)
|
|
|
|
def getCpuValue(self):
|
|
return int(self.height * (psutil.cpu_percent() / 100))
|
|
|
|
def getNetworkValue(self):
|
|
connections = psutil.net_connections(kind='inet')
|
|
remoteConnections = [c for c in connections if c.raddr]
|
|
|
|
return min(self.height, len(remoteConnections))
|
|
|
|
|
|
def cleanExit(systemStatus, button):
|
|
def exit(signum, frame):
|
|
button.close()
|
|
systemStatus.clear()
|
|
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))
|
|
|
|
powerButton.when_held = poweroff(systemStatus)
|
|
|
|
while True:
|
|
systemStatus.display()
|
|
sleep(2)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|