2021-07-05 14:55:47 +02:00
|
|
|
"""
|
|
|
|
A module to show currently active network connection (ethernet or wifi)
|
|
|
|
and connection strength.
|
|
|
|
"""
|
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
|
|
|
|
import util.cli
|
|
|
|
import util.format
|
2021-07-05 14:55:47 +02:00
|
|
|
|
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
|
|
|
|
|
|
|
|
class Module(core.module.Module):
|
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.network))
|
|
|
|
self._is_wireless = True
|
|
|
|
self._interface = None
|
|
|
|
self._message = None
|
2021-07-05 18:26:46 +02:00
|
|
|
self.__signal = -110
|
2021-07-05 14:55:47 +02:00
|
|
|
|
|
|
|
def network(self, widgets):
|
2021-07-05 17:29:37 +02:00
|
|
|
# run ip route command, tokenize output
|
|
|
|
cmd = "ip route get 8.8.8.8"
|
|
|
|
std_out = util.cli.execute(cmd)
|
|
|
|
route_str = " ".join(std_out.split())
|
2021-07-05 14:55:47 +02:00
|
|
|
route_tokens = route_str.split(" ")
|
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
# Attempt to extract a valid network interface device
|
2021-07-05 14:55:47 +02:00
|
|
|
try:
|
2021-07-05 17:29:37 +02:00
|
|
|
self._interface = route_tokens[route_tokens.index("dev") + 1]
|
2021-07-05 14:55:47 +02:00
|
|
|
except ValueError:
|
|
|
|
self._interface = None
|
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
# Check to see if the interface (if it exists) is wireless
|
|
|
|
if self._interface:
|
|
|
|
with open("/proc/net/wireless", "r") as f:
|
2021-07-05 14:55:47 +02:00
|
|
|
self._is_wireless = self._interface in f.read()
|
2021-07-05 17:29:37 +02:00
|
|
|
f.close()
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
# setup message to send to the user
|
2021-07-05 14:55:47 +02:00
|
|
|
if self._interface is None:
|
|
|
|
self._message = "Not connected to a network"
|
|
|
|
elif self._is_wireless:
|
2021-07-05 17:29:37 +02:00
|
|
|
cmd = "iwgetid"
|
|
|
|
iw_dat = util.cli.execute(cmd)
|
|
|
|
has_ssid = "ESSID" in iw_dat
|
|
|
|
ssid = iw_dat[iw_dat.index(":") + 2: -2] if has_ssid else "Unknown"
|
|
|
|
|
|
|
|
# Get connection strength
|
|
|
|
cmd = "iwconfig {}".format(self._interface)
|
|
|
|
config_dat = " ".join(util.cli.execute(cmd).split())
|
|
|
|
config_tokens = config_dat.replace("=", " ").split()
|
|
|
|
strength = config_tokens[config_tokens.index("level") + 1]
|
2021-07-05 18:26:46 +02:00
|
|
|
self.__signal = util.format.asint(strength, minimum=-110, maximum=-30)
|
2021-07-05 17:29:37 +02:00
|
|
|
|
2021-07-05 18:26:46 +02:00
|
|
|
self._message = self.__generate_wireless_message(ssid, self.__signal)
|
2021-07-05 14:55:47 +02:00
|
|
|
else:
|
|
|
|
self._message = self._message
|
|
|
|
|
|
|
|
return self._message
|
|
|
|
|
|
|
|
|
2021-07-05 18:26:46 +02:00
|
|
|
def state(self, widget):
|
|
|
|
if self.__signal < -65:
|
|
|
|
return "warning"
|
|
|
|
if self.__signal < -80:
|
|
|
|
return "critical"
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
def __generate_wireless_message(self, ssid, strength):
|
2021-07-05 18:26:46 +02:00
|
|
|
computed_strength = 100 * ((strength + 100) / 70.0)
|
|
|
|
if computed_strength < 30:
|
|
|
|
return ssid + " poor"
|
2021-07-05 17:29:37 +02:00
|
|
|
if computed_strength < 50:
|
|
|
|
return ssid + " fair"
|
|
|
|
if computed_strength < 75:
|
|
|
|
return ssid + " good"
|
|
|
|
|
|
|
|
return ssid + " excellent"
|
2021-07-05 14:55:47 +02:00
|
|
|
|