domotz-remote-pawn
Version:
Domotz Agent
126 lines (109 loc) • 5.43 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 19/10/16.
*
*/
var protocolDiscoverySenderFactory = function (domotzApp, localDeviceMaps) {
"use strict";
var crypto = domotzApp.crypto;
var engineRequest = domotzApp.engineRequest;
var deviceMaps = localDeviceMaps;
var discoveryConsole = domotzApp.log.decorateLogs('DISCOVERY - ');
var snmp_discovery = domotzApp.discovery.snmp_basic_discovery;
// Cache of sent data indexed by protocol and device ID
var sentDataCache = {
bonjour: {},
upnp: {},
snmp: {}
};
function sendAndCacheDiscoveryResult(deviceId, payload, protocolDriver) {
var md5 = crypto.createHash('md5').update(JSON.stringify(payload)).digest("hex");
var protocolName = protocolDriver.getProtocolName();
discoveryConsole.info("(%s): examining result for device ", protocolName.toUpperCase(), deviceId);
if (!sentDataCache[protocolName][deviceId]) {
_uploadInspectionDataAndCacheIt(deviceId, payload, md5, protocolName);
return;
}
var cached_md5 = sentDataCache[protocolName][deviceId].md5;
discoveryConsole.debug("(%s): driver cache hit for device id %s , cached md5 is %s",
protocolName.toUpperCase(), deviceId, cached_md5);
discoveryConsole.debug("(%s): computed md5 is %s ", protocolName.toUpperCase(), md5);
if (cached_md5 !== md5) {
_uploadInspectionDataAndCacheIt(deviceId, payload, md5, protocolName);
return;
}
discoveryConsole.info("(%s): md5 values match, Avoiding Upload service data for device %s",
protocolName.toUpperCase(), deviceId);
}
function _uploadInspectionDataAndCacheIt(deviceId, payload, md5, protocolName) {
discoveryConsole.debug("(%s): ********* ACTUALLY UPLOADING RESULT FOR DEVICE %s WITH PAYLOAD %s (md5 %s) ",
protocolName.toUpperCase(), deviceId, JSON.stringify(payload), md5);
engineRequest.sendWithPromise('/device/' + deviceId + '/inspection-data/' + protocolName,
'PUT', payload)
.then(function () {
discoveryConsole.info("(%s): result successfully uploaded for device %s - md5 %s ",
protocolName.toUpperCase(), deviceId, md5);
sentDataCache[protocolName][deviceId] = {
md5: md5,
payload: payload
};
});
}
function sendSnmpInterfaceNumber(ip, discoveryData, protocolName) {
var deviceId = deviceMaps.ipToId[ip];
discoveryConsole.debug("Sending interfaces number to engine %s %s", ip, deviceId);
try {
var prettyProtocolName = protocolName.toUpperCase();
if (!deviceId) {
deviceId = deviceMaps.newDeviceIdByIp[ip];
}
if (deviceId && !discoveryData.error) {
var interfaceNumber = discoveryData.oids[snmp_discovery.getOidForInterfaceNumber()];
if (interfaceNumber === undefined || interfaceNumber === null) {
discoveryConsole.info("(%s): Interface number not found; deviceId=%d", deviceId);
return;
}
if (deviceMaps.interfaceNumberByIp[ip].interface_number_snmp === interfaceNumber) {
discoveryConsole.debug("(%s): Interface number unchanged; deviceId=%d", deviceId);
return;
}
discoveryConsole.info("(%s): Updating InterfaceNumber=%d for deviceId=%d", prettyProtocolName,
interfaceNumber, deviceId);
var payload = {interface_number_snmp: interfaceNumber};
engineRequest.sendWithPromise('/device/' + deviceId + '/interface-number', 'PUT', payload)
.then(function () {
discoveryConsole.debug("(%s): Interface Number successfully uploaded for device %d",
prettyProtocolName, deviceId);
})
.catch(function (e) {
discoveryConsole.error("Error on Snmp Interface Number sending: %s", e);
discoveryConsole.error(e.stack);
}
);
}
} catch (e) {
discoveryConsole.error("Error on Snmp Interface Number sending: %s", e);
discoveryConsole.error(e.stack);
}
}
return {
sendAndCacheDiscoveryResult: sendAndCacheDiscoveryResult,
sendSnmpInterfaceNumber: sendSnmpInterfaceNumber
};
};
module.exports.factory = protocolDiscoverySenderFactory;