Add network connection status
This commit is contained in:
parent
bcf490828d
commit
9cca07d4db
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue