mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Use environment variable to set path for nodes....
This commit is contained in:
@@ -13,45 +13,45 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
**/
|
||||
var RED = require("../../red/red");
|
||||
|
||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
||||
var spawn = require('child_process').spawn;
|
||||
var plat = require('os').platform();
|
||||
|
||||
function PingNode(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.host = n.host;
|
||||
this.timer = n.timer * 1000;
|
||||
var node = this;
|
||||
RED.nodes.createNode(this,n);
|
||||
this.host = n.host;
|
||||
this.timer = n.timer * 1000;
|
||||
var node = this;
|
||||
|
||||
node.tout = setInterval(function() {
|
||||
var ex;
|
||||
if (plat == "linux") ex = spawn('ping', ['-n', '-w 5', '-c 1', node.host]);
|
||||
else if (plat.match(/^win/)) ex = spawn('ping', ['-n 1', '-w 5000', node.host]);
|
||||
else if (plat == "darwin") ex = spawn('ping', ['-n', '-t 5', '-c 1', node.host]);
|
||||
else node.error("Sorry - your platform - "+plat+" - is not recognised.");
|
||||
var res="";
|
||||
node.tout = setInterval(function() {
|
||||
var ex;
|
||||
if (plat == "linux") ex = spawn('ping', ['-n', '-w 5', '-c 1', node.host]);
|
||||
else if (plat.match(/^win/)) ex = spawn('ping', ['-n 1', '-w 5000', node.host]);
|
||||
else if (plat == "darwin") ex = spawn('ping', ['-n', '-t 5', '-c 1', node.host]);
|
||||
else node.error("Sorry - your platform - "+plat+" - is not recognised.");
|
||||
var res="";
|
||||
ex.stdout.on('data', function (data) {
|
||||
//console.log('[ping] stdout: ' + data.toString());
|
||||
var regex = /time=(.*)ms/;
|
||||
var m = regex.exec(data.toString())||[""];
|
||||
res = Number(m[1]);
|
||||
});
|
||||
ex.stderr.on('data', function (data) {
|
||||
//console.log('[ping] stderr: ' + data);
|
||||
});
|
||||
ex.on('close', function (code) {
|
||||
//console.log('[ping] result: ' + code);
|
||||
var msg = { payload: false, topic:node.host };
|
||||
if (code == 0) msg = { payload: res, topic:node.host };
|
||||
node.send(msg);
|
||||
});
|
||||
}, node.timer);
|
||||
|
||||
ex.stdout.on('data', function (data) {
|
||||
//console.log('[ping] stdout: ' + data.toString());
|
||||
var regex = /time=(.*)ms/;
|
||||
var m = regex.exec(data.toString())||[""];
|
||||
res = Number(m[1]);
|
||||
});
|
||||
ex.stderr.on('data', function (data) {
|
||||
//console.log('[ping] stderr: ' + data);
|
||||
});
|
||||
ex.on('close', function (code) {
|
||||
//console.log('[ping] result: ' + code);
|
||||
var msg = { payload: false, topic:node.host };
|
||||
if (code == 0) msg = { payload: res, topic:node.host };
|
||||
node.send(msg);
|
||||
});
|
||||
this.on("close", function() {
|
||||
clearInterval(this.tout);
|
||||
});
|
||||
|
||||
}, node.timer);
|
||||
}
|
||||
|
||||
RED.nodes.registerType("ping",PingNode);
|
||||
|
||||
PingNode.prototype.close = function() {
|
||||
clearInterval(this.tout);
|
||||
}
|
||||
|
Reference in New Issue
Block a user