-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.js
More file actions
79 lines (66 loc) · 2.39 KB
/
Status.js
File metadata and controls
79 lines (66 loc) · 2.39 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
module.exports = function (RED) {
var dc = require('./Calls.js');
function DCloudStatus(cfg) {
"use strict";
RED.nodes.createNode(this, cfg);
var node = this;
node.config = cfg;
// node-specific code goes here
var util = require('util');
var async = require('async');
var request = require('request');
this.source = RED.nodes.getNode(node.config.source);
testSource();
var timerRef = 0;
//Trigger a test when something comes in
this.on('input', function (msg) {
clearTimeout(timerRef);
testSource();
});
this.on('close', function () {
clearTimeout(timerRef);
this.log('Status close');
});
function testSource() {
try {
var msg = {};
msg.topic = "statusErr";
if (node.source) {
node.status({ fill: "yellow", shape: "ring", text: "testing source" });
dc.status(node.source, function (err, res) {
if (err) {
console.log(err);
node.status({ fill: "red", shape: "ring", text: "err" });
msg.topic = 'statusErr';
msg.payload = err;
}
else {
msg.payload = res;
node.status({ fill: "green", shape: "ring", text: "ok" });
msg.topic = "statusOk";
}
txandrerun(msg);
});
}
else {
node.status({ fill: "red", shape: "ring", text: "source missing" });
msg.payload = "Source missing";
txandrerun(msg);
}
}
catch (err) {
console.error(err);
timerRef = setTimeout(testSource, 30 * 1000);
}
}
/**
* Transmit the message and rearm the timer
* @param msg
*/
function txandrerun(msg) {
node.send(msg);
timerRef = setTimeout(testSource, 30 * 1000);
}
}
RED.nodes.registerType("DCloud-Status", DCloudStatus);
}