Skip to content

Commit

Permalink
Merge pull request #343 from outdoorbits/displaymenu_add_tools
Browse files Browse the repository at this point in the history
add transfer rate to statusbar
  • Loading branch information
outdoorbits authored Dec 21, 2024
2 parents 3304ee9 + f2ff365 commit 77ae8bd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 31 deletions.
2 changes: 1 addition & 1 deletion scripts/constants.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const_LOGFILE="/var/www/little-backup-box/tmp/little-backup-box.log"
const_DISPLAY_LINES_LIMIT=10
const_DISPLAY_CONTENT_FOLDER="/var/www/little-backup-box/tmp/display-content"
const_DISPLAY_CONTENT_OLD_FILE="/var/www/little-backup-box/tmp/display-content-old.txt"
const_DISPLAY_STATUSBAR_MAX_SEC=2
const_DISPLAY_STATUSBAR_MAX_SEC=2.0
const_IP_QR_FILE_PATTERN="/var/www/little-backup-box/tmp/ip-qr-link-__.png"
const_IP_SENT_MARKERFILE="/var/www/little-backup-box/tmp/ip-sent.txt"
const_MENU_TIMEOUT_SEC=15
Expand Down
72 changes: 43 additions & 29 deletions scripts/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import threading
import time

import lib_network
import lib_setup

from luma.core.interface.serial import i2c, spi, pcf8574
Expand Down Expand Up @@ -196,6 +197,11 @@ def __init__(self):
# calculate line dimensions
self.calculate_LineSize()

# prepare statusbar
if self.conf_DISP_SHOW_STATUSBAR:
self.traffic_monitor = lib_network.traffic_monitor()
self.statusbar_toggle = False

## start display menu
self.menu_controller = displaymenu.MENU_CONTROLLER()

Expand All @@ -222,11 +228,11 @@ def calculate_LineSize(self):
else:
self.maxLines = self.const_DISPLAY_LINES_LIMIT

def get_Statusbar(self, active):
if not active:
def get_statusbar(self):
if not self.conf_DISP_SHOW_STATUSBAR:
return(None)

Statusbar = []
statusbar = []

#comitup
try:
Expand All @@ -238,35 +244,42 @@ def get_Statusbar(self, active):
if status.endswith(' state'):

if status.startswith('HOTSPOT'):
Statusbar += ['HOT']
statusbar += ['HOT']
elif status.startswith('CONNECTING'):
Statusbar += ['..?']
statusbar += ['..?']
elif status.startswith('CONNECTED'):
Statusbar += ['WiFi']
statusbar += ['WiFi']
break

# CPU usage
try:
vmstat = subprocess.check_output(['vmstat']).decode().strip().split('\n')
except:
vmstat = []
# dispay network traffic or CPU?
self.statusbar_toggle = not self.statusbar_toggle

if self.statusbar_toggle:
#network traffic
statusbar +=[self.traffic_monitor.get_traffic()]
else:
# CPU usage
try:
vmstat = subprocess.check_output(['vmstat']).decode().strip().split('\n')
except:
vmstat = []

if vmstat:
vmstat_fields = vmstat[-1].split()
if vmstat:
vmstat_fields = vmstat[-1].split()

if len(vmstat_fields) >= 14:
Statusbar += [f'{100-float(vmstat_fields[14]):.0f}%']
if len(vmstat_fields) >= 14:
statusbar += [f'{100-float(vmstat_fields[14]):.0f}%']

# temperature
try:
temp_c = float(subprocess.check_output(['sudo', 'cat', '/sys/class/thermal/thermal_zone0/temp']).decode()) / 1000
Statusbar += [f'{temp_c:.0f}°C']
except:
pass
# temperature
try:
temp_c = float(subprocess.check_output(['sudo', 'cat', '/sys/class/thermal/thermal_zone0/temp']).decode()) / 1000
statusbar += [f'{temp_c:.0f}°C']
except:
pass

return(Statusbar)
return(statusbar)

