forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
55-dweetio.js
62 lines (53 loc) · 1.95 KB
/
55-dweetio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
if (dweet.content.hasOwnProperty("payload")) {
dweet.payload = dweet.content.payload;
}
else {
dweet.payload = dweet.content;
}
delete dweet.content;
node.send(dweet);
});
this.on("close", function() {
dweetio.stop_listening_for(node.thing);
});
}
RED.nodes.registerType("dweetio in",DweetioInNode);
}