UNPKG

node-red-contrib-redmond

Version:

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

222 lines (207 loc) 12.3 kB
module.exports = function (RED) { const noble = require('noble'); function NodeDevice(n) { RED.nodes.createNode(this, n); this.mac = n.mac; } RED.nodes.registerType("RMF-3310Sdevice", 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) }); /* public SetFullHumidifier3310Program(int program, int mode, int humidity, int timeHours, boolean heat, OnAnswerListener<SuccessResponse> listener) { int i = 1; super(listener); this.data = new byte[8]; this.data[0] = (byte) program; this.data[1] = (byte) mode; this.data[2] = (byte) humidity; this.data[3] = (byte) timeHours; this.data[4] = (byte) 0; this.data[5] = (byte) 0; this.data[6] = (byte) 0; byte[] bArr = this.data; if (!heat) { i = 0; } bArr[7] = (byte) i; } */ 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) //Humidifier3310Response{ // program = this.value[0] // , mode = (this.value[1]) // , setHumidity = (this.value[2]) // , timeHours = (this.value[3]) // , remainingTimeHours = (this.value[5]) // , remainingTimeMinutes = (this.value[6]) // , heat = // , state = (this.value[8]) // , currentHumidity = (this.value[9]) //} 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.setHumidity = data[5]; msg.payload.timeHours = data[6]; //msg.payload.timeMinutes = data[7]; msg.payload.remainingTimeHours = data[8]; msg.payload.remainingTimeMinutes = data[9]; msg.payload.heat = data[10]; msg.payload.state = data[11]; msg.payload.currentHumidity = data[12]; msg.raw = data; node.send(msg); peripheral.disconnect(); }); }); /*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; } this.data = new byte[8]; this.data[0] = (byte) program; this.data[1] = (byte) mode; this.data[2] = (byte) humidity; this.data[3] = (byte) timeHours; this.data[4] = (byte) 0; this.data[5] = (byte) 0; this.data[6] = (byte) 0; byte[] bArr = this.data; if (!heat) { i = 0; }*/ /*peripheral.writeHandle(0x000e, new Buffer([0x55, 2, 0x05, 0x01, 60, 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 "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("RMF-3310S", broadlinkNode); }