mirror of
https://github.com/FAUSheppy/config
synced 2025-12-09 16:18:33 +01:00
panel improvements
This commit is contained in:
@@ -6,9 +6,15 @@ import sys
|
||||
import subprocess
|
||||
import re
|
||||
import hl_utils
|
||||
import signal
|
||||
from hl_constants import *
|
||||
|
||||
battery_average=[]
|
||||
bat_prev = -1
|
||||
|
||||
def sigusr1_handler(signum, frame):
|
||||
save()
|
||||
|
||||
|
||||
def battery():
|
||||
try:
|
||||
@@ -24,36 +30,66 @@ def battery():
|
||||
return hl_utils.color_panel("On Supply and fully charged",GREEN)
|
||||
|
||||
## calculate average time remaining ##
|
||||
sph = 60*60
|
||||
spm = 60
|
||||
|
||||
cur_time = bat.split('%, ')[1].split(' ')[0].split(':')
|
||||
cur = int(cur_time[0]) + int(cur_time[1]) * 60 + int(cur_time[2]) * 60 * 60
|
||||
cur = int(cur_time[0]) * sph + int(cur_time[1]) * spm + int(cur_time[2])
|
||||
|
||||
global battery_average
|
||||
battery_average+=[int(cur)]
|
||||
global bat_prev
|
||||
battery_average += [int(cur)]
|
||||
if len(battery_average) == 0:
|
||||
bat_avg = "unkown"
|
||||
else:
|
||||
bat_avg=int(sum(battery_average) / float(len(battery_average)))
|
||||
|
||||
## keep max 10 items in queue ##
|
||||
if len(battery_average) > VALUES_KEPT:
|
||||
battery_average = battery_average[1:]
|
||||
|
||||
## color fine tuning ##
|
||||
if plain > 10:
|
||||
plain += BAT_COLOR_OFFSET
|
||||
|
||||
## build string ##
|
||||
tmp=hl_utils.color_panel(bat.lstrip("Charging ,").lstrip("Discharging ,").split(",")[0],hl_utils.get_color(plain,0,100),False)
|
||||
bat_avg=hl_utils.color_panel(str(bat_avg),RED,False)
|
||||
color=hl_utils.get_color(plain,0,100)
|
||||
tmp=hl_utils.color_panel(bat.lstrip("Charging ,").lstrip("Discharging ,").split(",")[0],color,False)
|
||||
|
||||
dez_count = 2
|
||||
avg_h = str(int(bat_avg/sph)).zfill(dez_count)
|
||||
avg_m = str(int((bat_avg%sph)/spm)).zfill(dez_count)
|
||||
avg_s = str(int(bat_avg%spm)).zfill(dez_count)
|
||||
|
||||
#print(cur_time)
|
||||
#print(bat_avg,avg_h,avg_m,avg_s)
|
||||
#print(battery_average)
|
||||
|
||||
## prevent "flickering"
|
||||
if(abs(int(avg_m))-abs(int(bat_prev))<5 and int(avg_h) >= 1):
|
||||
avg_m = bat_prev
|
||||
else:
|
||||
bat_prev = avg_m
|
||||
|
||||
bat_str = "{}:{}:{}".format(avg_h,avg_m,"00")
|
||||
bat_avg=hl_utils.color_panel(bat_str,color,False)
|
||||
|
||||
|
||||
## conditional coloring ##
|
||||
if bat.startswith("Charging"):
|
||||
return hl_utils.color_panel("Charging, ",GREEN,False) + str(tmp) + str(bat_avg)
|
||||
stat = hl_utils.color_panel("Charging, ",GREEN,False) + str(tmp) + str(bat_avg)
|
||||
stat += hl_utils.color_panel("until charged",color,True)
|
||||
elif plain <= BATTERY_CRITICAL:
|
||||
return hl_utils.color_panel(">>>>>>>>>>>>>>>> ------------ WARNING BATTER FAILURE IMMINENT ------------ <<<<<<<<<<<<<",RED)
|
||||
stat = hl_utils.color_panel(BAT_WARNING_STR,RED)
|
||||
elif bat.startswith("Discharging"):
|
||||
return hl_utils.color_panel("Discharging, ",RED,False) + str(tmp) + str(bat_avg)
|
||||
stat = hl_utils.color_panel("Discharging, ",RED,False) + str(tmp) + str(bat_avg)
|
||||
stat += hl_utils.color_panel("remaining",color,True)
|
||||
else:
|
||||
return hl_utils.color_panel(bat.strip('\n'),hl_utils.get_color(plain,0,100))
|
||||
stat = hl_utils.color_panel(bat.strip('\n'),hl_utils.get_color(plain,0,100))
|
||||
return stat
|
||||
except ValueError as e:
|
||||
return hl_utils.color_panel(str(e),RED)
|
||||
|
||||
|
||||
def pr_acct_status():
|
||||
if hl_utils.is_cip():
|
||||
out = hl_utils.color_remove(hl_utils.shexec(PRINT_LOG).split("\n")[0]).split(' ')[-1]
|
||||
@@ -62,14 +98,12 @@ def pr_acct_status():
|
||||
|
||||
def vpn_status():
|
||||
if not hl_utils.is_cip():
|
||||
out_vpn = hl_utils.shexec("ps -ef")
|
||||
#out_vpn = subprocess.check_output(["ps","-ef"]).decode().split('\n')
|
||||
out_vpn = hl_utils.shexec("ps -ef").split("\n")
|
||||
|
||||
ret = 0
|
||||
for l in out_vpn:
|
||||
if 'openvpn' in l and not 'sudo' in l and not 'grep' in l and not 'cip.sh' in l:
|
||||
ret += 1;
|
||||
|
||||
if ret == 0:
|
||||
out_vpn = hl_utils.color_panel("VPN: Link Down",RED)
|
||||
elif ret == 1:
|
||||
@@ -77,7 +111,7 @@ def vpn_status():
|
||||
elif ret > 1:
|
||||
out_vpn = hl_utils.color_panel("multiple VPNs connected",YELLOW)
|
||||
else:
|
||||
out_vpn = hl_utils.color_panel("VPN: ret was "+str(ret)+" ??",RED)
|
||||
out_vpn = hl_utils.color_panel("VPN: STATUS UNKOWN ??",RED)
|
||||
|
||||
with open(hl_utils.hlpath(VPN_LOG),'w+') as g:
|
||||
g.write(out_vpn)
|
||||
@@ -91,18 +125,22 @@ def ip_status():
|
||||
with open(hl_utils.hlpath(IP_LOG),'w') as g:
|
||||
p="Public IP: "
|
||||
try:
|
||||
tmp=hl_utils.color_panel(p+hl_utils.shexec("wget --timeout=3 -O- --quiet https://atlantishq.de/ipcheck"),GREEN)
|
||||
tmp = hl_utils.color_panel(p+hl_utils.shexec("wget --timeout=3 -O- --quiet https://atlantishq.de/ipcheck"),GREEN)
|
||||
except:
|
||||
tmp=hl_utils.color_panel("Offline",RED)
|
||||
tmp = hl_utils.color_panel("Offline",RED)
|
||||
g.write(tmp)
|
||||
|
||||
def save():
|
||||
vpn_status()
|
||||
pr_acct_status()
|
||||
battery_status()
|
||||
ip_status()
|
||||
|
||||
if __name__ == '__main__':
|
||||
signal.signal(signal.SIGUSR1,sigusr1_handler)
|
||||
signal.siginterrupt(signal.SIGUSR1, True)
|
||||
while(True):
|
||||
vpn_status()
|
||||
pr_acct_status()
|
||||
battery_status()
|
||||
ip_status()
|
||||
if sys.argv[-1]=='--refresh':
|
||||
if sys.argv[-1] in ['--refresh','-r']:
|
||||
break
|
||||
time.sleep(5)
|
||||
save()
|
||||
time.sleep(10)
|
||||
|
||||
Reference in New Issue
Block a user