1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Add timeout option to snap nodes

to address #397
This commit is contained in:
Dave Conway-Jones 2018-01-28 17:50:41 +00:00
parent 17886b7296
commit b3fd755527
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
3 changed files with 31 additions and 13 deletions

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-snmp",
"version" : "0.0.15",
"version" : "0.0.16",
"description" : "A Node-RED node that looks for SNMP oids.",
"dependencies" : {
"net-snmp" : "^1.1.19"

View File

@ -9,10 +9,12 @@
</div>
<div class="form-row">
<label for="node-input-version"><i class="fa fa-bookmark"></i> Version</label>
<select type="text" id="node-input-version" style="width: 150px;">
<select type="text" id="node-input-version" style="width:150px;">
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
<span style="margin-left:50px;">Timeout</span>
<input type="text" id="node-input-timeout" placeholder="secs" style="width:50px; direction:rtl; vertical-align:baseline;">&nbsp;S
</div>
<div class="form-row">
<label for="node-input-oids"><i class="fa fa-tags"></i> OIDs</label>
@ -42,6 +44,7 @@
community: { value: "public" },
version: { value: "1", required: true },
oids: { value: "" },
timeout: { value: 5 },
name: { value: "" }
},
inputs: 1,
@ -72,6 +75,8 @@
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
<span style="margin-left:50px;">Timeout</span>
<input type="text" id="node-input-timeout" placeholder="secs" style="width:50px; direction:rtl; vertical-align:baseline;">&nbsp;S
</div>
<div class="form-row">
<label for="node-input-varbinds"><i class="fa fa-tags"></i> Varbinds</label>
@ -140,6 +145,8 @@
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
<span style="margin-left:50px;">Timeout</span>
<input type="text" id="node-input-timeout" placeholder="secs" style="width:50px; direction:rtl; vertical-align:baseline;">&nbsp;S
</div>
<div class="form-row">
<label for="node-input-oids"><i class="fa fa-tags"></i> OID</label>
@ -169,6 +176,7 @@
community: { value: "public" },
version: { value: "1", required: true },
oids: { value: "" },
timeout: { value: 5 },
name: { value: "" }
},
inputs: 1,
@ -199,6 +207,8 @@
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
<span style="margin-left:50px;">Timeout</span>
<input type="text" id="node-input-timeout" placeholder="secs" style="width:50px; direction:rtl; vertical-align:baseline;">&nbsp;S
</div>
<div class="form-row">
<label for="node-input-oids"><i class="fa fa-tags"></i> OID</label>
@ -228,6 +238,7 @@
community: { value: "public" },
version: { value: "1", required: true },
oids: { value: "" },
timeout: { value: 5 },
name: { value: "" }
},
inputs: 1,
@ -260,6 +271,8 @@
<option value="1">v1</option>
<option value="2c">v2c</option>
</select>
<span style="margin-left:50px;">Timeout</span>
<input type="text" id="node-input-timeout" placeholder="secs" style="width:50px; direction:rtl; vertical-align:baseline;">&nbsp;S
</div>
<div class="form-row">
<label for="node-input-oids"><i class="fa fa-tags"></i> OID</label>
@ -289,6 +302,7 @@
community: { value: "public" },
version: { value: "1", required: true },
oids: { value: "" },
timeout: { value: 5 },
name: { value: "" }
},
inputs: 1,

View File

