UNPKG

node-red-contrib-openhab2-helper

Version:

Node-RED openhab2 helper nodes

99 lines (81 loc) 3.46 kB
/** * Copyright 2017 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ module.exports = function(RED) { "use strict"; var convertColor = require('color-convert'); function convert(n) { RED.nodes.createNode(this,n); this.output = n.output; this.input = n.input; var node = this; node.on('input', function(msg){ var responseValue; var input = msg.payload; switch(node.input) { case 'openhab': if (typeof input === 'object' && 'state' in input) { input = input.state.split(','); } else if (typeof input === 'string') { input = input.split(','); } var obj = { hue: parseInt(input[0]), saturation: parseInt(input[1]), value: parseInt(input[2]) }; switch(node.output){ case 'homekit': responseValue = {"Hue":obj.hue, "Saturation":obj.saturation, "Brightness":obj.value}; // if ('payload_in' in msg) { // msg.payload_in.FromOpenhab = true; // } else { // msg.payload_in = {"FromOpenhab":true}; // } break; } break; case 'homekit': var payload_in = msg.payload_in; switch(node.output) { case 'openhab': var hsv = msg.payload.state.split(','); if ('Hue' in payload_in) { responseValue = parseInt(payload_in.Hue)+',100,'+parseInt(hsv[2]); } if ('Saturation' in payload_in) { //responseValue = parseInt(hsv[0])+',100,'+parseInt(hsv[2]); responseValue = '';//nothing to update } if ('Brightness' in payload_in) { responseValue = parseInt(hsv[0])+',100,'+parseInt(payload_in.Brightness); } if ('On' in payload_in) { responseValue = payload_in.On === true?'ON':'OFF'; } break; } break; } if (responseValue) { msg.payload = responseValue; node.send(msg); } else { node.error("no output"); } }); } RED.nodes.registerType("openhab2-helper-color-convert",convert); }