Add network connection status

This commit is contained in:
Mike Cifelli 2021-06-17 10:46:40 -04:00
parent bcf490828d
commit 9cca07d4db
1 changed files with 18 additions and 0 deletions

View File

@ -11,6 +11,7 @@ from unicornhatmini import UnicornHATMini, BUTTON_A
POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1 POWER_BUTTON_HOLD_TIME_IN_SECONDS = 1
TEMP_COLOR = [64, 0, 0] TEMP_COLOR = [64, 0, 0]
CPU_COLOR = [0, 16, 32] CPU_COLOR = [0, 16, 32]
NETWORK_COLOR = [64, 16, 0]
MAX_TEMP = 80 MAX_TEMP = 80
MIN_TEMP = 40 MIN_TEMP = 40
BLACK = [0, 0, 0] BLACK = [0, 0, 0]
@ -26,10 +27,13 @@ class SystemStatus:
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.networkEndColumn = self.width
def display(self): def display(self):
self.displayTemp() self.displayTemp()
self.displayCpu() self.displayCpu()
self.displayNetwork()
self.hat.show() self.hat.show()
def displayTemp(self): def displayTemp(self):
@ -48,6 +52,14 @@ class SystemStatus:
color=CPU_COLOR 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): def displayField(self, start, end, value, color):
for x in range(start, end): for x in range(start, end):
for y in range(value): for y in range(value):
@ -65,6 +77,12 @@ class SystemStatus:
def getCpuValue(self): def getCpuValue(self):
return int(self.height * (psutil.cpu_percent() / 100)) 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): def exit(self):
self.hat.set_all(0, 0, 0) self.hat.set_all(0, 0, 0)
self.hat.show() self.hat.show()