diff options
Diffstat (limited to 'src/lib/helpers/action.ts')
| -rw-r--r-- | src/lib/helpers/action.ts | 70 |
1 files changed, 63 insertions, 7 deletions
diff --git a/src/lib/helpers/action.ts b/src/lib/helpers/action.ts index 59677ea..87364ba 100644 --- a/src/lib/helpers/action.ts +++ b/src/lib/helpers/action.ts @@ -7,14 +7,10 @@ import { sendMessage } from "@lib/mqtt.ts" import allActions from "@lib/config/actions.ts" import allDevices from "@lib/config/devices.ts" -// run on button press -export function onPress(action) { - - console.debug(`-> running ${action.id}`) +// run actions +function _runActions(actions) { - const actionDo = action.do - - actionDo.forEach((action) => { + actions.forEach((action) => { const device = allDevices.find((device) => device.id === action.device) @@ -29,6 +25,66 @@ export function onPress(action) { }) } +// run on button press +export function onPress(action) { + + console.debug(`-> running ${action.id}`) + + _runActions(action.do) +} + +// run on motion +export function onMotion(action, payload) { + + if (payload.presence === true) { + + console.debug(`-> running ${action.id}`) + + if (!global.activeActions.includes(action.id)) { + global.activeActions.push(action.id) + } + + if (global.activeUndos.includes(action.id)) { + global.activeUndos = global.activeUndos.filter((trigger) => trigger !== action.id) + } + + _runActions(action.do) + + } else { + + if (global.activeActions.includes(action.id)) { + + console.debug(`-> undoing ${action.id}`) + + const inverseActions = action.do.map((inverseAction) => { + let actionDo + + switch (inverseAction.action) { + case "on": + actionDo = "off" + break + + case "off": + actionDo = "on" + break + + default: + actionDo = inverseAction.action + } + + return { + ...inverseAction, + action: actionDo + } + }) + + global.activeActions = global.activeActions.filter((trigger) => trigger !== action.id) + + _runActions(inverseActions) + } + } +} + // validate actions config export function validate() { |
