Made netscan more robust by requiring two offline pings to report device as offline
This commit is contained in:
parent
9687f28eb0
commit
18b15569d6
4 changed files with 43 additions and 26 deletions
|
|
@ -179,14 +179,29 @@ public class HalContext {
|
||||||
value = dbConf.getProperty(key);
|
value = dbConf.getProperty(key);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
public static String getStringProperty(String key, String defaultValue){
|
||||||
|
if (!HalContext.containsProperty(key))
|
||||||
|
return defaultValue;
|
||||||
|
return getStringProperty(key);
|
||||||
|
}
|
||||||
|
public static boolean containsProperty(String key) {
|
||||||
|
return getStringProperty(key) != null;
|
||||||
|
}
|
||||||
public static int getIntegerProperty(String key){
|
public static int getIntegerProperty(String key){
|
||||||
return Integer.parseInt(getStringProperty(key));
|
return Integer.parseInt(getStringProperty(key));
|
||||||
}
|
}
|
||||||
|
public static int getIntegerProperty(String key, int defaultValue){
|
||||||
|
if (!HalContext.containsProperty(key))
|
||||||
|
return defaultValue;
|
||||||
|
return getIntegerProperty(key);
|
||||||
|
}
|
||||||
public static boolean getBooleanProperty(String key) {
|
public static boolean getBooleanProperty(String key) {
|
||||||
return Boolean.parseBoolean(getStringProperty(key));
|
return Boolean.parseBoolean(getStringProperty(key));
|
||||||
}
|
}
|
||||||
public static boolean containsProperty(String key) {
|
public static boolean getBooleanProperty(String key, boolean defaultValue) {
|
||||||
return getStringProperty(key) != null;
|
if (!HalContext.containsProperty(key))
|
||||||
|
return defaultValue;
|
||||||
|
return getBooleanProperty(key);
|
||||||
}
|
}
|
||||||
public static void setProperty(String key, String value) throws SQLException {
|
public static void setProperty(String key, String value) throws SQLException {
|
||||||
PreparedStatement stmt = db.getPreparedStatement("REPLACE INTO conf (key, value) VALUES (?, ?)");
|
PreparedStatement stmt = db.getPreparedStatement("REPLACE INTO conf (key, value) VALUES (?, ?)");
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,7 @@ import zutil.net.InetScanner;
|
||||||
import zutil.net.InetScanner.InetScanListener;
|
import zutil.net.InetScanner.InetScanListener;
|
||||||
import zutil.osal.MultiCommandExecutor;
|
import zutil.osal.MultiCommandExecutor;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
@ -33,7 +30,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
|
||||||
private ScheduledExecutorService executor;
|
private ScheduledExecutorService executor;
|
||||||
private HalEventReportListener listener;
|
private HalEventReportListener listener;
|
||||||
/** A register and a cache of previous state **/
|
/** A register and a cache of previous state **/
|
||||||
private HashMap<LocalNetworkDevice,SwitchEventData> devices = new HashMap<>();
|
private HashMap<NetworkDevice,SwitchEventData> devices = new HashMap<>();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -46,7 +43,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
|
||||||
public void initialize() throws Exception {
|
public void initialize() throws Exception {
|
||||||
executor = Executors.newScheduledThreadPool(2);
|
executor = Executors.newScheduledThreadPool(2);
|
||||||
executor.scheduleAtFixedRate(NetScanController.this, 10_000, PING_INTERVAL, TimeUnit.MILLISECONDS);
|
executor.scheduleAtFixedRate(NetScanController.this, 10_000, PING_INTERVAL, TimeUnit.MILLISECONDS);
|
||||||
if (!HalContext.containsProperty(PARAM_IPSCAN) || HalContext.getBooleanProperty(PARAM_IPSCAN)) {
|
if (HalContext.getBooleanProperty(PARAM_IPSCAN, true)) {
|
||||||
executor.scheduleAtFixedRate(new Runnable() {
|
executor.scheduleAtFixedRate(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
@ -66,18 +63,23 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try(MultiCommandExecutor executor = new MultiCommandExecutor();){
|
try(MultiCommandExecutor executor = new MultiCommandExecutor()){
|
||||||
for (Map.Entry<LocalNetworkDevice,SwitchEventData> entry : devices.entrySet()) {
|
for (Map.Entry<NetworkDevice,SwitchEventData> entry : devices.entrySet()) {
|
||||||
LocalNetworkDevice device = entry.getKey();
|
NetworkDevice device = entry.getKey();
|
||||||
SwitchEventData data = entry.getValue();
|
SwitchEventData prevData = entry.getValue();
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
//logger.finest("Pinging IP "+ device.getHost());
|
//logger.finest("Pinging IP "+ device.getHost());
|
||||||
boolean online = InetScanner.isReachable(device.getHost(), executor);
|
SwitchEventData newData = new SwitchEventData(
|
||||||
if (data == null || data.isOn() != online) {
|
InetScanner.isReachable(device.getHost(), executor),
|
||||||
data = new SwitchEventData(online, System.currentTimeMillis());
|
System.currentTimeMillis());
|
||||||
entry.setValue(data);
|
entry.setValue(newData);
|
||||||
logger.fine("IP "+device.getHost() +" state has changed to "+ data);
|
|
||||||
listener.reportReceived(device, data);
|
// Should we report?
|
||||||
|
if (prevData == null ||
|
||||||
|
(!prevData.isOn() && newData.isOn()) ||
|
||||||
|
(!prevData.isOn() && !newData.isOn()) ) { // require two off measurements to report off
|
||||||
|
logger.fine("IP "+device.getHost() +" state has changed to "+ newData.isOn());
|
||||||
|
listener.reportReceived(device, newData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -90,7 +92,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
|
||||||
logger.fine("Auto Detected ip: "+ip.getHostAddress());
|
logger.fine("Auto Detected ip: "+ip.getHostAddress());
|
||||||
if (listener != null)
|
if (listener != null)
|
||||||
listener.reportReceived(
|
listener.reportReceived(
|
||||||
new LocalNetworkDevice(ip.getHostAddress()),
|
new NetworkDevice(ip.getHostAddress()),
|
||||||
new SwitchEventData(true, System.currentTimeMillis()));
|
new SwitchEventData(true, System.currentTimeMillis()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -98,8 +100,8 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void register(HalEventConfig event) {
|
public void register(HalEventConfig event) {
|
||||||
if (event instanceof LocalNetworkDevice)
|
if (event instanceof NetworkDevice)
|
||||||
devices.put((LocalNetworkDevice) event, null);
|
devices.put((NetworkDevice) event, null);
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void deregister(HalEventConfig event) {
|
public void deregister(HalEventConfig event) {
|
||||||
|
|
|
||||||
|
|
@ -9,15 +9,15 @@ import zutil.ui.Configurator;
|
||||||
/**
|
/**
|
||||||
* Created by Ziver on 2016-10-02.
|
* Created by Ziver on 2016-10-02.
|
||||||
*/
|
*/
|
||||||
public class LocalNetworkDevice implements HalEventConfig {
|
public class NetworkDevice implements HalEventConfig {
|
||||||
|
|
||||||
@Configurator.Configurable("IP Address")
|
@Configurator.Configurable("IP Address")
|
||||||
private String host;
|
private String host;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public LocalNetworkDevice() { }
|
public NetworkDevice() { }
|
||||||
public LocalNetworkDevice(String hostAddress) {
|
public NetworkDevice(String hostAddress) {
|
||||||
this.host = hostAddress;
|
this.host = hostAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -32,8 +32,8 @@ public class LocalNetworkDevice implements HalEventConfig {
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj){
|
public boolean equals(Object obj){
|
||||||
if (obj instanceof LocalNetworkDevice)
|
if (obj instanceof NetworkDevice)
|
||||||
return host != null && host.equals(((LocalNetworkDevice) obj).host);
|
return host != null && host.equals(((NetworkDevice) obj).host);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -3,6 +3,6 @@
|
||||||
"name": "Network Scanner",
|
"name": "Network Scanner",
|
||||||
"interfaces": [
|
"interfaces": [
|
||||||
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.netscan.NetScanController"},
|
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.netscan.NetScanController"},
|
||||||
{"se.hal.intf.HalEventConfig": "se.hal.plugin.netscan.LocalNetworkDevice"}
|
{"se.hal.intf.HalEventConfig": "se.hal.plugin.netscan.NetworkDevice"}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue