Skip to content

Commit 85716bc

Browse files
committed
update #4 - more alova
1 parent 52d4505 commit 85716bc

17 files changed

+130
-189
lines changed

interface/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"@emotion/react": "^11.11.0",
2424
"@emotion/styled": "^11.11.0",
2525
"@mui/icons-material": "^5.11.16",
26-
"@mui/material": "^5.13.3",
26+
"@mui/material": "^5.13.4",
2727
"@table-library/react-table-library": "4.1.4",
2828
"@types/lodash-es": "^4.17.7",
2929
"@types/node": "^20.2.5",
@@ -47,8 +47,8 @@
4747
"typescript": "^5.1.3"
4848
},
4949
"devDependencies": {
50-
"@typescript-eslint/eslint-plugin": "^5.59.8",
51-
"@typescript-eslint/parser": "^5.59.8",
50+
"@typescript-eslint/eslint-plugin": "^5.59.9",
51+
"@typescript-eslint/parser": "^5.59.9",
5252
"@vitejs/plugin-react-swc": "^3.3.2",
5353
"eslint": "^8.42.0",
5454
"eslint-config-airbnb": "^19.0.4",

interface/src/api/endpoints.ts

+20-26
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { xhrRequestAdapter } from '@alova/adapter-xhr';
22
import { createAlova } from 'alova';
3-
import GlobalFetch from 'alova/GlobalFetch';
3+
// import GlobalFetch from 'alova/GlobalFetch';
44
import ReactHook from 'alova/react';
55
import axios from 'axios';
6-
import { unpack } from './unpack';
6+
import { unpack } from '../api/unpack';
77

88
import type { AxiosPromise, CancelToken, AxiosProgressEvent } from 'axios';
99

@@ -31,32 +31,26 @@ export const alovaInstance = createAlova({
3131
method.config.headers.token = 'Bearer ' + localStorage.getItem(ACCESS_TOKEN);
3232
}
3333
},
34-
responsed: (response) => response.data
3534

36-
// TODO add error handling for Push?
37-
38-
// return JSON.stringify(response.data);
39-
40-
// responded: {
41-
// // When using the GlobalFetch request adapter, the first parameter receives the Response object
42-
// // 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
43-
// onSuccess: async (response, method) => {
44-
// if (response.status >= 400) {
45-
// throw new Error(response.statusText);
46-
// }
47-
// console.log('response', response);
48-
// const json = await response.json();
49-
// // The parsed response data will be passed to the transformData hook function of the method instance, and these functions will be explained later
50-
// return json;
51-
// },
35+
responded: {
36+
onSuccess: async (response, method) => {
37+
if (response.status === 400) {
38+
throw new Error('Invalid command');
39+
}
40+
if (response.status >= 400) {
41+
throw new Error(response.statusText);
42+
}
43+
const data = await response.data;
44+
if (response.data instanceof ArrayBuffer) {
45+
return unpack(data);
46+
}
47+
return data;
48+
},
5249

53-
// // Interceptor for request failure
54-
// // This interceptor will be entered when the request is wrong.
55-
// // 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
56-
// onError: (error, method) => {
57-
// alert(error.message);
58-
// }
59-
// }
50+
onError: (error) => {
51+
alert(error.message);
52+
}
53+
}
6054
});
6155

