-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxbeeapi.js
More file actions
102 lines (79 loc) · 3.05 KB
/
xbeeapi.js
File metadata and controls
102 lines (79 loc) · 3.05 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module.exports = function(RED) {
"use strict";
var settings = RED.settings;
var xbee_api = require("xbee-api");
function XbeeApiConfigNode(n) {
RED.nodes.createNode(this,n);
this.api_mode = n.api_mode || "1";
this.module = n.module || "Any";
this.convert_adc = n.convert_adc || "true";
this.vref_adc = parseInt(n.vref_adc) || 1200;
this.raw_frames = false;
}
RED.nodes.registerType("xbeeapi-config", XbeeApiConfigNode);
function XbeeApiOutNode(n) {
RED.nodes.createNode(this,n);
this.xbeeapiconfig = n.xbeeapiconfig;
this.xbeeApiConfigNode = RED.nodes.getNode(this.xbeeapiconfig);
if (this.xbeeApiConfigNode) {
var node = this;
node.xbeeAPI = new xbee_api.XBeeAPI(node.xbeeApiConfigNode);
node.on("input",function(msg) {
if (msg.hasOwnProperty("payload")) {
var payload = msg.payload;
if (typeof payload !== "object") {
if (Buffer.isBuffer(payload)) {
payload = payload.toString();
}
payload = JSON.parse(payload);
}
try {
payload = node.xbeeAPI.buildFrame(payload);
} catch (error) {
node.error(error.message, msg);
}
node.send({"payload": payload});
}
});
} else {
this.error(RED._("xbeeapi.errors.missing-conf"));
}
this.on("close", function(done) {
done();
});
}
RED.nodes.registerType("xbeeapi out",XbeeApiOutNode);
function XbeeApiInNode(n) {
RED.nodes.createNode(this,n);
this.xbeeapiconfig = n.xbeeapiconfig;
this.xbeeApiConfigNode = RED.nodes.getNode(this.xbeeapiconfig);
if (this.xbeeApiConfigNode) {
var node = this;
node.xbeeAPI = new xbee_api.XBeeAPI(node.xbeeApiConfigNode);
node.on("input",function(msg) {
if (msg.hasOwnProperty("payload")) {
var payload = msg.payload;
if (!Buffer.isBuffer(payload)) {
if (typeof payload === "object") {
payload = JSON.stringify(payload);
}
else {
payload = payload.toString();
}
payload = Buffer.from(payload, 'utf-8');
}
node.xbeeAPI.parseRaw(payload);
}
});
node.xbeeAPI.on("frame_object", function(frame) {
node.send({"payload": frame});
});
} else {
this.error(RED._("xbeeapi.errors.missing-conf"));
}
this.on("close", function(done) {
done();
});
}
RED.nodes.registerType("xbeeapi in",XbeeApiInNode);
}