node-red-contrib-redmond
Version:
Управление техникой Redmond по технологии Ready For Sky (R4S)
100 lines (93 loc) • 6.51 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("RCM-M1505Sdevice", 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" });
}
if (typeof (msg.payload) != "object") { msg.payload = { address: peripheral.address, rssi: peripheral.rssi}; }
msg.payload.state = ({ 0: 'выключатель: выкл', 1: 'выключатель: зерна', 2: 'выключатель: молотый', 3: 'помолка', 4: 'варка', 5: 'сварился и подогревается' })[data[11]];
msg.payload.timeHours = data[6];
msg.payload.timeMinutes = data[7];
msg.payload.remainingTimeHours = data[8];
msg.payload.remainingTimeMinutes = data[9];
msg.payload.pause = data[10];
msg.raw = data;
node.send(msg);
peripheral.disconnect();
});
});
break;
case "grind":
peripheral.writeHandle(0x000e, new Buffer([0x55, 1, 0x01, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x05, 0x14, 0x00, 0x1e, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });//30s время помола//55:09:05:16:00:1e:aa
peripheral.writeHandle(0x000e, new Buffer([0x55, 3, 0x03, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
peripheral.writeHandle(0x000e, new Buffer([0x55, 4, 0x05, 0x17, 0x05, 0x14, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
msg.payload.status = 'OK';
node.send(msg);
break;
case "brew":
peripheral.writeHandle(0x000e, new Buffer([0x55, 1, 0x01, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x05, 0x16, 0x00, 0x1e, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
peripheral.writeHandle(0x000e, new Buffer([0x55, 3, 0x03, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
peripheral.writeHandle(0x000e, new Buffer([0x55, 4, 0x05, 0x17, 0x06, 0x00, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });
msg.payload.status = 'OK';
node.send(msg);
break;
case "pause":
peripheral.writeHandle(0x000e, new Buffer([0x55, 1, 0x16, 0x01, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });//pause
//peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x16, 0x00, 0xaa]), false, function (err) { if (err) console.error('ERROR', err) });//unpause
break;
}
});
}
});
}
node.on("close", function () {
noble.stopScanning();
noble.removeAllListeners();
});
}
RED.nodes.registerType("RCM-M1505S", broadlinkNode);
}