[modules/ping] Use framework background update functionality

see #640
This commit is contained in:
tobi-wan-kenobi 2020-05-30 17:25:47 +02:00
parent 547874dafd
commit d38a21983c

View file

@ -15,7 +15,6 @@ Parameters:
import re import re
import time import time
import threading
import core.module import core.module
import core.widget import core.widget
@ -25,37 +24,6 @@ import core.decorators
import util.cli import util.cli
def get_rtt(module, widget):
try:
widget.set("rtt-unreachable", False)
res = util.cli.execute(
"ping -n -q -c {} -W {} {}".format(
widget.get("rtt-probes"),
widget.get("rtt-timeout"),
widget.get("address"),
)
)
for line in res.split("\n"):
if line.startswith(
"{} packets transmitted".format(widget.get("rtt-probes"))
):
m = re.search(r"(\d+)% packet loss", line)
widget.set("packet-loss", m.group(1))
if not line.startswith("rtt"):
continue
m = re.search(r"([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+)\s+(\S+)", line)
widget.set("rtt-min", float(m.group(1)))
widget.set("rtt-avg", float(m.group(2)))
widget.set("rtt-max", float(m.group(3)))
widget.set("rtt-unit", m.group(5))
except Exception as e:
widget.set("rtt-unreachable", True)
core.event.trigger("update", [module.id], redraw_only=True)
class Module(core.module.Module): class Module(core.module.Module):
@ -63,6 +31,8 @@ class Module(core.module.Module):
def __init__(self, config, theme): def __init__(self, config, theme):
super().__init__(config, theme, core.widget.Widget(self.rtt)) super().__init__(config, theme, core.widget.Widget(self.rtt))
self.background = True
widget = self.widget() widget = self.widget()
widget.set("address", self.parameter("address", "8.8.8.8")) widget.set("address", self.parameter("address", "8.8.8.8"))
@ -88,8 +58,35 @@ class Module(core.module.Module):
return self.threshold_state(widget.get("rtt-avg"), 1000.0, 2000.0) return self.threshold_state(widget.get("rtt-avg"), 1000.0, 2000.0)
def update(self): def update(self):
thread = threading.Thread(target=get_rtt, args=(self, self.widget(),)) widget = self.widget()
thread.start() try:
widget.set("rtt-unreachable", False)
res = util.cli.execute(
"ping -n -q -c {} -W {} {}".format(
widget.get("rtt-probes"),
widget.get("rtt-timeout"),
widget.get("address"),
)
)
for line in res.split("\n"):
if line.startswith(
"{} packets transmitted".format(widget.get("rtt-probes"))
):
m = re.search(r"(\d+)% packet loss", line)
widget.set("packet-loss", m.group(1))
if not line.startswith("rtt"):
continue
m = re.search(r"([0-9\.]+)/([0-9\.]+)/([0-9\.]+)/([0-9\.]+)\s+(\S+)", line)
widget.set("rtt-min", float(m.group(1)))
widget.set("rtt-avg", float(m.group(2)))
widget.set("rtt-max", float(m.group(3)))
widget.set("rtt-unit", m.group(5))
except Exception as e:
widget.set("rtt-unreachable", True)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4