Skip to content

Commit

Permalink
add ble remote
Browse files Browse the repository at this point in the history
  • Loading branch information
seriousme committed Dec 31, 2024
1 parent 956538d commit 32ec4ef
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/mqttRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
36 changes: 35 additions & 1 deletion rules/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ const {
SNS,
actionTopics = {},
sunblock = {},
mqttServer = {},
} = ConfigJson;

const lastPid = {};

const app = new mqttRoute();
const State = new Map();
const sleep = (sec) =>
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

0 comments on commit 32ec4ef

Please sign in to comment.