2021-07-05 14:55:47 +02:00
|
|
|
"""
|
2021-07-05 20:54:28 +02:00
|
|
|
A module to show currently active network connection (ethernet or wifi) and connection strength if the connection is wireless.
|
|
|
|
|
|
|
|
Dependencies: nm-connection-editor if users would like a graphical
|
|
|
|
network manager when left-clicking the module
|
2021-07-05 14:55:47 +02:00
|
|
|
"""
|
|
|
|
|
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
|
2021-07-05 19:34:42 +02:00
|
|
|
import core.input
|
2021-07-05 14:55:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Module(core.module.Module):
|
2021-07-05 19:34:42 +02:00
|
|
|
@core.decorators.every(seconds=10)
|
2021-07-05 14:55:47 +02:00
|
|
|
def __init__(self, config, theme):
|
|
|
|
super().__init__(config, theme, core.widget.Widget(self.network))
|
2021-07-05 20:54:28 +02:00
|
|
|
self.__is_wireless = False
|
2021-07-05 20:09:17 +02:00
|
|
|
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
|
|
|
|
2021-07-05 19:34:42 +02:00
|
|
|
# Set up event handler for left mouse click
|
2021-07-05 20:54:28 +02:00
|
|
|
core.input.register(
|
|
|
|
self, button=core.input.LEFT_MOUSE, cmd="nm-connection-editor"
|
|
|
|
)
|
2021-07-05 19:34:42 +02:00
|
|
|
|
2021-07-05 20:54:28 +02:00
|
|
|
# Get network information to display to the user
|
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 20:54:28 +02:00
|
|
|
self.__interface = route_tokens[route_tokens.index("dev") + 1]
|
2021-07-05 14:55:47 +02:00
|
|
|
except ValueError:
|
2021-07-05 20:09:17 +02:00
|
|
|
self.__interface = None
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 17:29:37 +02:00
|
|
|
# Check to see if the interface (if it exists) is wireless
|
2021-07-05 20:09:17 +02:00
|
|
|
if self.__interface:
|
2021-07-05 17:29:37 +02:00
|
|
|
with open("/proc/net/wireless", "r") as f:
|
2021-07-05 20:09:17 +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 20:09:17 +02:00
|
|
|
if self.__interface is None:
|
|
|
|
self.__message = " No connection"
|
|
|
|
elif self.__is_wireless:
|
2021-07-05 20:54:28 +02:00
|
|
|
iw_dat = util.cli.execute("iwgetid")
|
2021-07-05 17:29:37 +02:00
|
|
|
has_ssid = "ESSID" in iw_dat
|
2021-07-05 20:54:28 +02:00
|
|
|
ssid = (
|
|
|
|
iw_dat[iw_dat.index(":") + 1 :].replace('"', "").strip()
|
|
|
|
if has_ssid
|
|
|
|
else "Unknown"
|
|
|
|
)
|
2021-07-05 17:29:37 +02:00
|
|
|
|
|
|
|
# Get connection strength
|
2021-07-05 20:09:17 +02:00
|
|
|
cmd = "iwconfig {}".format(self.__interface)
|
2021-07-05 17:29:37 +02:00
|
|
|
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 20:09:17 +02:00
|
|
|
self.__message = self.__generate_wireles_message(ssid, self.__signal)
|
2021-07-05 14:55:47 +02:00
|
|
|
else:
|
2021-07-05 20:54:28 +02:00
|
|
|
# Set signal to -30 as ethernet shouldn't have signal issues
|
2021-07-05 20:09:17 +02:00
|
|
|
self.__signal = -30
|
2021-07-05 20:54:28 +02:00
|
|
|
self.__message = " Ethernet"
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 20:09:17 +02:00
|
|
|
return self.__message
|
2021-07-05 14:55:47 +02:00
|
|
|
|
2021-07-05 20:54:28 +02:00
|
|
|
# The signal is measured in decibels/milliwatt, hence the weird numbers
|
2021-07-05 18:26:46 +02:00
|
|
|
def state(self, widget):
|
|
|
|
if self.__signal < -80:
|
|
|
|
return "critical"
|
2021-07-05 20:09:17 +02:00
|
|
|
if self.__signal < -65:
|
|
|
|
return "warning"
|
|
|
|
|
2021-07-05 18:26:46 +02:00
|
|
|
return None
|
|
|
|
|
2021-07-05 20:54:28 +02:00
|
|
|
# manually done for better granularity / ease of parsing strength data
|
2021-07-05 20:09:17 +02:00
|
|
|
def __generate_wireles_message(self, ssid, strength):
|
2021-07-05 18:26:46 +02:00
|
|
|
computed_strength = 100 * ((strength + 100) / 70.0)
|
2021-07-05 19:34:42 +02:00
|
|
|
return " {} {}%".format(ssid, int(computed_strength))
|