useAutoRequest tidy up

This commit is contained in:
proddy
2024-08-11 10:40:11 +02:00
parent 962d007d91
commit f5968412a0
5 changed files with 60 additions and 87 deletions

View File

@@ -55,7 +55,7 @@ import {
import { useTheme } from '@table-library/react-table-library/theme'; import { useTheme } from '@table-library/react-table-library/theme';
import type { Action, State } from '@table-library/react-table-library/types/common'; import type { Action, State } from '@table-library/react-table-library/types/common';
import { dialogStyle } from 'CustomTheme'; import { dialogStyle } from 'CustomTheme';
import { useAutoRequest, useRequest } from 'alova/client'; import { useRequest } from 'alova/client';
import { MessageBox, SectionContent, useLayoutTitle } from 'components'; import { MessageBox, SectionContent, useLayoutTitle } from 'components';
import { AuthenticatedContext } from 'contexts/authentication'; import { AuthenticatedContext } from 'contexts/authentication';
import { useI18nContext } from 'i18n/i18n-react'; import { useI18nContext } from 'i18n/i18n-react';
@@ -83,16 +83,12 @@ const Devices = () => {
useLayoutTitle(LL.DEVICES()); useLayoutTitle(LL.DEVICES());
const { data: coreData, send: sendCoreData } = useAutoRequest( const { data: coreData, send: sendCoreData } = useRequest(() => readCoreData(), {
() => readCoreData(),
{
initialData: { initialData: {
connected: true, connected: true,
devices: [] devices: []
},
pollingTime: 2000
} }
); });
const { data: deviceData, send: sendDeviceData } = useRequest( const { data: deviceData, send: sendDeviceData } = useRequest(
(id: number) => readDeviceData(id), (id: number) => readDeviceData(id),
@@ -320,12 +316,6 @@ const Devices = () => {
}; };
}, [escFunction]); }, [escFunction]);
const refreshData = () => {
if (!deviceValueDialogOpen) {
selectedDevice ? void sendDeviceData(selectedDevice) : void sendCoreData();
}
};
const customize = () => { const customize = () => {
if (selectedDevice == 99) { if (selectedDevice == 99) {
navigate('/customentities'); navigate('/customentities');
@@ -436,7 +426,12 @@ const Devices = () => {
}; };
useEffect(() => { useEffect(() => {
const timer = setInterval(() => refreshData(), 60000); const timer = setInterval(() => {
if (deviceValueDialogOpen) {
return;
}
selectedDevice ? void sendDeviceData(selectedDevice) : void sendCoreData();
}, 2000);
return () => { return () => {
clearInterval(timer); clearInterval(timer);
}; };
@@ -591,6 +586,7 @@ const Devices = () => {
const deviceValueDialogClose = () => { const deviceValueDialogClose = () => {
setDeviceValueDialogOpen(false); setDeviceValueDialogOpen(false);
void sendDeviceData(selectedDevice);
}; };
const renderDeviceData = () => { const renderDeviceData = () => {

View File

@@ -1,4 +1,4 @@
import { useContext, useEffect, useState } from 'react'; import { useContext, useState } from 'react';
import { toast } from 'react-toastify'; import { toast } from 'react-toastify';
import AddCircleOutlineOutlinedIcon from '@mui/icons-material/AddCircleOutlineOutlined'; import AddCircleOutlineOutlinedIcon from '@mui/icons-material/AddCircleOutlineOutlined';
@@ -59,9 +59,7 @@ const Sensors = () => {
const [analogDialogOpen, setAnalogDialogOpen] = useState<boolean>(false); const [analogDialogOpen, setAnalogDialogOpen] = useState<boolean>(false);
const [creating, setCreating] = useState<boolean>(false); const [creating, setCreating] = useState<boolean>(false);
const { data: sensorData, send: sendSensorData } = useAutoRequest( const { data: sensorData } = useAutoRequest(() => readSensorData(), {
() => readSensorData(),
{
initialData: { initialData: {
ts: [], ts: [],
as: [], as: [],
@@ -69,8 +67,7 @@ const Sensors = () => {
platform: 'ESP32' platform: 'ESP32'
}, },
pollingTime: 2000 pollingTime: 2000
} });
);
const { send: sendTemperatureSensor } = useRequest( const { send: sendTemperatureSensor } = useRequest(
(data: WriteTemperatureSensor) => writeTemperatureSensor(data), (data: WriteTemperatureSensor) => writeTemperatureSensor(data),
@@ -198,13 +195,6 @@ const Sensors = () => {
} }
); );
useEffect(() => {
const timer = setInterval(() => sendSensorData(), 30000);
return () => {
clearInterval(timer);
};
});
useLayoutTitle(LL.SENSORS()); useLayoutTitle(LL.SENSORS());
const formatDurationMin = (duration_min: number) => { const formatDurationMin = (duration_min: number) => {
@@ -276,10 +266,9 @@ const Sensors = () => {
.catch(() => { .catch(() => {
toast.error(LL.UPDATE_OF(LL.SENSOR(2)) + ' ' + LL.FAILED(1)); toast.error(LL.UPDATE_OF(LL.SENSOR(2)) + ' ' + LL.FAILED(1));
}) })
.finally(async () => { .finally(() => {
setTemperatureDialogOpen(false); setTemperatureDialogOpen(false);
setSelectedTemperatureSensor(undefined); setSelectedTemperatureSensor(undefined);
await sendSensorData();
}); });
}; };
@@ -330,10 +319,9 @@ const Sensors = () => {
.catch(() => { .catch(() => {
toast.error(LL.UPDATE_OF(LL.ANALOG_SENSOR(5)) + ' ' + LL.FAILED(1)); toast.error(LL.UPDATE_OF(LL.ANALOG_SENSOR(5)) + ' ' + LL.FAILED(1));
}) })
.finally(async () => { .finally(() => {
setAnalogDialogOpen(false); setAnalogDialogOpen(false);
setSelectedAnalogSensor(undefined); setSelectedAnalogSensor(undefined);
await sendSensorData();
}); });
}; };
@@ -477,8 +465,6 @@ const Sensors = () => {
)} )}
/> />
)} )}
{sensorData?.analog_enabled === true && (
<>
<Typography sx={{ pt: 4, pb: 1 }} variant="h6" color="secondary"> <Typography sx={{ pt: 4, pb: 1 }} variant="h6" color="secondary">
{LL.ANALOG_SENSORS()} {LL.ANALOG_SENSORS()}
</Typography> </Typography>
@@ -498,20 +484,18 @@ const Sensors = () => {
)} )}
/> />
)} )}
</>
)}
<Box mt={1} display="flex" flexWrap="wrap">
{sensorData?.analog_enabled === true && me.admin && ( {sensorData?.analog_enabled === true && me.admin && (
<Box mt={1} display="flex" flexWrap="wrap" justifyContent="flex-end">
<Button <Button
variant="outlined" variant="outlined"
color="primary" color="primary"
startIcon={<AddCircleOutlineOutlinedIcon />} startIcon={<AddCircleOutlineOutlinedIcon />}
onClick={addAnalogSensor} onClick={addAnalogSensor}
> >
{LL.ADD(0) + ' ' + LL.ANALOG_SENSOR(1)} {LL.ADD(0)}
</Button> </Button>
)}
</Box> </Box>
)}
</SectionContent> </SectionContent>
); );
}; };

