Skip to content

Commit

Permalink
Add network settings to ui
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Apr 5, 2018
1 parent c240422 commit d426b53
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 1 deletion.
72 changes: 72 additions & 0 deletions vidhubcontrol/kivyui/ipsettings.kv
@@ -0,0 +1,72 @@
#:include vidhubcontrol/kivyui/newdevice.kv
<IPSettingsPopup>:
app: app
device: app.selected_device
ip_widget: ip_widget
netmask_widget: netmask_widget
gateway_widget: gateway_widget
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint_y: .1
ToggleButton:
text: 'Dynamic Address'
state: 'down' if root.use_dhcp else 'normal'
on_release: root.use_dhcp = True
ToggleButton:
text: 'Static Address'
state: 'normal' if root.use_dhcp else 'down'
on_release: root.use_dhcp = False
AttrField:
id: ip_widget
size_hint_y: .1
label_text: 'IP Address:'
value: root.ip_address
on_value: root.ip_address = self.value
disabled: root.use_dhcp
AttrField:
id: netmask_widget
size_hint_y: .1
label_text: 'Netmask:'
value: root.netmask
on_value: root.netmask = self.value
disabled: root.use_dhcp
AttrField:
id: gateway_widget
size_hint_y: .1
label_text: 'Gateway:'
value: root.gateway
on_value: root.gateway = self.value
disabled: root.use_dhcp
BoxLayout:
size_hint_y: .2
orientation: 'vertical'
OutlinedBoxLayout:
orientation: 'horizontal'
OutlinedLabel:
text: 'Current DHCP'
OutlinedLabel:
text: str(root.device_use_dhcp)
OutlinedBoxLayout:
orientation: 'horizontal'
OutlinedLabel:
text: 'Current IP'
OutlinedLabel:
text: str(root.device_ip.ip)
OutlinedBoxLayout:
orientation: 'horizontal'
OutlinedLabel:
text: 'Current netmask'
OutlinedLabel:
text: str(root.device_ip.netmask)
OutlinedBoxLayout:
orientation: 'horizontal'
OutlinedLabel:
text: 'Current gateway'
OutlinedLabel:
text: str(root.device_gateway)
SubmitRow:
size_hint_y: .1
on_cancel: root.dismiss()
on_submit: root.on_submit()
76 changes: 76 additions & 0 deletions vidhubcontrol/kivyui/ipsettings.py
@@ -0,0 +1,76 @@
import asyncio
import ipaddress

from kivy.properties import (
ObjectProperty,
StringProperty,
BooleanProperty,
)
from kivy.uix.popup import Popup

class IPSettingsPopup(Popup):
app = ObjectProperty(None)
device = ObjectProperty(None, allownone=True)
ip_widget = ObjectProperty(None)
netmask_widget = ObjectProperty(None)
gateway_widget = ObjectProperty(None)
use_dhcp = BooleanProperty(False)
ip_address = StringProperty('')
netmask = StringProperty('')
gateway = StringProperty('')
device_use_dhcp = BooleanProperty(False)
device_ip = ObjectProperty(ipaddress.ip_interface('0.0.0.0/32'))
device_gateway = ObjectProperty(ipaddress.ip_address('0.0.0.0'))
def on_device(self, *args):
device = self.device
self.use_dhcp = device.using_dhcp
self.ip_address = str(device.static_ip.ip)
self.netmask = str(device.static_ip.netmask)
self.gateway = str(device.static_gateway)
self.device_use_dhcp = device.using_dhcp
self.device_ip = device.current_ip
self.device_gateway = device.current_gateway
def validate_ip_prop(self, prop, widget):
address = getattr(self, prop)
if not len(address):
widget.validation_message = 'Required Field'
widget.validation_error = True
return False
try:
addr = ipaddress.ip_address(address)
except ValueError:
widget.validation_message = 'Invalid Address'
widget.validation_error = True
return False
return True
def validate(self):
self.ip_widget.validation_error = False
self.netmask_widget.validation_error = False
self.gateway_widget.validation_error = False
if self.use_dhcp:
return True
r = set()
r.add(self.validate_ip_prop('ip_address', self.ip_widget))
r.add(self.validate_ip_prop('netmask', self.netmask_widget))
r.add(self.validate_ip_prop('gateway', self.gateway_widget))
if False in r:
return False
addr = ipaddress.ip_interface('/'.join([self.ip_address, self.netmask]))
gateway = ipaddress.ip_address(self.gateway)
if gateway not in addr.network:
self.gateway_widget.validation_message = 'Invalid gateway for network'
self.gateway_widget.validation_error = True
return False
return True
def on_submit(self, *args, **kwargs):
if not self.validate():
return
self.submit_changes()
self.dismiss()
def submit_changes(self):
if self.use_dhcp:
self.app.run_async_coro(self.device.set_dhcp())
else:
addr = ipaddress.ip_interface('/'.join([self.ip_address, self.netmask]))
gateway = ipaddress.ip_address(self.gateway)
self.app.run_async_coro(self.device.set_device_static_ip(addr, gateway))
5 changes: 5 additions & 0 deletions vidhubcontrol/kivyui/main.py
Expand Up @@ -23,6 +23,7 @@
from vidhubcontrol.kivyui.vidhubedit import VidhubEditView
from vidhubcontrol.kivyui.smartview import SmartViewWidget
from vidhubcontrol.kivyui.newdevice import NewDevicePopup
from vidhubcontrol.kivyui.ipsettings import IPSettingsPopup

APP_SETTINGS = [
{
Expand Down Expand Up @@ -199,6 +200,10 @@ def on_active_widget(self, *args):
def update_active_widget_props(self, *args, **kwargs):
self.name = self.active_widget.name
self.connected = self.active_widget.connected
def launch_ip_settings(self):
popup = IPSettingsPopup()
self.app.popup_widget = popup
popup.open()

class VidhubControlApp(App):
async_server = ObjectProperty(None)
Expand Down
8 changes: 7 additions & 1 deletion vidhubcontrol/kivyui/vidhubcontrol.kv
Expand Up @@ -3,6 +3,7 @@
#:include vidhubcontrol/kivyui/vidhubpresetedit.kv
#:include vidhubcontrol/kivyui/smartview.kv
#:include vidhubcontrol/kivyui/newdevice.kv
#:include vidhubcontrol/kivyui/ipsettings.kv
#:include vidhubcontrol/kivyui/utils.kv

RootWidget:
Expand Down Expand Up @@ -46,7 +47,12 @@ RootWidget:
StatusIndicator:
state: root.connected
BoxLayout:
size_hint_x: .75
size_hint_x: .2
Button:
size_hint_x: .4
text: 'IP Settings'
on_release: root.launch_ip_settings()
disabled: app.selected_device is None or app.selected_device.connected is False

<VidhubPanel>:
size_hint: 1, 1
Expand Down

0 comments on commit d426b53

Please sign in to comment.