From 61f0e48e62701b0b0961bd38d2f0b803030288a9 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 30 Apr 2022 12:20:49 -0600 Subject: [PATCH 1/5] Hotspot SSID/password can be changed. A QR Code is displayed in UI to allow easy joining of the hotspot. --- app/package.json | 1 + app/src/components/Admin.js | 84 ++++++++++++++++++++++++++++++++++-- wrolpi/admin.py | 4 +- wrolpi/common.py | 18 ++++++++ wrolpi/conftest.py | 2 +- wrolpi/root_api.py | 2 + wrolpi/schema.py | 2 + wrolpi/test/test_admin.py | 26 +++++++++++ wrolpi/test/test_root_api.py | 8 ++++ 9 files changed, 140 insertions(+), 7 deletions(-) diff --git a/app/package.json b/app/package.json index 8b32f9aa..e4229e37 100644 --- a/app/package.json +++ b/app/package.json @@ -10,6 +10,7 @@ "react-dom": "^16.13.1", "react-dropzone": "^11.4.2", "react-keyed-file-browser": "^1.14.0", + "react-qr-code": "^2.0.7", "react-router-dom": "^5.2.0", "react-scripts": "^3.4.1", "react-semantic-toasts": "^0.6.5", diff --git a/app/src/components/Admin.js b/app/src/components/Admin.js index d067846d..4e844d64 100644 --- a/app/src/components/Admin.js +++ b/app/src/components/Admin.js @@ -7,6 +7,8 @@ import { Divider, Form, Header, + Icon, + Input, Loader, Placeholder, Table @@ -33,6 +35,7 @@ import { WROLModeMessage } from "./Common"; import {Route} from "react-router-dom"; +import QRCode from "react-qr-code"; class Settings extends React.Component { @@ -40,10 +43,15 @@ class Settings extends React.Component { super(props); this.state = { disabled: false, + hotspot_encryption: 'WPA', + qrCodeValue: '', + qrHidden: true, ready: false, download_on_startup: null, hotspot_on_startup: null, + hotspot_password: null, + hotspot_ssid: null, hotspot_status: null, throttle_on_startup: null, throttle_status: null, @@ -51,6 +59,8 @@ class Settings extends React.Component { } this.handleSubmit = this.handleSubmit.bind(this); + this.toggleQRCode = this.toggleQRCode.bind(this); + this.handleHotspotPasswordChange = this.handleHotspotPasswordChange.bind(this); } async componentDidMount() { @@ -60,11 +70,13 @@ class Settings extends React.Component { disabled: settings.wrol_mode, download_on_startup: settings.download_on_startup, hotspot_on_startup: settings.hotspot_on_startup, + hotspot_password: settings.hotspot_password, + hotspot_ssid: settings.hotspot_ssid, hotspot_status: settings.hotspot_status, throttle_on_startup: settings.throttle_on_startup, throttle_status: settings.throttle_status, timezone: {value: settings.timezone, label: settings.timezone}, - }); + }, this.handleHotspotPasswordChange); } async handleSubmit(e) { @@ -72,6 +84,8 @@ class Settings extends React.Component { let settings = { download_on_startup: this.state.download_on_startup, hotspot_on_startup: this.state.hotspot_on_startup, + hotspot_password: this.state.hotspot_password, + hotspot_ssid: this.state.hotspot_ssid, throttle_on_startup: this.state.throttle_on_startup, timezone: this.state.timezone.value, } @@ -82,9 +96,23 @@ class Settings extends React.Component { this.setState({timezone: timezone}); } - handleInputChange = async (e, name, checked) => { + handleInputChange = async (e, name, value) => { e.preventDefault(); - this.setState({[name]: checked}); + this.setState({[name]: value}); + } + + handleHotspotPasswordChange = async () => { + let {hotspot_ssid, hotspot_encryption, hotspot_password} = this.state; + // Special string which allows a mobile device to connect to a specific Wi-Fi. + let qrCodeValue = `WIFI:S:${hotspot_ssid};T:${hotspot_encryption};P:${hotspot_password};;`; + this.setState({qrCodeValue}); + } + + toggleQRCode(e) { + if (e !== undefined) { + e.preventDefault(); + } + this.setState({qrHidden: !this.state.qrHidden}); } render() { @@ -92,7 +120,16 @@ class Settings extends React.Component { return } - let {disabled, download_on_startup, hotspot_on_startup, throttle_on_startup, timezone} = this.state; + let { + disabled, + download_on_startup, + hotspot_on_startup, + hotspot_password, + hotspot_ssid, + qrCodeValue, + throttle_on_startup, + timezone, + } = this.state; return ( @@ -138,6 +175,45 @@ class Settings extends React.Component { onChange={(e, d) => this.handleInputChange(e, 'throttle_on_startup', d.checked)} /> + + + +
+ + this.setState({hotspot_ssid: d.value}, this.handleHotspotPasswordChange)} + /> +
+ + +
+ + this.setState({hotspot_password: d.value}, this.handleHotspotPasswordChange)} + /> +
+ + + + +
+ str: + return self._config['hotspot_password'] + + @hotspot_password.setter + def hotspot_password(self, value: str): + self.update({'hotspot_password': value}) + + @property + def hotspot_ssid(self) -> str: + return self._config['hotspot_ssid'] + + @hotspot_ssid.setter + def hotspot_ssid(self, value: str): + self.update({'hotspot_ssid': value}) + @property def throttle_on_startup(self) -> bool: return self._config['throttle_on_startup'] diff --git a/wrolpi/conftest.py b/wrolpi/conftest.py index 49bd2d29..d7552df3 100644 --- a/wrolpi/conftest.py +++ b/wrolpi/conftest.py @@ -83,7 +83,7 @@ def test_directory() -> pathlib.Path: @pytest.fixture(autouse=True) -def test_config(test_directory): +def test_config(test_directory) -> pathlib.Path: """ Create a test config based off the example config. """ diff --git a/wrolpi/root_api.py b/wrolpi/root_api.py index 77e93f71..6b9342e7 100644 --- a/wrolpi/root_api.py +++ b/wrolpi/root_api.py @@ -119,6 +119,8 @@ def get_settings(_: Request): settings = { 'download_on_startup': config.download_on_startup, 'hotspot_on_startup': config.hotspot_on_startup, + 'hotspot_password': config.hotspot_password, + 'hotspot_ssid': config.hotspot_ssid, 'hotspot_status': admin.hotspot_status().name, 'media_directory': get_media_directory(), 'throttle_on_startup': config.throttle_on_startup, diff --git a/wrolpi/schema.py b/wrolpi/schema.py index a1969cdd..4b01b9ef 100644 --- a/wrolpi/schema.py +++ b/wrolpi/schema.py @@ -16,6 +16,8 @@ class SettingsResponse: class SettingsRequest: download_on_startup: Optional[bool] = None hotspot_on_startup: Optional[bool] = None + hotspot_password: Optional[str] = None + hotspot_ssid: Optional[str] = None hotspot_status: Optional[bool] = None media_directory: Optional[str] = None throttle_on: Optional[bool] = None diff --git a/wrolpi/test/test_admin.py b/wrolpi/test/test_admin.py index cc7fd2be..76d52552 100644 --- a/wrolpi/test/test_admin.py +++ b/wrolpi/test/test_admin.py @@ -4,6 +4,7 @@ from unittest.mock import call from wrolpi import admin from wrolpi.admin import HotspotStatus +from wrolpi.common import WROLPI_CONFIG from wrolpi.test.common import skip_circleci @@ -68,6 +69,31 @@ def test_enable_hotspot_connected(): ) +def test_change_hotspot(test_config): + """The hotspot can be configured.""" + with mock.patch('wrolpi.admin.hotspot_status') as mock_hotspot_status, \ + mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \ + mock.patch('wrolpi.admin.NMCLI', PosixPath('/usr/bin/nmcli')): + mock_subprocess.check_output.return_value = 0 + mock_hotspot_status.return_value = HotspotStatus.disconnected + assert admin.enable_hotspot() is True + mock_subprocess.check_call.assert_called_once_with( + (PosixPath('/usr/bin/sudo'), PosixPath('/usr/bin/nmcli'), 'device', 'wifi', 'hotspot', 'ifname', 'wlan0', + 'ssid', 'WROLPi', 'password', 'wrolpi hotspot')) + + WROLPI_CONFIG.hotspot_ssid = 'New SSID' + WROLPI_CONFIG.hotspot_password = '$uper Secure *#' + with mock.patch('wrolpi.admin.hotspot_status') as mock_hotspot_status, \ + mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \ + mock.patch('wrolpi.admin.NMCLI', PosixPath('/usr/bin/nmcli')): + mock_subprocess.check_output.return_value = 0 + mock_hotspot_status.return_value = HotspotStatus.disconnected + assert admin.enable_hotspot() is True + mock_subprocess.check_call.assert_called_once_with( + (PosixPath('/usr/bin/sudo'), PosixPath('/usr/bin/nmcli'), 'device', 'wifi', 'hotspot', 'ifname', 'wlan0', + 'ssid', 'New SSID', 'password', '$uper Secure *#')) + + def test_throttle_on(): with mock.patch('wrolpi.admin.subprocess') as mock_subprocess, \ mock.patch('wrolpi.admin.CPUFREQ_SET', PosixPath('/usr/bin/cpufreq-set')): diff --git a/wrolpi/test/test_root_api.py b/wrolpi/test/test_root_api.py index 45f27bf1..a50dce75 100644 --- a/wrolpi/test/test_root_api.py +++ b/wrolpi/test/test_root_api.py @@ -200,6 +200,14 @@ def test_hotspot_settings(test_session, test_client, test_config): config = get_config() assert config.hotspot_on_startup is True + # Hotspot password can be changed. + mock_admin.disable_hotspot.return_value = True + content = {'hotspot_password': 'new password', 'hotspot_ssid': 'new ssid'} + request, response = test_client.patch('/api/settings', content=json.dumps(content)) + assert response.status_code == HTTPStatus.NO_CONTENT + assert config.hotspot_password == 'new password' + assert config.hotspot_ssid == 'new ssid' + @skip_circleci def test_throttle_toggle(test_session, test_client, test_config): From 9aeda34672423094687ecb96b253d635e419a78b Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 30 Apr 2022 12:29:43 -0600 Subject: [PATCH 2/5] Using Modal to display Hotspot QR Code. --- app/src/components/Admin.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/src/components/Admin.js b/app/src/components/Admin.js index 4e844d64..ca546689 100644 --- a/app/src/components/Admin.js +++ b/app/src/components/Admin.js @@ -10,6 +10,7 @@ import { Icon, Input, Loader, + Modal, Placeholder, Table } from "semantic-ui-react"; @@ -199,18 +200,19 @@ class Settings extends React.Component { /> - - + + From 049058a5626e28b5250f8ed6d31b88760a813509 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 30 Apr 2022 12:30:16 -0600 Subject: [PATCH 3/5] Bumping version for hotspot password changing. --- wrolpi/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wrolpi/version.py b/wrolpi/version.py index fcf670a8..732a278f 100644 --- a/wrolpi/version.py +++ b/wrolpi/version.py @@ -1,4 +1,4 @@ -__version__ = '0.4.5-beta' +__version__ = '0.4.6-beta' import subprocess From df31f0239d8f36be3d6ba56c02093db2800de562 Mon Sep 17 00:00:00 2001 From: Roland Date: Sat, 30 Apr 2022 12:35:48 -0600 Subject: [PATCH 4/5] Converting open state to more understandable name. --- app/src/components/Admin.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/src/components/Admin.js b/app/src/components/Admin.js index ca546689..14198e80 100644 --- a/app/src/components/Admin.js +++ b/app/src/components/Admin.js @@ -46,7 +46,7 @@ class Settings extends React.Component { disabled: false, hotspot_encryption: 'WPA', qrCodeValue: '', - qrHidden: true, + qrOpen: false, ready: false, download_on_startup: null, @@ -113,7 +113,7 @@ class Settings extends React.Component { if (e !== undefined) { e.preventDefault(); } - this.setState({qrHidden: !this.state.qrHidden}); + this.setState({qrOpen: !this.state.qrOpen}); } render() { @@ -203,7 +203,7 @@ class Settings extends React.Component { } > From 9fecdc9ce3800b65fcc9363a1f790f7eb2b07b36 Mon Sep 17 00:00:00 2001 From: Roland Date: Sun, 1 May 2022 08:38:43 -0600 Subject: [PATCH 5/5] Reworking hotspot inputs. --- app/src/components/Admin.js | 87 ++++++++++++++----------------------- 1 file changed, 32 insertions(+), 55 deletions(-) diff --git a/app/src/components/Admin.js b/app/src/components/Admin.js index 14198e80..58843847 100644 --- a/app/src/components/Admin.js +++ b/app/src/components/Admin.js @@ -8,7 +8,6 @@ import { Form, Header, Icon, - Input, Loader, Modal, Placeholder, @@ -60,8 +59,7 @@ class Settings extends React.Component { } this.handleSubmit = this.handleSubmit.bind(this); - this.toggleQRCode = this.toggleQRCode.bind(this); - this.handleHotspotPasswordChange = this.handleHotspotPasswordChange.bind(this); + this.handleHotspotChange = this.handleHotspotChange.bind(this); } async componentDidMount() { @@ -77,7 +75,7 @@ class Settings extends React.Component { throttle_on_startup: settings.throttle_on_startup, throttle_status: settings.throttle_status, timezone: {value: settings.timezone, label: settings.timezone}, - }, this.handleHotspotPasswordChange); + }, this.handleHotspotChange); } async handleSubmit(e) { @@ -93,29 +91,18 @@ class Settings extends React.Component { await saveSettings(settings); } - handleTimezoneChange = async (timezone) => { - this.setState({timezone: timezone}); - } - handleInputChange = async (e, name, value) => { - e.preventDefault(); + if (e) {e.preventDefault()} this.setState({[name]: value}); } - handleHotspotPasswordChange = async () => { + handleHotspotChange = async () => { let {hotspot_ssid, hotspot_encryption, hotspot_password} = this.state; // Special string which allows a mobile device to connect to a specific Wi-Fi. let qrCodeValue = `WIFI:S:${hotspot_ssid};T:${hotspot_encryption};P:${hotspot_password};;`; this.setState({qrCodeValue}); } - toggleQRCode(e) { - if (e !== undefined) { - e.preventDefault(); - } - this.setState({qrOpen: !this.state.qrOpen}); - } - render() { if (this.state.ready === false) { return @@ -177,50 +164,40 @@ class Settings extends React.Component { /> - - -
- - this.setState({hotspot_ssid: d.value}, this.handleHotspotPasswordChange)} - /> -
- - -
- - this.setState({hotspot_password: d.value}, this.handleHotspotPasswordChange)} - /> -
- - } - > - - Scan this code to join the hotspot - - - - - - + + this.setState({hotspot_ssid: d.value}, this.handleHotspotChange)} + /> + + 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(null, 'timezone', v)} />