mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-10 01:39:54 +03:00
optimizations
This commit is contained in:
@@ -20,19 +20,18 @@ import type {
|
||||
WriteTemperatureSensor
|
||||
} from '../app/main/types';
|
||||
|
||||
const MSGPACK_CONFIG = { responseType: 'arraybuffer' as const };
|
||||
|
||||
// Dashboard
|
||||
export const readDashboard = () =>
|
||||
alovaInstance.Get<DashboardData>('/rest/dashboardData', {
|
||||
responseType: 'arraybuffer' // uses msgpack
|
||||
});
|
||||
alovaInstance.Get<DashboardData>('/rest/dashboardData', MSGPACK_CONFIG);
|
||||
|
||||
// Devices
|
||||
export const readCoreData = () => alovaInstance.Get<CoreData>(`/rest/coreData`);
|
||||
export const readCoreData = () => alovaInstance.Get<CoreData>('/rest/coreData');
|
||||
export const readDeviceData = (id: number) =>
|
||||
alovaInstance.Get<DeviceData>('/rest/deviceData', {
|
||||
// alovaInstance.Get<DeviceData>(`/rest/deviceData/${id}`, {
|
||||
params: { id },
|
||||
responseType: 'arraybuffer' // uses msgpack
|
||||
...MSGPACK_CONFIG
|
||||
});
|
||||
export const writeDeviceValue = (data: { id: number; c: string; v: unknown }) =>
|
||||
alovaInstance.Post('/rest/writeDeviceValue', data);
|
||||
@@ -66,13 +65,13 @@ export const callAction = (action: Action) =>
|
||||
|
||||
// SettingsCustomization
|
||||
export const readDeviceEntities = (id: number) =>
|
||||
// alovaInstance.Get<DeviceEntity[]>(`/rest/deviceEntities/${id}`, {
|
||||
alovaInstance.Get<DeviceEntity[]>(`/rest/deviceEntities`, {
|
||||
alovaInstance.Get<DeviceEntity[]>('/rest/deviceEntities', {
|
||||
params: { id },
|
||||
responseType: 'arraybuffer',
|
||||
...MSGPACK_CONFIG,
|
||||
// @ts-expect-error - exactOptionalPropertyTypes compatibility issue
|
||||
transform(data) {
|
||||
return (data as DeviceEntity[]).map((de: DeviceEntity) => ({
|
||||
const entities = data as DeviceEntity[];
|
||||
return entities.map((de) => ({
|
||||
...de,
|
||||
o_m: de.m,
|
||||
o_cn: de.cn,
|
||||
@@ -95,7 +94,8 @@ export const readSchedule = () =>
|
||||
alovaInstance.Get<ScheduleItem[]>('/rest/schedule', {
|
||||
// @ts-expect-error - exactOptionalPropertyTypes compatibility issue
|
||||
transform(data) {
|
||||
return (data as Schedule).schedule.map((si: ScheduleItem) => ({
|
||||
const schedule = (data as Schedule).schedule;
|
||||
return schedule.map((si) => ({
|
||||
...si,
|
||||
o_id: si.id,
|
||||
o_active: si.active,
|
||||
@@ -115,7 +115,8 @@ export const writeSchedule = (data: Schedule) =>
|
||||
export const readModules = () =>
|
||||
alovaInstance.Get<ModuleItem[]>('/rest/modules', {
|
||||
transform(data) {
|
||||
return (data as Modules).modules.map((mi: ModuleItem) => ({
|
||||
const modules = (data as Modules).modules;
|
||||
return modules.map((mi) => ({
|
||||
...mi,
|
||||
o_enabled: mi.enabled,
|
||||
o_license: mi.license
|
||||
@@ -133,7 +134,8 @@ export const readCustomEntities = () =>
|
||||
alovaInstance.Get<EntityItem[]>('/rest/customEntities', {
|
||||
// @ts-expect-error - exactOptionalPropertyTypes compatibility issue
|
||||
transform(data) {
|
||||
return (data as Entities).entities.map((ei: EntityItem) => ({
|
||||
const entities = (data as Entities).entities;
|
||||
return entities.map((ei) => ({
|
||||
...ei,
|
||||
o_id: ei.id,
|
||||
o_ram: ei.ram,
|
||||
|
||||
@@ -4,55 +4,57 @@ import ReactHook from 'alova/react';
|
||||
|
||||
import { unpack } from './unpack';
|
||||
|
||||
export const ACCESS_TOKEN = 'access_token';
|
||||
export const ACCESS_TOKEN = 'access_token' as const;
|
||||
|
||||
// Cached token to avoid repeated localStorage access
|
||||
let cachedToken: string | null = null;
|
||||
|
||||
const getAccessToken = (): string | null => {
|
||||
if (cachedToken === null) {
|
||||
cachedToken = localStorage.getItem(ACCESS_TOKEN);
|
||||
}
|
||||
return cachedToken;
|
||||
};
|
||||
|
||||
// Clear token cache when needed (e.g., on logout)
|
||||
export const clearTokenCache = (): void => {
|
||||
cachedToken = null;
|
||||
};
|
||||
|
||||
const handleResponse = async (response: AlovaXHRResponse) => {
|
||||
// Handle various HTTP status codes
|
||||
if (response.status === 205) {
|
||||
throw new Error('Reboot required');
|
||||
}
|
||||
if (response.status === 400) {
|
||||
throw new Error('Request Failed');
|
||||
}
|
||||
if (response.status >= 400) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
|
||||
const data = (await response.data) as ArrayBuffer;
|
||||
|
||||
// Unpack MessagePack data if ArrayBuffer
|
||||
if (data instanceof ArrayBuffer) {
|
||||
return unpack(data) as ArrayBuffer;
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
export const alovaInstance = createAlova({
|
||||
statesHook: ReactHook,
|
||||
// timeout: 3000, // 3 seconds before throwing a timeout error, default is 0 = none
|
||||
cacheFor: null, // disable cache
|
||||
// cacheFor: {
|
||||
// GET: {
|
||||
// mode: 'memory',
|
||||
// expire: 60 * 10 * 1000 // 60 seconds in cache
|
||||
// }
|
||||
// },
|
||||
requestAdapter: xhrRequestAdapter(),
|
||||
beforeRequest(method) {
|
||||
if (localStorage.getItem(ACCESS_TOKEN)) {
|
||||
method.config.headers.Authorization =
|
||||
'Bearer ' + localStorage.getItem(ACCESS_TOKEN);
|
||||
const token = getAccessToken();
|
||||
if (token) {
|
||||
method.config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
// for simulating very slow networks
|
||||
// return new Promise((resolve) => {
|
||||
// const random = 3000 + Math.random() * 2000;
|
||||
// setTimeout(resolve, Math.floor(random));
|
||||
// });
|
||||
},
|
||||
|
||||
responded: {
|
||||
onSuccess: async (response: AlovaXHRResponse) => {
|
||||
// if (response.status === 202) {
|
||||
// throw new Error('Wait'); // wifi scan in progress
|
||||
// } else
|
||||
if (response.status === 205) {
|
||||
throw new Error('Reboot required');
|
||||
} else if (response.status === 400) {
|
||||
throw new Error('Request Failed');
|
||||
} else if (response.status >= 400) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const data: ArrayBuffer = (await response.data) as ArrayBuffer;
|
||||
if (response.data instanceof ArrayBuffer) {
|
||||
return unpack(data) as ArrayBuffer;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
// Interceptor for request failure. This interceptor will be entered when the request is wrong.
|
||||
// http errors like 401 (unauthorized) are handled either in the methods or AuthenticatedRouting()
|
||||
// onError: (error, method) => {
|
||||
// alert(error.message);
|
||||
// }
|
||||
onSuccess: handleResponse
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -2,12 +2,14 @@ import type { NetworkSettingsType, NetworkStatusType, WiFiNetworkList } from 'ty
|
||||
|
||||
import { alovaInstance } from './endpoints';
|
||||
|
||||
const LIST_NETWORKS_TIMEOUT = 20000; // 20 seconds
|
||||
|
||||
export const readNetworkStatus = () =>
|
||||
alovaInstance.Get<NetworkStatusType>('/rest/networkStatus');
|
||||
export const scanNetworks = () => alovaInstance.Get('/rest/scanNetworks');
|
||||
export const listNetworks = () =>
|
||||
alovaInstance.Get<WiFiNetworkList>('/rest/listNetworks', {
|
||||
timeout: 20000 // 20 seconds
|
||||
timeout: LIST_NETWORKS_TIMEOUT
|
||||
});
|
||||
export const readNetworkSettings = () =>
|
||||
alovaInstance.Get<NetworkSettingsType>('/rest/networkSettings');
|
||||
|
||||
@@ -6,7 +6,7 @@ export const readNTPStatus = () =>
|
||||
alovaInstance.Get<NTPStatusType>('/rest/ntpStatus');
|
||||
|
||||
export const readNTPSettings = () =>
|
||||
alovaInstance.Get<NTPSettingsType>('/rest/ntpSettings', {});
|
||||
alovaInstance.Get<NTPSettingsType>('/rest/ntpSettings');
|
||||
export const updateNTPSettings = (data: NTPSettingsType) =>
|
||||
alovaInstance.Post<NTPSettingsType>('/rest/ntpSettings', data);
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ export const readSystemStatus = () =>
|
||||
|
||||
// SystemLog
|
||||
export const readLogSettings = () =>
|
||||
alovaInstance.Get<LogSettings>(`/rest/logSettings`);
|
||||
alovaInstance.Get<LogSettings>('/rest/logSettings');
|
||||
export const updateLogSettings = (data: LogSettings) =>
|
||||
alovaInstance.Post('/rest/logSettings', data);
|
||||
export const fetchLogES = () => alovaInstance.Get('/es/log');
|
||||
@@ -36,10 +36,12 @@ export const getDevVersion = () =>
|
||||
}
|
||||
});
|
||||
|
||||
const UPLOAD_TIMEOUT = 60000; // 1 minute
|
||||
|
||||
export const uploadFile = (file: File) => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
return alovaInstance.Post('/rest/uploadFile', formData, {
|
||||
timeout: 60000 // override timeout for uploading firmware - 1 minute
|
||||
timeout: UPLOAD_TIMEOUT
|
||||
});
|
||||
};
|
||||
|
||||
@@ -54,7 +54,7 @@ export class Unpackr {
|
||||
}
|
||||
Object.assign(this, options);
|
||||
}
|
||||
unpack(source, options?: any) {
|
||||
unpack(source, options?: { start?: number; end?: number; lazy?: boolean }) {
|
||||
if (src) {
|
||||
return saveState(() => {
|
||||
clearSource();
|
||||
@@ -184,7 +184,7 @@ export class Unpackr {
|
||||
function getPosition() {
|
||||
return position;
|
||||
}
|
||||
function checkedRead(options: any) {
|
||||
function checkedRead(options?: { lazy?: boolean }) {
|
||||
try {
|
||||
if (!currentUnpackr.trusted && !sequentialMode) {
|
||||
const sharedLength = currentStructures.sharedLength || 0;
|
||||
|
||||
Reference in New Issue
Block a user