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

View File

@ -28,6 +28,7 @@ __description__ = 'A plugin that will display memory/cpu usage and temperature'
from pwnagotchi.ui.components import LabeledValue
from pwnagotchi.ui.view import BLACK
import pwnagotchi.ui.fonts as fonts
import pwnagotchi
import subprocess
import logging
@ -39,40 +40,13 @@ def on_loaded():
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 int((used / tot) * 100)
return 0
return int(pwnagotchi.mem_usage() * 100)
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 int(((user_n + sys_n) / tot) * 100)
return 0
return int(pwnagotchi.cpu_load() * 100)
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):
if OPTIONS['orientation'] == "horizontal":
ui.add_element('memtemp', LabeledValue(color=BLACK, label='', value='mem cpu temp\n - - -', position=(ui.width() / 2 + 30, ui.height() /2 + 15),
@ -83,7 +57,7 @@ def on_ui_setup(ui):
def on_ui_update(ui):
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":
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()))