74 lines
1.6 KiB
Python
74 lines
1.6 KiB
Python
import subprocess
|
|
import os
|
|
import logging
|
|
import time
|
|
import pwnagotchi.ui.view as view
|
|
|
|
version = '1.0.0'
|
|
|
|
_name = None
|
|
|
|
|
|
def name():
|
|
global _name
|
|
if _name is None:
|
|
with open('/etc/hostname', 'rt') as fp:
|
|
_name = fp.read().strip()
|
|
return _name
|
|
|
|
|
|
def uptime():
|
|
with open('/proc/uptime') as fp:
|
|
return int(fp.read().split('.')[0])
|
|
|
|
|
|
def mem_usage():
|
|
out = subprocess.getoutput("free -m")
|
|
for line in out.split("\n"):
|
|
line = line.strip()
|
|
if line.startswith("Mem:"):
|
|
parts = list(map(int, line.split()[1:]))
|
|
tot = parts[0]
|
|
used = parts[1]
|
|
free = parts[2]
|
|
return used / tot
|
|
|
|
return 0
|
|
|
|
|
|
def cpu_load():
|
|
with open('/proc/stat', 'rt') as fp:
|
|
for line in fp:
|
|
line = line.strip()
|
|
if line.startswith('cpu '):
|
|
parts = list(map(int, line.split()[1:]))
|
|
user_n = parts[0]
|
|
sys_n = parts[2]
|
|
idle_n = parts[3]
|
|
tot = user_n + sys_n + idle_n
|
|
return (user_n + sys_n) / tot
|
|
return 0
|
|
|
|
|
|
def temperature(celsius=True):
|
|
with open('/sys/class/thermal/thermal_zone0/temp', 'rt') as fp:
|
|
temp = int(fp.read().strip())
|
|
c = int(temp / 1000)
|
|
return c if celsius else ((c * (9 / 5)) + 32)
|
|
|
|
|
|
def shutdown():
|
|
logging.warning("shutting down ...")
|
|
if view.ROOT:
|
|
view.ROOT.on_shutdown()
|
|
# give it some time to refresh the ui
|
|
time.sleep(5)
|
|
os.system("sync")
|
|
os.system("halt")
|
|
|
|
|
|
def reboot():
|
|
logging.warning("rebooting ...")
|
|
os.system("sync")
|
|
os.system("shutdown -r now")
|