Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making showUnknown and showOffline work together #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions MMM-NetworkScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ Module.register("MMM-NetworkScanner", {
);

if (this.config.showOffline) {
var networkDevicesByMac = getKeyedObject(this.networkDevices, 'macAddress');
var payloadDevicesByMac = getKeyedObject(nextState, 'macAddress');

nextState = this.config.devices.map(device => {
var networkDevicesByMac = getKeyedObject(this.networkDevices, 'macAddress');
var payloadDevicesByMac = getKeyedObject(nextState, 'macAddress');
nextState = this.config.devices.map((device,index) => {
if (device.macAddress) {
var oldDeviceState = networkDevicesByMac[device.macAddress];
var payloadDeviceState = payloadDevicesByMac[device.macAddress];
Expand All @@ -103,12 +103,25 @@ Module.register("MMM-NetworkScanner", {
var isStale = (sinceLastSeen >= this.config.keepAlive);

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

//here I remove the device if it goes offline (this could be decided by users through another config parameter
if (!newDeviceState.online && 'type' in newDeviceState && newDeviceState.type == 'Unknown') {
this.config.devices.splice(index,index);
}

return newDeviceState;
} else {
return device;
}
});
if (this.config.showUnknown) {
// Integrate to this.config.devices every unknown device in payloadDevicesByMac, it makes sure the same device is not added twice.
for (device in payloadDevicesByMac) {
if ('type' in payloadDevicesByMac[device] && payloadDevicesByMac[device].type == 'Unknown') {
let filtered = this.config.devices.filter((v,i,a)=> v.macAddress == payloadDevicesByMac[device].macAddress);
if (filtered.length == 0) this.config.devices.push(payloadDevicesByMac[device]);
}
}
}
}

this.networkDevices = nextState;
Expand All @@ -126,13 +139,15 @@ Module.register("MMM-NetworkScanner", {

// Send notification if user status has changed
if (this.config.residents.length > 0) {
var anyoneHome, command;
var anyoneHome, command, resident_found;
// self = this;
anyoneHome = 0;

this.networkDevices.forEach(function(device) {
if (self.config.residents.indexOf(device.name) >= 0) {
anyoneHome = anyoneHome + device.online;
if (device.online)
resident_found = device;
}
});

Expand All @@ -144,6 +159,7 @@ Module.register("MMM-NetworkScanner", {
if (this.config.debug) Log.info("Someone has come home");
if (this.config.occupiedCMD) {
var occupiedCMD = self.config.occupiedCMD;
occupiedCMD.payload.resident_found = resident_found;
this.sendNotification(occupiedCMD.notification, occupiedCMD.payload);
}
this.occupied = true;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"ping": "^0.1.10"
},
"devDependencies": {
"grunt": "latest",
"grunt-contrib-jshint": "latest",
"grunt-contrib-nodeunit": "latest"
"grunt": "latest",
"grunt-contrib-jshint": "latest",
"grunt-contrib-nodeunit": "latest"
}
}