mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 16:29:51 +03:00
update #4 - more alova
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { xhrRequestAdapter } from '@alova/adapter-xhr';
|
||||
import { createAlova } from 'alova';
|
||||
import GlobalFetch from 'alova/GlobalFetch';
|
||||
// import GlobalFetch from 'alova/GlobalFetch';
|
||||
import ReactHook from 'alova/react';
|
||||
import axios from 'axios';
|
||||
import { unpack } from './unpack';
|
||||
import { unpack } from '../api/unpack';
|
||||
|
||||
import type { AxiosPromise, CancelToken, AxiosProgressEvent } from 'axios';
|
||||
|
||||
@@ -31,32 +31,26 @@ export const alovaInstance = createAlova({
|
||||
method.config.headers.token = 'Bearer ' + localStorage.getItem(ACCESS_TOKEN);
|
||||
}
|
||||
},
|
||||
responsed: (response) => response.data
|
||||
|
||||
// TODO add error handling for Push?
|
||||
responded: {
|
||||
onSuccess: async (response, method) => {
|
||||
if (response.status === 400) {
|
||||
throw new Error('Invalid command');
|
||||
}
|
||||
if (response.status >= 400) {
|
||||
throw new Error(response.statusText);
|
||||
}
|
||||
const data = await response.data;
|
||||
if (response.data instanceof ArrayBuffer) {
|
||||
return unpack(data);
|
||||
}
|
||||
return data;
|
||||
},
|
||||
|
||||
// return JSON.stringify(response.data);
|
||||
|
||||
// responded: {
|
||||
// // When using the GlobalFetch request adapter, the first parameter receives the Response object
|
||||
// // The second parameter is the method instance of the current request, you can use it to synchronize the configuration information before and after the request
|
||||
// onSuccess: async (response, method) => {
|
||||
// if (response.status >= 400) {
|
||||
// throw new Error(response.statusText);
|
||||
// }
|
||||
// console.log('response', response);
|
||||
// const json = await response.json();
|
||||
// // The parsed response data will be passed to the transformData hook function of the method instance, and these functions will be explained later
|
||||
// return json;
|
||||
// },
|
||||
|
||||
// // Interceptor for request failure
|
||||
// // This interceptor will be entered when the request is wrong.
|
||||
// // The second parameter is the method instance of the current request, you can use it to synchronize the configuration information before and after the request
|
||||
// onError: (error, method) => {
|
||||
// alert(error.message);
|
||||
// }
|
||||
// }
|
||||
onError: (error) => {
|
||||
alert(error.message);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const AXIOS = axios.create({
|
||||
|
||||
@@ -51,7 +51,7 @@ const de: Translation = {
|
||||
REMOVE: 'Entfernen',
|
||||
PROBLEM_UPDATING: 'Problem beim Aktualisieren',
|
||||
PROBLEM_LOADING: 'Problem beim Laden',
|
||||
ACCESS_DENIED: 'Zugriff abgelehnt',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: 'Analogsensor',
|
||||
ANALOG_SENSORS: 'Analogsensoren',
|
||||
SETTINGS: 'Einstellungen',
|
||||
@@ -71,7 +71,6 @@ const de: Translation = {
|
||||
TEMP_SENSOR: 'Temperatursensor',
|
||||
TEMP_SENSORS: 'Temperatursensoren',
|
||||
WRITE_CMD_SENT: 'Befehl schreiben wurde gesendet',
|
||||
WRITE_CMD_FAILED: 'Befehl schreiben failed', // TODO translate
|
||||
EMS_BUS_WARNING: 'EMS-Bus getrennt. Wenn diese Warnung nach einigen Sekunden immer noch besteht, überprüfen Sie bitte die Einstellungen und das Board-Profil',
|
||||
EMS_BUS_SCANNING: 'Suche nach EMS Geräten...',
|
||||
CONNECTED: 'Verbunden',
|
||||
|
||||
@@ -51,7 +51,7 @@ const en: Translation = {
|
||||
REMOVE: 'Remove',
|
||||
PROBLEM_UPDATING: 'Problem updating',
|
||||
PROBLEM_LOADING: 'Problem loading',
|
||||
ACCESS_DENIED: 'Access Denied',
|
||||
HTTP_ERROR: 'Error {0}',
|
||||
ANALOG_SENSOR: 'Analog Sensor',
|
||||
ANALOG_SENSORS: 'Analog Sensors',
|
||||
SETTINGS: 'Settings',
|
||||
@@ -71,7 +71,6 @@ const en: Translation = {
|
||||
TEMP_SENSOR: 'Temperature Sensor',
|
||||
TEMP_SENSORS: 'Temperature Sensors',
|
||||
WRITE_CMD_SENT: 'Write command sent',
|
||||
WRITE_CMD_FAILED: 'Write command failed',
|
||||
EMS_BUS_WARNING: 'EMS bus disconnected. If this warning still persists after a few seconds please check settings and board profile',
|
||||
EMS_BUS_SCANNING: 'Scanning for EMS devices...',
|
||||
CONNECTED: 'Connected',
|
||||
|
||||
@@ -51,7 +51,7 @@ const fr: Translation = {
|
||||
REMOVE: 'Enlever',
|
||||
PROBLEM_UPDATING: 'Problème lors de la mise à jour',
|
||||
PROBLEM_LOADING: 'Problème lors du chargement',
|
||||
ACCESS_DENIED: 'Accès refusé',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: 'Capteur analogique',
|
||||
ANALOG_SENSORS: 'Capteurs analogiques',
|
||||
SETTINGS: 'Paramètres',
|
||||
@@ -71,7 +71,6 @@ const fr: Translation = {
|
||||
TEMP_SENSOR: 'Capteur de température',
|
||||
TEMP_SENSORS: 'Capteurs de température',
|
||||
WRITE_CMD_SENT: 'Envoyer la commande sent', // TODO translate
|
||||
WRITE_CMD_FAILED: 'Envoyer la commande failed', // TODO translate
|
||||
EMS_BUS_WARNING: 'Bus EMS déconnecté. Si ce message persiste après quelques secondes, vérifiez les paramètres et la configuration de la carte.',
|
||||
EMS_BUS_SCANNING: 'Scan des appareils EMS...',
|
||||
CONNECTED: 'Connecté',
|
||||
|
||||
@@ -51,7 +51,7 @@ const nl: Translation = {
|
||||
REMOVE: 'Verwijderen',
|
||||
PROBLEM_UPDATING: 'Probleem met updaten',
|
||||
PROBLEM_LOADING: 'Probleem met laden',
|
||||
ACCESS_DENIED: 'Toegang geweigerd',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: 'Analoge sensor',
|
||||
ANALOG_SENSORS: 'Analoge Sensoren',
|
||||
SETTINGS: 'Instellingen',
|
||||
@@ -71,7 +71,6 @@ const nl: Translation = {
|
||||
TEMP_SENSOR: 'Temperatuur sensor',
|
||||
TEMP_SENSORS: 'Temperatuur Sensoren',
|
||||
WRITE_CMD_SENT: 'Schrijf commando sent', // TODO translate
|
||||
WRITE_CMD_FAILED: 'Schrijf commando failed', // TODO translate
|
||||
EMS_BUS_WARNING: 'EMS bus niet gevonden. Als deze waarschuwing blijft staan na een paar seconden dan loop de instellingen na en in het bijzonder het apparaat type profiel na.',
|
||||
EMS_BUS_SCANNING: 'Scannen naar EMS apparaten...',
|
||||
CONNECTED: 'Verbonden',
|
||||
|
||||
@@ -51,7 +51,7 @@ const no: Translation = {
|
||||
REMOVE: 'Fjern',
|
||||
PROBLEM_UPDATING: 'Problem med oppdatering',
|
||||
PROBLEM_LOADING: 'Problem med opplasting',
|
||||
ACCESS_DENIED: 'Tilgang nektet',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: 'Analog Sensor',
|
||||
ANALOG_SENSORS: 'Analoge Sensorer',
|
||||
SETTINGS: 'Innstillinger',
|
||||
@@ -71,7 +71,6 @@ const no: Translation = {
|
||||
TEMP_SENSOR: 'Temperatursensor',
|
||||
TEMP_SENSORS: 'Temperaturesensorer',
|
||||
WRITE_CMD_SENT: 'Skriv kommando sent',
|
||||
WRITE_CMD_FAILED: 'Skriv kommando som har feilet',
|
||||
EMS_BUS_WARNING: 'EMS bussen koblet ned. Hvis denne advarselen fortsetter etter noen f¨sekunder sjekk instillinger og prosessorkort',
|
||||
EMS_BUS_SCANNING: 'Søker etter EMS enheter...',
|
||||
CONNECTED: 'Tilkoblet',
|
||||
|
||||
@@ -51,7 +51,7 @@ const pl: BaseTranslation = {
|
||||
REMOVE: 'Usuń',
|
||||
PROBLEM_UPDATING: 'Problem z uaktualnieniem!',
|
||||
PROBLEM_LOADING: 'Problem z załadowaniem!',
|
||||
ACCESS_DENIED: 'Brak dostępu!',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: '{{u|u||ustawienia u|ustawień u}}rządzeni{{a podłączonego do EMS-ESP|e||a podłączonego do EMS-ESP|a podłączonego do EMS-ESP}}',
|
||||
ANALOG_SENSORS: 'Urządzenia podłączone do EMS-ESP',
|
||||
SETTINGS: 'ustawienia',
|
||||
@@ -71,7 +71,6 @@ const pl: BaseTranslation = {
|
||||
TEMP_SENSOR: 'czujnika temperatury',
|
||||
TEMP_SENSORS: 'Czujniki temperatury 1-Wire®',
|
||||
WRITE_CMD_SENT: 'Komenda zapisu została wysłana.',
|
||||
WRITE_CMD_FAILED: 'Komenda zapisu nie powiodła się!',
|
||||
EMS_BUS_WARNING: 'Brak połączenia z magistralą EMS. Jeśli ten błąd występuje dłużej niż kilka sekund, sprawdź ustawienia oraz profil płytki interfejsu.',
|
||||
EMS_BUS_SCANNING: 'Trwa skanowanie urządzeń na magistrali EMS...',
|
||||
CONNECTED: '{{połączono|połączenie|}}',
|
||||
|
||||
@@ -51,7 +51,7 @@ const sv: Translation = {
|
||||
REMOVE: 'Ta bort',
|
||||
PROBLEM_UPDATING: 'Problem vid uppdatering',
|
||||
PROBLEM_LOADING: 'Problem vid hämtning',
|
||||
ACCESS_DENIED: 'Åtkomst Nekad',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: 'Analog Sensor',
|
||||
ANALOG_SENSORS: 'Analoga Sensorer',
|
||||
SETTINGS: 'Inställningar',
|
||||
@@ -71,7 +71,6 @@ const sv: Translation = {
|
||||
TEMP_SENSOR: 'Temperatursensor',
|
||||
TEMP_SENSORS: 'Temperatursensorer',
|
||||
WRITE_CMD_SENT: 'Skrivkommandon skickade',
|
||||
WRITE_CMD_FAILED: 'Skrivkommandon misslyckade',
|
||||
EMS_BUS_WARNING: 'EMS-buss nedkopplad. Om denna varning kvarstår efter några sekunder, kontrollera inställningar och enhets-profil.',
|
||||
EMS_BUS_SCANNING: 'Söker efter EMS-enheter...',
|
||||
CONNECTED: 'Ansluten',
|
||||
|
||||
@@ -51,7 +51,7 @@ const tr: Translation = {
|
||||
REMOVE: 'Kaldır',
|
||||
PROBLEM_UPDATING: 'Güncelleme Sorunu',
|
||||
PROBLEM_LOADING: 'Yükleme Sorunu',
|
||||
ACCESS_DENIED: 'Erişim Reddedildi',
|
||||
HTTP_ERROR: 'Error {0}', // TODO translate
|
||||
ANALOG_SENSOR: 'Analog Sensör',
|
||||
ANALOG_SENSORS: 'Analog Sensörler',
|
||||
SETTINGS: 'Ayarlar',
|
||||
@@ -71,7 +71,6 @@ const tr: Translation = {
|
||||
TEMP_SENSOR: 'Sıcaklık Sensörü',
|
||||
TEMP_SENSORS: 'Sıcaklık Sensörleri',
|
||||
WRITE_CMD_SENT: 'Yazma komutu gönderildi',
|
||||
WRITE_CMD_FAILED: 'Yazma komutu başarısız oldu',
|
||||
EMS_BUS_WARNING: 'EMS hat bağlantısı kesildi. Eğer bu uyarı birkaç saniye sonra devam ediyorsa lütfen ayarları ve kart tipini kontrol edin',
|
||||
EMS_BUS_SCANNING: 'EMS cihazları aranıyor...',
|
||||
CONNECTED: 'Bağlandı',
|
||||
|
||||
@@ -49,6 +49,8 @@ import { ButtonRow, SectionContent, MessageBox } from 'components';
|
||||
import { AuthenticatedContext } from 'contexts/authentication';
|
||||
|
||||
import { useI18nContext } from 'i18n/i18n-react';
|
||||
|
||||
// TODO what to do with extractErrorMessage?
|
||||
import { extractErrorMessage } from 'utils';
|
||||
|
||||
const DashboardDevices: FC = () => {
|
||||
@@ -61,13 +63,6 @@ const DashboardDevices: FC = () => {
|
||||
const [showDeviceInfo, setShowDeviceInfo] = useState<boolean>(false);
|
||||
const [selectedDevice, setSelectedDevice] = useState<number>();
|
||||
|
||||
// TODO remove
|
||||
// const [deviceData, setDeviceData] = useState<DeviceData>({ data: [] });
|
||||
// const [coreData, setCoreData] = useState<CoreData>({
|
||||
// connected: true,
|
||||
// devices: []
|
||||
// });
|
||||
|
||||
const { data: coreData, send: readCoreData } = useRequest(() => EMSESP.readCoreData(), {
|
||||
initialData: {
|
||||
connected: true,
|
||||
@@ -88,6 +83,7 @@ const DashboardDevices: FC = () => {
|
||||
const { loading: submitting, send: writeDeviceValue } = useRequest(
|
||||
(id: number, deviceValue: DeviceValue) => EMSESP.writeDeviceValue(id, deviceValue),
|
||||
{
|
||||
// } = useRequest((data) => alovaInstance.Post('/writeDeviceValue', data), {
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
@@ -239,15 +235,6 @@ const DashboardDevices: FC = () => {
|
||||
}
|
||||
);
|
||||
|
||||
// TODO remove
|
||||
// const fetchDeviceData = async (id: number) => {
|
||||
// try {
|
||||
// setDeviceData((await EMSESP.readDeviceData({ id })).data);
|
||||
// } catch (error) {
|
||||
// toast.error(extractErrorMessage(error, LL.PROBLEM_LOADING()));
|
||||
// }
|
||||
// };
|
||||
|
||||
async function onSelectChange(action: any, state: any) {
|
||||
setSelectedDevice(state.id);
|
||||
if (action.type === 'ADD_BY_ID_EXCLUSIVELY') {
|
||||
@@ -284,21 +271,6 @@ const DashboardDevices: FC = () => {
|
||||
};
|
||||
}, [escFunction]);
|
||||
|
||||
// TODO remove
|
||||
// const fetchCoreData = useCallback(async () => {
|
||||
// try {
|
||||
// setSelectedDevice(undefined);
|
||||
// setCoreData((await EMSESP.readCoreData()).data);
|
||||
// } catch (error) {
|
||||
// toast.error(extractErrorMessage(error, LL.PROBLEM_LOADING()));
|
||||
// }
|
||||
// }, [LL]);
|
||||
|
||||
// TODO remove
|
||||
// useEffect(() => {
|
||||
// void fetchCoreData2();
|
||||
// }, [fetchCoreData2]);
|
||||
|
||||
const refreshData = () => {
|
||||
if (deviceValueDialogOpen) {
|
||||
return;
|
||||
@@ -375,34 +347,20 @@ const DashboardDevices: FC = () => {
|
||||
};
|
||||
});
|
||||
|
||||
const deviceValueDialogSave = async (dv: DeviceValue) => {
|
||||
const selectedDeviceID = Number(device_select.state.id);
|
||||
// TODO For all Push, do error handling?
|
||||
const response = await writeDeviceValue(selectedDeviceID, dv);
|
||||
console.log(response);
|
||||
setDeviceValueDialogOpen(false);
|
||||
await readDeviceData(selectedDeviceID);
|
||||
setSelectedDeviceValue(undefined);
|
||||
|
||||
// try {
|
||||
// const response = await EMSESP.writeDeviceValue({
|
||||
// id: selectedDeviceID,
|
||||
// devicevalue: dv
|
||||
// });
|
||||
// if (response.status === 204) {
|
||||
// toast.error(LL.WRITE_CMD_FAILED());
|
||||
// } else if (response.status === 403) {
|
||||
// toast.error(LL.ACCESS_DENIED());
|
||||
// } else {
|
||||
// toast.success(LL.WRITE_CMD_SENT());
|
||||
// }
|
||||
// } catch (error) {
|
||||
// toast.error(extractErrorMessage(error, LL.PROBLEM_UPDATING()));
|
||||
// } finally {
|
||||
// setDeviceValueDialogOpen(false);
|
||||
// await readDeviceData(selectedDeviceID);
|
||||
// setSelectedDeviceValue(undefined);
|
||||
// }
|
||||
const deviceValueDialogSave = async (devicevalue: DeviceValue) => {
|
||||
const id = Number(device_select.state.id);
|
||||
await writeDeviceValue({ id, devicevalue })
|
||||
.then(() => {
|
||||
toast.success(LL.WRITE_CMD_SENT());
|
||||
})
|
||||
.catch((error) => {
|
||||
toast.error(LL.HTTP_ERROR(error));
|
||||
})
|
||||
.finally(async () => {
|
||||
setDeviceValueDialogOpen(false);
|
||||
await readDeviceData(id);
|
||||
setSelectedDeviceValue(undefined);
|
||||
});
|
||||
};
|
||||
|
||||
const renderDeviceDetails = () => {
|
||||
|
||||
@@ -238,7 +238,8 @@ const DashboardSensors: FC = () => {
|
||||
if (response.status === 204) {
|
||||
toast.error(LL.UPDATE_OF(LL.SENSOR(2)) + ' ' + LL.FAILED(1));
|
||||
} else if (response.status === 403) {
|
||||
toast.error(LL.ACCESS_DENIED());
|
||||
// TODO fix
|
||||
toast.error(LL.HTTP_ERROR('poep'));
|
||||
} else {
|
||||
toast.success(LL.UPDATED_OF(LL.SENSOR(1)));
|
||||
}
|
||||
@@ -295,7 +296,8 @@ const DashboardSensors: FC = () => {
|
||||
if (response.status === 204) {
|
||||
toast.error(LL.UPDATE_OF(LL.ANALOG_SENSOR(5)) + ' ' + LL.FAILED(1));
|
||||
} else if (response.status === 403) {
|
||||
toast.error(LL.ACCESS_DENIED());
|
||||
// TODO fix
|
||||
toast.error(LL.HTTP_ERROR('poep'));
|
||||
} else {
|
||||
toast.success(LL.UPDATED_OF(LL.ANALOG_SENSOR(2)));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { unpack } from '../api/unpack';
|
||||
import type {
|
||||
BoardProfile,
|
||||
BoardProfileName,
|
||||
@@ -23,14 +22,10 @@ import { AXIOS, AXIOS_API, AXIOS_BIN, alovaInstance } from 'api/endpoints';
|
||||
|
||||
export const readCoreData = () => alovaInstance.Get<CoreData>(`/coreData`);
|
||||
|
||||
// uses msgpack
|
||||
export const readDeviceData = (id: number) =>
|
||||
alovaInstance.Get<DeviceData>('/deviceData', {
|
||||
params: { id },
|
||||
responseType: 'arraybuffer',
|
||||
transformData(data) {
|
||||
return unpack(data);
|
||||
}
|
||||
responseType: 'arraybuffer' // uses msgpack
|
||||
});
|
||||
|
||||
export const writeDeviceValue = (id: number, devicevalue: DeviceValue) =>
|
||||
|
||||
Reference in New Issue
Block a user