diff --git a/interface/src/AuthenticatedRouting.tsx b/interface/src/AuthenticatedRouting.tsx index 1bab479ae..ae4eeb1b6 100644 --- a/interface/src/AuthenticatedRouting.tsx +++ b/interface/src/AuthenticatedRouting.tsx @@ -1,6 +1,7 @@ import { memo, useContext } from 'react'; import { Navigate, Route, Routes } from 'react-router'; +import Commands from 'app/main/Commands'; import CustomEntities from 'app/main/CustomEntities'; import Customizations from 'app/main/Customizations'; import Dashboard from 'app/main/Dashboard'; @@ -65,6 +66,7 @@ const AuthenticatedRouting = memo(() => { } /> } /> + } /> } /> } /> diff --git a/interface/src/api/app.ts b/interface/src/api/app.ts index 668e61904..e352b8692 100644 --- a/interface/src/api/app.ts +++ b/interface/src/api/app.ts @@ -4,6 +4,8 @@ import type { APIcall, Action, Activity, + CommandItem, + Commands, CoreData, DashboardData, DeviceData, @@ -102,8 +104,7 @@ export const readSchedule = () => o_deleted: si.deleted, o_flags: si.flags, o_time: si.time, - o_cmd: si.cmd, - o_value: si.value, + o_cmd_name: si.cmd_name, o_name: si.name })); } @@ -111,6 +112,24 @@ export const readSchedule = () => export const writeSchedule = (data: Schedule) => alovaInstance.Post('/rest/schedule', data); +// Commands +export const readCommands = () => + alovaInstance.Get('/rest/commands', { + // @ts-expect-error - exactOptionalPropertyTypes compatibility issue + transform(data) { + const commands = (data as Commands).commands; + return commands.map((ci) => ({ + ...ci, + o_id: ci.id, + o_cmd: ci.cmd, + o_value: ci.value, + o_name: ci.name + })); + } + }); +export const writeCommands = (data: Commands) => + alovaInstance.Post('/rest/commands', data); + // Modules export const readModules = () => alovaInstance.Get('/rest/modules', { diff --git a/interface/src/app/main/Commands.tsx b/interface/src/app/main/Commands.tsx new file mode 100644 index 000000000..495d8634d --- /dev/null +++ b/interface/src/app/main/Commands.tsx @@ -0,0 +1,284 @@ +import { useEffect, useState } from 'react'; +import { useBlocker } from 'react-router'; +import { toast } from 'react-toastify'; + +import AddIcon from '@mui/icons-material/Add'; +import CancelIcon from '@mui/icons-material/Cancel'; +import WarningIcon from '@mui/icons-material/Warning'; +import { Box, Button, Typography } from '@mui/material'; + +import { + Body, + Cell, + Header, + HeaderCell, + HeaderRow, + Row, + Table +} from '@table-library/react-table-library/table'; +import { useTheme } from '@table-library/react-table-library/theme'; +import { updateState, useRequest } from 'alova/client'; +import { + BlockNavigation, + ButtonRow, + FormLoader, + SectionContent, + useLayoutTitle +} from 'components'; +import { useI18nContext } from 'i18n/i18n-react'; +import { useInterval } from 'utils'; + +import { readCommands, writeCommands } from '../../api/app'; +import CommandsDialog from './CommandsDialog'; +import type { CommandItem, Commands as CommandsType } from './types'; +import { commandItemValidation } from './validators'; + +const INTERVAL_DELAY = 30000; +const MIN_ID = -100; +const MAX_ID = 100; + +const DEFAULT_COMMAND_ITEM: Omit = { + cmd: '', + value: '', + name: '', + deleted: false +}; + +const commandsTheme = { + Table: ` + --data-table-library_grid-template-columns: repeat(1, minmax(100px, 1fr)) repeat(1, minmax(100px, 1fr)) 160px; + `, + BaseRow: ` + font-size: 14px; + .td { + height: 32px; + } + `, + BaseCell: ` + &:nth-of-type(1) { + padding: 8px; + } + `, + HeaderRow: ` + text-transform: uppercase; + background-color: black; + color: #90CAF9; + .th { + border-bottom: 1px solid #565656; + height: 36px; + } + `, + Row: ` + background-color: #1e1e1e; + position: relative; + cursor: pointer; + .td { + border-bottom: 1px solid #565656; + } + &:hover .td { + background-color: #177ac9; + } + ` +}; + +const CommandsPage = () => { + const { LL } = useI18nContext(); + const [numChanges, setNumChanges] = useState(0); + const blocker = useBlocker(numChanges !== 0); + const [selectedItem, setSelectedItem] = useState(); + const [creating, setCreating] = useState(false); + const [dialogOpen, setDialogOpen] = useState(false); + + useLayoutTitle(LL.COMMANDS()); + + const { + data: commands, + send: fetchCommands, + error + } = useRequest(readCommands, { + initialData: [] + }); + + const { send: updateCommands } = useRequest( + (data: CommandsType) => writeCommands(data), + { immediate: false } + ); + + const hasChanged = (ci: CommandItem) => + ci.id !== ci.o_id || + (ci.name || '') !== (ci.o_name || '') || + ci.cmd !== ci.o_cmd || + ci.value !== ci.o_value || + ci.deleted !== ci.o_deleted; + + useInterval(() => { + if (numChanges === 0) { + void fetchCommands(); + } + }, INTERVAL_DELAY); + + const theme = useTheme(commandsTheme); + + const saveCommands = async () => { + try { + await updateCommands({ + commands: commands + .filter((ci: CommandItem) => !ci.deleted) + .map((ci: CommandItem) => ({ + id: ci.id, + cmd: ci.cmd, + value: ci.value, + name: ci.name + })) + }); + toast.success(LL.UPDATED_OF(LL.COMMANDS(1))); + } catch (error: unknown) { + const message = error instanceof Error ? error.message : String(error); + toast.error(message); + } finally { + await fetchCommands(); + setNumChanges(0); + } + }; + + const editItem = (ci: CommandItem) => { + setCreating(false); + setSelectedItem(ci); + setDialogOpen(true); + if (ci.o_name === undefined) { + ci.o_name = ci.name; + } + }; + + const onDialogClose = () => { + setDialogOpen(false); + }; + + const onDialogCancel = async () => { + await fetchCommands().then(() => { + setNumChanges(0); + }); + }; + + const onDialogSave = (updatedItem: CommandItem) => { + setDialogOpen(false); + void updateState(readCommands(), (data: CommandItem[]) => { + const new_data = creating + ? [...data, updatedItem] + : data.map((ci) => + ci.id === updatedItem.id ? { ...ci, ...updatedItem } : ci + ); + setNumChanges(new_data.filter((ci) => hasChanged(ci)).length); + return new_data; + }); + }; + + const addItem = () => { + setCreating(true); + const newItem: CommandItem = { + id: Math.floor(Math.random() * (MAX_ID - MIN_ID) + MIN_ID), + ...DEFAULT_COMMAND_ITEM + }; + setSelectedItem(newItem); + setDialogOpen(true); + }; + + const filteredCommands = commands.filter((ci: CommandItem) => !ci.deleted); + + const renderCommands = () => { + if (!commands) { + return ( + + ); + } + + return ( + + {(tableList: CommandItem[]) => ( + <> +
+ + {LL.COMMAND(0)} + {LL.VALUE(0)} + {LL.NAME(0)} + +
+ + {tableList.map((ci: CommandItem) => ( + editItem(ci)}> + {ci.cmd} + {ci.value} + {ci.name} + + ))} + + + )} +
+ ); + }; + + return ( + + {blocker ? : null} + + {LL.COMMANDS_HELP_1()}. + + {renderCommands()} + + {selectedItem && ( + + )} + + + + {numChanges !== 0 && ( + + + + + )} + + + + + + + + + ); +}; + +export default CommandsPage; diff --git a/interface/src/app/main/CommandsDialog.tsx b/interface/src/app/main/CommandsDialog.tsx new file mode 100644 index 000000000..1d71995cc --- /dev/null +++ b/interface/src/app/main/CommandsDialog.tsx @@ -0,0 +1,188 @@ +import { useEffect, useState } from 'react'; +import { toast } from 'react-toastify'; + +import AddIcon from '@mui/icons-material/Add'; +import CancelIcon from '@mui/icons-material/Cancel'; +import DoneIcon from '@mui/icons-material/Done'; +import PlayArrowIcon from '@mui/icons-material/PlayArrow'; +import RemoveIcon from '@mui/icons-material/RemoveCircleOutlined'; +import { + Box, + Button, + Dialog, + DialogActions, + DialogContent, + DialogTitle, + TextField +} 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'; +import { useI18nContext } from 'i18n/i18n-react'; +import { updateValue } from 'utils'; +import { ValidationError, validate } from 'validators'; + +import type { CommandItem } from './types'; + +interface CommandsDialogProps { + open: boolean; + creating: boolean; + onClose: () => void; + onSave: (ci: CommandItem) => void; + selectedItem: CommandItem; + validator: Schema; +} + +const CommandsDialog = ({ + open, + creating, + onClose, + onSave, + selectedItem, + validator +}: CommandsDialogProps) => { + const { LL } = useI18nContext(); + const [editItem, setEditItem] = useState(selectedItem); + const [fieldErrors, setFieldErrors] = useState(); + + const updateFormValue = updateValue( + setEditItem as unknown as React.Dispatch< + React.SetStateAction> + > + ); + + useEffect(() => { + if (open) { + setFieldErrors(undefined); + setEditItem(selectedItem); + } + }, [open, selectedItem]); + + const handleSave = async (itemToSave: CommandItem) => { + try { + setFieldErrors(undefined); + await validate(validator, itemToSave); + onSave(itemToSave); + } catch (error) { + setFieldErrors((error as ValidationError).fieldErrors); + } + }; + + const save = async () => { + await handleSave(editItem); + }; + + const { send: executeCommand } = useRequest( + (id: string) => callAction({ action: 'executeCommand', param: id }), + { immediate: false } + ) + .onSuccess(() => { + toast.success(LL.EXECUTE_COMMAND_SENT()); + }) + .onError((error) => { + toast.error(String(error.error?.message || 'An error occurred')); + }); + + const execute = async () => { + await executeCommand(editItem.name); + }; + + const remove = () => { + onSave({ ...editItem, deleted: true }); + }; + + const handleClose = ( + _event: React.SyntheticEvent, + reason: 'backdropClick' | 'escapeKeyDown' + ) => { + if (reason !== 'backdropClick') { + onClose(); + } + }; + + return ( + + + {creating ? `${LL.ADD(1)} ${LL.NEW(0)}` : LL.EDIT()}  + {LL.COMMAND(1)} + + + + + + + + + {!creating && ( + + + + )} + + + {!creating && editItem.cmd !== '' && ( + + )} + + + ); +}; + +export default CommandsDialog; diff --git a/interface/src/app/main/Dashboard.tsx b/interface/src/app/main/Dashboard.tsx index f404ed3f7..e90cbca19 100644 --- a/interface/src/app/main/Dashboard.tsx +++ b/interface/src/app/main/Dashboard.tsx @@ -180,6 +180,8 @@ const Dashboard = memo(() => { return LL.ANALOG_SENSORS(); case DeviceType.TEMPERATURESENSOR: return LL.TEMP_SENSORS(); + case DeviceType.COMMAND: + return LL.COMMANDS(); case DeviceType.SCHEDULER: return LL.SCHEDULER(); default: diff --git a/interface/src/app/main/DeviceIcon.tsx b/interface/src/app/main/DeviceIcon.tsx index 79207ff37..1266e9b55 100644 --- a/interface/src/app/main/DeviceIcon.tsx +++ b/interface/src/app/main/DeviceIcon.tsx @@ -38,6 +38,7 @@ const deviceIconLookup: Record = { [DeviceType.CUSTOM]: MdPlaylistAdd, [DeviceType.UNKNOWN]: MdOutlineSensors, [DeviceType.SYSTEM]: null, + [DeviceType.COMMAND]: MdPlaylistAdd, [DeviceType.SCHEDULER]: MdMoreTime, [DeviceType.GENERIC]: MdOutlineSensors, [DeviceType.VENTILATION]: PiFan diff --git a/interface/src/app/main/DevicesDialog.tsx b/interface/src/app/main/DevicesDialog.tsx index ce0526292..314476024 100644 --- a/interface/src/app/main/DevicesDialog.tsx +++ b/interface/src/app/main/DevicesDialog.tsx @@ -64,12 +64,12 @@ const DevicesDialog = ({ } }, [open, selectedItem]); - const { send: executeSchedule } = useRequest( - (id: string) => callAction({ action: 'executeSchedule', param: id }), + const { send: executeCommand } = useRequest( + (id: string) => callAction({ action: 'executeCommand', param: id }), { immediate: false } ) .onSuccess(() => { - toast.success(LL.EXECUTE_SCHEDULE_SENT()); + toast.success(LL.EXECUTE_COMMAND_SENT()); }) .onError((error) => { toast.error(String(error.error?.message || 'An error occurred')); @@ -79,7 +79,7 @@ const DevicesDialog = ({ try { setFieldErrors(undefined); if (editItem.v === undefined && editItem.c !== undefined) { - await executeSchedule(editItem.c); + await executeCommand(editItem.c); } else { await validate(validator, editItem); } diff --git a/interface/src/app/main/Scheduler.tsx b/interface/src/app/main/Scheduler.tsx index 1bb612854..e30a54e89 100644 --- a/interface/src/app/main/Scheduler.tsx +++ b/interface/src/app/main/Scheduler.tsx @@ -29,7 +29,7 @@ import { import { useI18nContext } from 'i18n/i18n-react'; import { useInterval } from 'utils'; -import { readSchedule, writeSchedule } from '../../api/app'; +import { readCommands, readSchedule, writeSchedule } from '../../api/app'; import SettingsSchedulerDialog from './SchedulerDialog'; import { ScheduleFlag } from './types'; import type { Schedule, ScheduleItem } from './types'; @@ -54,14 +54,13 @@ const DEFAULT_SCHEDULE_ITEM: Omit = { deleted: false, flags: FLAG_ALL_DAYS, time: '', - cmd: '', - value: '', + cmd_name: '', name: '' }; const scheduleTheme = { Table: ` - --data-table-library_grid-template-columns: 36px 210px 100px 192px repeat(1, minmax(100px, 1fr)) 160px; + --data-table-library_grid-template-columns: 36px 220px repeat(1, minmax(20px, 1fr)) 192px 160px; `, BaseRow: ` font-size: 14px; @@ -70,11 +69,8 @@ const scheduleTheme = { } `, BaseCell: ` - &:nth-of-type(2) { - text-align: center; - } &:nth-of-type(1) { - text-align: center; + text-align: 8px; } `, HeaderRow: ` @@ -100,7 +96,6 @@ const scheduleTheme = { }; const scheduleTypeLabels: Record = { - [ScheduleFlag.SCHEDULE_IMMEDIATE]: 'Immediate', [ScheduleFlag.SCHEDULE_TIMER]: 'Timer', [ScheduleFlag.SCHEDULE_CONDITION]: 'Condition', [ScheduleFlag.SCHEDULE_ONCHANGE]: 'On Change' @@ -125,6 +120,11 @@ const Scheduler = () => { initialData: [] }); + const { data: commandNames } = useRequest(readCommands, { + initialData: [], + initializing: true + }); + const { send: updateSchedule } = useRequest( (data: Schedule) => writeSchedule(data), { @@ -140,8 +140,7 @@ const Scheduler = () => { si.deleted !== si.o_deleted || si.flags !== si.o_flags || si.time !== si.o_time || - si.cmd !== si.o_cmd || - si.value !== si.o_value + si.cmd_name !== si.o_cmd_name ); }; @@ -177,8 +176,7 @@ const Scheduler = () => { active: condensed_si.active, flags: condensed_si.flags, time: condensed_si.time, - cmd: condensed_si.cmd, - value: condensed_si.value, + cmd_name: condensed_si.cmd_name, name: condensed_si.name })) }); @@ -289,7 +287,6 @@ const Scheduler = () => { {LL.SCHEDULE(0)} {LL.TIME(0)}/Cond. {LL.COMMAND(0)} - {LL.VALUE(0)} {LL.NAME(0)} @@ -297,12 +294,10 @@ const Scheduler = () => { {tableList.map((si: ScheduleItem) => ( editScheduleItem(si)}> - {si.flags !== ScheduleFlag.SCHEDULE_IMMEDIATE && ( - - )} + @@ -322,8 +317,7 @@ const Scheduler = () => { {si.time} - {si.cmd} - {si.value} + {si.cmd_name} {si.name} ))} @@ -351,6 +345,7 @@ const Scheduler = () => { selectedItem={selectedScheduleItem} validator={schedulerItemValidation(schedule, selectedScheduleItem)} dow={dow} + commandNames={commandNames.map((ci) => ci.name)} /> )} diff --git a/interface/src/app/main/SchedulerDialog.tsx b/interface/src/app/main/SchedulerDialog.tsx index 8e1be27b5..bd124b9eb 100644 --- a/interface/src/app/main/SchedulerDialog.tsx +++ b/interface/src/app/main/SchedulerDialog.tsx @@ -1,10 +1,8 @@ import { useEffect, useState } from 'react'; -import { toast } from 'react-toastify'; import AddIcon from '@mui/icons-material/Add'; import CancelIcon from '@mui/icons-material/Cancel'; import DoneIcon from '@mui/icons-material/Done'; -import PlayArrowIcon from '@mui/icons-material/PlayArrow'; import RemoveIcon from '@mui/icons-material/RemoveCircleOutlined'; import { Box, @@ -15,15 +13,14 @@ import { DialogContent, DialogTitle, Grid, + MenuItem, TextField, ToggleButton, ToggleButtonGroup, 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 { BlockFormControlLabel, ValidatedTextField } from 'components'; @@ -77,6 +74,7 @@ interface SchedulerDialogProps { selectedItem: ScheduleItem; validator: Schema; dow: string[]; + commandNames: string[]; } const SchedulerDialog = ({ @@ -86,7 +84,8 @@ const SchedulerDialog = ({ onSave, selectedItem, validator, - dow + dow, + commandNames }: SchedulerDialogProps) => { const { LL } = useI18nContext(); const [editItem, setEditItem] = useState(selectedItem); @@ -103,12 +102,6 @@ const SchedulerDialog = ({ if (open) { setFieldErrors(undefined); setEditItem(selectedItem); - // Set the flags based on type when page is loaded: - // 0-127 is day schedule - // 128 is timer - // 129 is on change - // 130 is on condition - // 132 is immediate setScheduleType( selectedItem.flags <= SCHEDULE_TYPE_THRESHOLD ? ScheduleFlag.SCHEDULE_DAY @@ -131,21 +124,6 @@ const SchedulerDialog = ({ await handleSave(editItem); }; - 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 execute = async () => { - await executeSchedule(editItem.name); - }; - const remove = () => { onSave({ ...editItem, deleted: true }); }; @@ -197,7 +175,6 @@ const SchedulerDialog = ({ const isDaySchedule = scheduleType === ScheduleFlag.SCHEDULE_DAY; const isTimerSchedule = scheduleType === ScheduleFlag.SCHEDULE_TIMER; - const isImmediateSchedule = scheduleType === ScheduleFlag.SCHEDULE_IMMEDIATE; const needsTimeField = isDaySchedule || isTimerSchedule; const dowFlags = getFlagDOWstring(editItem.flags); @@ -214,7 +191,6 @@ const SchedulerDialog = ({ if (scheduleType === ScheduleFlag.SCHEDULE_TIMER) return LL.TIMER(1); if (scheduleType === ScheduleFlag.SCHEDULE_CONDITION) return LL.CONDITION(); if (scheduleType === ScheduleFlag.SCHEDULE_ONCHANGE) return LL.ONCHANGE(); - if (scheduleType === ScheduleFlag.SCHEDULE_IMMEDIATE) return LL.IMMEDIATE(); return LL.TIME(1); })(); @@ -269,14 +245,6 @@ const SchedulerDialog = ({ {LL.CONDITION()} - - - {LL.IMMEDIATE()} - - {isDaySchedule && ( @@ -294,74 +262,66 @@ const SchedulerDialog = ({ )} - {!isImmediateSchedule && ( - <> - - - } - label={LL.ACTIVE()} + + - - - {needsTimeField ? ( - <> - - {isTimerSchedule && ( - - {LL.SCHEDULER_HELP_2()} - - )} - - ) : ( - + } + label={LL.ACTIVE()} + /> + + + {needsTimeField ? ( + <> + + {isTimerSchedule && ( + + {LL.SCHEDULER_HELP_2()} + )} - - - )} - + + ) : ( + + )} + + > + {commandNames.map((name) => ( + + {name} + + ))} + {creating ? LL.ADD(0) : LL.UPDATE()} - {isImmediateSchedule && !creating && editItem.cmd !== '' && ( - - )} ); diff --git a/interface/src/app/main/types.ts b/interface/src/app/main/types.ts index 391c22a01..137078120 100644 --- a/interface/src/app/main/types.ts +++ b/interface/src/app/main/types.ts @@ -354,16 +354,14 @@ export interface ScheduleItem { deleted?: boolean; flags: number; time: string; // also used for Condition and On Change - cmd: string; - value: string; + cmd_name: string; // references a named Command name: string; o_id?: number; o_active?: boolean; o_deleted?: boolean; o_flags?: number; o_time?: string; - o_cmd?: string; - o_value?: string; + o_cmd_name?: string; o_name?: string; } @@ -371,6 +369,23 @@ export interface Schedule { readonly schedule: readonly ScheduleItem[]; } +export interface CommandItem { + id: number; + cmd: string; + value: string; + name: string; + deleted?: boolean; + o_id?: number; + o_cmd?: string; + o_value?: string; + o_name?: string; + o_deleted?: boolean; +} + +export interface Commands { + readonly commands: readonly CommandItem[]; +} + export interface ModuleItem { id: number; // unique index key: string; @@ -401,8 +416,7 @@ export enum ScheduleFlag { SCHEDULE_DAY = 0, // no bits set SCHEDULE_TIMER = 128, // bit 8 SCHEDULE_ONCHANGE = 129, // bit 1 - SCHEDULE_CONDITION = 130, // bit 2 - SCHEDULE_IMMEDIATE = 132 // bit 3 + SCHEDULE_CONDITION = 130 // bit 2 } export interface EntityItem { @@ -445,6 +459,7 @@ export const enum DeviceType { ANALOGSENSOR = 2, SCHEDULER = 3, CUSTOM = 4, + COMMAND = 5, BOILER, THERMOSTAT, MIXER, diff --git a/interface/src/app/main/validators.ts b/interface/src/app/main/validators.ts index 0c87821ea..4bef5f07f 100644 --- a/interface/src/app/main/validators.ts +++ b/interface/src/app/main/validators.ts @@ -4,6 +4,7 @@ import { IP_OR_HOSTNAME_VALIDATOR } from 'validators/shared'; import type { AnalogSensor, + CommandItem, DeviceValue, EntityItem, ScheduleItem, @@ -237,6 +238,24 @@ export const schedulerItemValidation = ( NAME_PATTERN_REQUIRED, uniqueNameValidator(schedule, scheduleItem.o_name) ], + cmd_name: [ + { required: true, message: 'Command is required' } + ] + }); + +export const uniqueCommandNameValidator = (commands: CommandItem[], o_name?: string) => + createUniqueNameValidator(commands, o_name); + +export const commandItemValidation = ( + commands: CommandItem[], + commandItem: CommandItem +) => + new Schema({ + name: [ + { required: true, message: 'Name is required' }, + NAME_PATTERN_REQUIRED, + uniqueCommandNameValidator(commands, commandItem.o_name) + ], cmd: [ { required: true, message: 'Command is required' }, { diff --git a/interface/src/components/layout/LayoutMenu.tsx b/interface/src/components/layout/LayoutMenu.tsx index 5d52921a6..090dadf4c 100644 --- a/interface/src/components/layout/LayoutMenu.tsx +++ b/interface/src/components/layout/LayoutMenu.tsx @@ -7,6 +7,7 @@ import ConstructionIcon from '@mui/icons-material/Construction'; import KeyboardArrowDown from '@mui/icons-material/KeyboardArrowDown'; import LiveHelpIcon from '@mui/icons-material/LiveHelp'; import MoreTimeIcon from '@mui/icons-material/MoreTime'; +import PlaylistPlayIcon from '@mui/icons-material/PlaylistPlay'; import PlaylistAddIcon from '@mui/icons-material/PlaylistAdd'; import SensorsIcon from '@mui/icons-material/Sensors'; import SettingsIcon from '@mui/icons-material/Settings'; @@ -80,6 +81,12 @@ const LayoutMenuComponent = () => { disabled={!me.admin} to={`/customizations`} /> + item.name); + const namedSchedules = emsesp_schedule.schedule.filter((item: any) => item.name); if (namedSchedules.length > 0) { - const scheduler_data = namedSchedules.map((item, index) => ({ + const scheduler_data = namedSchedules.map((item: any, index: number) => ({ id: DeviceTypeUniqueID.SCHEDULER_UID * 100 + index, dv: { id: '00' + item.name, c: item.name, - ...(item.flags === ScheduleFlag.SCHEDULE_IMMEDIATE - ? {} - : { - v: item.active ? 'on' : 'off', - l: ['off', 'on'] - }) + v: item.active ? 'on' : 'off', + l: ['off', 'on'] } })); dashboard_object = { @@ -4788,6 +4814,24 @@ router }; dashboard_nodes.push(dashboard_object); } + + // add the command items (executable from dashboard) + const namedCommands = emsesp_commands.commands.filter((item: any) => item.name); + if (namedCommands.length > 0) { + const command_data = namedCommands.map((item: any, index: number) => ({ + id: DeviceTypeUniqueID.COMMAND_UID * 100 + index, + dv: { + id: '00' + item.name, + c: item.name + } + })); + dashboard_object = { + id: DeviceTypeUniqueID.COMMAND_UID, + t: DeviceType.COMMAND, + nodes: command_data + }; + dashboard_nodes.push(dashboard_object); + } } else { // for testing only // add the custom entity data @@ -4877,6 +4921,15 @@ router return status(200); }) + // Commands + .post(EMSESP_COMMANDS_ENDPOINT, async (request: any) => { + const content = await request.json(); + emsesp_commands = content; + console.log('commands saved', emsesp_commands); + return status(200); + }) + .get(EMSESP_COMMANDS_ENDPOINT, () => emsesp_commands) + // Scheduler .post(EMSESP_SCHEDULE_ENDPOINT, async (request: any) => { const content = await request.json(); @@ -4977,15 +5030,17 @@ router } if (id === DeviceTypeUniqueID.SCHEDULER_UID) { // toggle scheduler - // find the schedule in emsesp_schedule via the name and toggle the active const objIndex = emsesp_schedule.schedule.findIndex( - (obj) => obj.name === command + (obj: any) => obj.name === command ); if (objIndex !== -1) { emsesp_schedule.schedule[objIndex].active = value; console.log("Toggle schedule '" + command + "' to " + value); } } + if (id === DeviceTypeUniqueID.COMMAND_UID) { + console.log("Execute command '" + command + "'"); + } // await delay(1000); // wait to show spinner // console.log( @@ -5246,9 +5301,9 @@ router } else if (action === 'upgradeImportantMessages') { // check upgrade important messages return upgradeImportantMessages(content.param); - } else if (action === 'executeSchedule') { - // execute schedule - return executeSchedule(content.param); + } else if (action === 'executeCommand') { + // execute command + return executeCommand(content.param); } } return status(404); // cmd not found diff --git a/src/core/command.cpp b/src/core/command.cpp index 39cc17c1a..264509dfb 100644 --- a/src/core/command.cpp +++ b/src/core/command.cpp @@ -716,6 +716,10 @@ bool Command::device_has_commands(const uint8_t device_type) { return true; } + if (device_type == EMSdevice::DeviceType::COMMAND) { + return true; + } + if (device_type == EMSdevice::DeviceType::CUSTOM) { return true; } @@ -741,6 +745,7 @@ bool Command::device_has_commands(const uint8_t device_type) { void Command::show_devices(uuid::console::Shell & shell) { shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SYSTEM)); shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::CUSTOM)); + shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::COMMAND)); shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::SCHEDULER)); if (EMSESP::sensor_enabled()) { shell.printf("%s ", EMSdevice::device_type_2_device_name(EMSdevice::DeviceType::TEMPERATURESENSOR)); @@ -779,6 +784,7 @@ void Command::show_all(uuid::console::Shell & shell) { // show system ones first show(shell, EMSdevice::DeviceType::SYSTEM, true); show(shell, EMSdevice::DeviceType::CUSTOM, true); + show(shell, EMSdevice::DeviceType::COMMAND, true); show(shell, EMSdevice::DeviceType::SCHEDULER, true); // then sensors diff --git a/src/core/emsdevice.cpp b/src/core/emsdevice.cpp index 8b7ed7c11..41a83ecb8 100644 --- a/src/core/emsdevice.cpp +++ b/src/core/emsdevice.cpp @@ -145,6 +145,8 @@ const char * EMSdevice::device_type_2_device_name(const uint8_t device_type) { return F_(scheduler); case DeviceType::CUSTOM: return F_(custom); + case DeviceType::COMMAND: + return F_(commands); case DeviceType::BOILER: return F_(boiler); case DeviceType::THERMOSTAT: @@ -297,6 +299,9 @@ uint8_t EMSdevice::device_name_2_device_type(const char * topic) { if (!strcmp(lowtopic, F_(scheduler))) { return DeviceType::SCHEDULER; } + if (!strcmp(lowtopic, F_(commands))) { + return DeviceType::COMMAND; + } if (!strcmp(lowtopic, F_(system))) { return DeviceType::SYSTEM; } diff --git a/src/core/emsdevice.h b/src/core/emsdevice.h index 44e781f88..93a0595f9 100644 --- a/src/core/emsdevice.h +++ b/src/core/emsdevice.h @@ -405,6 +405,7 @@ class EMSdevice { // Unique Identifiers for each Device type, used in Dashboard table // 100 and above is reserved for DeviceType enum DeviceTypeUniqueID : uint8_t { + COMMAND_UID = 95, SCHEDULER_UID = 96, ANALOGSENSOR_UID = 97, TEMPERATURESENSOR_UID = 98, @@ -417,6 +418,7 @@ class EMSdevice { ANALOGSENSOR, // for internal analog sensors SCHEDULER, // for internal schedule CUSTOM, // for user defined entities + COMMAND, // for user defined commands BOILER, // from here on enum the ems-devices THERMOSTAT, MIXER, diff --git a/src/core/emsesp.cpp b/src/core/emsesp.cpp index 202bf3d59..e49c07b00 100644 --- a/src/core/emsesp.cpp +++ b/src/core/emsesp.cpp @@ -56,6 +56,7 @@ ESP32React EMSESP::esp32React(&webServer, &dummyFS); WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &dummyFS, EMSESP::esp32React.getSecurityManager()); WebCustomizationService EMSESP::webCustomizationService = WebCustomizationService(&webServer, &dummyFS, EMSESP::esp32React.getSecurityManager()); WebSchedulerService EMSESP::webSchedulerService = WebSchedulerService(&webServer, &dummyFS, EMSESP::esp32React.getSecurityManager()); +WebCommandService EMSESP::webCommandService = WebCommandService(&webServer, &dummyFS, EMSESP::esp32React.getSecurityManager()); WebCustomEntityService EMSESP::webCustomEntityService = WebCustomEntityService(&webServer, &dummyFS, EMSESP::esp32React.getSecurityManager()); WebModulesService EMSESP::webModulesService = WebModulesService(&webServer, &dummyFS, EMSESP::esp32React.getSecurityManager()); #else @@ -63,6 +64,7 @@ ESP32React EMSESP::esp32React(&webServer, &LittleFS); WebSettingsService EMSESP::webSettingsService = WebSettingsService(&webServer, &LittleFS, EMSESP::esp32React.getSecurityManager()); WebCustomizationService EMSESP::webCustomizationService = WebCustomizationService(&webServer, &LittleFS, EMSESP::esp32React.getSecurityManager()); WebSchedulerService EMSESP::webSchedulerService = WebSchedulerService(&webServer, &LittleFS, EMSESP::esp32React.getSecurityManager()); +WebCommandService EMSESP::webCommandService = WebCommandService(&webServer, &LittleFS, EMSESP::esp32React.getSecurityManager()); WebCustomEntityService EMSESP::webCustomEntityService = WebCustomEntityService(&webServer, &LittleFS, EMSESP::esp32React.getSecurityManager()); WebModulesService EMSESP::webModulesService = WebModulesService(&webServer, &LittleFS, EMSESP::esp32React.getSecurityManager()); #endif @@ -682,6 +684,7 @@ void EMSESP::publish_other_values() { // publish_device_values(EMSdevice::DeviceType::GENERIC); webSchedulerService.publish(); + webCommandService.publish(); webCustomEntityService.publish(); } @@ -788,6 +791,11 @@ bool EMSESP::get_device_value_info(JsonObject root, const char * cmd, const int8 return webSchedulerService.get_value_info(root, cmd); } + // commands + if (devicetype == DeviceType::COMMAND) { + return webCommandService.get_value_info(root, cmd); + } + // custom entities if (devicetype == DeviceType::CUSTOM) { return webCustomEntityService.get_value_info(root, cmd); @@ -1761,6 +1769,7 @@ void EMSESP::start() { // this will also handle any MQTT subscriptions webCustomizationService.begin(); // load the customizations webSchedulerService.begin(); // load the scheduler events + webCommandService.begin(); // load the user commands webCustomEntityService.begin(); // load the custom telegram reads // perform any system upgrades diff --git a/src/core/emsesp.h b/src/core/emsesp.h index 920f16309..5ec896b9b 100644 --- a/src/core/emsesp.h +++ b/src/core/emsesp.h @@ -51,6 +51,7 @@ #include "../web/WebSettingsService.h" #include "../web/WebCustomizationService.h" #include "../web/WebSchedulerService.h" +#include "../web/WebCommandService.h" #include "../web/WebAPIService.h" #include "../web/WebLogService.h" #include "../web/WebCustomEntityService.h" @@ -260,6 +261,7 @@ class EMSESP { static WebLogService webLogService; static WebCustomizationService webCustomizationService; static WebSchedulerService webSchedulerService; + static WebCommandService webCommandService; static WebCustomEntityService webCustomEntityService; static WebModulesService webModulesService; diff --git a/src/core/locale_translations.h b/src/core/locale_translations.h index 06e397e7a..db34b25f5 100644 --- a/src/core/locale_translations.h +++ b/src/core/locale_translations.h @@ -76,6 +76,7 @@ MAKE_WORD_TRANSLATION(watch_cmd, "watch incoming telegrams", "Beobachte eingehen MAKE_WORD_TRANSLATION(publish_cmd, "publish all to MQTT", "Publiziere MQTT", "publiceer alles naar MQTT", "publicera allt till MQTT", "opublikuj wszystko na MQTT", "Publiser alt til MQTT", "publier tout vers MQTT", "Hepsini MQTTye gönder", "pubblica tutto su MQTT", "zverejniť všetko na MQTT", "publikovat vše do MQTT") MAKE_WORD_TRANSLATION(system_info_cmd, "show system info", "Zeige Systeminformationen", "toon systeemstatus", "visa systeminformation", "pokaż status systemu", "vis system status", "afficher les informations système", "Sistem Durumunu Göster", "visualizza stati di sistema", "zobraziť stav systému", "zobrazit informace o systému") MAKE_WORD_TRANSLATION(schedule_cmd, "enable schedule item", "Aktiviere Zeitplanelemente", "activeer tijdschema item", "aktivera schemalagt objekt", "aktywuj wybrany harmonogram", "aktiver planlagt element", "activer élément programmé", "program öğesini etkinleştir", "abilitare l'elemento programmato", "povoliť položku plánovania", "povolit položku plánování") +MAKE_WORD_TRANSLATION(command_cmd, "execute command", "Befehl ausführen", "opdracht uitvoeren", "kör kommando", "wykonaj polecenie", "kjør kommando", "exécuter commande", "komut çalıştır", "esegui comando", "vykonať príkaz", "provést příkaz") MAKE_WORD_TRANSLATION(entity_cmd, "set custom value", "Sende eigene Entitäten", "verstuur custom waarde", "sätt ett eget värde", "wyślij własną wartość", "sett egendefinert verdi", "définir valeur personnalisée", "özel değer ayarla", "imposta valori personalizzati", "nastaviť vlastnú hodnotu", "nastavit vlastní hodnotu") MAKE_WORD_TRANSLATION(commands_response, "get response", "Hole Antwort", "Verzoek om antwoord", "hämta svar", "uzyskaj odpowiedź", "få svar", "obtenir réponse", "yanıt al", "ottieni risposta", "získať odpoveď", "získat odpověď") MAKE_WORD_TRANSLATION(coldshot_cmd, "send a cold shot of water", "Zugabe einer Menge kalten Wassers", "stuur koud water", "sckicka en liten mängd kallvatten", "uruchom tryśnięcie zimnej wody", "send kaldtvannspuls", "envoyer de l'eau froide", "soğuk su gönder", "invia acqua fredda", "pošlite studenú dávku vody", "poslat studenou vodu") diff --git a/src/core/mqtt.cpp b/src/core/mqtt.cpp index 44e9c85cc..25113bfdb 100644 --- a/src/core/mqtt.cpp +++ b/src/core/mqtt.cpp @@ -510,6 +510,7 @@ void Mqtt::on_connect() { // send initial MQTT messages for some of our services EMSESP::system_.send_heartbeat(); // send heartbeat EMSESP::webCustomEntityService.publish(true); + EMSESP::webCommandService.publish(true); EMSESP::webSchedulerService.publish(true); EMSESP::analogsensor_.publish_values(true); EMSESP::temperaturesensor_.publish_values(true); diff --git a/src/core/system.cpp b/src/core/system.cpp index 3190fc77e..2815fa28d 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -2610,6 +2610,12 @@ bool System::command_info(const char * value, const int8_t id, JsonObject output obj["name"] = F_(scheduler); obj["entities"] = EMSESP::webSchedulerService.count_entities(); } + if (EMSESP::webCommandService.count_entities()) { + JsonObject obj = devices.add(); + obj["type"] = F_(commands); + obj["name"] = F_(commands); + obj["entities"] = EMSESP::webCommandService.count_entities(); + } if (EMSESP::webCustomEntityService.count_entities()) { JsonObject obj = devices.add(); obj["type"] = F_(custom); diff --git a/src/web/WebCommandService.cpp b/src/web/WebCommandService.cpp new file mode 100644 index 000000000..cf841c188 --- /dev/null +++ b/src/web/WebCommandService.cpp @@ -0,0 +1,292 @@ +/* + * EMS-ESP - https://github.com/emsesp/EMS-ESP + * Copyright 2020-2025 emsesp.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "emsesp.h" +#include "WebCommandService.h" + +#include "shuntingYard.h" + +namespace emsesp { + +WebCommandService::WebCommandService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager) + : _httpEndpoint(WebCommands::read, WebCommands::update, this, server, EMSESP_COMMAND_SERVICE_PATH, securityManager, AuthenticationPredicates::IS_AUTHENTICATED) + , _fsPersistence(WebCommands::read, WebCommands::update, this, fs, EMSESP_COMMAND_FILE) { +} + +void WebCommandService::begin() { + _fsPersistence.readFromFS(); + + EMSESP::webCommandService.read([&](WebCommands & webCommands) { commandItems_ = &webCommands.commandItems; }); + + EMSESP::logger().info("Starting Commands service"); + char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; + snprintf(topic, sizeof(topic), "%s/#", F_(commands)); + Mqtt::subscribe(EMSdevice::DeviceType::COMMAND, topic, nullptr); +} + +void WebCommands::read(WebCommands & webCommands, JsonObject root) { + JsonArray items = root["commands"].to(); + uint8_t counter = 1; + for (const CommandItem & ci : webCommands.commandItems) { + JsonObject obj = items.add(); + obj["id"] = counter++; + obj["cmd"] = ci.cmd; + obj["value"] = ci.value; + obj["name"] = (const char *)ci.name; + } +} + +StateUpdateResult WebCommands::update(JsonObject root, WebCommands & webCommands) { + Command::erase_device_commands(EMSdevice::DeviceType::COMMAND); + webCommands.commandItems.clear(); + + auto items = root["commands"].as(); + for (const JsonObject item : items) { + auto ci = CommandItem(); + ci.cmd = item["cmd"].as(); + ci.value = item["value"].as(); + strlcpy(ci.name, item["name"].as(), sizeof(ci.name)); + + webCommands.commandItems.push_back(ci); + if (webCommands.commandItems.back().name[0] != '\0') { + Command::add( + EMSdevice::DeviceType::COMMAND, + webCommands.commandItems.back().name, + [](const char * value, const int8_t id) { + return EMSESP::webCommandService.executeCommand(value); + }, + FL_(command_cmd), + CommandFlag::ADMIN_ONLY); + } + } + return StateUpdateResult::CHANGED; +} + +// find a command item by name (case-insensitive) +const CommandItem * WebCommandService::find(const char * name) { + if (name == nullptr || name[0] == '\0') { + return nullptr; + } + auto lower_name = Helpers::toLower(name); + for (const CommandItem & ci : *commandItems_) { + if (ci.name[0] != '\0' && Helpers::toLower(ci.name) == lower_name) { + return &ci; + } + } + return nullptr; +} + +// execute a named command — looks up by name and runs it +// called from console 'call commands ', API/MQTT, web UI +bool WebCommandService::executeCommand(const char * name) { + const CommandItem * ci = find(name); + if (!ci) { + EMSESP::logger().warning("Command '%s' not found", name ? name : ""); + return false; + } + return executeCommand(ci->name, ci->cmd, ci->value); +} + +// execute a command with explicit cmd and value strings +// handles both HTTP URLs (JSON format) and internal API commands +bool WebCommandService::executeCommand(const char * name, const std::string & command, const std::string & data) { + std::string cmd = Helpers::toLower(command); + + // handle HTTP commands (JSON with url/method/value) + JsonDocument doc; + if (deserializeJson(doc, cmd) == DeserializationError::Ok) { + std::string url = doc["url"] | ""; + auto q = url.find_first_of('?'); + if (q != std::string::npos) { + auto s = url.substr(q + 1); + auto l = s.length(); + commands(s, false); + url.replace(q + 1, l, s); + } + std::string value = doc["value"] | data; + std::string method = doc["method"] | "GET"; + commands(value, false); + auto lower_url = Helpers::toLower(url.c_str()); + if (lower_url.starts_with("http://") || lower_url.starts_with("https://")) { + std::string result; + int httpResult = http_request(url, method, value, doc["header"].as(), result); + if (httpResult != 200) { + EMSESP::logger().warning("Command '%s': URL command failed with http code %d", name, httpResult); + return false; + } +#if defined(EMSESP_DEBUG) + EMSESP::logger().debug("Command '%s': URL '%s' successful with http code %d", name, url.c_str(), httpResult); +#endif + return true; + } + } + + // handle internal API commands + doc.clear(); + JsonObject input = doc.to(); + if (!data.empty()) { + input["data"] = data; + } + + JsonDocument doc_output; + JsonObject output = doc_output.to(); + + char command_str[COMMAND_MAX_LENGTH]; + snprintf(command_str, sizeof(command_str), "/api/%s", cmd.c_str()); + + uint8_t return_code = Command::process(command_str, true, input, output); + if (return_code == CommandRet::OK) { +#if defined(EMSESP_DEBUG) + EMSESP::logger().debug("Command '%s' (%s with data '%s') was successful", name, cmd.c_str(), data.c_str()); +#endif + if (data.empty() && output.size()) { + Mqtt::queue_publish("response", output); + } + return true; + } + + char error[100]; + if (output.size()) { + snprintf(error, sizeof(error), "Command '%s': %s", name ? name : "", (const char *)output["message"]); + } else { + snprintf(error, sizeof(error), "Command '%s': %s failed with error %s", name, cmd.c_str(), Command::return_code_string(return_code)); + } + EMSESP::logger().warning(error); + return false; +} + +bool WebCommandService::get_value_info(JsonObject output, const char * cmd) { + if (commandItems_->empty()) { + return true; + } + + if (!strlen(cmd) || !strcmp(cmd, F_(values)) || !strcmp(cmd, F_(info))) { + for (const CommandItem & ci : *commandItems_) { + if (ci.name[0] != '\0') { + output[(const char *)ci.name] = ci.cmd; + } + } + return true; + } + + if (!strcmp(cmd, F_(entities))) { + for (const CommandItem & ci : *commandItems_) { + if (ci.name[0] != '\0') { + get_value_json(output[ci.name].to(), ci); + } + } + return true; + } + + if (!strcmp(cmd, F_(metrics))) { + std::string metrics = get_metrics_prometheus(); + if (!metrics.empty()) { + output["api_data"] = metrics; + return true; + } + return false; + } + + // look up specific command by name + const char * attribute_s = Command::get_attribute(cmd); + for (const CommandItem & ci : *commandItems_) { + if (Helpers::toLower(ci.name) == cmd) { + get_value_json(output, ci); + return Command::get_attribute(output, cmd, attribute_s); + } + } + + return false; +} + +std::string WebCommandService::get_metrics_prometheus() { + std::string result; + result.reserve(commandItems_->size() * 100); + for (const CommandItem & ci : *commandItems_) { + if (ci.name[0] == '\0') { + continue; + } + result += (std::string) "# HELP emsesp_cmd_" + ci.name + " " + ci.name + "\n"; + result += (std::string) "# TYPE emsesp_cmd_" + ci.name + " gauge\n"; + result += (std::string) "emsesp_cmd_" + ci.name + " 1\n"; + } + return result; +} + +void WebCommandService::get_value_json(JsonObject output, const CommandItem & ci) { + output["name"] = (const char *)ci.name; + output["fullname"] = (const char *)ci.name; + output["type"] = "command"; + output["command"] = ci.cmd; + output["cmd_data"] = ci.value; + bool hasName = ci.name[0] != '\0'; + output["readable"] = hasName; + output["writeable"] = hasName; + output["visible"] = hasName; +} + +void WebCommandService::publish(const bool force) { + if (!Mqtt::enabled() || commandItems_->empty()) { + return; + } + if (force && !EMSESP::mqtt_.get_publish_onchange(0)) { + return; + } + + JsonDocument doc(PSRAM_DOC); + JsonObject output = doc.to(); + for (const CommandItem & ci : *commandItems_) { + if (ci.name[0] != '\0' && !output[ci.name].is()) { + output[(const char *)ci.name] = ci.cmd; + } + } + + if (!doc.isNull()) { + char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; + snprintf(topic, sizeof(topic), "%s_data", F_(commands)); + Mqtt::queue_publish(topic, output); + } +} + +uint8_t WebCommandService::count_entities() { + return static_cast(commandItems_ ? commandItems_->size() : 0); +} + +#if defined(EMSESP_TEST) +void WebCommandService::load_test_data() { + update([&](WebCommands & webCommands) { + webCommands.commandItems.clear(); + + auto ci = CommandItem(); + ci.cmd = "system/fetch"; + ci.value = "10"; + strcpy(ci.name, "test_cmd1"); + webCommands.commandItems.push_back(ci); + + ci = CommandItem(); + ci.cmd = "system/message"; + ci.value = "hello"; + strcpy(ci.name, "test_cmd2"); + webCommands.commandItems.push_back(ci); + + return StateUpdateResult::CHANGED; + }); +} +#endif + +} // namespace emsesp diff --git a/src/web/WebCommandService.h b/src/web/WebCommandService.h new file mode 100644 index 000000000..563a4ed50 --- /dev/null +++ b/src/web/WebCommandService.h @@ -0,0 +1,75 @@ +/* + * EMS-ESP - https://github.com/emsesp/EMS-ESP + * Copyright 2020-2025 emsesp.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include + +#ifndef WebCommandService_h +#define WebCommandService_h + +#define EMSESP_COMMAND_FILE "/config/emsespCommands.json" +#define EMSESP_COMMAND_SERVICE_PATH "/rest/commands" // GET and POST + +namespace emsesp { + +class CommandItem { + public: + stringPSRAM cmd; + stringPSRAM value; + char name[20]; +}; + +class WebCommands { + public: + std::list> commandItems; + + static void read(WebCommands & webCommands, JsonObject root); + static StateUpdateResult update(JsonObject root, WebCommands & webCommands); +}; + +class WebCommandService : public StatefulService { + public: + WebCommandService(AsyncWebServer * server, FS * fs, SecurityManager * securityManager); + + void begin(); + void publish(const bool force = false); + bool get_value_info(JsonObject output, const char * cmd); + void get_value_json(JsonObject output, const CommandItem & commandItem); + + bool executeCommand(const char * name); + bool executeCommand(const char * name, const std::string & cmd, const std::string & value); + + const CommandItem * find(const char * name); + + uint8_t count_entities(); + + std::string get_metrics_prometheus(); + +#if defined(EMSESP_TEST) + void load_test_data(); +#endif + + private: + HttpEndpoint _httpEndpoint; + FSPersistence _fsPersistence; + + std::list> * commandItems_; +}; + +} // namespace emsesp + +#endif diff --git a/src/web/WebDataService.cpp b/src/web/WebDataService.cpp index d8a8a5195..e2f5c4469 100644 --- a/src/web/WebDataService.cpp +++ b/src/web/WebDataService.cpp @@ -250,6 +250,9 @@ void WebDataService::write_device_value(AsyncWebServerRequest * request, JsonVar case EMSdevice::DeviceTypeUniqueID::SCHEDULER_UID: device_type = EMSdevice::DeviceType::SCHEDULER; break; + case EMSdevice::DeviceTypeUniqueID::COMMAND_UID: + device_type = EMSdevice::DeviceType::COMMAND; + break; case EMSdevice::DeviceTypeUniqueID::TEMPERATURESENSOR_UID: device_type = EMSdevice::DeviceType::TEMPERATURESENSOR; break; @@ -478,11 +481,11 @@ void WebDataService::dashboard_data(AsyncWebServerRequest * request) { } } - // show scheduler items + // show scheduler items (active state toggles) if (EMSESP::webSchedulerService.count_entities()) { JsonObject obj = nodes.add(); - obj["id"] = EMSdevice::DeviceTypeUniqueID::SCHEDULER_UID; // it's unique id - obj["t"] = EMSdevice::DeviceType::SCHEDULER; // device type number + obj["id"] = EMSdevice::DeviceTypeUniqueID::SCHEDULER_UID; + obj["t"] = EMSdevice::DeviceType::SCHEDULER; JsonArray nodes = obj["nodes"].to(); uint8_t count = 0; @@ -495,14 +498,31 @@ void WebDataService::dashboard_data(AsyncWebServerRequest * request) { dv["id"] = std::string("00") + scheduleItem.name; dv["c"] = scheduleItem.name; - // for immediate schedules, we don't show the active/inactive state or on/off options - if (scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE) { - char s[12]; - dv["v"] = Helpers::render_boolean(s, scheduleItem.active, true); - JsonArray l = dv["l"].to(); - l.add(Helpers::render_boolean(s, false, true)); // False option - l.add(Helpers::render_boolean(s, true, true)); // True option - } + char s[12]; + dv["v"] = Helpers::render_boolean(s, scheduleItem.active, true); + JsonArray l = dv["l"].to(); + l.add(Helpers::render_boolean(s, false, true)); + l.add(Helpers::render_boolean(s, true, true)); + } + }); + } + + // show command items (executable from dashboard) + if (EMSESP::webCommandService.count_entities()) { + JsonObject obj = nodes.add(); + obj["id"] = EMSdevice::DeviceTypeUniqueID::COMMAND_UID; + obj["t"] = EMSdevice::DeviceType::COMMAND; + JsonArray nodes = obj["nodes"].to(); + uint8_t count = 0; + + EMSESP::webCommandService.read([&](const WebCommands & webCommands) { + for (const CommandItem & ci : webCommands.commandItems) { + JsonObject node = nodes.add(); + node["id"] = (EMSdevice::DeviceTypeUniqueID::COMMAND_UID * 100) + count++; + + JsonObject dv = node["dv"].to(); + dv["id"] = std::string("00") + ci.name; + dv["c"] = ci.name; } }); } diff --git a/src/web/WebSchedulerService.cpp b/src/web/WebSchedulerService.cpp index 0fdf60984..2c01d9777 100644 --- a/src/web/WebSchedulerService.cpp +++ b/src/web/WebSchedulerService.cpp @@ -57,21 +57,18 @@ void WebScheduler::read(WebScheduler & webScheduler, JsonObject root) { JsonArray schedule = root["schedule"].to(); uint8_t counter = 1; for (const ScheduleItem & scheduleItem : webScheduler.scheduleItems) { - JsonObject si = schedule.add(); - si["id"] = counter++; // id is only used to render the table and must be unique. 0 is for Dashboard - si["flags"] = scheduleItem.flags; - si["active"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.active : false; - si["time"] = scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? scheduleItem.time : ""; - si["cmd"] = scheduleItem.cmd; - si["value"] = scheduleItem.value; - si["name"] = (const char *)scheduleItem.name; + JsonObject si = schedule.add(); + si["id"] = counter++; + si["flags"] = scheduleItem.flags; + si["active"] = scheduleItem.active; + si["time"] = scheduleItem.time; + si["cmd_name"] = scheduleItem.cmd_name; + si["name"] = (const char *)scheduleItem.name; } } // call on initialization and also when the Schedule web page is saved -// this loads the data into the internal class StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webScheduler) { - // reset the list Command::erase_device_commands(EMSdevice::DeviceType::SCHEDULER); for (ScheduleItem & scheduleItem : webScheduler.scheduleItems) { char key[sizeof(scheduleItem.name) + 2]; @@ -83,28 +80,23 @@ StateUpdateResult WebScheduler::update(JsonObject root, WebScheduler & webSchedu webScheduler.scheduleItems.clear(); EMSESP::webSchedulerService.ha_reset(); - // build up the list of schedule items auto scheduleItems = root["schedule"].as(); for (const JsonObject schedule : scheduleItems) { - // create each schedule item, overwriting any previous settings - // ignore the id (as this is only used in the web for table rendering) - auto si = ScheduleItem(); - si.active = schedule["active"]; - si.flags = schedule["flags"]; - si.time = si.flags == SCHEDULEFLAG_SCHEDULE_IMMEDIATE ? "" : schedule["time"].as(); - si.cmd = schedule["cmd"].as(); - si.value = schedule["value"].as(); + auto si = ScheduleItem(); + si.active = schedule["active"]; + si.flags = schedule["flags"]; + si.time = schedule["time"].as(); + si.cmd_name = schedule["cmd_name"].as(); strlcpy(si.name, schedule["name"].as(), sizeof(si.name)); - // calculated elapsed minutes si.elapsed_min = Helpers::string2minutes(si.time.c_str()); - si.retry_cnt = 0xFF; // no startup retries + si.retry_cnt = 0xFF; - webScheduler.scheduleItems.push_back(si); // add to list + webScheduler.scheduleItems.push_back(si); if (webScheduler.scheduleItems.back().name[0] != '\0') { char key[sizeof(webScheduler.scheduleItems.back().name) + 2]; snprintf(key, sizeof(key), "s:%s", webScheduler.scheduleItems.back().name); - if (EMSESP::nvs_.isKey(key) && webScheduler.scheduleItems.back().flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE) { + if (EMSESP::nvs_.isKey(key)) { webScheduler.scheduleItems.back().active = EMSESP::nvs_.getBool(key); } Command::add( @@ -140,12 +132,9 @@ bool WebSchedulerService::command_setvalue(const char * value, const int8_t id, publish(); } - // save new state to nvs #2946 - if (scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE) { - char key[sizeof(scheduleItem.name) + 2]; - snprintf(key, sizeof(key), "s:%s", scheduleItem.name); - EMSESP::nvs_.putBool(key, scheduleItem.active); - } + char key[sizeof(scheduleItem.name) + 2]; + snprintf(key, sizeof(key), "s:%s", scheduleItem.name); + EMSESP::nvs_.putBool(key, scheduleItem.active); return true; } } @@ -225,11 +214,10 @@ void WebSchedulerService::get_value_json(JsonObject output, const ScheduleItem & output["onchange"] = scheduleItem.time; } else if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER) { output["timer"] = scheduleItem.time; - } else if (scheduleItem.flags != SCHEDULEFLAG_SCHEDULE_IMMEDIATE) { + } else { output["time"] = scheduleItem.time; } - output["command"] = scheduleItem.cmd; - output["cmd_data"] = scheduleItem.value; + output["cmd_name"] = scheduleItem.cmd_name; bool hasName = scheduleItem.name[0] != '\0'; output["readable"] = hasName; output["writeable"] = hasName; @@ -339,80 +327,14 @@ uint8_t WebSchedulerService::count_entities() { return static_cast(scheduleItems_ ? scheduleItems_->size() : 0); } -// execute scheduled command -// return true if successful, false if not -bool WebSchedulerService::command(const char * name, const std::string & command, const std::string & data) { - std::string cmd = Helpers::toLower(command); - - // check http commands. e.g. - // tasmota(get): http:///cm?cmnd=power%20ON - // shelly(get): http:///relais/0?turn=on - // parse json - JsonDocument doc; - if (deserializeJson(doc, cmd) == DeserializationError::Ok) { - std::string url = doc["url"] | ""; - // for a GET with parameters replace commands with values - // don't search the complete url, it may contain a devicename in path - auto q = url.find_first_of('?'); - if (q != std::string::npos) { - auto s = url.substr(q + 1); // copy only parameters - auto l = s.length(); - commands(s, false); - url.replace(q + 1, l, s); - } - std::string value = doc["value"] | data; // extract value if its in the command, or take the data - std::string method = doc["method"] | "GET"; // default GET - commands(value, false); - auto lower_url = Helpers::toLower(url.c_str()); - if (lower_url.starts_with("http://") || lower_url.starts_with("https://")) { - std::string result; - int httpResult = http_request(url, method, value, doc["header"].as(), result); - if (httpResult != 200) { - EMSESP::logger().warning("Schedule '%s': URL command failed with http code %d", name, httpResult); - return false; - } -#if defined(EMSESP_DEBUG) - EMSESP::logger().debug("Schedule %s: URL '%s' command successful with http code %d", name, url.c_str(), httpResult); -#endif - return true; - } - // we can add other json tests here +// execute the command associated with a schedule item +// looks up the named command in WebCommandService and runs it +bool WebSchedulerService::runScheduleCommand(const ScheduleItem & si) { + if (si.cmd_name.empty()) { + EMSESP::logger().warning("Schedule '%s': no command assigned", si.name); + return false; } - - doc.clear(); - JsonObject input = doc.to(); - if (!data.empty()) { // empty data queries a value - input["data"] = data; - } - - JsonDocument doc_output; // only for commands without output - JsonObject output = doc_output.to(); - - // prefix "api/" to command string - char command_str[COMMAND_MAX_LENGTH]; - snprintf(command_str, sizeof(command_str), "/api/%s", cmd.c_str()); - - uint8_t return_code = Command::process(command_str, true, input, output); // admin set - if (return_code == CommandRet::OK) { -#if defined(EMSESP_DEBUG) - EMSESP::logger().debug("Schedule command '%s' with data '%s' was successful", cmd.c_str(), data.c_str()); -#endif - if (data.empty() && output.size()) { - Mqtt::queue_publish("response", output); - } - return true; - } - - char error[100]; - if (output.size()) { - // check for empty name - snprintf(error, sizeof(error), "Schedule %s: %s", name ? name : "", (const char *)output["message"]); // use error message if we have it - } else { - snprintf(error, sizeof(error), "Schedule %s: command %s failed with error %s", name, cmd.c_str(), Command::return_code_string(return_code)); - } - - EMSESP::logger().warning(error); - return false; + return EMSESP::webCommandService.executeCommand(si.cmd_name.c_str()); } // called from emsesp.cpp on every entity-change @@ -427,31 +349,16 @@ bool WebSchedulerService::onChange(const char * cmd) { return false; } -// system/message evaluates its own argument later (deferred via raw_value, computed in loop()), -// so pre-computing it here would make any {url} or expression inside it run twice. Pass -// system/message its value raw; compute() everything else as before. -// templated because ScheduleItem's strings use a PSRAM allocator, not std::string. -template -static std::string compute_cmd_value(const C & cmd, const V & value) { - if (Helpers::toLower(cmd.c_str()) == "system/message") { - return std::string(value.c_str()); - } - return compute(value.c_str()); -} - // handle condition schedules, parse string stored in schedule.time field void WebSchedulerService::condition() { for (ScheduleItem & scheduleItem : *scheduleItems_) { if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_CONDITION) { auto match = compute(scheduleItem.time.c_str()); -#ifdef EMESESP_DEBUG - // EMSESP::logger().debug("condition match: %s", match.c_str()); -#endif if (match.length() == 1 && match[0] == '1' && scheduleItem.retry_cnt == 0xFF) { - scheduleItem.retry_cnt = command(scheduleItem.name, scheduleItem.cmd.c_str(), compute_cmd_value(scheduleItem.cmd, scheduleItem.value)) ? 1 : 0xFF; + scheduleItem.retry_cnt = runScheduleCommand(scheduleItem) ? 1 : 0xFF; } else if (match.length() == 1 && match[0] == '0' && scheduleItem.retry_cnt == 1) { scheduleItem.retry_cnt = 0xFF; - } else if (match.length() != 1) { // the match is not boolean + } else if (match.length() != 1) { #if defined(EMSESP_DEBUG) EMSESP::logger().debug("condition result: %s", match.c_str()); #endif @@ -462,17 +369,15 @@ void WebSchedulerService::condition() { // process any scheduled jobs void WebSchedulerService::loop() { - // initialize static value on startup static int8_t last_tm_min = -2; // invalid value also used for startup commands static uint32_t last_uptime_min = 0; static uint32_t last_uptime_sec = 0; - if (!raw_value.empty()) { // process a value from system/message command + if (!raw_value.empty()) { computed_value = compute(raw_value); raw_value.clear(); } - // get list of scheduler events and exit if it's empty if (scheduleItems_->empty()) { return; } @@ -480,21 +385,10 @@ void WebSchedulerService::loop() { // check if we have onChange events while (!cmd_changed_.empty()) { ScheduleItem si = *cmd_changed_.front(); - command(si.name, si.cmd.c_str(), compute_cmd_value(si.cmd, si.value)); + runScheduleCommand(si); cmd_changed_.pop_front(); } - for (ScheduleItem & scheduleItem : *scheduleItems_) { - if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_IMMEDIATE) { - command(scheduleItem.name, scheduleItem.cmd.c_str(), compute_cmd_value(scheduleItem.cmd, scheduleItem.value)); - scheduleItem.active = false; - publish_single(scheduleItem.name, false); - if (EMSESP::mqtt_.get_publish_onchange(0)) { - publish(); - } - } - } - // check conditions every 10 seconds, start after one minute uint32_t uptime_sec = uuid::get_uptime_sec() / 10; if (last_uptime_sec != uptime_sec && uptime_sec > 5) { @@ -506,64 +400,49 @@ void WebSchedulerService::loop() { if (last_tm_min == -2) { for (ScheduleItem & scheduleItem : *scheduleItems_) { if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min == 0) { - scheduleItem.retry_cnt = command(scheduleItem.name, scheduleItem.cmd.c_str(), compute_cmd_value(scheduleItem.cmd, scheduleItem.value)) ? 0xFF : 0; + scheduleItem.retry_cnt = runScheduleCommand(scheduleItem) ? 0xFF : 0; } } - last_tm_min = -1; // startup done, now use for RTC + last_tm_min = -1; } // check timer every minute, sync to EMS-ESP clock uint32_t uptime_min = uuid::get_uptime_sec() / 60; if (last_uptime_min != uptime_min) { for (ScheduleItem & scheduleItem : *scheduleItems_) { - // retry startup commands not yet executed if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min == 0 && scheduleItem.retry_cnt < MAX_STARTUP_RETRIES) { - scheduleItem.retry_cnt = command(scheduleItem.name, scheduleItem.cmd.c_str(), scheduleItem.value.c_str()) ? 0xFF : scheduleItem.retry_cnt + 1; + scheduleItem.retry_cnt = runScheduleCommand(scheduleItem) ? 0xFF : scheduleItem.retry_cnt + 1; } - // scheduled timer commands if (scheduleItem.active && scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_TIMER && scheduleItem.elapsed_min > 0 && (uptime_min % scheduleItem.elapsed_min == 0)) { - command(scheduleItem.name, scheduleItem.cmd.c_str(), compute_cmd_value(scheduleItem.cmd, scheduleItem.value)); + runScheduleCommand(scheduleItem); } } last_uptime_min = uptime_min; } - // check calender, sync to RTC, only execute if year is valid + // check calendar, sync to RTC, only execute if year is valid time_t now = time(nullptr); tm * tm = localtime(&now); if (tm->tm_min != last_tm_min && tm->tm_year > 120) { - // find the real dow and minute from RTC - uint8_t real_dow = 1 << tm->tm_wday; // 1 is Sunday + uint8_t real_dow = 1 << tm->tm_wday; uint16_t real_min = tm->tm_hour * 60 + tm->tm_min; for (const ScheduleItem & scheduleItem : *scheduleItems_) { uint8_t dow = scheduleItem.flags & SCHEDULEFLAG_SCHEDULE_TIMER ? 0 : scheduleItem.flags; if (scheduleItem.active && (real_dow & dow) && real_min == scheduleItem.elapsed_min) { - command(scheduleItem.name, scheduleItem.cmd.c_str(), compute_cmd_value(scheduleItem.cmd, scheduleItem.value)); + runScheduleCommand(scheduleItem); } } last_tm_min = tm->tm_min; } } -// execute a schedule item immediately -bool WebSchedulerService::executeSchedule(const char * name) { - for (ScheduleItem & scheduleItem : *scheduleItems_) { - if (scheduleItem.flags == SCHEDULEFLAG_SCHEDULE_IMMEDIATE && strcmp(scheduleItem.name, name) == 0) { - EMSESP::logger().info("Executing schedule '%s'", name); - return command(scheduleItem.name, scheduleItem.cmd.c_str(), compute_cmd_value(scheduleItem.cmd, scheduleItem.value)); - } - } - EMSESP::logger().warning("Schedule '%s' not found", name); - return false; // not found -} - // process schedules async void WebSchedulerService::scheduler_task(void * pvParameters) { while (1) { - delay(10); // no need to hurry + delay(10); if (EMSESP::system_.systemStatus() == SYSTEM_STATUS::SYSTEM_STATUS_NORMAL) { EMSESP::webSchedulerService.loop(); } @@ -573,39 +452,34 @@ void WebSchedulerService::scheduler_task(void * pvParameters) { #endif } -// hard coded tests #if defined(EMSESP_TEST) void WebSchedulerService::load_test_data() { update([&](WebScheduler & webScheduler) { - webScheduler.scheduleItems.clear(); // delete all existing schedules + webScheduler.scheduleItems.clear(); - // test 1 - auto si = ScheduleItem(); - si.active = true; - si.flags = 1; // day schedule - si.time = "12:00"; - si.cmd = "system/fetch"; - si.value = "10"; + auto si = ScheduleItem(); + si.active = true; + si.flags = 1; // day schedule + si.time = "12:00"; + si.cmd_name = "test_cmd1"; strcpy(si.name, "test_scheduler1"); si.elapsed_min = 0; - si.retry_cnt = 0xFF; // no startup retries + si.retry_cnt = 0xFF; webScheduler.scheduleItems.push_back(si); - // test 2 - si = ScheduleItem(); - si.active = false; - si.flags = SCHEDULEFLAG_SCHEDULE_IMMEDIATE; // immediate - si.time = "13:00"; - si.cmd = "system/message"; - si.value = "20"; - strcpy(si.name, "test_scheduler2"); // to make sure its excluded from Dashboard - si.elapsed_min = 0; - si.retry_cnt = 0xFF; // no startup retries + si = ScheduleItem(); + si.active = true; + si.flags = SCHEDULEFLAG_SCHEDULE_TIMER; + si.time = "01:00"; + si.cmd_name = "test_cmd2"; + strcpy(si.name, "test_scheduler2"); + si.elapsed_min = 60; + si.retry_cnt = 0xFF; webScheduler.scheduleItems.push_back(si); - return StateUpdateResult::CHANGED; // persist the changes + return StateUpdateResult::CHANGED; }); } #endif diff --git a/src/web/WebSchedulerService.h b/src/web/WebSchedulerService.h index 281f44a0b..4ee03e0c3 100644 --- a/src/web/WebSchedulerService.h +++ b/src/web/WebSchedulerService.h @@ -41,11 +41,9 @@ // 128 (0x80) is timer // 129 (0x81) is on change // 130 (0x82) is on condition -// 132 (0x84) is immediate #define SCHEDULEFLAG_SCHEDULE_TIMER 0x80 // 7th bit for Timer #define SCHEDULEFLAG_SCHEDULE_ONCHANGE 0x81 // 7th+1st bit for OnChange #define SCHEDULEFLAG_SCHEDULE_CONDITION 0x82 // 7th+2nd bit for Condition -#define SCHEDULEFLAG_SCHEDULE_IMMEDIATE 0x84 // 7th+3rd bit for Immediate #define MAX_STARTUP_RETRIES 3 // retry the start-up commands x times @@ -57,8 +55,7 @@ class ScheduleItem { uint8_t flags; // bit flags, see SCHEDULEFLAG_* defines uint16_t elapsed_min; // total mins from 00:00 stringPSRAM time; // HH:MM - stringPSRAM cmd; - stringPSRAM value; + stringPSRAM cmd_name; // references a named command from WebCommandService char name[20]; uint8_t retry_cnt; }; @@ -88,8 +85,6 @@ class WebSchedulerService : public StatefulService { uint8_t count_entities(); bool onChange(const char * cmd); - bool executeSchedule(const char * name); - std::string get_metrics_prometheus(); std::string raw_value; @@ -105,7 +100,7 @@ class WebSchedulerService : public StatefulService { #endif static void scheduler_task(void * pvParameters); - bool command(const char * name, const std::string & cmd, const std::string & data); + bool runScheduleCommand(const ScheduleItem & si); void condition(); HttpEndpoint _httpEndpoint; diff --git a/src/web/WebStatusService.cpp b/src/web/WebStatusService.cpp index 8cb934cdf..4ae37bb47 100644 --- a/src/web/WebStatusService.cpp +++ b/src/web/WebStatusService.cpp @@ -232,8 +232,8 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json) EMSESP::mqtt_.reset_mqtt(); } else if (action == "upgradeImportantMessages") { root["upgradeImportantMessageType"] = upgradeImportantMessages(param); - } else if (action == "executeSchedule") { - ok = EMSESP::webSchedulerService.executeSchedule(param.c_str()); + } else if (action == "executeCommand") { + ok = EMSESP::webCommandService.executeCommand(param.c_str()); } #if defined(EMSESP_STANDALONE) && !defined(EMSESP_UNITY)