diff --git a/scripts/constants.sh b/scripts/constants.sh index c509079..42a56f0 100644 --- a/scripts/constants.sh +++ b/scripts/constants.sh @@ -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 diff --git a/scripts/display.py b/scripts/display.py index 4ad5f74..afc3157 100644 --- a/scripts/display.py +++ b/scripts/display.py @@ -58,6 +58,7 @@ import threading import time +import lib_network import lib_setup from luma.core.interface.serial import i2c, spi, pcf8574 @@ -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() @@ -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: @@ -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 @@ -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: @@ -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) @@ -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) diff --git a/scripts/lib_network.py b/scripts/lib_network.py index fef715e..316b91c 100644 --- a/scripts/lib_network.py +++ b/scripts/lib_network.py @@ -24,6 +24,7 @@ import subprocess from urllib import request import sys +import time import lib_setup @@ -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', diff --git a/scripts/lib_setup.py b/scripts/lib_setup.py index 0c18036..d3bd889 100644 --- a/scripts/lib_setup.py +++ b/scripts/lib_setup.py @@ -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'},