Skip to content

Commit

Permalink
reduce system load during discovery process
Browse files Browse the repository at this point in the history
  • Loading branch information
Jey-Cee committed Mar 4, 2024
1 parent 7f20037 commit c928613
Showing 1 changed file with 49 additions and 43 deletions.
92 changes: 49 additions & 43 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ let pingTimeout = null;
let taskList = [];
let cronJob = null;


const FORBIDDEN_CHARS = /[\]\[*,;'"`<>\\?]/g;

class NetTools extends utils.Adapter {
Expand Down Expand Up @@ -322,56 +323,61 @@ class NetTools extends utils.Adapter {
try {
const ips = this.getIpRange();
const decimalSeparator = getDecimalSeparator();
for(let ip in ips) {
ping.probe(ips[ip], {timeout: parseFloat(`0${decimalSeparator}25`), log: this.log.info}, async (error, result) => {
if (result.alive === true) {
result.mac = await arp.toMAC(result.host);
if (result.mac !== undefined && result.mac !== null) {
result.vendor = oui(result.mac)
} else {
return;
}
try {
result.name = await nslookup(result.host);
} catch (error) {
result.name = result.host;
}
let exists = false;

for (const entry of oldDevices) {
if (entry.native !== undefined && entry.native.mac === result.mac) {
exists = true;
}
if (exists === true && entry.native !== undefined && entry.native.ip !== result.host) {
const idName = result.mac.replace(/:/g, '');
await this.extendObjectAsync(this.namespace + '.' + idName, {
native: {
ip: result.host,
vendor: result.vendor
}
});
}
}
if (!exists) {
await this.addDevice(result.host, result.name, true, result.mac);
}
}
});
const promises = [];
for (let i = 0; i < ips.length; i += 10){
for(let j = 0; j < 10; j++) {
if (i + j < ips.length) {
promises.push(this.handleDiscoveryProbe(ips[i+j], oldDevices, decimalSeparator));
}
}
await Promise.all(promises);
}

const preparedObjects = await this.prepareObjectsByConfig();
try {
this.pingAll();
} catch (error){
this.log.error('Ping all: ' + error.toString());
}
return true;
} catch (err) {
this.log.warn('Discovery faild: ' + err);
return false;
}
}

async handleDiscoveryProbe(ip, oldDevices, decimalSeparator){
return new Promise(async (resolve) => {
ping.probe(ip, {timeout: parseFloat(`0${decimalSeparator}25`), log: this.log.info}, async (error, result) => {
if (result.alive === true) {
result.mac = await arp.toMAC(result.host);
if (result.mac !== undefined && result.mac !== null) {
result.vendor = oui(result.mac)
} else {
return;
}
try {
result.name = await nslookup(result.host);
} catch (error) {
result.name = result.host;
}
let exists = false;

for (const entry of oldDevices) {
if (entry.native !== undefined && entry.native.mac === result.mac) {
exists = true;
}
if (exists === true && entry.native !== undefined && entry.native.ip !== result.host) {
const idName = result.mac.replace(/:/g, '');
await this.extendObjectAsync(this.namespace + '.' + idName, {
native: {
ip: result.host,
vendor: result.vendor
}
});
}
}
if (!exists) {
await this.addDevice(result.host, result.name, true, result.mac);
}
}
resolve(true);
});
})
}

/**
* Retrieves the range of IP addresses based on the given configuration.
*
Expand Down

0 comments on commit c928613

Please sign in to comment.