-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataview.js
94 lines (83 loc) · 2.86 KB
/
dataview.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
module.exports = function(RED) {
function DataView(config) {
RED.nodes.createNode(this, config);
this.active = (config.active === null || typeof config.active === "undefined") || config.active;
this.passthru = config.passthru;
this.property = config.property || 'payload';
var node = this;
var errorCondition = false;
function sendDataToClient(data, msg) {
var d = {
id:node.id,
};
if (data) {
d.data = data;
}
try {
RED.comms.publish("data-view", d);
}
catch(e) {
node.error("Error sending data", msg);
}
}
function handleError(err, msg, statusText) {
if (!errorCondition) {
node.status({ fill:"red", shape:"dot", text:statusText });
errorCondition = true;
}
node.error(err, msg);
}
function clearError() {
if (errorCondition) {
node.status({});
errorCondition = false;
}
}
node.on("input", function(msg) {
if (this.active !== true) { return; }
let value = msg[node.property];
if (value == null) { // null or undefined
clearError();
sendDataToClient(null, msg); // delete chart
return;
}
if (typeof value !== 'number') {
handleError(`msg.${node.property} is not a number`, msg, `msg.${node.property} is not a number`);
return;
}
if (node.passthru) { node.send(msg); }
clearError();
data = {
value,
time: new Date()
}
sendDataToClient(data, msg);
});
node.on("close", function() {
// send empty data to close the view
RED.comms.publish("data", { id:this.id });
node.status({});
});
}
RED.nodes.registerType("data-view", DataView);
// Via the button on the node (in the FLOW EDITOR), the image pushing can be enabled or disabled
RED.httpAdmin.post("/data-view/:id/:state", RED.auth.needsPermission("image-output.write"), function(req,res) {
var state = req.params.state;
var node = RED.nodes.getNode(req.params.id);
if(node === null || typeof node === "undefined") {
res.sendStatus(404);
return;
}
if (state === "enable") {
node.active = true;
res.send('activated');
}
else if (state === "disable") {
node.active = false;
res.send('deactivated');
}
else {
res.sendStatus(404);
}
});
};