View File

@@ -220,8 +220,8 @@ const ManageUsers = () => {
)} )}
<Box display="flex" flexWrap="wrap"> <Box display="flex" flexWrap="wrap">
<Box flexGrow={1} sx={{ '& button': { mt: 2 } }}>
{changed !== 0 && ( {changed !== 0 && (
<Box flexGrow={1} sx={{ '& button': { mt: 2 } }}>
<ButtonRow> <ButtonRow>
<Button <Button
startIcon={<CancelIcon />} startIcon={<CancelIcon />}
@@ -244,9 +244,8 @@ const ManageUsers = () => {
{LL.APPLY_CHANGES(changed)} {LL.APPLY_CHANGES(changed)}
</Button> </Button>
</ButtonRow> </ButtonRow>
)}
</Box> </Box>
)}
<Box flexWrap="nowrap" whiteSpace="nowrap"> <Box flexWrap="nowrap" whiteSpace="nowrap">
<ButtonRow> <ButtonRow>
<Button <Button

View File

@@ -1,5 +1,3 @@
import { useEffect } from 'react';
import { import {
Body, Body,
Cell, Cell,
@@ -67,13 +65,6 @@ const SystemActivity = () => {
` `
}); });
useEffect(() => {
const timer = setInterval(() => loadData(), 30000);
return () => {
clearInterval(timer);
};
});
const showName = (id: number) => { const showName = (id: number) => {
const name: keyof Translation['STATUS_NAMES'] = id; const name: keyof Translation['STATUS_NAMES'] = id;
return LL.STATUS_NAMES[name](); return LL.STATUS_NAMES[name]();

View File

@@ -15,9 +15,8 @@ const logger: ResponseHandler = (response, request) => {
const router = AutoRouter({ const router = AutoRouter({
port: 3080, port: 3080,
missing: () => error(404, 'Error, endpoint not found'), missing: () => error(404, 'Error, endpoint not found')
finally: [logger] // finally: [logger]
}); });
const REST_ENDPOINT_ROOT = '/rest/'; const REST_ENDPOINT_ROOT = '/rest/';
@@ -870,7 +869,7 @@ const emsesp_sensordata = {
ts: [ ts: [
{ id: '28-233D-9497-0C03', n: 'Dallas 1', t: 25.7, o: 1.2, u: 1 }, { id: '28-233D-9497-0C03', n: 'Dallas 1', t: 25.7, o: 1.2, u: 1 },
{ id: '28-243D-7437-1E3A', n: 'Dallas 2 outside', t: 26.1, o: 0, u: 1 }, { id: '28-243D-7437-1E3A', n: 'Dallas 2 outside', t: 26.1, o: 0, u: 1 },
{ id: '28-243E-7437-1E3B', n: 'Zolder', t: 27.1, o: 0, u: 16 }, { id: '28-243E-7437-1E3B', n: 'Zolder', t: 27.1, o: 0, u: 1 },
{ id: '28-183D-1892-0C33', n: 'Roof', o: 2, u: 1 } // no temperature { id: '28-183D-1892-0C33', n: 'Roof', o: 2, u: 1 } // no temperature
], ],
// as: [], // as: [],
@@ -4285,6 +4284,8 @@ function deviceData(id: number) {
return new Response(encoder.encode(emsesp_devicedata_7), { headers }); return new Response(encoder.encode(emsesp_devicedata_7), { headers });
} }
if (id == 8) { if (id == 8) {
// test changing the selected flow temp on a Bosch Compress 7000i AW Heat Pump (Boiler/HP)
emsesp_devicedata_8.data[4].v = Math.floor(Math.random() * 100);
return new Response(encoder.encode(emsesp_devicedata_8), { headers }); return new Response(encoder.encode(emsesp_devicedata_8), { headers });
} }
if (id == 9) { if (id == 9) {
@@ -4352,12 +4353,14 @@ router
sorted_devices.push(emsesp_coredata_custom); sorted_devices.push(emsesp_coredata_custom);
return { connected: true, devices: sorted_devices }; return { connected: true, devices: sorted_devices };
}) })
.get(EMSESP_SENSOR_DATA_ENDPOINT, () => emsesp_sensordata) .get(EMSESP_SENSOR_DATA_ENDPOINT, () => {
// random change the zolder temperature 0-100
emsesp_sensordata.ts[2].t = Math.floor(Math.random() * 100);
return emsesp_sensordata })
.get(EMSESP_DEVICES_ENDPOINT, () => { .get(EMSESP_DEVICES_ENDPOINT, () => {
// sort by type // sort by type
const sorted_devices = emsesp_devices.devices.sort((a, b) => a.t - b.t); const sorted_devices = emsesp_devices.devices.sort((a, b) => a.t - b.t);
return { devices: sorted_devices }; return { devices: sorted_devices };
}) })
.get(EMSESP_DEVICEDATA_ENDPOINT1, (request) => .get(EMSESP_DEVICEDATA_ENDPOINT1, (request) =>
request.query.id ? deviceData(Number(request.query.id)) : status(404) request.query.id ? deviceData(Number(request.query.id)) : status(404)