Skip to content

Commit

Permalink
Merge pull request #19 from leveneg/keepalive_fix
Browse files Browse the repository at this point in the history
Fix KeepAlive refresh
  • Loading branch information
ianperrin committed Feb 7, 2018
2 parents ac29d8c + d13532e commit 6d5d1b1
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions MMM-NetworkScanner.js
Expand Up @@ -59,6 +59,10 @@ Module.register("MMM-NetworkScanner", {
if (this.config.debug) Log.info(this.name + " received a notification: " + notification, payload);

var self = this;
var getKeyedObject = (objects = [], key) => objects.reduce(
(acc, object) => (Object.assign(acc, { [object[key]]: object })),
{}
);

if (notification === 'IP_ADDRESS') {
if (payload.hasOwnProperty("ipAddress")) {
Expand All @@ -74,38 +78,32 @@ Module.register("MMM-NetworkScanner", {
if (notification === 'MAC_ADDRESSES') {
if (this.config.debug) Log.info(this.name + " MAC_ADDRESSES payload: ", payload);

this.networkDevices = payload;
var nextState = payload.map(device =>
Object.assign(device, { lastSeen: moment() })
);

// Update device info
for (var i = 0; i < this.networkDevices.length; i++) {
var device = this.networkDevices[i];
// Set last seen
if (device.online) {
device.lastSeen = moment();
}
// Keep alive?
device.online = (moment().diff(device.lastSeen, 'seconds') < this.config.keepAlive);
}

// Add offline devices from config
if (this.config.showOffline) {
for (var d = 0; d < this.config.devices.length; d++) {
var device = this.config.devices[d];

for(var n = 0; n < this.networkDevices.length; n++){
if( device.macAddress && this.networkDevices[n].macAddress && this.networkDevices[n].macAddress.toUpperCase() === device.macAddress.toUpperCase()) {
n = -1;
break;
}
}
var networkDevicesByMac = getKeyedObject(this.networkDevices, 'macAddress');
var payloadDevicesByMac = getKeyedObject(nextState, 'macAddress');

if (n != -1) {
device.online = false;
this.networkDevices.push(device);
}
}
nextState = this.config.devices.map(device => {
var oldDeviceState = networkDevicesByMac[device.macAddress];
var payloadDeviceState = payloadDevicesByMac[device.macAddress];
var newDeviceState = payloadDeviceState || oldDeviceState || device;

var sinceLastSeen = newDeviceState.lastSeen
? moment().diff(newDeviceState.lastSeen, 'seconds')
: null;
var isStale = (sinceLastSeen >= this.config.keepAlive);

newDeviceState.online = (sinceLastSeen != null) && (!isStale);

return newDeviceState;
});
}

this.networkDevices = nextState;

// Sort list by known device names, then unknown device mac addresses
this.networkDevices.sort(function (a, b) {
var stringA, stringB;
Expand Down

3 comments on commit 6d5d1b1

@ndom91
Copy link

@ndom91 ndom91 commented on 6d5d1b1 Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be that the device name is not wrapped in quotes somewhere? I'm having issues where keepalive does not work if my device name has any spaces in it. For example, "Nexus 6P" doesn't work, but "Nexus6P" and "Nexus_6P" do work. @ianperrin @leveneg

@leveneg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ndom91 Are you implying that behavior worked before this change? I'd recommend opening an issue which at minimum describes what you mean by "doesn't work", as well as your NetworkScanner configuration.

@ndom91
Copy link

@ndom91 ndom91 commented on 6d5d1b1 Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean after this commit, keepalive works as intended, but only when I give the device a name without a space in it. I'll open an issue, I guess that is the more appropriate way to respond.

Please sign in to comment.