node-red-contrib-indigo
Version:
indigo home automation for node-red
576 lines (433 loc) • 16 kB
JavaScript
/*
Indigo nodes for IBM's Node-Red
https://github.com/pdmangel/node-red-contrib-indigo
(c) 2017, Peter De Mangelaere <peter.demangelaere@gmail.com>
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.
*/
var EventSource = require('@joeybaker/eventsource');
var indigodomo = require('indigodomo-node');
var isUtf8 = require('isutf8');
var moment = require ('moment');
function getConnectionString(config) {
var url;
if (config.protocol)
url = config.protocol;
else
url = "http";
url += "://";
if ((config.username != undefined) && (config.username.trim().length != 0)) {
url += config.username.trim();
if ((config.password != undefined) && (config.password.length != 0)) {
url += ":" + config.password;
}
url += "@";
}
url += config.host;
if ((config.port != undefined) && (config.port.trim().length != 0)) {
url += ":" + config.port.trim();
}
if ((config.path != undefined) && (config.path.trim().length != 0)) {
var path = config.path.trim();
path = path.replace(/^[\/]+/, '');
path = path.replace(/[\/]+$/, '');
url += "/" + path;
}
return url;
}
module.exports = function (RED) {
/**
* ====== indigo-controller ================
* Holds the hostname and port of the
* Indigo server
* ===========================================
*/
function IndigoControllerNode(config) {
console.log('controller node initialised')
indigodomo.init(config.protocol + "://" + config.host, config.port, config.username, config.password)
RED.nodes.createNode(this, config);
this.getConfig = function () {
return config;
}
var node = this;
node.log(JSON.stringify(config));
// this controller node handles all communication with the configured indigo server
this.connection = function () {
return indigodomo;
}
this.getDevice = function (name) {
return indigodomo.GetDevice(name);
};
this.setDevice = function (name, key, value) {
return indigodomo.SetDevicePropertyValue(name, key, value);
};
this.getVariable = function (name) {
return indigodomo.GetVariable(name);
};
this.execAction = function (name) {
return indigodomo.ExecuteActionGroup(name);
};
this.setVariable = function (name, value) {
return indigodomo.SetVariable(name, value);
};
this.getActionGroup = function (name) {
return indigodomo.GetActionGroup(name);
};
this.on("close", function () {
node.log('close');
node.emit('CommunicationStatus', "OFF");
});
}
RED.nodes.registerType("indigo-controller", IndigoControllerNode);
// start a web service for enabling the node configuration ui to query for available Indigo items
RED.httpNode.get("/indigo/MQTTdevices", function (req, res, next) {
console.log('getting devices')
res.send(devices)
});
RED.httpNode.get("/indigo/MQTTvariables", function (req, res, next) {
console.log('getting variables')
res.send(variables)
});
RED.httpNode.get("/indigo/MQTTactions", function (req, res, next) {
console.log('getting actions')
res.send(actions)
});
RED.httpNode.get("/indigo/devices",function(req, res, next) {
console.log('getting devices')
indigodomo.GetDevices().then((response)=> res.send(response))
});
RED.httpNode.get("/indigo/variables",function(req, res, next) {
console.log('getting variables')
indigodomo.GetVariables().then((response)=> res.send(response))
});
RED.httpNode.get("/indigo/actions",function(req, res, next) {
console.log('getting actions')
indigodomo.GetActionGroups().then((response)=> res.send(response))
});
/**
* ====== indigo-in ========================
* Handles incoming indigo events, injecting
* json into node-red flows
* ===========================================
*/
function IndigoIn(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var node = this;
var indigoController = RED.nodes.getNode(config.controller);
var itemName = config.itemname;
if (itemName != undefined) itemName = itemName.trim();
//node.log('IndigoIn, config: ' + JSON.stringify(config));
this.refreshNodeStatus = function () {
var currentState = node.context().get("currentState");
if (currentState == null)
node.status({ fill: "yellow", shape: "ring", text: "state:" + currentState });
else if (currentState == "ON")
node.status({ fill: "green", shape: "dot", text: "state:" + currentState });
else if (currentState == "OFF")
node.status({ fill: "green", shape: "ring", text: "state:" + currentState });
else
node.status({ fill: "blue", shape: "ring", text: "state:" + currentState });
};
this.processStateEvent = function (event) {
var currentState = node.context().get("currentState");
if ((event.state != currentState) && (event.state != "null")) {
// update node's context variable
currentState = event.state;
node.context().set("currentState", currentState);
// update node's visual status
node.refreshNodeStatus();
// inject the state in the node-red flow
var msgid = RED.util.generateId();
node.send([{ _msgid: msgid, payload: currentState, item: itemName, event: "StateEvent" }, null]);
}
};
this.processRawEvent = function (event) {
// inject the state in the node-red flow
var msgid = RED.util.generateId();
node.send([null, { _msgid: msgid, payload: event, item: itemName, event: "RawEvent" }]);
};
node.context().set("currentState", "?");
indigoController.addListener(itemName + '/RawEvent', node.processRawEvent);
indigoController.addListener(itemName + '/StateEvent', node.processStateEvent);
node.refreshNodeStatus();
/* ===== Node-Red events ===== */
this.on("input", function (msg) {
if (msg != null) {
};
});
this.on("close", function () {
node.log('close');
indigoController.removeListener(itemName + '/StateEvent', node.processStateEvent);
indigoController.removeListener(itemName + '/RawEvent', node.processRawEvent);
});
}
//
RED.nodes.registerType("indigo-in", IndigoIn);
/**
* ====== indigo-get-device ===================
* Gets the device data when
* messages received via node-red flows
* =======================================
*/
function IndigoSetDevice(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var indigoController = RED.nodes.getNode(config.controller);
var node = this;
// handle incoming node-red message
this.on("input", function (msg) {
var item = (config.itemname && (config.itemname.length != 0)) ? config.itemname : msg.item;
var key = (config.key && (config.key.length != 0)) ? config.key : msg.topic;
var value = config.value == '' ? msg.payload : config.value;
if ((typeof value ==='string' && (value.toLowerCase() == "true" || value.toLowerCase()=='on'|| value.toLowerCase()=='yes')) || value==true)
{
value=1;
}
if ((typeof value ==='string' && (value.toLowerCase() == "false" || value.toLowerCase()=='off'|| value.toLowerCase()=='no' )) || value==false)
{
value=0;
}
indigoController.setDevice(item, key, value)
.then((res) => {
node.status({ fill: "green", shape: "dot", text: " " });
msg.payload_in = msg.payload;
msg.payload = JSON.parse(JSON.stringify(res));
node.send(msg);
})
.catch((err) => {
node.status({ fill: "red", shape: "ring", text: err });
node.warn(err);
})
});
this.on("close", function () {
node.log('close');
});
}
//
RED.nodes.registerType("indigo-set-device", IndigoSetDevice);
/**
* ====== indigo-get-device ===================
* Gets the device data when
* messages received via node-red flows
* =======================================
*/
function IndigoGetDevice(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var indigoController = RED.nodes.getNode(config.controller);
var node = this;
// handle incoming node-red message
this.on("input", function (msg) {
var item = (config.itemname && (config.itemname.length != 0)) ? config.itemname : msg.item;
console.log('getting device: ' + item)
indigoController.getDevice(item)
.then((res) => {
node.status({ fill: "green", shape: "dot", text: " " });
msg.payload_in = msg.payload;
msg.payload = JSON.parse(JSON.stringify(res));
node.send(msg);
})
.catch((err) => {
node.status({ fill: "red", shape: "ring", text: err });
node.warn(err);
})
});
this.on("close", function () {
node.log('close');
});
}
//
RED.nodes.registerType("indigo-get-device", IndigoGetDevice);
function IndigoExecAction(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var indigoController = RED.nodes.getNode(config.controller);
var node = this;
// handle incoming node-red message
this.on("input", function (msg) {
var item = (config.itemname && (config.itemname.length != 0)) ? config.itemname : msg.topic;
indigoController.execAction(item)
.then((res) => {
node.status({ fill: "green", shape: "dot", text: " " });
msg.payload_in = msg.payload;
msg.payload = JSON.parse(JSON.stringify(res));
node.send(msg);
})
.catch((err) => {
node.status({ fill: "red", shape: "ring", text: err });
node.warn(err);
})
});
this.on("close", function () {
node.log('close');
});
}
//
RED.nodes.registerType("indigo-exec-action", IndigoExecAction);
function IndigoGetVariable(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var indigoController = RED.nodes.getNode(config.controller);
var node = this;
// handle incoming node-red message
this.on("input", function (msg) {
var item = (config.itemname && (config.itemname.length != 0)) ? config.itemname : msg.item;
indigoController.getVariable(item)
.then((res) => {
node.status({ fill: "green", shape: "dot", text: " " });
msg.payload_in = msg.payload;
msg.payload = JSON.parse(JSON.stringify(res));
node.send(msg);
})
.catch((err) => {
node.status({ fill: "red", shape: "ring", text: err });
node.warn(err);
})
});
this.on("close", function () {
node.log('close');
});
}
//
RED.nodes.registerType("indigo-get-variable", IndigoGetVariable);
function IndigoSetVariable(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
var indigoController = RED.nodes.getNode(config.controller);
var node = this;
// handle incoming node-red message
this.on("input", function (msg) {
var item = (config.itemname && (config.itemname.length != 0)) ? config.itemname : msg.topic;
var value = msg.payload;
indigoController.setVariable(item, value)
.then((res) => {
node.status({ fill: "green", shape: "dot", text: " " });
msg.payload_in = msg.payload;
msg.payload = JSON.parse(JSON.stringify(res));
node.send(msg);
})
.catch((err) => {
node.status({ fill: "red", shape: "ring", text: err });
node.warn(err);
})
});
this.on("close", function () {
node.log('close');
});
}
//
RED.nodes.registerType("indigo-set-variable", IndigoSetVariable);
devices=[];
actions=[];
variables=[];
function IndigoMQTTInNode(n) {
var autoDiscoveryTopic = '/GS-Indigo-Autodiscover'
RED.nodes.createNode(this, n);
this.topic = n.topic;
this.qos = parseInt(n.qos);
if (isNaN(this.qos) || this.qos < 0 || this.qos > 2) {
this.qos = 2;
}
this.broker = n.broker;
this.itemname = n.itemname;
this.onlyonchange = n.onlyonchange;
this.valuefilter = n.valuefilter;
this.ignoreretainedmessages = n.ignoreretainedmessages
this.autodisc = {};
this.brokerConn = RED.nodes.getNode(this.broker);
if (!/^(#$|(\+|[^+#]*)(\/(\+|[^+#]*))*(\/(\+|#|[^+#]*))?$)/.test(this.topic)) {
return this.warn(RED._("mqtt.errors.invalid-topic"));
}
var node = this;
if (this.brokerConn) {
node.brokerConn.register(this);
node.status({ fill: "orange", shape: "dot", text: "Waiting for AutoDiscovery" });
console.log('AutoDiscovery')
this.brokerConn.subscribe(autoDiscoveryTopic, this.qos,
function (topic, payload, packet) {
payload = payload.toString();
console.log(payload)
node.autodisc = JSON.parse(payload);
var t = node.autodisc.superBridgePatternOut.replace('{DeviceName}', node.itemname).replace('{State}', '') + '#';
devices = node.autodisc.devices;
variables = node.autodisc.variables;
actions = node.autodisc.actions;
this.topic = t;
node.brokerConn.subscribe(this.topic, node.qos, function (topic, payload, packet) {
if (isUtf8(payload)) { payload = payload.toString(); }
try {
var p = JSON.parse(payload)
if (p.deviceName) {
var DeviceName = p.deviceName;
var State = p.state;
if (n.state){
if (n.state != p.state) {
return;
}else{
node.status({ fill: "green", shape: "dot", text: p.state + ": " + p.newValue + ' at ' + moment().format('DD-MMM HH:mm:ss')});
}
}
var t_in = node.autodisc.superBridgePatternIn.replace('{DeviceName}', DeviceName).replace('{State}', State);
//If the topic matches the In topic, then this is not a message for this system to deal with. Ignore.
if (topic != t_in) {
var msg = { topic: topic, payload: p, qos: packet.qos, retain: packet.retain };
if ((node.brokerConn.broker === "localhost") || (node.brokerConn.broker === "127.0.0.1")) {
msg._topic = topic;
}
var send = true;
if(n.valuefilter != undefined && n.valuefilter != ''){
if(msg.payload.newValue != n.valuefilter){
send = false;
}
}
if(n.onlyonchange != undefined && n.onlyonchange){
if(msg.payload.oldValue == msg.payload.newValue ){
send = false;
}
}
if(n.ignoreretainedmessages != undefined && n.ignoreretainedmessages){
if(msg.retain == true ){
send = false;
}
}
if(send){
node.send(msg);
}
}
}
} catch (error) {
console.log(error)
}
}, this.id);
if (node.brokerConn.connected) {
node.status({ fill: "green", shape: "dot", text: "Connected" });
}
}, this.id);
this.status({ fill: "red", shape: "ring", text: "Disconnected" });
this.on('close', function (done) {
if (node.brokerConn) {
node.brokerConn.unsubscribe(node.topic, node.id);
node.brokerConn.deregister(node, done);
}
});
} else {
this.error(RED._("mqtt.errors.missing-config"));
}
}
RED.nodes.registerType("indigo mqtt in", IndigoMQTTInNode);
function trimStart(character, string) {
var startIndex = 0;
while (string[startIndex] === character) {
startIndex++;
}
return string.substr(startIndex);
}
}