domotz-remote-pawn
Version:
Domotz Agent
148 lines (129 loc) • 5.34 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.
*/
function _dumpOidVarbinds(varbinds) {
var dataDump = [];
for (var i = 0; i < varbinds.length; i++) {
dataDump.push(
JSON.stringify({
oid: varbinds[i].oid,
type: varbinds[i].type.name,
value: varbinds[i].value,
})
);
}
return dataDump.join(', ');
}
function _oidTypeConversion(typeName, snmp) {
var convertedType = null;
switch (typeName) {
case 'int':
convertedType = snmp.ObjectType.Integer32;
break;
case 'string':
convertedType = snmp.ObjectType.OctetString;
break;
}
return convertedType;
}
function _createOidVarbinds(oidsMapping, snmp) {
'use strict';
var oidBinds = [];
for (var oid in oidsMapping) {
if (oidsMapping.hasOwnProperty(oid)) {
var varBind = {
oid: oid,
type: _oidTypeConversion(oidsMapping[oid].type, snmp),
value: oidsMapping[oid].value,
};
if (varBind.type === null) {
var message = 'Invalid OID type received: ' + oid + ' -> ' + oidsMapping[oid].type;
throw new TypeError(message);
}
oidBinds.push(varBind);
}
}
return oidBinds;
}
var snmpSet = function (message, resourceLocator) {
var snmp = resourceLocator.netSnmp;
var cmdUtils = resourceLocator.utils.command;
var snmpCommon = resourceLocator.snmpCommon;
var cid = message.correlationId;
var myConsole = resourceLocator.log.decorateLogs().decorate(String(cid));
var options = message.payload;
var oidsMapping = cmdUtils.extractFromOptions(options, 'oids', {});
var host = cmdUtils.extractFromOptions(options, 'host', 'localhost');
return {
execute: function (callback) {
var bindings = snmpCommon.getInterfaceBindings(options, resourceLocator);
var snmpSession = null;
try {
snmpSession = snmpCommon.getSession(bindings, host, options);
} catch (error) {
callback({ error: error.toString() });
return;
}
myConsole.debug('actually using version %d', snmpSession.version);
var oidBinds = _createOidVarbinds(oidsMapping, snmp);
myConsole.info('Sending SNMP set request to host %s' + ', setting up oids: %s', host, _dumpOidVarbinds(oidBinds));
snmpSession.set(oidBinds, function (error, response) {
myConsole.debug('response_callback invoked ...');
myConsole.debug('error is %s', JSON.stringify(error));
myConsole.debug('response is %s', JSON.stringify(response));
if (error) {
if (snmpCommon.isEmpty(error) && response === undefined) {
myConsole.info('ignoring callback since error is empty and response is undefined');
return;
} else {
myConsole.debug('SNMP error received: %s', error);
response = { error: error };
}
} else {
if (snmp.isVarbindError(response[0])) {
var varbindError = snmp.varbindError(response[0]);
myConsole.warn('varbind error - %s', varbindError);
response = { error: varbindError };
} else {
myConsole.info('Got response - number of returned oids: %s', response.length);
var values = {};
response = { oids: values }; // for the time being we do not return any detail on changed items
}
}
myConsole.debug('command execution concluded, yielding result: %s', JSON.stringify(response));
try {
snmpSession.close();
} catch (error) {
myConsole.error('cannot close session: ' + error);
} finally {
callback(response);
}
});
},
describe: function () {
var oidBinds = _createOidVarbinds(oidsMapping, snmp);
return 'snmpSet - ' + host + ' - ' + _dumpOidVarbinds(oidBinds);
},
getOidBindsCreator: function () {
return _createOidVarbinds;
},
};
};
module.exports.command = snmpSet;