Created toggle for hotspot in admin settings. User needs sudo privileges.

This commit is contained in:
Roland 2022-02-04 09:41:08 -07:00
parent bc22fb6a6c
commit f27e9fdf63
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
8 changed files with 398 additions and 6 deletions

View file

@ -34,7 +34,8 @@ async function apiCall(url, method, body, ms = 10000) {
// await the response or error.
let response = await timeoutPromise(ms, promise);
let copy = response.clone();
if (response.status === 403 && (await copy.json())['code'] === 17) {
let code = (await copy.json())['code'];
if (response.status === 403 && code === 17) {
toast({
type: 'error',
title: 'WROL Mode is enabled!',
@ -423,3 +424,40 @@ export async function getVersion() {
let data = await response.json();
return data['version'];
}
export async function getDevices() {
let response = await apiGet(`${API_URI}/hotspot`);
if (response.status === 200) {
let {devices} = await response.json();
return devices;
} else {
let code = (await response.json())['code'];
if (response.status === 400 && code === 34) {
// Must not be running on a RPi. Don't warn the user.
return null;
}
}
return null;
}
export async function setHotspot(on) {
let response;
if (on) {
response = await apiPost(`${API_URI}/hotspot/on`);
} else {
response = await apiPost(`${API_URI}/hotspot/off`);
}
if (response.status === 204) {
return null;
} else {
let code = (await response.json())['code'];
if (code === 34) {
toast({
type: 'error',
title: 'Unsupported!',
description: 'Hotspot is only supported on a Raspberry Pi!',
time: 5000,
});
}
}
}

View file

@ -24,6 +24,22 @@ import {
import TimezoneSelect from 'react-timezone-select';
import {PageContainer, secondsToDate, secondsToFrequency, TabLinks, textEllipsis, WROLModeMessage} from "./Common";
import {Route} from "react-router-dom";
import {useHotspot} from "../hooks/customHooks";
function HotspotToggle() {
let {on, setHotspot} = useHotspot();
return (
<>
<Checkbox toggle
label='Toggle the WiFi Hotspot. Only supported on Raspberry Pi.'
disabled={on === null}
defaultIndeterminate={on === null}
checked={on === true}
onChange={(e, data) => setHotspot(data.checked)}
/>
</>
);
}
class Settings extends React.Component {
@ -78,6 +94,12 @@ class Settings extends React.Component {
return (
<Container fluid>
<WROLModeMessage content='Settings are disabled because WROL Mode is enabled.'/>
<p>
<HotspotToggle/>
</p>
<Divider/>
<p>
The global settings for your server.
</p>

View file

@ -1,5 +1,14 @@
import {useEffect, useState} from "react";
import {fetchDomains, filesSearch, getFiles, getVersion, searchArchives, searchVideos} from "../api";
import {
fetchDomains,
filesSearch,
getDevices,
getFiles,
getVersion,
searchArchives,
searchVideos,
setHotspot
} from "../api";
import {useHistory} from "react-router-dom";
export const useSearchParam = (key, defaultValue = null) => {
@ -178,3 +187,27 @@ export const useBrowseFiles = () => {
return {browseFiles, openFolders, setOpenFolders, fetchFiles};
}
export const useHotspot = () => {
const [on, setOn] = useState(null);
const fetchDevices = async () => {
const devices = await getDevices();
if (devices && devices['wlan0']) {
let wlan0 = devices['wlan0'];
setOn(wlan0['connection'] === 'connected');
}
}
useEffect(() => {
fetchDevices();
});
const localSetHotspot = async (on) => {
setOn(null);
setHotspot(on);
fetchDevices();
}
return {on, setOn, setHotspot: localSetHotspot};
}