Hotspot device can be changed.

This commit is contained in:
Roland 2022-06-17 14:25:24 -06:00
parent 6a94ffd0b2
commit 58835ce356
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
6 changed files with 57 additions and 21 deletions

View file

@ -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 = <Button icon style={{marginBottom: '1em'}}><Icon name='qrcode' size='big'/></Button>;
return (
<Container fluid>
<WROLModeMessage content='Settings are disabled because WROL Mode is enabled.'/>
@ -216,21 +222,28 @@ class Settings extends React.Component {
onChange={(e, d) =>
this.setState({hotspot_password: d.value}, this.handleHotspotChange)}
/>
<Modal closeIcon
onClose={() => this.setState({qrOpen: false})}
onOpen={() => this.setState({qrOpen: true})}
open={this.state.qrOpen}
trigger={<Button icon><Icon name='qrcode' size='big'/></Button>}
>
<Modal.Header>
Scan this code to join the hotspot
</Modal.Header>
<Modal.Content>
<QRCode value={qrCodeValue} size={300}/>
</Modal.Content>
</Modal>
<Form.Input
label='Hotspot Device'
disabled={disabled || hotspot_password === null}
value={hotspot_device}
onChange={(e, d) => this.handleInputChange(e,'hotspot_device', d.value)}
/>
</Form.Group>
<Modal closeIcon
onClose={() => this.setState({qrOpen: false})}
onOpen={() => this.setState({qrOpen: true})}
open={this.state.qrOpen}
trigger={qrButton}
>
<Modal.Header>
Scan this code to join the hotspot
</Modal.Header>
<Modal.Content>
<QRCode value={qrCodeValue} size={300}/>
</Modal.Content>
</Modal>
<Form.Field>
<label>Timezone</label>
<TimezoneSelect

View file

@ -34,21 +34,22 @@ def hotspot_status() -> 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)

View file

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

View file

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

View file

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

View file

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