6256
export const AXIOS = axios.create({

interface/src/i18n/de/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const de: Translation = {
5151
REMOVE: 'Entfernen',
5252
PROBLEM_UPDATING: 'Problem beim Aktualisieren',
5353
PROBLEM_LOADING: 'Problem beim Laden',
54-
ACCESS_DENIED: 'Zugriff abgelehnt',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
ANALOG_SENSOR: 'Analogsensor',
5656
ANALOG_SENSORS: 'Analogsensoren',
5757
SETTINGS: 'Einstellungen',
@@ -71,7 +71,6 @@ const de: Translation = {
7171
TEMP_SENSOR: 'Temperatursensor',
7272
TEMP_SENSORS: 'Temperatursensoren',
7373
WRITE_CMD_SENT: 'Befehl schreiben wurde gesendet',
74-
WRITE_CMD_FAILED: 'Befehl schreiben failed', // TODO translate
7574
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',
7675
EMS_BUS_SCANNING: 'Suche nach EMS Geräten...',
7776
CONNECTED: 'Verbunden',

interface/src/i18n/en/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const en: Translation = {
5151
REMOVE: 'Remove',
5252
PROBLEM_UPDATING: 'Problem updating',
5353
PROBLEM_LOADING: 'Problem loading',
54-
ACCESS_DENIED: 'Access Denied',
54+
HTTP_ERROR: 'Error {0}',
5555
ANALOG_SENSOR: 'Analog Sensor',
5656
ANALOG_SENSORS: 'Analog Sensors',
5757
SETTINGS: 'Settings',
@@ -71,7 +71,6 @@ const en: Translation = {
7171
TEMP_SENSOR: 'Temperature Sensor',
7272
TEMP_SENSORS: 'Temperature Sensors',
7373
WRITE_CMD_SENT: 'Write command sent',
74-
WRITE_CMD_FAILED: 'Write command failed',
7574
EMS_BUS_WARNING: 'EMS bus disconnected. If this warning still persists after a few seconds please check settings and board profile',
7675
EMS_BUS_SCANNING: 'Scanning for EMS devices...',
7776
CONNECTED: 'Connected',

interface/src/i18n/fr/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const fr: Translation = {
5151
REMOVE: 'Enlever',
5252
PROBLEM_UPDATING: 'Problème lors de la mise à jour',
5353
PROBLEM_LOADING: 'Problème lors du chargement',
54-
ACCESS_DENIED: 'Accès refusé',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
ANALOG_SENSOR: 'Capteur analogique',
5656
ANALOG_SENSORS: 'Capteurs analogiques',
5757
SETTINGS: 'Paramètres',
@@ -71,7 +71,6 @@ const fr: Translation = {
7171
TEMP_SENSOR: 'Capteur de température',
7272
TEMP_SENSORS: 'Capteurs de température',
7373
WRITE_CMD_SENT: 'Envoyer la commande sent', // TODO translate
74-
WRITE_CMD_FAILED: 'Envoyer la commande failed', // TODO translate
7574
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.',
7675
EMS_BUS_SCANNING: 'Scan des appareils EMS...',
7776
CONNECTED: 'Connecté',

interface/src/i18n/nl/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const nl: Translation = {
5151
REMOVE: 'Verwijderen',
5252
PROBLEM_UPDATING: 'Probleem met updaten',
5353
PROBLEM_LOADING: 'Probleem met laden',
54-
ACCESS_DENIED: 'Toegang geweigerd',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
ANALOG_SENSOR: 'Analoge sensor',
5656
ANALOG_SENSORS: 'Analoge Sensoren',
5757
SETTINGS: 'Instellingen',
@@ -71,7 +71,6 @@ const nl: Translation = {
7171
TEMP_SENSOR: 'Temperatuur sensor',
7272
TEMP_SENSORS: 'Temperatuur Sensoren',
7373
WRITE_CMD_SENT: 'Schrijf commando sent', // TODO translate
74-
WRITE_CMD_FAILED: 'Schrijf commando failed', // TODO translate
7574
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.',
7675
EMS_BUS_SCANNING: 'Scannen naar EMS apparaten...',
7776
CONNECTED: 'Verbonden',

interface/src/i18n/no/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const no: Translation = {
5151
REMOVE: 'Fjern',
5252
PROBLEM_UPDATING: 'Problem med oppdatering',
5353
PROBLEM_LOADING: 'Problem med opplasting',
54-
ACCESS_DENIED: 'Tilgang nektet',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
ANALOG_SENSOR: 'Analog Sensor',
5656
ANALOG_SENSORS: 'Analoge Sensorer',
5757
SETTINGS: 'Innstillinger',
@@ -71,7 +71,6 @@ const no: Translation = {
7171
TEMP_SENSOR: 'Temperatursensor',
7272
TEMP_SENSORS: 'Temperaturesensorer',
7373
WRITE_CMD_SENT: 'Skriv kommando sent',
74-
WRITE_CMD_FAILED: 'Skriv kommando som har feilet',
7574
EMS_BUS_WARNING: 'EMS bussen koblet ned. Hvis denne advarselen fortsetter etter noen f¨sekunder sjekk instillinger og prosessorkort',
7675
EMS_BUS_SCANNING: 'Søker etter EMS enheter...',
7776
CONNECTED: 'Tilkoblet',

interface/src/i18n/pl/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const pl: BaseTranslation = {
5151
REMOVE: 'Usuń',
5252
PROBLEM_UPDATING: 'Problem z uaktualnieniem!',
5353
PROBLEM_LOADING: 'Problem z załadowaniem!',
54-
ACCESS_DENIED: 'Brak dostępu!',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
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}}',
5656
ANALOG_SENSORS: 'Urządzenia podłączone do EMS-ESP',
5757
SETTINGS: 'ustawienia',
@@ -71,7 +71,6 @@ const pl: BaseTranslation = {
7171
TEMP_SENSOR: 'czujnika temperatury',
7272
TEMP_SENSORS: 'Czujniki temperatury 1-Wire®',
7373
WRITE_CMD_SENT: 'Komenda zapisu została wysłana.',
74-
WRITE_CMD_FAILED: 'Komenda zapisu nie powiodła się!',
7574
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.',
7675
EMS_BUS_SCANNING: 'Trwa skanowanie urządzeń na magistrali EMS...',
7776
CONNECTED: '{{połączono|połączenie|}}',

interface/src/i18n/sv/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const sv: Translation = {
5151
REMOVE: 'Ta bort',
5252
PROBLEM_UPDATING: 'Problem vid uppdatering',
5353
PROBLEM_LOADING: 'Problem vid hämtning',
54-
ACCESS_DENIED: 'Åtkomst Nekad',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
ANALOG_SENSOR: 'Analog Sensor',
5656
ANALOG_SENSORS: 'Analoga Sensorer',
5757
SETTINGS: 'Inställningar',
@@ -71,7 +71,6 @@ const sv: Translation = {
7171
TEMP_SENSOR: 'Temperatursensor',
7272
TEMP_SENSORS: 'Temperatursensorer',
7373
WRITE_CMD_SENT: 'Skrivkommandon skickade',
74-
WRITE_CMD_FAILED: 'Skrivkommandon misslyckade',
7574
EMS_BUS_WARNING: 'EMS-buss nedkopplad. Om denna varning kvarstår efter några sekunder, kontrollera inställningar och enhets-profil.',
7675
EMS_BUS_SCANNING: 'Söker efter EMS-enheter...',
7776
CONNECTED: 'Ansluten',

interface/src/i18n/tr/index.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const tr: Translation = {
5151
REMOVE: 'Kaldır',
5252
PROBLEM_UPDATING: 'Güncelleme Sorunu',
5353
PROBLEM_LOADING: 'Yükleme Sorunu',
54-
ACCESS_DENIED: 'Erişim Reddedildi',
54+
HTTP_ERROR: 'Error {0}', // TODO translate
5555
ANALOG_SENSOR: 'Analog Sensör',
5656
ANALOG_SENSORS: 'Analog Sensörler',
5757
SETTINGS: 'Ayarlar',
@@ -71,7 +71,6 @@ const tr: Translation = {
7171
TEMP_SENSOR: 'Sıcaklık Sensörü',
7272
TEMP_SENSORS: 'Sıcaklık Sensörleri',
7373
WRITE_CMD_SENT: 'Yazma komutu gönderildi',
74-
WRITE_CMD_FAILED: 'Yazma komutu başarısız oldu',
7574
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',
7675
EMS_BUS_SCANNING: 'EMS cihazları aranıyor...',
7776
CONNECTED: 'Bağlandı',

interface/src/project/DashboardDevices.tsx

+17-59
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import { ButtonRow, SectionContent, MessageBox } from 'components';
4949
import { AuthenticatedContext } from 'contexts/authentication';
5050

5151
import { useI18nContext } from 'i18n/i18n-react';
52+
53+
// TODO what to do with extractErrorMessage?
5254
import { extractErrorMessage } from 'utils';
5355

5456
const DashboardDevices: FC = () => {
@@ -61,13 +63,6 @@ const DashboardDevices: FC = () => {
6163
const [showDeviceInfo, setShowDeviceInfo] = useState<boolean>(false);
6264
const [selectedDevice, setSelectedDevice] = useState<number>();
6365

64-
// TODO remove
65-
// const [deviceData, setDeviceData] = useState<DeviceData>({ data: [] });
66-
// const [coreData, setCoreData] = useState<CoreData>({
67-
// connected: true,
68-
// devices: []
69-
// });
70-
7166
const { data: coreData, send: readCoreData } = useRequest(() => EMSESP.readCoreData(), {
7267
initialData: {
7368
connected: true,
@@ -88,6 +83,7 @@ const DashboardDevices: FC = () => {
8883
const { loading: submitting, send: writeDeviceValue } = useRequest(
8984
(id: number, deviceValue: DeviceValue) => EMSESP.writeDeviceValue(id, deviceValue),
9085
{
86+
// } = useRequest((data) => alovaInstance.Post('/writeDeviceValue', data), {
9187
immediate: false
9288
}
9389
);
@@ -239,15 +235,6 @@ const DashboardDevices: FC = () => {
239235
}
240236
);
241237

242-
// TODO remove
243-
// const fetchDeviceData = async (id: number) => {
244-
// try {
245-
// setDeviceData((await EMSESP.readDeviceData({ id })).data);
246-
// } catch (error) {
247-
// toast.error(extractErrorMessage(error, LL.PROBLEM_LOADING()));
248-
// }
249-
// };
250-
251238
async function onSelectChange(action: any, state: any) {
252239
setSelectedDevice(state.id);
253240
if (action.type === 'ADD_BY_ID_EXCLUSIVELY') {
@@ -284,21 +271,6 @@ const DashboardDevices: FC = () => {
284271
};
285272
}, [escFunction]);
286273

287-
// TODO remove
288-
// const fetchCoreData = useCallback(async () => {
289-
// try {
290-
// setSelectedDevice(undefined);
291-
// setCoreData((await EMSESP.readCoreData()).data);
292-
// } catch (error) {
293-
// toast.error(extractErrorMessage(error, LL.PROBLEM_LOADING()));
294-
// }
295-
// }, [LL]);
296-
297-
// TODO remove
298-
// useEffect(() => {
299-
// void fetchCoreData2();
300-
// }, [fetchCoreData2]);
301-
302274
const refreshData = () => {
303275
if (deviceValueDialogOpen) {
304276
return;
@@ -375,34 +347,20 @@ const DashboardDevices: FC = () => {
375347
};
376348
});
377349

378-
const deviceValueDialogSave = async (dv: DeviceValue) => {
379-
const selectedDeviceID = Number(device_select.state.id);
380-
// TODO For all Push, do error handling?
381-
const response = await writeDeviceValue(selectedDeviceID, dv);
382-
console.log(response);
383-
setDeviceValueDialogOpen(false);
384-
await readDeviceData(selectedDeviceID);
385-
setSelectedDeviceValue(undefined);
386-
387-
// try {
388-
// const response = await EMSESP.writeDeviceValue({
389-
// id: selectedDeviceID,
390-
// devicevalue: dv
391-
// });
392-
// if (response.status === 204) {
393-
// toast.error(LL.WRITE_CMD_FAILED());
394-
// } else if (response.status === 403) {
395-
// toast.error(LL.ACCESS_DENIED());
396-
// } else {
397-
// toast.success(LL.WRITE_CMD_SENT());
398-
// }
399-
// } catch (error) {
400-
// toast.error(extractErrorMessage(error, LL.PROBLEM_UPDATING()));
401-
// } finally {
402-
// setDeviceValueDialogOpen(false);
403-
// await readDeviceData(selectedDeviceID);
404-
// setSelectedDeviceValue(undefined);
405-
// }
350+
const deviceValueDialogSave = async (devicevalue: DeviceValue) => {
351+
const id = Number(device_select.state.id);
352+
await writeDeviceValue({ id, devicevalue })
353+
.then(() => {
354+
toast.success(LL.WRITE_CMD_SENT());
355+
})
356+
.catch((error) => {
357+
toast.error(LL.HTTP_ERROR(error));
358+
})
359+
.finally(async () => {
360+
setDeviceValueDialogOpen(false);
361+
await readDeviceData(id);
362+
setSelectedDeviceValue(undefined);
363+
});
406364
};
407365

408366
const renderDeviceDetails = () => {

interface/src/project/DashboardSensors.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,8 @@ const DashboardSensors: FC = () => {
238238
if (response.status === 204) {
239239
toast.error(LL.UPDATE_OF(LL.SENSOR(2)) + ' ' + LL.FAILED(1));
240240
} else if (response.status === 403) {
241-
toast.error(LL.ACCESS_DENIED());
241+
// TODO fix
242+
toast.error(LL.HTTP_ERROR('poep'));
242243
} else {
243244
toast.success(LL.UPDATED_OF(LL.SENSOR(1)));
244245
}
@@ -295,7 +296,8 @@ const DashboardSensors: FC = () => {
295296
if (response.status === 204) {
296297
toast.error(LL.UPDATE_OF(LL.ANALOG_SENSOR(5)) + ' ' + LL.FAILED(1));
297298
} else if (response.status === 403) {
298-
toast.error(LL.ACCESS_DENIED());
299+
// TODO fix
300+
toast.error(LL.HTTP_ERROR('poep'));
299301
} else {
300302
toast.success(LL.UPDATED_OF(LL.ANALOG_SENSOR(2)));
301303
}

interface/src/project/api.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { unpack } from '../api/unpack';
21
import type {
32
BoardProfile,
43
BoardProfileName,
@@ -23,14 +22,10 @@ import { AXIOS, AXIOS_API, AXIOS_BIN, alovaInstance } from 'api/endpoints';
2322

2423
export const readCoreData = () => alovaInstance.Get<CoreData>(`/coreData`);
2524

26-
// uses msgpack
2725
export const readDeviceData = (id: number) =>
2826
alovaInstance.Get<DeviceData>('/deviceData', {
2927
params: { id },
30-
responseType: 'arraybuffer',
31-
transformData(data) {
32-
return unpack(data);
33-
}
28+
responseType: 'arraybuffer' // uses msgpack
3429
});
3530

3631
export const writeDeviceValue = (id: number, devicevalue: DeviceValue) =>

0 commit comments

Comments
 (0)