aboutsummaryrefslogtreecommitdiff
path: root/src/lib/init.ts
blob: 99730e9e237ccfac8997b54bf6dec7df05e65419 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// ~~~ homecontrol init
//

// imports
import { validate, onPress, onMotion } 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() {

    global.activeActions = []
    global.activeUndos = []

    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
        }

        // todo: use a single callback / listener & just subscribe to each topic
        // first param on return is the topic so case / switch that

        let topic

        if (["on", "off", "toggle"].includes(action.on)) {

            topic = `${onDevice.mqtt}/action`

        } else {

            topic = onDevice.mqtt

        }

        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

                default:

                    // probbaly a json string
                    const jsonMessageAction = JSON.parse(messageAction)

                    if (jsonMessageAction?.presence !== undefined && action.on.action === "motion") {

                        onMotion(action, jsonMessageAction)

                    } else {

                        console.warn(`?> unknown action: ${messageAction} from ${topic}`)

                    }
            }
        }

        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()
    }
}