diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/helpers/action.ts | 46 | ||||
| -rw-r--r-- | src/lib/init.ts | 58 | ||||
| -rw-r--r-- | src/server.ts | 4 |
3 files changed, 107 insertions, 1 deletions
diff --git a/src/lib/helpers/action.ts b/src/lib/helpers/action.ts new file mode 100644 index 0000000..59677ea --- /dev/null +++ b/src/lib/helpers/action.ts @@ -0,0 +1,46 @@ +// +// ~~~ action helper utilities +// + +// imports +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}`) + + const actionDo = action.do + + actionDo.forEach((action) => { + + const device = allDevices.find((device) => device.id === action.device) + + // todo: error handling + + const topic = `${device.mqtt}/set` + const message = JSON.stringify({ state: action.action.toUpperCase() }) + + console.debug(` -> send state ${action.action} to ${action.device}`) + sendMessage(topic, message) + + }) +} + +// validate actions config +export function validate() { + + if (allActions.length < 1) return true + + return allActions.every((action) => { + + const onDevice = allDevices.find((device) => device.id === action.on.device) + if (!onDevice) return false + + // todo: rest of the props + + return true + }) +} diff --git a/src/lib/init.ts b/src/lib/init.ts new file mode 100644 index 0000000..c4ba0ea --- /dev/null +++ b/src/lib/init.ts @@ -0,0 +1,58 @@ +// +// ~~~ homecontrol init +// + +// imports +import { validate, onPress } from "@lib/helpers/action.ts" +import { addListener } from "@lib/mqtt.ts" +import allActions, { ACTION_ON_ACTION } from "@lib/config/actions.ts" +import allDevices from "@lib/config/devices.ts" + +// initialise actions +function _initActions() { + + if (!allActions || allActions.length < 1) return + + allActions.forEach((action) => { + + console.debug(`-> setting up ${action.id}`) + + const onDevice = allDevices.find((device) => device.id === action.on.device) + + if (!onDevice) { + console.error(`!> unable to find ${action.on.device}`) + return + } + + const topic = `${onDevice.mqtt}/action` + const callback = (message) => { + + const messageAction = message.toString() + + switch (messageAction) { + + case "toggle": + if (action.on.action === "press") { // todo: use enum + onPress(action) + } + + } + } + + addListener(topic, callback) + }) +} + +// initialise homecontrol +export default function init() { + + console.debug("=> initialising homecontrol") + + const isActionsValid = validate() + + if (!isActionsValid) { + console.error("!> invalid actions config") + } else { + _initActions() + } +} diff --git a/src/server.ts b/src/server.ts index 424a4f0..551ed48 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,13 +4,15 @@ // imports import app from "./app.ts" +import init from "@lib/init.ts" import loadDotenv from "@lib/dotenv.ts" // setup app const dotenv = loadDotenv() - const PORT = dotenv?.HC_PORT || 3000 +init() + // start app app.listen(PORT, () => { console.log(`=> starting homecontrol on port ${PORT}`) |
