From 58835ce3563c8b4aab9c802749311e6c8d984e2c Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 17 Jun 2022 14:25:24 -0600 Subject: [PATCH] Hotspot device can be changed. --- app/src/components/Admin.js | 41 ++++++++++++++++++++++++------------- wrolpi/admin.py | 15 +++++++------- wrolpi/common.py | 9 ++++++++ wrolpi/root_api.py | 1 + wrolpi/schema.py | 1 + wrolpi/test/test_admin.py | 11 ++++++++++ 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/app/src/components/Admin.js b/app/src/components/Admin.js index 19ff55c8..5d16a547 100644 --- a/app/src/components/Admin.js +++ b/app/src/components/Admin.js @@ -53,6 +53,7 @@ class Settings extends React.Component { download_on_startup: null, download_timeout: null, + hotspot_device: null, hotspot_on_startup: null, hotspot_password: null, hotspot_ssid: null, @@ -74,6 +75,7 @@ class Settings extends React.Component { disabled: settings.wrol_mode, download_on_startup: settings.download_on_startup, download_timeout: settings.download_timeout || '', + hotspot_device: settings.hotspot_device, hotspot_on_startup: settings.hotspot_on_startup, hotspot_password: settings.hotspot_password, hotspot_ssid: settings.hotspot_ssid, @@ -93,6 +95,7 @@ class Settings extends React.Component { let settings = { download_on_startup: this.state.download_on_startup, download_timeout: this.state.download_timeout ? parseInt(this.state.download_timeout) : 0, + hotspot_device: this.state.hotspot_device, hotspot_on_startup: this.state.hotspot_on_startup, hotspot_password: this.state.hotspot_password, hotspot_ssid: this.state.hotspot_ssid, @@ -122,7 +125,7 @@ class Settings extends React.Component { e.preventDefault() } // Restrict timeout to numbers. - value = value.replace(/[^\d]/,''); + value = value.replace(/[^\d]/, ''); this.setState({[name]: value}); } @@ -135,6 +138,7 @@ class Settings extends React.Component { disabled, download_on_startup, download_timeout, + hotspot_device, hotspot_on_startup, hotspot_password, hotspot_ssid, @@ -144,6 +148,8 @@ class Settings extends React.Component { timezone, } = this.state; + const qrButton = ; + return ( @@ -216,21 +222,28 @@ class Settings extends React.Component { onChange={(e, d) => this.setState({hotspot_password: d.value}, this.handleHotspotChange)} /> - this.setState({qrOpen: false})} - onOpen={() => this.setState({qrOpen: true})} - open={this.state.qrOpen} - trigger={} - > - - Scan this code to join the hotspot - - - - - + this.handleInputChange(e,'hotspot_device', d.value)} + /> + this.setState({qrOpen: false})} + onOpen={() => this.setState({qrOpen: true})} + open={this.state.qrOpen} + trigger={qrButton} + > + + Scan this code to join the hotspot + + + + + + HotspotStatus: logger.debug(f'Could not get hotspot status', exc_info=e) return HotspotStatus.unknown + device = WROLPI_CONFIG.hotspot_device for line in output.splitlines(): - if line.startswith('wlan0: connected'): + if line.startswith(f'{device}: connected'): return HotspotStatus.connected - elif line.startswith('wlan0: disconnected'): + elif line.startswith(f'{device}: disconnected'): return HotspotStatus.disconnected - elif line.startswith('wlan0: unavailable'): + elif line.startswith(f'{device}: unavailable'): return HotspotStatus.unavailable return HotspotStatus.unknown def enable_hotspot(): - """Turn the wlan0 interface into a hotspot. + """Turn the wifi interface into a hotspot. - If wlan0 is already running, replace that with a hotspot. + If wifi is already running, replace that with a hotspot. """ status = hotspot_status() logger.warning(f'Hotspot status: {status}') @@ -59,7 +60,7 @@ def enable_hotspot(): return enable_hotspot() elif status == HotspotStatus.disconnected: # Radio is on, but not connected. Good, turn it into a hotspot. - cmd = (SUDO, NMCLI, 'device', 'wifi', 'hotspot', 'ifname', 'wlan0', + cmd = (SUDO, NMCLI, 'device', 'wifi', 'hotspot', 'ifname', WROLPI_CONFIG.hotspot_device, 'ssid', WROLPI_CONFIG.hotspot_ssid, 'password', WROLPI_CONFIG.hotspot_password) subprocess.check_call(cmd) logger.info('Enabling hotspot successful') @@ -75,7 +76,7 @@ def enable_hotspot(): def disable_hotspot(): - """Turn off the wlan0 interface.""" + """Turn off the wifi interface.""" cmd = (SUDO, NMCLI, 'radio', 'wifi', 'off') try: subprocess.check_call(cmd) diff --git a/wrolpi/common.py b/wrolpi/common.py index e8e34084..a8f90c6f 100644 --- a/wrolpi/common.py +++ b/wrolpi/common.py @@ -258,6 +258,7 @@ class WROLPiConfig(ConfigFile): default_config = dict( download_on_startup=True, download_timeout=0, + hotspot_device='wlan0', hotspot_on_startup=True, hotspot_password='wrolpi hotspot', hotspot_ssid='WROLPi', @@ -282,6 +283,14 @@ class WROLPiConfig(ConfigFile): def download_timeout(self, value: int): self.update({'download_timeout': value}) + @property + def hotspot_device(self) -> str: + return self._config['hotspot_device'] + + @hotspot_device.setter + def hotspot_device(self, value: str): + self.update({'hotspot_device': value}) + @property def hotspot_on_startup(self) -> bool: return self._config['hotspot_on_startup'] diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 37f7ffd0..3094ab07 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -121,6 +121,7 @@ def get_settings(_: Request): settings = { 'download_on_startup': config.download_on_startup, 'download_timeout': config.download_timeout, + 'hotspot_device': config.hotspot_device, 'hotspot_on_startup': config.hotspot_on_startup, 'hotspot_password': config.hotspot_password, 'hotspot_ssid': config.hotspot_ssid, diff --git a/wrolpi/schema.py b/wrolpi/schema.py index 8909b201..45f65474 100644 --- a/wrolpi/schema.py +++ b/wrolpi/schema.py @@ -16,6 +16,7 @@ class SettingsResponse: class SettingsRequest: download_on_startup: Optional[bool] = None download_timeout: Optional[int] = None + hotspot_device: Optional[str] = None hotspot_on_startup: Optional[bool] = None hotspot_password: Optional[str] = None hotspot_ssid: Optional[str] = None diff --git a/wrolpi/test/test_admin.py b/wrolpi/test/test_admin.py index 76d52552..41ebb526 100644 --- a/wrolpi/test/test_admin.py +++ b/wrolpi/test/test_admin.py @@ -110,3 +110,14 @@ def test_throttle_off(): mock_subprocess.check_call.assert_called_once_with( (PosixPath('/usr/bin/sudo'), PosixPath('/usr/bin/cpufreq-set'), '-g', 'ondemand') ) + + +@skip_circleci +def test_hotspot_device(): + """Changing the hotspot device changes what device is turned on.""" + from wrolpi.common import WROLPI_CONFIG + WROLPI_CONFIG.hotspot_device = 'wlp2s0' + + with mock.patch('wrolpi.admin.subprocess') as mock_subprocess: + mock_subprocess.check_output.return_value = b'wlp2s0: connected' + assert admin.hotspot_status() == HotspotStatus.connected