1
0
Fork 0
mirror of https://github.com/roles-ansible/ansible_role_sway.git synced 2024-08-16 13:49:49 +02:00

Update wlan script

This commit is contained in:
L3D 2023-08-22 14:53:13 +02:00
parent eeb97cb94d
commit dbcaa676aa
Signed by: l3d
GPG key ID: CD08445BFF4313D1
4 changed files with 80 additions and 72 deletions

78
files/i3blocks/wlan.py Normal file
View file

@ -0,0 +1,78 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import subprocess
import sys
import argparse
def find_interface(interfaces):
for iface in interfaces:
if os.path.exists(f"/sys/class/net/{iface}"):
return iface
return None
def get_signal_quality(interface):
try:
iw_output = subprocess.check_output(["/sbin/iw", "dev", interface, "link"], text=True)
signal_match = re.search(r'(\-\d{2}) dBm', iw_output)
return int(signal_match.group(1)) if signal_match else None
except subprocess.CalledProcessError:
return None
def get_ssid(interface):
try:
iw_output = subprocess.check_output(["/sbin/iw", "dev", interface, "link"], text=True)
ssid_match = re.search(r'SSID: (.+)', iw_output)
return ssid_match.group(1).encode("utf-8").decode("unicode-escape") if ssid_match else None
except subprocess.CalledProcessError:
return None
def print_color(quality):
if quality >= 70:
print("#00FF00")
elif quality >= 50:
print("#CCFF00")
elif quality >= 30:
print("#FFFF00")
elif quality >= 10:
print("#FFAA00")
else:
print("#FF0000")
def main():
interfaces = ["wlp3s0", "wlan0"]
parser = argparse.ArgumentParser(description="Display WiFi signal strength and SSID")
parser.add_argument("--interface", help="Specify the wireless interface")
args = parser.parse_args()
if args.interface:
interface = args.interface
else:
interface = find_interface(interfaces)
if not interface:
sys.exit(0)
signal_quality = get_signal_quality(interface)
ssid = get_ssid(interface)
if signal_quality is None:
sys.exit(1)
# quality = int((signal_quality / 70) * 100) # Adjust the calculation as needed
quality = int(signal_quality) # Adjust the calculation as needed
emoji_signal = "📶"
if ssid:
print(f"{emoji_signal} {ssid} {quality}dBm")
else:
print(f"{emoji_signal} {quality}dBm")
print_color(quality)
if __name__ == "__main__":
main()

View file

@ -1,70 +0,0 @@
#!/usr/bin/env bash
#
# i3blocks blocklet script to display wifi signal in dBm and IP address, if WIFI is available
# try to detect interface name
if [[ -z "$IFACE" ]] ; then
if ( ip a s dev wlp3s0 ) > /dev/null 2>& 1 ; then
IFACE="wlp3s0"
elif ( ip a s dev wlan0 ) > /dev/null 2>& 1 ; then
IFACE="wlan0"
else
# No wlan interface found
exit 0
fi
fi
USE_PERCENT=${USE_PERCENT:-0}
IW=$(which iw || echo "/sbin/iw")
if [[ ! -x $IW ]]; then
echo "No iw binary was found on the system." 1>2
exit 1
fi
while getopts p opt; do
case "$opt" in
p) USE_PERCENT=1 ;;
esac
done
if ( ip a s dev $IFACE | grep "state DOWN" ) > /dev/null 2>& 1 ; then
echo "📶 DOWN"
fi
dbm=$($IW dev "$IFACE" link | grep 'dBm$' | grep -Eoe '-[0-9]{2}')
ssid=$($IW dev "$IFACE" link | grep 'SSID' | cut -d ':' -f2 | sed "s/\ //g")
ipv4=$(ip -o -4 addr list "$IFACE" | awk '{print $4}')
ipv6=$(ip -o -6 addr list "$IFACE" | awk '{print $4}')
[[ -n "$dbm" ]] || exit 1
[[ -n "$ssid" ]] || exit 1
if [[ $USE_PERCENT -eq 0 ]]; then
echo "📶($ssid) $dbm"dBm
echo "📶($ssid) $dbm"dBm
else
if [[ "$dbm" -le -100 ]]; then
quality=0
elif [[ $dbm -ge -50 ]]; then
quality=100
else
quality=$((2 * (dbm + 100)))
fi
echo "📶($ssid) $quality%"
echo "📶($ssid) $quality%"
fi
if [[ $dbm -ge -55 ]]; then
echo "#00FF00"
elif [[ $dbm -ge -60 ]]; then
echo "#CCFF00"
elif [[ $dbm -ge -70 ]]; then
echo "#FFFF00"
elif [[ $dbm -ge -80 ]]; then
echo "#FFAA00"
else
echo "#FF0000"
fi

View file

@ -69,7 +69,7 @@
group: "{{ i3wm_usr.user }}"
with_items:
- 'volume.sh'
- 'wlan.sh'
- 'wlan.py'
- 'battery.py'
- name: Create rofi config folder

View file

@ -18,7 +18,7 @@ signal=10
{% if i3_i3blocks_options.wifisignal | bool %}
[wlan]
command={{ i3wm_usr.home }}/.config/i3blocks/wlan.sh
command={{ i3wm_usr.home }}/.config/i3blocks/wlan.py
interval=1
signal=10
{% endif %}