-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 830 Bytes
/
index.js
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
const MQTT = require("mqtt")
const Livolo = require("./modules/livolo")
const IR = require("./modules/ir")
const IRMapping = require("./params/ir").default
const TOPICS = ["home/office/light", "home/living/tv"]
let client = MQTT.connect("mqtt://localhost")
let started = false
client.on("connect", function() {
client.subscribe(TOPICS)
})
client.on("message", function(topic, message) {
// avoid actions on connect
if (!started) {
started = true
return
}
const receivedMessage = message.toString()
console.log("Received message on " + topic + ": " + receivedMessage)
switch (topic) {
case "home/office/light":
Livolo.switch(receivedMessage)
break
case "home/living/tv":
IR.send(IRMapping.living.tv[receivedMessage])
break
default:
throw "Unknown topic"
}
})