@ -5,10 +5,10 @@ module.exports = function (RED) {
var sessions = {};
function getSession(host, community, version) {
function getSession(host, community, version, timeout) {
var sessionKey = host + ":" + community + ":" + version;
if (!(sessionKey in sessions)) {
sessions[sessionKey] = snmp.createSession(host, community, { version: version });
sessions[sessionKey] = snmp.createSession(host, community, { version:version, timeout:(timeout || 5000) });
}
return sessions[sessionKey];
}
@ -19,6 +19,7 @@ module.exports = function (RED) {
this.host = n.host;
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g, "");
this.timeout = Number(n.timeout || 5) * 1000;
var node = this;
this.on("input", function (msg) {
@ -26,7 +27,7 @@ module.exports = function (RED) {
var community = node.community || msg.community;
var oids = node.oids || msg.oid;
if (oids) {
getSession(host, community, node.version).get(oids.split(","), function (error, varbinds) {
getSession(host, community, node.version, node.timeout).get(oids.split(","), function (error, varbinds) {
if (error) {
node.error(error.toString(), msg);
}
@ -54,13 +55,13 @@ module.exports = function (RED) {
}
RED.nodes.registerType("snmp", SnmpNode);
function SnmpSNode(n) {
RED.nodes.createNode(this, n);
this.community = n.community;
this.host = n.host;
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.varbinds = n.varbinds;
this.timeout = Number(n.timeout || 5) * 1000;
var node = this;
this.on("input", function (msg) {
var host = node.host || msg.host;
@ -70,7 +71,7 @@ module.exports = function (RED) {
for (var i = 0; i < varbinds.length; i++) {
varbinds[i].type = snmp.ObjectType[varbinds[i].type];
}
getSession(host, community, node.version).set(varbinds, function (error, varbinds) {
getSession(host, community, node.version, node.timeout).set(varbinds, function (error, varbinds) {
if (error) {
node.error(error.toString(), msg);
}
@ -93,14 +94,13 @@ module.exports = function (RED) {
RED.nodes.registerType("snmp set", SnmpSNode);
function SnmpTNode(n) {
RED.nodes.createNode(this, n);
this.community = n.community;
this.host = n.host;
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g, "");
this.timeout = Number(n.timeout || 5) * 1000;
var node = this;
var maxRepetitions = 20;
@ -116,7 +116,7 @@ module.exports = function (RED) {
var oids = node.oids || msg.oid;
if (oids) {
msg.oid = oids;
getSession(host, community, node.version).table(oids, maxRepetitions, function (error, table) {
getSession(host, community, node.version, node.timeout).table(oids, maxRepetitions, function (error, table) {
if (error) {
node.error(error.toString(), msg);
}
@ -153,12 +153,14 @@ module.exports = function (RED) {
}
RED.nodes.registerType("snmp table", SnmpTNode);
function SnmpSubtreeNode(n) {
RED.nodes.createNode(this, n);
this.community = n.community;
this.host = n.host;
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g, "");
this.timeout = Number(n.timeout || 5) * 1000;
var node = this;
var maxRepetitions = 20;
var response = [];
@ -181,7 +183,7 @@ module.exports = function (RED) {
var oids = node.oids || msg.oid;
if (oids) {
msg.oid = oids;
getSession(host, community, node.version).subtree(msg.oid, maxRepetitions, feedCb, function (error) {
getSession(host, community, node.version, node.timeout).subtree(msg.oid, maxRepetitions, feedCb, function (error) {
if (error) {
node.error(error.toString(), msg);
}
@ -200,12 +202,14 @@ module.exports = function (RED) {
}
RED.nodes.registerType("snmp subtree", SnmpSubtreeNode);
function SnmpWalkerNode(n) {
RED.nodes.createNode(this, n);
this.community = n.community;
this.host = n.host;
this.version = (n.version === "2c") ? snmp.Version2c : snmp.Version1;
this.oids = n.oids.replace(/\s/g, "");
this.timeout = Number(n.timeout || 5) * 1000;
var node = this;
var maxRepetitions = 20;
var response = [];
@ -229,7 +233,7 @@ module.exports = function (RED) {
var community = node.community || msg.community;
if (oids) {
msg.oid = oids;
getSession(host, community, node.version).walk(msg.oid, maxRepetitions, feedCb, function (error) {
getSession(host, community, node.version, node.timeout).walk(msg.oid, maxRepetitions, feedCb, function (error) {
if (error) {
node.error(error.toString(), msg);
}