mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2026-06-14 11:56:32 +03:00
call scheduler immediate from dashboard
This commit is contained in:
@@ -7,6 +7,7 @@ import ChevronRightIcon from '@mui/icons-material/ChevronRight';
|
||||
import EditIcon from '@mui/icons-material/Edit';
|
||||
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
|
||||
import HelpOutlineIcon from '@mui/icons-material/HelpOutlined';
|
||||
import PlayArrowIcon from '@mui/icons-material/PlayArrow';
|
||||
import UnfoldLessIcon from '@mui/icons-material/UnfoldLess';
|
||||
import UnfoldMoreIcon from '@mui/icons-material/UnfoldMore';
|
||||
import {
|
||||
@@ -75,24 +76,25 @@ const Dashboard = memo(() => {
|
||||
{
|
||||
immediate: false
|
||||
}
|
||||
);
|
||||
)
|
||||
.onSuccess(() => {
|
||||
toast.success(LL.WRITE_CMD_SENT());
|
||||
})
|
||||
.onError((error) => {
|
||||
toast.error(String(error.error?.message || 'An error occurred'));
|
||||
});
|
||||
|
||||
const deviceValueDialogSave = async (devicevalue: DeviceValue) => {
|
||||
if (!selectedDashboardItem) {
|
||||
return;
|
||||
}
|
||||
const id = selectedDashboardItem.parentNode.id; // this is the parent ID
|
||||
await sendDeviceValue({ id, c: devicevalue.c ?? '', v: devicevalue.v })
|
||||
.then(() => {
|
||||
toast.success(LL.WRITE_CMD_SENT());
|
||||
})
|
||||
.catch((error: Error) => {
|
||||
toast.error(error.message);
|
||||
})
|
||||
.finally(() => {
|
||||
setDeviceValueDialogOpen(false);
|
||||
setSelectedDashboardItem(undefined);
|
||||
});
|
||||
// skip if we're executing an immediate schedule
|
||||
if (devicevalue.v !== undefined) {
|
||||
const id = selectedDashboardItem.parentNode.id; // this is the parent ID
|
||||
await sendDeviceValue({ id, c: devicevalue.c ?? '', v: devicevalue.v });
|
||||
}
|
||||
setDeviceValueDialogOpen(false);
|
||||
setSelectedDashboardItem(undefined);
|
||||
};
|
||||
|
||||
const dashboard_theme = useTheme({
|
||||
@@ -210,7 +212,12 @@ const Dashboard = memo(() => {
|
||||
(parseInt(id.slice(0, 2), 16) & mask) === mask;
|
||||
|
||||
const editDashboardValue = (di: DashboardItem) => {
|
||||
if (me.admin && di.dv?.c) {
|
||||
// don't execute on parent nodes
|
||||
if (!me.admin || di.id <= 99) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (di.dv?.c) {
|
||||
setSelectedDashboardItem(di);
|
||||
setDeviceValueDialogOpen(true);
|
||||
}
|
||||
@@ -321,7 +328,19 @@ const Dashboard = memo(() => {
|
||||
<Cell>
|
||||
{me.admin &&
|
||||
di.dv?.c &&
|
||||
!hasMask(di.dv.id, DeviceEntityMask.DV_READONLY) && (
|
||||
!hasMask(di.dv.id, DeviceEntityMask.DV_READONLY) &&
|
||||
(di.dv.v === '' || di.dv.v === undefined ? (
|
||||
<IconButton
|
||||
size="small"
|
||||
aria-label={LL.RUN_COMMAND()}
|
||||
onClick={() => editDashboardValue(di)}
|
||||
>
|
||||
<PlayArrowIcon
|
||||
color="primary"
|
||||
sx={{ fontSize: 16 }}
|
||||
/>
|
||||
</IconButton>
|
||||
) : (
|
||||
<IconButton
|
||||
size="small"
|
||||
aria-label={
|
||||
@@ -334,7 +353,7 @@ const Dashboard = memo(() => {
|
||||
sx={{ fontSize: 16 }}
|
||||
/>
|
||||
</IconButton>
|
||||
)}
|
||||
))}
|
||||
</Cell>
|
||||
</>
|
||||
) : (
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { toast } from 'react-toastify';
|
||||
|
||||
import CancelIcon from '@mui/icons-material/Cancel';
|
||||
import WarningIcon from '@mui/icons-material/Warning';
|
||||
@@ -18,7 +19,9 @@ import {
|
||||
Typography
|
||||
} from '@mui/material';
|
||||
|
||||
import { callAction } from '@/api/app';
|
||||
import { dialogStyle } from 'CustomTheme';
|
||||
import { useRequest } from 'alova/client';
|
||||
import type Schema from 'async-validator';
|
||||
import type { ValidateFieldsError } from 'async-validator';
|
||||
import { ValidatedTextField } from 'components';
|
||||
@@ -61,10 +64,25 @@ const DevicesDialog = ({
|
||||
}
|
||||
}, [open, selectedItem]);
|
||||
|
||||
const save = async () => {
|
||||
const { send: executeSchedule } = useRequest(
|
||||
(id: string) => callAction({ action: 'executeSchedule', param: id }),
|
||||
{ immediate: false }
|
||||
)
|
||||
.onSuccess(() => {
|
||||
toast.success(LL.EXECUTE_SCHEDULE_SENT());
|
||||
})
|
||||
.onError((error) => {
|
||||
toast.error(String(error.error?.message || 'An error occurred'));
|
||||
});
|
||||
|
||||
const doAction = async () => {
|
||||
try {
|
||||
setFieldErrors(undefined);
|
||||
await validate(validator, editItem);
|
||||
if (editItem.v === undefined && editItem.c !== undefined) {
|
||||
await executeSchedule(editItem.c);
|
||||
} else {
|
||||
await validate(validator, editItem);
|
||||
}
|
||||
onSave(editItem);
|
||||
} catch (error) {
|
||||
setFieldErrors((error as ValidationError).fieldErrors);
|
||||
@@ -100,9 +118,14 @@ const DevicesDialog = ({
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const isCommand = selectedItem.v === '' && selectedItem.c;
|
||||
const isCommand =
|
||||
(selectedItem.v === '' || selectedItem.v === undefined) &&
|
||||
Boolean(selectedItem.c);
|
||||
const isSchedulerImmediate = selectedItem.v === undefined;
|
||||
const dialogTitle = isCommand
|
||||
? LL.RUN_COMMAND()
|
||||
? isSchedulerImmediate
|
||||
? LL.EXECUTE() + ' ' + LL.SCHEDULE(0)
|
||||
: LL.RUN_COMMAND()
|
||||
: writeable
|
||||
? LL.CHANGE_VALUE()
|
||||
: LL.VALUE(0);
|
||||
@@ -118,67 +141,69 @@ const DevicesDialog = ({
|
||||
<Typography sx={{ mb: 2 }} color="warning" variant="body2">
|
||||
{editItem.id.slice(2)}
|
||||
</Typography>
|
||||
<Grid container>
|
||||
<Grid size={12}>
|
||||
{editItem.l ? (
|
||||
<TextField
|
||||
name="v"
|
||||
value={editItem.v}
|
||||
aria-label={valueLabel}
|
||||
disabled={!writeable}
|
||||
sx={{ width: '30ch' }}
|
||||
select
|
||||
onChange={updateFormValue}
|
||||
>
|
||||
{editItem.l.map((val) => (
|
||||
<MenuItem value={val} key={val}>
|
||||
{val}
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? (
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors || {}}
|
||||
name="v"
|
||||
label={valueLabel}
|
||||
value={numberValue(Math.round((editItem.v as number) * 10) / 10)}
|
||||
autoFocus
|
||||
disabled={!writeable}
|
||||
type="number"
|
||||
sx={{ width: '30ch' }}
|
||||
onChange={updateFormValue}
|
||||
slotProps={{
|
||||
htmlInput: editItem.s
|
||||
? { min: editItem.m, max: editItem.x, step: editItem.s }
|
||||
: {},
|
||||
input: {
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
{setUom(editItem.u)}
|
||||
</InputAdornment>
|
||||
)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors || {}}
|
||||
name="v"
|
||||
label={valueLabel}
|
||||
value={editItem.v}
|
||||
disabled={!writeable}
|
||||
sx={{ width: '30ch' }}
|
||||
multiline={!editItem.u}
|
||||
onChange={updateFormValue}
|
||||
/>
|
||||
{!isSchedulerImmediate && (
|
||||
<Grid container>
|
||||
<Grid size={12}>
|
||||
{editItem.l ? (
|
||||
<TextField
|
||||
name="v"
|
||||
value={editItem.v}
|
||||
aria-label={valueLabel}
|
||||
disabled={!writeable}
|
||||
sx={{ width: '30ch' }}
|
||||
select
|
||||
onChange={updateFormValue}
|
||||
>
|
||||
{editItem.l.map((val) => (
|
||||
<MenuItem value={val} key={val}>
|
||||
{val}
|
||||
</MenuItem>
|
||||
))}
|
||||
</TextField>
|
||||
) : editItem.s || editItem.u !== DeviceValueUOM.NONE ? (
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors || {}}
|
||||
name="v"
|
||||
label={valueLabel}
|
||||
value={numberValue(Math.round((editItem.v as number) * 10) / 10)}
|
||||
autoFocus
|
||||
disabled={!writeable}
|
||||
type="number"
|
||||
sx={{ width: '30ch' }}
|
||||
onChange={updateFormValue}
|
||||
slotProps={{
|
||||
htmlInput: editItem.s
|
||||
? { min: editItem.m, max: editItem.x, step: editItem.s }
|
||||
: {},
|
||||
input: {
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
{setUom(editItem.u)}
|
||||
</InputAdornment>
|
||||
)
|
||||
}
|
||||
}}
|
||||
/>
|
||||
) : (
|
||||
<ValidatedTextField
|
||||
fieldErrors={fieldErrors || {}}
|
||||
name="v"
|
||||
label={valueLabel}
|
||||
value={editItem.v}
|
||||
disabled={!writeable}
|
||||
sx={{ width: '30ch' }}
|
||||
multiline={!editItem.u}
|
||||
onChange={updateFormValue}
|
||||
/>
|
||||
)}
|
||||
</Grid>
|
||||
{writeable && helperText && (
|
||||
<Grid>
|
||||
<FormHelperText>{helperText}</FormHelperText>
|
||||
</Grid>
|
||||
)}
|
||||
</Grid>
|
||||
{writeable && helperText && (
|
||||
<Grid>
|
||||
<FormHelperText>{helperText}</FormHelperText>
|
||||
</Grid>
|
||||
)}
|
||||
</Grid>
|
||||
)}
|
||||
</DialogContent>
|
||||
|
||||
<DialogActions>
|
||||
@@ -202,7 +227,7 @@ const DevicesDialog = ({
|
||||
<Button
|
||||
startIcon={<WarningIcon color="warning" />}
|
||||
variant="outlined"
|
||||
onClick={save}
|
||||
onClick={doAction}
|
||||
color="primary"
|
||||
>
|
||||
{buttonLabel}
|
||||
|
||||
@@ -134,9 +134,13 @@ const SchedulerDialog = ({
|
||||
const { send: executeSchedule } = useRequest(
|
||||
(id: string) => callAction({ action: 'executeSchedule', param: id }),
|
||||
{ immediate: false }
|
||||
).onError((error) => {
|
||||
toast.error(String(error.error?.message || 'An error occurred'));
|
||||
});
|
||||
)
|
||||
.onSuccess(() => {
|
||||
toast.success(LL.EXECUTE_SCHEDULE_SENT());
|
||||
})
|
||||
.onError((error) => {
|
||||
toast.error(String(error.error?.message || 'An error occurred'));
|
||||
});
|
||||
|
||||
const execute = async () => {
|
||||
await executeSchedule(editItem.name);
|
||||
|
||||
Reference in New Issue
Block a user