2015-06-13 19:47:22 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var redis = require("redis");
|
|
|
|
|
|
|
|
var hashFieldRE = /^([^=]+)=(.*)$/;
|
|
|
|
|
2015-06-16 15:38:36 +02:00
|
|
|
var redisConnectionPool = (function() {
|
2015-06-13 19:47:22 +02:00
|
|
|
var connections = {};
|
|
|
|
var obj = {
|
|
|
|
get: function(host,port) {
|
|
|
|
var id = host+":"+port;
|
|
|
|
if (!connections[id]) {
|
|
|
|
connections[id] = redis.createClient(port,host);
|
|
|
|
connections[id].on("error",function(err) {
|
|
|
|
RED.log.error(err);
|
|
|
|
});
|
|
|
|
connections[id]._id = id;
|
|
|
|
connections[id]._nodeCount = 0;
|
|
|
|
}
|
|
|
|
connections[id]._nodeCount += 1;
|
|
|
|
return connections[id];
|
|
|
|
},
|
|
|
|
close: function(connection) {
|
|
|
|
connection._nodeCount -= 1;
|
|
|
|
if (connection._nodeCount === 0) {
|
|
|
|
if (connection) {
|
|
|
|
clearTimeout(connection.retry_timer);
|
|
|
|
connection.end();
|
|
|
|
}
|
2015-06-16 15:38:36 +02:00
|
|
|
delete connections[connection._id];
|
2015-06-13 19:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return obj;
|
2015-06-16 15:38:36 +02:00
|
|
|
}());
|
2015-06-13 19:47:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
function RedisOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.port = n.port||"6379";
|
|
|
|
this.hostname = n.hostname||"127.0.0.1";
|
|
|
|
this.key = n.key;
|
|
|
|
this.structtype = n.structtype;
|
|
|
|
|
|
|
|
this.client = redisConnectionPool.get(this.hostname,this.port);
|
|
|
|
|
|
|
|
if (this.client.connected) {
|
2015-07-07 22:31:28 +02:00
|
|
|
this.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-07-07 22:31:28 +02:00
|
|
|
this.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"},true);
|
2015-06-13 19:47:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var node = this;
|
|
|
|
this.client.on("end", function() {
|
2015-07-07 22:31:28 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.disconnected"});
|
2015-06-13 19:47:22 +02:00
|
|
|
});
|
|
|
|
this.client.on("connect", function() {
|
2015-07-07 22:31:28 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2015-06-13 19:47:22 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
this.on("input", function(msg) {
|
|
|
|
var k = this.key || msg.topic;
|
|
|
|
if (k) {
|
|
|
|
if (this.structtype == "string") {
|
|
|
|
this.client.set(k,RED.util.ensureString(msg.payload));
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else if (this.structtype == "hash") {
|
2015-06-13 19:47:22 +02:00
|
|
|
if (typeof msg.payload == "object") {
|
|
|
|
this.client.hmset(k,msg.payload);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-06-13 19:47:22 +02:00
|
|
|
var r = hashFieldRE.exec(msg.payload);
|
|
|
|
if (r) {
|
|
|
|
this.client.hset(k,r[1],r[2]);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-06-16 11:36:19 +02:00
|
|
|
this.warn(RED._("redisout.errors.invalidpayload"));
|
2015-06-13 19:47:22 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else if (this.structtype == "set") {
|
2015-06-13 19:47:22 +02:00
|
|
|
this.client.sadd(k,msg.payload);
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else if (this.structtype == "list") {
|
2015-06-13 19:47:22 +02:00
|
|
|
this.client.rpush(k,msg.payload);
|
|
|
|
}
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-06-16 11:36:19 +02:00
|
|
|
this.warn(RED._("redisout.errors.nokey"));
|
2015-06-13 19:47:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.on("close", function() {
|
|
|
|
redisConnectionPool.close(node.client);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("redis out",RedisOutNode);
|
|
|
|
}
|