1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/social/dweetio/55-dweetio.js

63 lines
1.9 KiB
JavaScript
Raw Permalink Normal View History

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;
if (dweetio == null) { dweetio = new DweetClient(); }
var node = this;
var isObject = function(a) {
if ((typeof(a) === "object") && (!Buffer.isBuffer(a)) && (!Array.isArray(a))) { return true; }
else { return false; }
};
this.on("input",function(msg) {
if (!isObject(msg.payload)) {
msg.payload = {payload:msg.payload};
}
var thing = node.thing || msg.thing;
try {
dweetio.dweet_for(thing, msg.payload, function(err, dweet) {
//console.log(dweet.thing); // "my-thing"
//console.log(dweet.content); // The content of the dweet
//console.log(dweet.created); // The create date of the dweet
});
}
catch (err) {
node.log(err);
}
});
}
RED.nodes.registerType("dweetio out",DweetioOutNode);
function DweetioInNode(n) {
RED.nodes.createNode(this,n);
this.thing = n.thing;
if (dweetio == null) { dweetio = new DweetClient(); }
var node = this;
dweetio.listen_for(node.thing, function(dweet) {
// 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;
2017-01-29 18:45:44 +01:00
}
else {
dweet.payload = dweet.content;
2014-08-07 10:52:16 +02:00
}
delete dweet.content;
node.send(dweet);
});
this.on("close", function() {
dweetio.stop_listening_for(node.thing);
});
}
RED.nodes.registerType("dweetio in",DweetioInNode);
}