🪄 Adds api service with updates
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
import Home from '~icons/tabler/home';
|
||||
import Devices from '~icons/tabler/devices';
|
||||
import type { ApSettings, ApStatus } from '$lib/types/models';
|
||||
import { api } from '$lib/api';
|
||||
|
||||
let apSettings: ApSettings;
|
||||
let apStatus: ApStatus;
|
||||
@@ -20,34 +21,22 @@
|
||||
let formField: any;
|
||||
|
||||
async function getAPStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/apStatus', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
apStatus = await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
const result = await api.get<ApStatus>('/api/apStatus');
|
||||
if (result.isErr()){
|
||||
console.error('Error:', result.inner);
|
||||
return
|
||||
}
|
||||
apStatus = result.inner
|
||||
return apStatus;
|
||||
}
|
||||
|
||||
async function getAPSettings() {
|
||||
try {
|
||||
const response = await fetch('/api/apSettings', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
apSettings = await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
const result = await api.get<ApSettings>('/api/apSetting');
|
||||
if (result.isErr()){
|
||||
console.error('Error:', result.inner);
|
||||
return
|
||||
}
|
||||
apSettings = result.inner
|
||||
return apSettings;
|
||||
}
|
||||
|
||||
@@ -94,24 +83,14 @@
|
||||
};
|
||||
|
||||
async function postAPSettings(data: ApSettings) {
|
||||
try {
|
||||
const response = await fetch('/api/apSettings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
if (response.status == 200) {
|
||||
notifications.success('Access Point settings updated.', 3000);
|
||||
apSettings = await response.json();
|
||||
} else {
|
||||
notifications.error('User not authorized.', 3000);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
const result = await api.post<ApSettings>('/api/apSettings', data);
|
||||
if (result.isErr()){
|
||||
notifications.error('User not authorized.', 3000);
|
||||
console.error('Error:', result.inner);
|
||||
return
|
||||
}
|
||||
notifications.success('Access Point settings updated.', 3000);
|
||||
apSettings = result.inner
|
||||
}
|
||||
|
||||
function handleSubmitAP() {
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
import { closeModal } from 'svelte-modals';
|
||||
import { focusTrap } from 'svelte-focus-trap';
|
||||
import { fly } from 'svelte/transition';
|
||||
import { user } from '$lib/stores/user';
|
||||
import { page } from '$app/stores';
|
||||
import Network from '~icons/tabler/router';
|
||||
import AP from '~icons/tabler/access-point';
|
||||
import Cancel from '~icons/tabler/x';
|
||||
@@ -11,6 +9,8 @@
|
||||
import { onMount, onDestroy } from 'svelte';
|
||||
import RssiIndicator from '$lib/components/RSSIIndicator.svelte';
|
||||
import type { NetworkItem } from '$lib/types/models';
|
||||
import { api } from '$lib/api';
|
||||
import type { NetworkList } from '$lib/models';
|
||||
|
||||
// provided by <Modals />
|
||||
export let isOpen: boolean;
|
||||
@@ -36,13 +36,7 @@
|
||||
|
||||
async function scanNetworks() {
|
||||
scanActive = true;
|
||||
const scan = await fetch('/api/scanNetworks', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
await api.get('/api/scanNetworks');
|
||||
if ((await pollingResults()) == false) {
|
||||
pollingId = setInterval(() => pollingResults(), 1000);
|
||||
}
|
||||
@@ -50,28 +44,19 @@
|
||||
}
|
||||
|
||||
async function pollingResults() {
|
||||
const response = await fetch('/api/listNetworks', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
try {
|
||||
const result = await response.json();
|
||||
listOfNetworks = result.networks;
|
||||
if (listOfNetworks.length) {
|
||||
scanActive = false;
|
||||
clearInterval(pollingId);
|
||||
pollingId = 0;
|
||||
return true;
|
||||
} else {
|
||||
scanActive = false;
|
||||
return false;
|
||||
}
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
const result = await api.get<NetworkList>('/api/listNetworks');
|
||||
if (result.isErr()){
|
||||
console.error(`Error occurred while fetching: `, result.inner);
|
||||
return false
|
||||
}
|
||||
let response = result.inner
|
||||
listOfNetworks = response.networks;
|
||||
scanActive = false;
|
||||
if (listOfNetworks.length) {
|
||||
clearInterval(pollingId);
|
||||
pollingId = 0;
|
||||
}
|
||||
return listOfNetworks.length;
|
||||
}
|
||||
|
||||
onMount(() => {
|
||||
@@ -96,7 +81,7 @@
|
||||
use:focusTrap
|
||||
>
|
||||
<div
|
||||
class="bg-base-100 shadow rounded-box pointer-events-auto flex max-h-full min-w-fit max-w-md flex-col justify-between p-4 shadow-lg"
|
||||
class="bg-base-100 rounded-box pointer-events-auto flex max-h-full min-w-fit max-w-md flex-col justify-between p-4 shadow-lg"
|
||||
>
|
||||
<h2 class="text-base-content text-start text-2xl font-bold">Scan Networks</h2>
|
||||
<div class="divider my-2" />
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
import InfoDialog from '$lib/components/InfoDialog.svelte';
|
||||
import type { KnownNetworkItem, WifiSettings, WifiStatus } from '$lib/types/models';
|
||||
import { socket } from '$lib/stores';
|
||||
import { api } from '$lib/api';
|
||||
|
||||
let networkEditable: KnownNetworkItem = {
|
||||
ssid: '',
|
||||
@@ -72,35 +73,23 @@
|
||||
let formErrorhostname = false;
|
||||
|
||||
async function getWifiStatus() {
|
||||
try {
|
||||
const response = await fetch('/api/wifiStatus', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
wifiStatus = await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
const result = await api.get<WifiStatus>('/api/wifiStatus');
|
||||
if (result.isErr()){
|
||||
console.error(`Error occurred while fetching: `, result.inner);
|
||||
return
|
||||
}
|
||||
wifiStatus = result.inner
|
||||
return wifiStatus;
|
||||
}
|
||||
|
||||
async function getWifiSettings() {
|
||||
try {
|
||||
const response = await fetch('/api/wifiSettings', {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
wifiSettings = await response.json();
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
dndNetworkList = wifiSettings.wifi_networks;
|
||||
const result = await api.get<WifiSettings>('/api/wifiSettings');
|
||||
if (result.isErr()){
|
||||
console.error(`Error occurred while fetching: `, result.inner);
|
||||
return
|
||||
}
|
||||
wifiSettings = result.inner
|
||||
dndNetworkList = wifiSettings.wifi_networks;
|
||||
return wifiSettings;
|
||||
}
|
||||
|
||||
@@ -114,24 +103,14 @@
|
||||
});
|
||||
|
||||
async function postWiFiSettings(data: WifiSettings) {
|
||||
try {
|
||||
const response = await fetch('/api/wifiSettings', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Authorization: $page.data.features.security ? 'Bearer ' + $user.bearer_token : 'Basic',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(data)
|
||||
});
|
||||
if (response.status == 200) {
|
||||
notifications.success('Wi-Fi settings updated.', 3000);
|
||||
wifiSettings = await response.json();
|
||||
} else {
|
||||
notifications.error('User not authorized.', 3000);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error:', error);
|
||||
}
|
||||
const result = await api.post<WifiSettings>('/api/wifiSettings', data);
|
||||
if (result.isErr()){
|
||||
console.error(`Error occurred while fetching: `, result.inner);
|
||||
notifications.error('User not authorized.', 3000);
|
||||
return
|
||||
}
|
||||
wifiSettings = result.inner
|
||||
notifications.success('Wi-Fi settings updated.', 3000);
|
||||
}
|
||||
|
||||
function validateHostName() {
|
||||
|
||||
Reference in New Issue
Block a user