-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode-red-enocean-node.js
171 lines (140 loc) · 4 KB
/
node-red-enocean-node.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
module.exports = function (RED) {
// Configuration node
function EnOceanConfig(n) {
RED.nodes.createNode(this, n);
this.serialport = n.serialport;
this.knownsensorarray = require(__dirname + "/knownSensors.json");
this.enocean = {};
this.enocean = require("node-enocean")({
sensorFilePath: __dirname + "/knownSensors.json",
configFilePath: __dirname + "/config.json",
timeout: 30
});
var node = this;
// this.enocean = enocean;
try {
node.enocean.listen(node.serialport);
} catch (err) {
console.log(err);
}
node.on('close', function () {
node.enocean.close();
});
node.enocean.on("error", function (error) {
console.log(error);
});
node.enocean.on("disconnect", function () {
// node.enocean.listen(node.serialport);
});
};
RED.nodes.registerType("enocean-config-node", EnOceanConfig);
function EnOceanListener(n) {
RED.nodes.createNode(this, n);
var jsonObject = require(__dirname + "/knownSensors.json");
//map to Array:
var knownsensorarray = Object.keys(jsonObject).map(function (key) { return jsonObject[key]; });
RED.httpAdmin.get('/knownsensors', function(req, res) {
res.json(knownsensorarray || []);
});
this.knownsensor = n.knownsensor;
this.devicefilter = n.devicefilter;
this.devices = n.devices;
var server = RED.nodes.getNode(n.serialport);
var node = this;
server.enocean.on("ready", function () {
node.status({
fill: 'green',
shape: "ring",
text: "connected"
});
});
node.on('close', function () {
server.enocean.close();
});
node.on('input', function (msg) {
// we can trigger a learning function
server.enocean.startLearning();
server.enocean.on("learned", function (data) {
if (msg.payload) {
data.title = msg.payload;
}
node.status({
fill: 'green',
shape: "ring",
text: "Sensor teached in"
});
sendPayload(data);
});
});
server.enocean.setMaxListeners(Infinity);
server.enocean.on("data", function (data) {
// DATA ID means we have no teach in telegram
if (!data.id) {
node.status({
fill: 'green',
shape: "ring",
text: "Data received"
});
//only react to incoming requests which are already learned in
if (node.knownsensor) {
//we can let all sensors through which are learned in or only selected
if (node.devicefilter === "target") {
if (node.devices.length > 0) {
for (var i = 0; i < node.devices.length; i++) {
if (data.senderId === node.devices[i]) {
sendPayload(data);
}
}
}
// else: all known Sensors are allowed to pass
} else {
for (var k = 0; k < knownsensorarray.length; k++) {
if (data.senderId === knownsensorarray[k].id) {
sendPayload(data);
}
}
}
} else {
sendPayload(data);
}
}
});
function sendPayload(data) {
var msg = {};
msg.payload = {
data
};
node.send(msg);
}
server.enocean.on("error", function (error) {
console.log(error);
node.status({
fill: 'red',
shape: "ring",
text: "Error: " + error
});
node.error(error);
});
server.enocean.on("close", function () {
node.status({
fill: 'red',
shape: "ring",
text: "No Connection to Serialport"
});
});
server.enocean.on("disconnect", function () {
node.status({
fill: 'yellow',
shape: "ring",
text: "Disconnected"
});
});
node.status({
fill: 'yellow',
shape: "ring",
text: "Connecting..."
});
};
RED.nodes.registerType("enocean-listener", EnOceanListener);
};