node-red-contrib-redmond
Version:
Управление техникой Redmond по технологии Ready For Sky (R4S)
152 lines (146 loc) • 8.89 kB
JavaScript
module.exports = function (RED) {
const noble = require('noble');
function NodeDevice(n) {
RED.nodes.createNode(this, n);
this.mac = n.mac;
}
RED.nodes.registerType("RMC-M222Sdevice", NodeDevice);
var ServiceUuid = '6e400001b5a3f393e0a9e50e24dcca9e';
function broadlinkNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.on('input', function (msg) {
if (noble.state === 'poweredOn') {
//noble.startScanning([], true);
run(msg, config);
} else {
// bind event to start scanning
noble.on('stateChange', function (state) {
if (state === 'poweredOn') {
//noble.startScanning([], true);
run(msg, config);
}
});
}
});
function run(msg, config) {
noble.startScanning([ServiceUuid], false);
noble.on('discover', function (peripheral) {
if (peripheral.address.toUpperCase() == RED.nodes.getNode(config.mydevice).mac.toUpperCase()) {
noble.stopScanning();
noble.removeAllListeners('discover');
peripheral.connect(function (err) {
if (err) console.error('ERROR', err)
peripheral.writeHandle(0x000c, new Buffer([0x01, 0x00]), 1, function (err) { if (err) console.error('ERROR', err) });
//Auth sequence
peripheral.writeHandle(0x000e, new Buffer([0x55, 1, 0xff, 0xb5, 0x4c, 0x75, 0xb1, 0xb4, 0x0c, 0x88, 0xef, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
switch (config.action) {
case "status":
peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x06, 0xaa]), false, function (err) {
if (err) console.error('ERROR', err)
peripheral.readHandle(0x000b, function (error, data) {
if (data[3] == 0 && data[3] == 0xaa) {
node.warn('Неавторизован. Зажмите кнопку "+" на устройстве и повторите ввод.');
node.status({ fill: "red", shape: "ring", text: "unauthorized" });
}
else {
node.status({ fill: "green", shape: "dot", text: "authorized" });
}
// program:value[0],
// mode:value[1],
// temperature:value[2],
// timeHours:value[3],
// timeMinutes:value[4],
// remainingTimeHours:value[5],
// remainingTimeMinutes:value[6],
// heating:value[7],
//state:value[8],
// blocking:value[9]
//+3
//console.log(data)
if (typeof (msg.payload) != "object") { msg.payload = { address: peripheral.address, rssi: peripheral.rssi}; }
msg.payload.state = data[11];//(['Выключена',''])
msg.payload.program = data[3];
msg.payload.mode = data[4];
msg.payload.temperature = data[5];
msg.payload.timeHours = data[6];
msg.payload.timeMinutes = data[7];
msg.payload.remainingTimeHours = data[8];
msg.payload.remainingTimeMinutes = data[9];
msg.payload.heating = data[10];
msg.payload.blocking = data[12];
msg.raw = data;
node.send(msg);
peripheral.disconnect();
});
});
break;
//case "off":
// peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x04, 0xaa]), false, function (err) {
// if (err) console.error('ERROR', err)
// peripheral.readHandle(0x000b, function (error, data) {
// node.send({
// payload: {
// status: data[3] == 1 ? 'OK' : 'ERROR'
// },
// raw: data
// });
// peripheral.disconnect();
// });
// });
// break;
//case "heat":
// console.log([0x55, 2, 0x05, 0x01, +config.temperature, 0x00, 0x00, 0xaa])
// peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x05, 0x01, +config.temperature, 0x00, 0x00, 0xaa]), false, function (err) {
// if (err) console.error('ERROR', err)
// peripheral.readHandle(0x000b, function (error, data) {
// node.send({
// payload: {
// status: data[3] == 1 ? 'OK' : 'ERROR'
// },
// raw: data
// });
// peripheral.disconnect();
// });
// });
// break;
//case "boil":
// peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x05, 0x00, 0x00, 0x00, 0x00, 0xaa]), false, function (err) {
// if (err) console.error('ERROR', err)
// peripheral.readHandle(0x000b, function (error, data) {
// node.send({
// payload: {
// status: data[3] == 1 ? 'OK' : 'ERROR'
// },
// raw: data
// });
// peripheral.disconnect();
// });
// });
// break;
//case "boilheat":
// peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x05, 0x00, +config.temperature, 0x00, 0x00, 0xaa]), false, function (err) {
// if (err) console.error('ERROR', err)
// peripheral.readHandle(0x000b, function (error, data) {
// node.send({
// payload: {
// status: data[3] == 1 ? 'OK' : 'ERROR'
// },
// raw: data
// });
// peripheral.disconnect();
// });
// });
// break;
}
});
}
});
}
node.on("close", function () {
noble.stopScanning();
noble.removeAllListeners();
});
}
RED.nodes.registerType("RMC-M222S", broadlinkNode);
}