UNPKG

node-red-contrib-redmond

Version:

Управление техникой Redmond по технологии Ready For Sky (R4S)

173 lines (166 loc) 9.7 kB
module.exports = function (RED) { const noble = require('noble'); function NodeDevice(n) { RED.nodes.createNode(this, n); this.mac = n.mac; } RED.nodes.registerType("RK-M170Sdevice", 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([ServiceUuid], true); //} else { // // bind event to start scanning // noble.on('stateChange', function (state) { // if (state === 'poweredOn') { // noble.startScanning([ServiceUuid], true); // } // }); //} 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], true); 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) }); var _config = { action: config.action, temperature: config.temperature }; if (_config.action == "_msg_") { _config.action = msg.payload.action; _config.temperature = msg.payload.temperature; } /* public SetFullKettleProgram(int program, int mode, int temperature, boolean isBlocked, OnAnswerListener<SuccessResponse> listener) { byte b = (byte) 1; super(listener); this.data = new byte[4]; this.data[0] = (byte) program; this.data[1] = (byte) mode; this.data[2] = (byte) temperature; byte[] bArr = this.data; if (!isBlocked) { b = (byte) 0; } bArr[3] = b; } */ 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" }); } var targetTemp = { 0: 'нет', 1: 40, 2: 55, 3: 70, 4: 85, 5: 95 }; var states = { 0: 'выключен', 2: 'включен' }; var programs = { 0: 'кипячение', 1: 'подогрев' }; if (typeof (msg.payload) != "object") { msg.payload = { address: peripheral.address, rssi: peripheral.rssi}; } msg.payload.state = states[data[11]]; msg.payload.Program = programs[data[3]]; msg.payload.setTemperature = targetTemp[data[4]]; msg.payload.currentTemperature = data[5]; msg.payload.remainingTimeHours = data[8]; msg.payload.remainingTimeMinutes = data[9]; 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) { if (typeof (msg.payload) != "object") { msg.payload = {}; } msg.payload.status = data[3] == 1 ? 'OK' : 'ERROR'; msg.raw = data; node.send(msg); 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) { if (typeof (msg.payload) != "object") { msg.payload = {}; } msg.payload.status = data[3] == 1 ? 'OK' : 'ERROR'; msg.raw = data; node.send(msg); 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) { if (typeof (msg.payload) != "object") { msg.payload = {}; } msg.payload.status = data[3] == 1 ? 'OK' : 'ERROR'; msg.raw = data; node.send(msg); 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) { if (typeof (msg.payload) != "object") { msg.payload = {}; } msg.payload.status = data[3] == 1 ? 'OK' : 'ERROR'; msg.raw = data; node.send(msg); peripheral.disconnect(); }); }); break; } }); } }); } node.on("close", function () { //noble.stopScanning(); //noble.removeAllListeners(); }); } RED.nodes.registerType("RK-M170S", broadlinkNode); }