refactored to use the already existing functions

Signed-off-by: spees <speeskonijn@gmail.com>
This commit is contained in:
spees 2019-10-19 22:25:12 +02:00
parent f52687642e
commit 58a085188f

@ -28,6 +28,7 @@ __description__ = 'A plugin that will display memory/cpu usage and temperature'
from pwnagotchi.ui.components import LabeledValue from pwnagotchi.ui.components import LabeledValue
from pwnagotchi.ui.view import BLACK from pwnagotchi.ui.view import BLACK
import pwnagotchi.ui.fonts as fonts import pwnagotchi.ui.fonts as fonts
import pwnagotchi
import subprocess import subprocess
import logging import logging
@ -39,39 +40,12 @@ def on_loaded():
def mem_usage(): def mem_usage():
out = subprocess.getoutput("free -m") return int(pwnagotchi.mem_usage() * 100)
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 int((used / tot) * 100)
return 0
def cpu_load(): def cpu_load():
with open('/proc/stat', 'rt') as fp: return int(pwnagotchi.cpu_load() * 100)
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 int(((user_n + sys_n) / tot) * 100)
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 on_ui_setup(ui): def on_ui_setup(ui):
if OPTIONS['orientation'] == "horizontal": if OPTIONS['orientation'] == "horizontal":
@ -83,7 +57,7 @@ def on_ui_setup(ui):
def on_ui_update(ui): def on_ui_update(ui):
if OPTIONS['orientation'] == "horizontal": if OPTIONS['orientation'] == "horizontal":
ui.set('memtemp', " mem cpu temp\n %s%% %s%% %sc" % (mem_usage(), cpu_load(), temperature())) ui.set('memtemp', " mem cpu temp\n %s%% %s%% %sc" % (mem_usage(), cpu_load(), pwnagotchi.temperature()))
elif OPTIONS['orientation'] == "vertical": elif OPTIONS['orientation'] == "vertical":
ui.set('memtemp', " mem:%s%%\n cpu:%s%%\ntemp:%sc" % (mem_usage(), cpu_load(), temperature())) ui.set('memtemp', " mem:%s%%\n cpu:%s%%\ntemp:%sc" % (mem_usage(), cpu_load(), pwnagotchi.temperature()))