diff --git a/lib/mqttRoute.js b/lib/mqttRoute.js index bdd801b..16bdc0d 100644 --- a/lib/mqttRoute.js +++ b/lib/mqttRoute.js @@ -46,12 +46,11 @@ class MqttRoute { return that; } - connect(brokerUrl, opts) { + connect(brokerUrl, opts = {}) { //debug(brokerUrl, opts); const id = MqttRoute._generateClientID(); - const cOpts = opts || brokerUrl || {}; - cOpts.clientId = cOpts.clientId || id; - //debug(brokerUrl, opts, brokerUrl || cOpts); + opts.clientId = opts.clientId || id; + debug(brokerUrl, opts, brokerUrl || opts); this.mqtt = MQTTconnect(brokerUrl || cOpts, opts); } diff --git a/rules/rules.js b/rules/rules.js index 9276d0b..f61141f 100644 --- a/rules/rules.js +++ b/rules/rules.js @@ -16,8 +16,11 @@ const { SNS, actionTopics = {}, sunblock = {}, + mqttServer = {}, } = ConfigJson; +const lastPid = {}; + const app = new mqttRoute(); const State = new Map(); const sleep = (sec) => @@ -176,6 +179,36 @@ async function handleBlindsSet(req) { } } +function toggleButton(topic) { + const newState = State.get(topic) === "on" ? "off" : "on"; + app.publish(`${topic}/set`, newState); +} + + +function handleRemote(req) { + const msg = req.data; + // check for valid messages + if (msg.BTHome_version !== 2) { + return; + } + // debounce + if (lastPid[req.topic === msg.pid]) { + return; + } + if (msg.button[0] === 1) { + toggleButton("lamp/1"); + } + if (msg.button[1] === 1) { + toggleButton("lamp/2"); + } + if (msg.button[2] === 1) { + toggleButton("lamp/3"); + } + if (msg.button[3] === 1) { + toggleButton("power"); + } +} + function handleStateSet(req) { const topic = req.topic.replace("/set", ""); app.pubRetain(topic, req.data); @@ -218,5 +251,6 @@ app.use("config/+", handleState); app.use("forecast/get", handleForecast); app.use("data/+/set", handleStateSet); app.use("data/+", handleState); +app.use("remote/ble", handleRemote); app.use("checkWeatherPi", handleCheckWeatherPi); -app.listen(); +app.listen(ConfigJson.mqttServer.URL);