Hotspot SSID/password can be changed. A QR Code is displayed in UI to allow easy joining of the hotspot.

This commit is contained in:
Roland 2022-04-30 12:20:49 -06:00
parent 20849017a5
commit 61f0e48e62
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
9 changed files with 140 additions and 7 deletions

View file

@ -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",

View file

@ -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 <Loader active inline='centered'/>
}
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 (
<Container fluid>
@ -138,6 +175,45 @@ class Settings extends React.Component {
onChange={(e, d) => this.handleInputChange(e, 'throttle_on_startup', d.checked)}
/>
<Form.Group inline>
<Form.Field>
<label>Hotspot SSID</label>
<br/>
<Input
id='hotspot_ssid'
value={hotspot_ssid}
disabled={disabled || hotspot_ssid === null}
onChange={(e, d) =>
this.setState({hotspot_ssid: d.value}, this.handleHotspotPasswordChange)}
/>
</Form.Field>
<Form.Field>
<label>Hotspot Password</label>
<br/>
<Input
id='hotspot_password'
disabled={disabled || hotspot_password === null}
value={hotspot_password}
onChange={(e, d) =>
this.setState({hotspot_password: d.value}, this.handleHotspotPasswordChange)}
/>
</Form.Field>
<Form.Field>
<div hidden={!this.state.qrHidden}>
<Button icon onClick={(e) => this.toggleQRCode(e)}>
<Icon name='qrcode' size='big'/>
</Button>
</div>
<div hidden={this.state.qrHidden}>
<h3>Scan this QR Code to join hotspot</h3>
<br/>
<a onClick={this.toggleQRCode}>
<QRCode value={qrCodeValue} size={300}/>
</a>
</div>
</Form.Field>
</Form.Group>
<Form.Field>
<label>Timezone</label>
<TimezoneSelect

View file

@ -2,7 +2,7 @@ import enum
import subprocess
from wrolpi.cmd import which
from wrolpi.common import logger
from wrolpi.common import logger, WROLPI_CONFIG
from wrolpi.vars import DEFAULT_CPU_FREQUENCY, DOCKERIZED
logger = logger.getChild(__name__)
@ -59,7 +59,7 @@ def 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',
'ssid', 'WROLPi', 'password', 'wrolpi hotspot')
'ssid', WROLPI_CONFIG.hotspot_ssid, 'password', WROLPI_CONFIG.hotspot_password)
subprocess.check_call(cmd)
return True
elif status == HotspotStatus.unavailable:

View file

@ -262,6 +262,8 @@ class WROLPiConfig(ConfigFile):
default_config = dict(
download_on_startup=True,
hotspot_on_startup=True,
hotspot_password='wrolpi hotspot',
hotspot_ssid='WROLPi',
throttle_on_startup=False,
timezone=str(DEFAULT_TIMEZONE),
wrol_mode=False,
@ -283,6 +285,22 @@ class WROLPiConfig(ConfigFile):
def hotspot_on_startup(self, value: bool):
self.update({'hotspot_on_startup': value})
@property
def hotspot_password(self) -> 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']

View file

@ -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.
"""

View file

@ -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,

View file

@ -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

View file

@ -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')):

View file

@ -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):