2014-06-29 00:36:36 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var DweetClient = require("node-dweetio");
|
|
|
|
var dweetio = null;
|
|
|
|
|
|
|
|
function DweetioOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.thing = n.thing;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (dweetio == null) { dweetio = new DweetClient(); }
|
2014-06-29 00:36:36 +02:00
|
|
|
var node = this;
|
|
|
|
|
2015-07-10 00:27:15 +02:00
|
|
|
var isObject = function(a) {
|
2015-10-29 00:57:09 +01:00
|
|
|
if ((typeof(a) === "object") && (!Buffer.isBuffer(a)) && (!Array.isArray(a))) { return true; }
|
|
|
|
else { return false; }
|
2015-07-10 00:27:15 +02:00
|
|
|
};
|
|
|
|
|
2014-06-29 00:36:36 +02:00
|
|
|
this.on("input",function(msg) {
|
2015-07-10 00:27:15 +02:00
|
|
|
if (!isObject(msg.payload)) {
|
|
|
|
msg.payload = {payload:msg.payload};
|
|
|
|
}
|
2015-05-11 20:25:39 +02:00
|
|
|
var thing = node.thing || msg.thing;
|
|
|
|
try {
|
2015-07-10 00:27:15 +02:00
|
|
|
dweetio.dweet_for(thing, msg.payload, function(err, dweet) {
|
|
|
|
//console.log(dweet.thing); // "my-thing"
|
2014-06-29 00:36:36 +02:00
|
|
|
//console.log(dweet.content); // The content of the dweet
|
|
|
|
//console.log(dweet.created); // The create date of the dweet
|
|
|
|
});
|
2015-05-11 20:25:39 +02:00
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
node.log(err);
|
|
|
|
}
|
2014-06-29 00:36:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("dweetio out",DweetioOutNode);
|
|
|
|
|
|
|
|
function DweetioInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.thing = n.thing;
|
2015-05-11 20:25:39 +02:00
|
|
|
if (dweetio == null) { dweetio = new DweetClient(); }
|
2014-06-29 00:36:36 +02:00
|
|
|
var node = this;
|
|
|
|
|
2015-05-11 21:04:51 +02:00
|
|
|
dweetio.listen_for(node.thing, function(dweet) {
|
2014-06-29 00:36:36 +02:00
|
|
|
// This will be called anytime there is a new dweet for my-thing
|
2014-08-07 10:52:16 +02:00
|
|
|
if (dweet.content.hasOwnProperty("payload")) {
|
2015-08-11 20:56:23 +02:00
|
|
|
dweet.payload = dweet.content.payload;
|
2014-08-07 10:52:16 +02:00
|
|
|
} else {
|
2015-05-11 20:25:39 +02:00
|
|
|
dweet.payload = dweet.content;
|
2014-08-07 10:52:16 +02:00
|
|
|
}
|
2014-06-29 00:36:36 +02:00
|
|
|
delete dweet.content;
|
|
|
|
node.send(dweet);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.on("close", function() {
|
|
|
|
dweetio.stop_listening_for(node.thing);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("dweetio in",DweetioInNode);
|
|
|
|
}
|