ianperrin / MMM-NetworkScanner

A module for MagicMirror which determines the status of devices on the network based on their MAC or IP address

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

showOffline=false and residents don't work reliably

thburghout opened this issue · comments

This is due to the keepAlive only being respected when showOffline is active.

I will leave a patch here, as this repository does not seem to be maintained. Alternatively, you may provide some instructions on what a pull request should contain.

--- MMM-NetworkScanner.js
+++ MMM-NetworkScanner.js
@@ -14,7 +14,7 @@
 	defaults: {
 		devices: [], // an array of device objects e.g. { macAddress: "aa:bb:cc:11:22:33", name: "DEVICE-NAME", icon: "FONT-AWESOME-ICON"}
 		network: "-l", // a Local Network IP mask to limit the mac address scan, i.e. `192.168.0.0/24`. Use `-l` for the entire localnet
-		showUnknown: true, // shows devices found on the network even if not specified in the 'devices' option 
+		showUnknown: true, // shows devices found on the network even if not specified in the 'devices' option
 		showOffline: true, // shows devices specified in the 'devices' option even when offline
 		showLastSeen: false, // shows when the device was last seen e.g. "Device Name - last seen 5 minutes ago"
 		keepAlive: 180, // how long (in seconds) a device should be considered 'alive' since it was last found on the network
@@ -87,31 +87,30 @@
 				})
 			);
 
-			if (this.config.showOffline) {
-				var networkDevicesByMac = getKeyedObject(this.networkDevices, 'macAddress');
-				var payloadDevicesByMac = getKeyedObject(nextState, 'macAddress');
+			var networkDevicesByMac = getKeyedObject(this.networkDevices, 'macAddress');
+			var payloadDevicesByMac = getKeyedObject(nextState, 'macAddress');
 
-				nextState = this.config.devices.map(device => {
-					if (device.macAddress) {
-						var oldDeviceState = networkDevicesByMac[device.macAddress];
-						var payloadDeviceState = payloadDevicesByMac[device.macAddress];
-						var newDeviceState = payloadDeviceState || oldDeviceState || device;
+			nextState = this.config.devices.map(device => {
+				if (device.macAddress) {
+					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);
+					var sinceLastSeen = newDeviceState.lastSeen ?
+						moment().diff(newDeviceState.lastSeen, 'seconds') :
+						null;
+					var isStale = (sinceLastSeen >= this.config.keepAlive);
 
-						newDeviceState.online = (sinceLastSeen != null) && (!isStale);
+					newDeviceState.online = (sinceLastSeen != null) && (!isStale);
 
-						return newDeviceState;
-					} else {
-						return device;
-					}
-				});
-			}
+					return newDeviceState;
+				} else {
+					return device;
+				}
+			});
 
-			this.networkDevices = nextState;
+			this.networkDevices = this.config.showOffline ?
+				nextState : nextState.filter(d => { return d.online; });
 
 			// Sort list by known device names, then unknown device mac addresses
 			if (this.config.sort) {
@@ -213,7 +212,7 @@
 				deviceRow.appendChild(deviceCell);
 
 				// When last seen
-				if ((self.config.showLastSeen && device.lastSeen  && !self.config.showLastSeenWhenOffline) || 
+				if ((self.config.showLastSeen && device.lastSeen  && !self.config.showLastSeenWhenOffline) ||
 					(self.config.showLastSeen && !device.lastSeen &&  self.config.showLastSeenWhenOffline)) {
 					var dateCell = document.createElement("td");
 					dateCell.classList.add("date", "dimmed", "light");