UNPKG

domotz-remote-pawn

Version:

Domotz Agent

149 lines (124 loc) 4.28 kB
function DeviceOidInfo() { this.validOidList = []; this.oidRootValidList = []; // mibOidRootSet this.dataSentTime = null; this.alreadySentData = false; this.firstIteration = true; } DeviceOidInfo.prototype.getOidToCheck = function (trie) { var toCheckForSearch = []; if (this.validOidList.length > 0) { console.info('SNMP_MIB_DISCOVERY validOidList is empty '); for (var i = 0; i < this.validOidList.length; i++) { console.info('SNMP_MIB_DISCOVERY checking valid oid ' + this.validOidList[i] + ' in trie '); var node = trie.find(this.validOidList[i]); if (node !== undefined) { toCheckForSearch = toCheckForSearch.concat(node.getPrefixListChildren()); console.info('SNMP_MIB_DISCOVERY found valid oid in trie ' + toCheckForSearch); } } this.validOidList = []; } else if (this.firstIteration === true) { toCheckForSearch = trie.getPrefixList(); console.info('SNMP_MIB_DISCOVERY this is first discovery iteration, returning first ' + toCheckForSearch.length + ' oid_root in list '); this.firstIteration = false; this.validOidList = []; this.oidRootValidList = []; } return toCheckForSearch; }; DeviceOidInfo.prototype.isDataToSend = function () { if (this.validOidList.length === 0) { if (this.oidRootValidList.length > 0 && this.oidRootValidList.length > 0) { console.info('SNMP_MIB_DISCOVERY should now send data to backend '); return true; } else { return false; } } else { return false; } }; module.exports.DeviceOidInfo = DeviceOidInfo; // https://gist.github.com/tpae/72e1c54471e88b689f85ad2b3940a8f0 function TrieNode(key, prefix) { this.parent = null; this.children = {}; this.oidRoot = false; this.key = typeof key === 'undefined' ? '' : key; this.prefix = typeof prefix === 'undefined' ? key : prefix + '.' + key; } TrieNode.prototype.navigateIfOneChildren = function () { if (this.isOIDRoot()) return this; if (Object.keys(this.children).length === 1) { return this.children[Object.keys(this.children)[0]].navigateIfOneChildren(); } else { return this; } }; TrieNode.prototype.getDepth = function () { if (typeof this.prefix === 'undefined') { return 0; } else { return this.prefix.split('.').length; } }; TrieNode.prototype.isLeaf = function () { return Object.keys(this.children).length === 0; }; TrieNode.prototype.isOIDRoot = function () { return this.oidRoot || this.isLeaf(); }; TrieNode.prototype.preparePrefix = function (oid) { if (oid === undefined) { return undefined; } return oid.split('.'); }; TrieNode.prototype.add = function (oid) { var node = this; var oidList = this.preparePrefix(oid); for (var i = 0; i < oidList.length; i++) { if (!node.children[oidList[i]]) { node.children[oidList[i]] = new TrieNode(oidList[i], node.prefix); node.children[oidList[i]].parent = node; } node = node.children[oidList[i]]; } node.oidRoot = true; }; TrieNode.prototype.find = function (prefix) { var node = this; var oidList = this.preparePrefix(prefix); if (oidList === undefined) { return undefined; } for (var i = 0; i < oidList.length; i++) { if (node.children[oidList[i]]) { node = node.children[oidList[i]]; } else { return undefined; } } return node; }; TrieNode.prototype.getPrefixList = function (requestedLength) { var length = typeof requestedLength === 'undefined' ? 4 : requestedLength; if (this.getDepth() >= length) { return [this.prefix]; } else { return this.getPrefixListChildren(); } }; TrieNode.prototype.getPrefixListChildren = function () { var toReturn = []; for (var key in this.children) { if (this.children.hasOwnProperty(key)) { var toAdd = this.children[key].navigateIfOneChildren().getPrefixList(); toReturn = toReturn.concat(toAdd); } } return toReturn; }; module.exports.TrieNode = TrieNode;