def show(self, Lines, Statusbar=None):
def show(self, Lines, statusbar=None):

if ":IMAGE=" in Lines[0]:
# PRINT IMAGE FROM FILE
Expand All @@ -288,7 +301,7 @@ def show(self, Lines, Statusbar=None):
else:
# Write lines

if not Statusbar is None:
if not statusbar is None:
Lines[self.maxLines-1] = f's=s:STATUSBAR'

with canvas(self.device) as draw:
Expand Down Expand Up @@ -405,11 +418,11 @@ def show(self, Lines, Statusbar=None):
if FormatType == 's' and FormatValue == 's':

i = 0
for item in Statusbar:
for item in statusbar:

if i < len(Statusbar) - 1:
if i < len(statusbar) - 1:
# align left
x = int(i * self.device.width / len(Statusbar))
x = int(i * self.device.width / len(statusbar))
else:
# align right
(left, top, right, bottom) = draw.textbbox((0,0), item,font=self.FONT)
Expand Down Expand Up @@ -545,16 +558,17 @@ def main(self):
oCF.write("\n".join(Lines))

if self.hardware_ready:
self.show(Lines, self.get_Statusbar(self.conf_DISP_SHOW_STATUSBAR))
self.show(Lines, self.get_statusbar())
display_time = time.time()

# statusbar
if (
self.hardware_ready and
self.conf_DISP_SHOW_STATUSBAR and
len(Lines) >= self.const_DISPLAY_LINES_LIMIT and
time.time() - display_time >= self.const_DISPLAY_STATUSBAR_MAX_SEC
):
self.show(Lines, self.get_Statusbar(self.conf_DISP_SHOW_STATUSBAR))
self.show(Lines, self.get_statusbar())
display_time = time.time()

time.sleep(FrameTime)
Expand Down
45 changes: 45 additions & 0 deletions scripts/lib_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import subprocess
from urllib import request
import sys
import time

import lib_setup

Expand Down Expand Up @@ -141,6 +142,50 @@ def get_qr_links(protocol='https'):

return(qr_links)

class traffic_monitor(object):
def __init__(self):
self.previous_time = 0
self.previous_transfer = 0

# init values
self.get_traffic()

def get_traffic(self):
transfer = 0

try:
with open('/proc/net/dev', 'r') as interfaces:
for interface in interfaces:
columns = interface.split()
if len(columns) >= 9 and columns[0].endswith(':'):
if columns[1].isdigit():
transfer += int(columns[1])
if columns[9].isdigit():
transfer += int(columns[9])
except:
pass

actualtime = time.time()
if actualtime - self.previous_time <= 0:
return('')

transferrate = (transfer - self.previous_transfer) / (actualtime - self.previous_time)

self.previous_time = actualtime
self.previous_transfer = transfer

return(f'{self.format_bytes(transferrate)}/s')

def format_bytes(self, number_of_bytes):
units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB']

i = 0
while number_of_bytes >= 1024 and i < len(units) - 1:
number_of_bytes /= 1024.0
i += 1

return f"{number_of_bytes:.0f} {units[i]}"

if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = 'Provides network related functions',
Expand Down
2 changes: 1 addition & 1 deletion scripts/lib_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ def __get_constants_types(self):
'const_DISPLAY_LINES_LIMIT': {'type': 'int'},
'const_DISPLAY_CONTENT_FOLDER': {'type': 'str'},
'const_DISPLAY_CONTENT_OLD_FILE': {'type': 'str'},
'const_DISPLAY_STATUSBAR_MAX_SEC': {'type': 'int'},
'const_DISPLAY_STATUSBAR_MAX_SEC': {'type': 'float'},
'const_IP_SENT_MARKERFILE': {'type': 'str'},
'const_MENU_TIMEOUT_SEC': {'type': 'int'},
'const_RCLONE_CONFIG_FILE': {'type': 'str'},
Expand Down

0 comments on commit 77ae8bd

Please sign in to comment.