UNPKG

node-red-contrib-homekit-abc

Version:

HAP-Nodejs based Node-RED nodes to create HomeKit Accessories

159 lines (126 loc) 5.92 kB
const mqtt = require("mqtt"); const path = require('path'); const net = require('net'); const hap = require('hap-nodejs'); const storage = require('node-persist'); const {uuid, Accessory, Service, Characteristic} = hap; const pkg = require('../package.json'); const accessories = {}; module.exports = function (RED) { //hap.init(path.join(RED.settings.userDir, 'homekit')); RED.httpAdmin.get('/hassbian-abc-homekit-windowCovering', (req, res) => { if (req.query.config) { const acc = accessories[req.query.config]; if (acc) { res.status(200).send(JSON.stringify({setupURI: acc.setupURI()})); } else { res.status(500).send(JSON.stringify({})); } } else { res.status(404).send(JSON.stringify({})); } }); class hassbianHomeKitwindowCovering { constructor(config) { RED.nodes.createNode(this, config); const that = this; var mqttHost = config.mqtthost; var mqttUsername = config.mqttname; var mqttPassword = config.mqttpass; var mqttOptions = { clientId: 'mqttjs_' + Math.random().toString(16).substr(2, 8), username: mqttUsername, password: mqttPassword }; var client = mqtt.connect(mqttHost, mqttOptions); client.on('error', () => { this.log('Error event on MQTT'); }); var topicset = config.setPosition; var topicstate = config.statePosition; try { client.subscribe(topicstate); } catch(e) { }; function logger(...args) { let str = args.join(' '); if (str.match(/^(error: )/i)) { str = str.replace(/^error: (.*)/i, '$1'); that.error(str); } else if (config.debug) { that.debug(str); } } if (accessories[this.id]) { this.debug('windowCovering already configured ' + this.name + ' ' + this.id + ' ' + this.username); return; } const [firstLogger] = Object.keys(RED.settings.logging); config.debug = config.debug && (RED.settings.logging[firstLogger].level === 'debug' || RED.settings.logging[firstLogger].level === 'trace'); this.name = config.name || ('windowCovering ' + this.id); storage.initSync(); const acc = new Accessory(this.name, uuid.generate(config.id), Accessory.Categories.OUTLET); accessories[this.id] = acc; this.debug('windowCovering created' + this.name + ' ' + this.id + ' ' + config.username); acc.getService(Service.AccessoryInformation) .setCharacteristic(hap.Characteristic.Manufacturer, 'hassbian-abc') .setCharacteristic(hap.Characteristic.Model, 'windowCovering') .setCharacteristic(hap.Characteristic.SerialNumber, config.username) .setCharacteristic(hap.Characteristic.FirmwareRevision, pkg.version); acc.on('identify', (paired, callback) => { this.log('identify ' + this.id + ' ' + this.username + ' ' + paired); callback(); }); var windowCoveringService = acc.addService(Service.WindowCovering, this.name, this.name); var currentPosition = windowCoveringService.getCharacteristic(Characteristic.CurrentPosition); var positionState = windowCoveringService.getCharacteristic(Characteristic.PositionState); var targetPosition = windowCoveringService.getCharacteristic(Characteristic.TargetPosition); targetPosition .on('set', (value, callback) => { var abc = value.toString(); client.publish(topicset, abc); callback(null); }); client.on('message', (topic, message) => { if (topic == topicstate) { var value = parseInt(message); currentPosition.updateValue(value); targetPosition.updateValue(value); } }); this.log('publishing windowCovering ' + this.name + ' ' + config.username); const testPort = net.createServer() .once('error', err => { this.error(err); this.status({fill: 'red', shape: 'dot', text: err.message}); }) .once('listening', () => { testPort.once('close', () => { acc.publish({ username: config.username, port: config.port, pincode: config.pincode, category: Accessory.Categories.OUTLET }); acc._server.on('listening', () => { this.log('windowCovering ' + this.name + ' listening on port ' + config.port); this.status({fill: 'green', shape: 'ring', text: ' '}); }); acc._server.on('pair', username => { this.log('windowCovering ' + this.name + ' paired', username); }); acc._server.on('unpair', username => { this.log('windowCovering ' + this.name + ' unpaired', username); }); acc._server.on('verify', () => { this.log('windowCovering ' + this.name + ' verify'); }); }).close(); }) .listen(config.port); this.on('close', () => { }); } } RED.nodes.registerType('hassbian-abc-homekit-windowCovering', hassbianHomeKitwindowCovering); };