blob: 03c9b63cf52f99d6ace160dc1007858e7eb5e7b2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//
// ~~~ 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)
}
break
case "on":
if (action.on.action === "press" || action.on.action === "on") {
onPress(action)
}
break
case "off":
if (action.on.action === "press" || action.on.action === "off") {
onPress(action)
}
break
}
}
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()
}
}
|