UNPKG

domotz-remote-pawn

Version:

Domotz Agent

118 lines (103 loc) 4.15 kB
/** This file is part of Domotz Agent. * Copyright (C) 2022 Domotz LTD * * 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/>. **/ const MAX_ID_LENGTH = 45; var _ = require('lodash'); function TimedSnmpSession(snmpSession, params, myConsole) { this.snmpSession = snmpSession; this.myConsole = myConsole; this.id = genID(params); } function genID(params) { var id = Object.keys(params).reduce(function (piece, key) { return piece.concat(params[key] + '-'); }, ''); if (id.length > MAX_ID_LENGTH) { id = id.substring(0, MAX_ID_LENGTH); id = _.uniqueId(id + '___'); } return id; } TimedSnmpSession.prototype.subtree = function (oid, maxRepetitions, valueArrived, done, hardTimeout) { var sessionClosing = false; var context = this; var timeout = setTimeout(function () { context.myConsole.info('Forcibly Closing Snmp Session ' + context.id); sessionClosing = true; try { context.snmpSession.close(); } catch (e) { context.myConsole.warn('Error in session close %s', e.toString()); } done('Snmp Session forcibly closed ' + context.id); }, hardTimeout); this.snmpSession.subtree(oid, maxRepetitions, valueArrived, function (error) { context.myConsole.verbose('Got response, removing timer for session ' + context.id); clearTimeout(timeout); if (sessionClosing) { context.myConsole.verbose('Session id ' + context.id + ' already closing, skipping all notifications'); } else { done(error); } }); }; TimedSnmpSession.prototype.get = function (oids, callback, hardTimeout) { var sessionClosing = false; var context = this; var timeout = setTimeout(function () { context.myConsole.info('Forcibly Closing Snmp Session ' + context.id); sessionClosing = true; try { context.snmpSession.close(); } catch (e) { context.myConsole.warn('Error in session close %s', e.toString()); } callback('Snmp Session forcibly closed ' + context.id); }, hardTimeout); this.snmpSession.get(oids, function (error, response) { context.myConsole.verbose('Got response, removing timer for session ' + context.id); clearTimeout(timeout); if (sessionClosing) { context.myConsole.verbose('Session id ' + context.id + ' already closing, skipping all notifications'); } else { callback(error, response); } }); }; TimedSnmpSession.prototype.getNext = function (oids, callback, hardTimeout) { var sessionClosing = false; var context = this; var timeout = setTimeout(function () { context.myConsole.info('Forcibly Closing Snmp Session ' + context.id); sessionClosing = true; try { context.snmpSession.close(); } catch (e) { context.myConsole.warn('Error in session close %s', e.toString()); } callback('Snmp Session forcibly closed ' + context.id); }, hardTimeout); this.snmpSession.getNext(oids, function (error, response) { context.myConsole.verbose('Got response, removing timer for session ' + context.id); clearTimeout(timeout); if (sessionClosing) { context.myConsole.verbose('Session id ' + context.id + ' already closing, skipping all notifications'); } else { callback(error, response); } }); }; module.exports = TimedSnmpSession;