From 9cca07d4db5a25098f85dbf4c171d6ff478087d0 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Thu, 17 Jun 2021 10:46:40 -0400 Subject: [PATCH] Add network connection status --- system-status.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/system-status.py b/system-status.py index 77df54e..36722a7 100644 --- a/system-status.py +++ b/system-status.py @@ -11,6 +11,7 @@ from unicornhatmini import UnicornHATMini, BUTTON_A POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1 TEMP_COLOR = [64, 0, 0] CPU_COLOR = [0, 16, 32] +NETWORK_COLOR = [64, 16, 0] MAX_TEMP = 80 MIN_TEMP = 40 BLACK = [0, 0, 0] @@ -26,10 +27,13 @@ class SystemStatus: 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 def display(self): self.displayTemp() self.displayCpu() + self.displayNetwork() self.hat.show() def displayTemp(self): @@ -48,6 +52,14 @@ class SystemStatus: 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): @@ -65,6 +77,12 @@ class SystemStatus: 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 exit(self): self.hat.set_all(0, 0, 0) self.hat.show()