2020-06-25 20:34:13 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Performs a speedtest - only updates when the "play" button is clicked
|
|
|
|
|
|
|
|
Requires the following python module:
|
|
|
|
* speedtest-cli
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-06-26 09:40:35 +02:00
|
|
|
import sys
|
|
|
|
|
2020-06-25 20:34:13 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
2020-06-25 20:44:55 +02:00
|
|
|
import core.input
|
2020-06-25 20:46:13 +02:00
|
|
|
import core.event
|
2020-06-25 20:34:13 +02:00
|
|
|
import core.decorators
|
|
|
|
|
|
|
|
import speedtest
|
|
|
|
|
|
|
|
|
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.never
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, [])
|
|
|
|
|
|
|
|
self.background = True
|
2020-06-26 09:40:35 +02:00
|
|
|
self.__result = "<speedtest>"
|
2020-06-25 20:34:13 +02:00
|
|
|
self.__running = False
|
|
|
|
|
|
|
|
start = self.add_widget(name="start")
|
|
|
|
main = self.add_widget(name="main", full_text=self.result)
|
|
|
|
|
2020-06-25 20:44:55 +02:00
|
|
|
core.input.register(start, button=core.input.LEFT_MOUSE, cmd=self.update_event)
|
|
|
|
|
2020-06-25 20:34:13 +02:00
|
|
|
def result(self, _):
|
|
|
|
return self.__result
|
|
|
|
|
2020-06-25 20:44:55 +02:00
|
|
|
def update_event(self, _):
|
2020-06-26 09:40:35 +02:00
|
|
|
self.__running = True
|
2020-06-25 20:44:55 +02:00
|
|
|
self.update()
|
|
|
|
|
2020-06-25 20:34:13 +02:00
|
|
|
def update(self):
|
2020-06-26 09:40:35 +02:00
|
|
|
if not self.__running:
|
|
|
|
return
|
2020-06-25 20:46:13 +02:00
|
|
|
core.event.trigger("update", [self.id], redraw_only=True)
|
2020-06-25 20:34:13 +02:00
|
|
|
s = speedtest.Speedtest()
|
|
|
|
s.get_best_server()
|
|
|
|
s.download(threads=None)
|
|
|
|
s.upload(threads=None)
|
|
|
|
|
|
|
|
self.__result = "ping: {:.2f}ms down: {:.2f}Mbps up: {:.2f}Mbps".format(
|
|
|
|
s.results.ping,
|
|
|
|
s.results.download / 1024 / 1024,
|
|
|
|
s.results.upload / 1024 / 1024,
|
|
|
|
)
|
|
|
|
self.__running = False
|
2020-06-26 09:40:35 +02:00
|
|
|
core.event.trigger("update", [self.id], redraw_only=True)
|
2020-06-25 20:34:13 +02:00
|
|
|
|
|
|
|
def state(self, widget):
|
|
|
|
if widget.name == "start":
|
|
|
|
return "running" if self.__running else "not-running"
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|