Basic functionaly for dealingn with signal strength
This commit is contained in:
parent
1232c4d960
commit
f141b95d8f
1 changed files with 37 additions and 12 deletions
|
@ -3,8 +3,9 @@ A module to show currently active network connection (ethernet or wifi)
|
||||||
and connection strength.
|
and connection strength.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import subprocess
|
|
||||||
import os
|
import util.cli
|
||||||
|
import util.format
|
||||||
|
|
||||||
import core.module
|
import core.module
|
||||||
import core.widget
|
import core.widget
|
||||||
|
@ -18,31 +19,55 @@ class Module(core.module.Module):
|
||||||
self._message = None
|
self._message = None
|
||||||
|
|
||||||
def network(self, widgets):
|
def network(self, widgets):
|
||||||
# start subprocess to get networked data
|
# run ip route command, tokenize output
|
||||||
std_out = os.popen("ip route get 8.8.8.8")
|
cmd = "ip route get 8.8.8.8"
|
||||||
route_str = " ".join(std_out.read().split())
|
std_out = util.cli.execute(cmd)
|
||||||
|
route_str = " ".join(std_out.split())
|
||||||
route_tokens = route_str.split(" ")
|
route_tokens = route_str.split(" ")
|
||||||
|
|
||||||
|
# Attempt to extract a valid network interface device
|
||||||
try:
|
try:
|
||||||
self._interface = route_tokens[route_tokens.index("dev") + 1] + ":"
|
self._interface = route_tokens[route_tokens.index("dev") + 1]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
self._interface = None
|
self._interface = None
|
||||||
|
|
||||||
with open("/proc/net/wireless", "r") as f:
|
# Check to see if the interface (if it exists) is wireless
|
||||||
if self._interface:
|
if self._interface:
|
||||||
|
with open("/proc/net/wireless", "r") as f:
|
||||||
self._is_wireless = self._interface in f.read()
|
self._is_wireless = self._interface in f.read()
|
||||||
|
f.close()
|
||||||
|
|
||||||
# setup message to send to bar
|
# setup message to send to the user
|
||||||
if self._interface is None:
|
if self._interface is None:
|
||||||
self._message = "Not connected to a network"
|
self._message = "Not connected to a network"
|
||||||
elif self._is_wireless:
|
elif self._is_wireless:
|
||||||
self._message = "Connected to WiFi"
|
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]
|
||||||
|
strength = util.format.asint(strength, minimum=-110, maximum=-30)
|
||||||
|
|
||||||
|
self._message = self.__generate_wireless_message(ssid, strength)
|
||||||
else:
|
else:
|
||||||
# self._message = "Connected to Ethernet"
|
|
||||||
self._message = self._message
|
self._message = self._message
|
||||||
|
|
||||||
|
|
||||||
return self._message
|
return self._message
|
||||||
|
|
||||||
|
|
||||||
|
def __generate_wireless_message(self, ssid, strength):
|
||||||
|
computed_strength = 100 * (strength + 110) / 70.0
|
||||||
|
if computed_strength < 25:
|
||||||
|
return ssid + " poor"
|
||||||
|
if computed_strength < 50:
|
||||||
|
return ssid + " fair"
|
||||||
|
if computed_strength < 75:
|
||||||
|
return ssid + " good"
|
||||||
|
|
||||||
|
return ssid + " excellent"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue