Skip to content

Commit

Permalink
minor: add disable to network panel, apply enables wlan
Browse files Browse the repository at this point in the history
  • Loading branch information
bartei81 committed Jun 2, 2024
1 parent 2822180 commit 69aff71
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 6 additions & 1 deletion rotary_controller_python/components/setup/network_panel.kv
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,10 @@

Button:
text: "Apply"
background_color: "#cf3a3a"
background_color: "#3acf3a"
on_release: root.test_configuration()

Button:
text: "Disable"
background_color: "#cf3a3a"
on_release: root.disable()
16 changes: 12 additions & 4 deletions rotary_controller_python/components/setup/network_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder

from rotary_controller_python.network import read_interfaces, render_interfaces, reload_interfaces
from rotary_controller_python.network import read_interfaces, render_interfaces, reload_interfaces, read_wlan_status, \
disable_wlan, enable_wlan
from rotary_controller_python.network.models import NetworkInterface, Wireless

log = Logger.getChild(__name__)
Expand All @@ -17,7 +18,7 @@

class NetworkPanel(BoxLayout):
device = StringProperty("")
dhcp = BooleanProperty(False)
dhcp = BooleanProperty(True)
address = StringProperty("")
netmask = StringProperty("")
gateway = StringProperty("")
Expand All @@ -31,6 +32,7 @@ def __init__(self, **kv):

configuration: NetworkInterface = read_interfaces()
self.device = configuration.name
self.rfkill_status = read_wlan_status()
self.dhcp = configuration.dhcp
if configuration.wireless is not None:
if configuration.wireless.ssid is not None:
Expand Down Expand Up @@ -68,8 +70,14 @@ def confirm(self):

self.status_text = status

def disable(self):
if self.rfkill_status is None:
return
disable_wlan(self.rfkill_status.id)

def test_configuration(self):
try:
enable_wlan(self.rfkill_status.id)
configuration = NetworkInterface(
name=self.device,
dhcp=self.dhcp,
Expand All @@ -82,6 +90,6 @@ def test_configuration(self):
)
)
render_interfaces(configuration=configuration)
status = reload_interfaces()
self.status_text = reload_interfaces()
except Exception as e:
status = e.__str__()
self.status_text = e.__str__()
6 changes: 3 additions & 3 deletions rotary_controller_python/network/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def read_interfaces(config_path: str = "/etc/network/interfaces"):
interface = models.NetworkInterface(
name="wlan0",
dhcp=False,
dhcp=True,
wireless=models.Wireless(
password="",
ssid=""
Expand Down Expand Up @@ -93,13 +93,13 @@ def reload_interfaces():
log.error(e.__str__())


def read_wlan_status():
def read_wlan_status() -> RfkillStatus:
try:
result = subprocess.run(["/usr/sbin/rfkill", "-J", "--output-all"], capture_output=True)
result.check_returncode()
json_data = result.stdout
rfkill_data = json.loads(json_data)
wlan_data = [v for k,v in rfkill_data.items()][0]
wlan_data = [v for k, v in rfkill_data.items()][0]
wlan_data = [item for item in wlan_data if item['type'] == 'wlan']
if len(wlan_data) == 0:
raise Exception("No wlan detected")
Expand Down

0 comments on commit 69aff71

Please sign in to comment.