Merge pull request #851 from deadbeef2000/nic_strength

[modules/nic] Added strength indicator for wifi signals
This commit is contained in:
tobi-wan-kenobi 2022-02-12 11:23:49 +01:00 committed by GitHub
commit a1ae6d4f34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,7 +13,9 @@ Parameters:
* nic.exclude: Comma-separated list of interface prefixes (supporting regular expressions) to exclude (defaults to 'lo,virbr,docker,vboxnet,veth,br,.*:avahi') * nic.exclude: Comma-separated list of interface prefixes (supporting regular expressions) to exclude (defaults to 'lo,virbr,docker,vboxnet,veth,br,.*:avahi')
* nic.include: Comma-separated list of interfaces to include * nic.include: Comma-separated list of interfaces to include
* nic.states: Comma-separated list of states to show (prefix with '^' to invert - i.e. ^down -> show all devices that are not in state down) * nic.states: Comma-separated list of states to show (prefix with '^' to invert - i.e. ^down -> show all devices that are not in state down)
* nic.format: Format string (defaults to '{intf} {state} {ip} {ssid}') * nic.format: Format string (defaults to '{intf} {state} {ip} {ssid} {strength}')
* nic.strength_warning: Integer to set the threshold for warning state (defaults to 50)
* nic.strength_critical: Integer to set the threshold for critical state (defaults to 30)
""" """
import re import re
@ -28,7 +30,7 @@ import util.format
class Module(core.module.Module): class Module(core.module.Module):
@core.decorators.every(seconds=10) @core.decorators.every(seconds=5)
def __init__(self, config, theme): def __init__(self, config, theme):
widgets = [] widgets = []
super().__init__(config, theme, widgets) super().__init__(config, theme, widgets)
@ -45,7 +47,15 @@ class Module(core.module.Module):
self._states["exclude"].append(state[1:]) self._states["exclude"].append(state[1:])
else: else:
self._states["include"].append(state) self._states["include"].append(state)
self._format = self.parameter("format", "{intf} {state} {ip} {ssid}") self._format = self.parameter("format", "{intf} {state} {ip} {ssid} {strength}")
self._strength_threshold_critical = self.parameter("strength_critical", 30)
self._strength_threshold_warning = self.parameter("strength_warning", 50)
# Limits for the accepted dBm values of wifi strength
self.__strength_dbm_lower_bound = -110
self.__strength_dbm_upper_bound = -30
self.iw = shutil.which("iw") self.iw = shutil.which("iw")
self._update_widgets(widgets) self._update_widgets(widgets)
@ -64,6 +74,13 @@ class Module(core.module.Module):
iftype = "wireless" if self._iswlan(intf) else "wired" iftype = "wireless" if self._iswlan(intf) else "wired"
iftype = "tunnel" if self._istunnel(intf) else iftype iftype = "tunnel" if self._istunnel(intf) else iftype
# "strength" is none if interface type is not wlan
if self._iswlan(intf):
if widget.get("strength") < self._strength_threshold_critical:
states.append("critical")
elif widget.get("strength") < self._strength_threshold_warning:
states.append("warning")
states.append("{}-{}".format(iftype, widget.get("state"))) states.append("{}-{}".format(iftype, widget.get("state")))
return states return states
@ -116,6 +133,9 @@ class Module(core.module.Module):
): ):
continue continue
strength_dbm = self.get_strength_dbm(intf)
strength_percent = self.convert_strength_dbm_percent(strength_dbm)
widget = self.widget(intf) widget = self.widget(intf)
if not widget: if not widget:
widget = self.add_widget(name=intf) widget = self.add_widget(name=intf)
@ -126,12 +146,14 @@ class Module(core.module.Module):
ip=", ".join(addr), ip=", ".join(addr),
intf=intf, intf=intf,
state=state, state=state,
strength=str(strength_percent) + "%" if strength_percent else "",
ssid=self.get_ssid(intf), ssid=self.get_ssid(intf),
).split() ).split()
) )
) )
widget.set("intf", intf) widget.set("intf", intf)
widget.set("state", state) widget.set("state", state)
widget.set("strength", strength_percent)
def get_ssid(self, intf): def get_ssid(self, intf):
if not self._iswlan(intf) or self._istunnel(intf) or not self.iw: if not self._iswlan(intf) or self._istunnel(intf) or not self.iw:
@ -145,5 +167,23 @@ class Module(core.module.Module):
return "" return ""
def get_strength_dbm(self, intf):
if not self._iswlan(intf) or self._istunnel(intf) or not self.iw:
return None
with open("/proc/net/wireless", "r") as file:
for line in file:
if intf in line:
# Remove trailing . by slicing it off ;)
strength_dbm = line.split()[3][:-1]
return util.format.asint(strength_dbm,
minium=self.__strength_dbm_lower_bound,
maximum=self.__strength_dbm_upper_bound)
return None
def convert_strength_dbm_percent(self, signal):
return int(100 * ((signal + 100) / 70.0)) if signal else None
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4