domotz-remote-pawn
Version:
Domotz Agent
140 lines (118 loc) • 5.16 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz UK LLP
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
*
* Created by Massimiliano Cuzzoli <massi@domotz.com> on 26/04/16.
*/
var common = require('./../../snmp/common.js');
function _dumpOidVarbinds(varbinds) {
var data_dump = [];
for (var i = 0; i < varbinds.length; i++) {
data_dump.push(JSON.stringify({
oid: varbinds[i].oid,
type: varbinds[i].type.name,
value: varbinds[i].value}))
}
return data_dump.join(", ");
}
function _oidTypeConversion(type_name, snmp) {
var converted_type = null;
switch (type_name) {
case 'int':
converted_type = snmp.ObjectType.Integer32;
break;
case 'string':
converted_type = snmp.ObjectType.OctetString;
break;
}
return converted_type;
}
function _createOidVarbinds(oids_mapping, snmp) {
"use strict";
var oid_binds = [];
for (var oid in oids_mapping) {
if (oids_mapping.hasOwnProperty(oid)) {
var var_bind = {
oid: oid,
type: _oidTypeConversion(oids_mapping[oid].type, snmp),
value: oids_mapping[oid].value
};
if (var_bind.type === null) {
var message = 'Invalid OID type received: ' + oid + ' -> ' + oids_mapping[oid].type;
console.error(message);
throw new TypeError(message);
}
oid_binds.push(var_bind);
}
}
return oid_binds;
}
var snmpSet = function (message, resourceLocator) {
var snmp = resourceLocator.netSnmp;
var cmdUtils = resourceLocator.utils.command;
var cid = message.correlationId;
var options = message.payload;
var oidsMapping = cmdUtils.extractFromOptions(options, 'oids', {});
var host = cmdUtils.extractFromOptions(options, 'host', 'localhost');
var community = cmdUtils.extractFromOptions(options, 'community', 'public');
var oids = common.extractOids(oidsMapping);
// Adjust the default
options.version = options.version || snmp.Version2c;
return {
execute: function (callback) {
var oid_binds = _createOidVarbinds(oidsMapping, snmp);
console.info('SNMP_SET - (%s): Sending SNMP set request to host %s' +
', setting up oids: %s', cid, host, _dumpOidVarbinds(oid_binds));
var snmpSession = snmp.createSession(host, community, options);
snmpSession.set(oid_binds, function (error, response) {
console.debug("SNMP_SET - (%s): response_callback invoked ...", cid);
console.debug("SNMP_SET - (%s): error is %s", cid, JSON.stringify(error));
console.debug("SNMP_SET - (%s): response is %s", cid, JSON.stringify(response));
if (error) {
if(common.isEmpty(error) && response === undefined){
console.info("SNMP_SET - (%s): ignoring callback since error is empty and response is undefined", cid);
return;
} else {
console.warn("SNMP_SET - (%s): SNMP error received: %s", cid, error);
response = {"error": error};
}
} else {
if (snmp.isVarbindError(response[0])) {
var varbind_error = snmp.varbindError(response[0]);
console.warn("SNMP_SET - (%s): varbind error - %s", cid, varbind_error);
response = {"error": varbind_error};
} else {
console.info("SNMP_SET - (%s): Got response - number of returned oids: %s", cid, response.length);
var values = {};
response = {"oids": values}; // for the time being we do not return any detail on changed items
}
}
console.debug("SNMP_SET - (%s): command execution concluded, yielding result: %s", cid, JSON.stringify(response));
snmpSession.close();
callback(response);
});
},
describe: function () {
var oid_binds = _createOidVarbinds(oidsMapping, snmp);
return "snmpSet - " + host + ' - ' + _dumpOidVarbinds(oid_binds);
},
getOidBindsCreator: function () {
return _createOidVarbinds;
}
};
};
module.exports.command = snmpSet;