Zutil updates and NetScan now only triggers on IP state changes and now is listening to the ipscan parameter

This commit is contained in:
Ziver Koc 2016-10-04 17:03:37 +02:00
parent 49ec412fff
commit 32a48a753a
3 changed files with 46 additions and 26 deletions

View file

@ -5,5 +5,5 @@ sync_port=6666
#tellstick.com_port=COM5 #tellstick.com_port=COM5
#nutups.host= #nutups.host=
#nutups.port= #nutups.port=
# Network scanning is disabled by default as it is not appropriate for all networks # Network scanning should probably be disabled in some networks (default on)
#netscan.scanip=true #netscan.ipscan=false

View file

@ -182,6 +182,12 @@ public class HalContext {
public static int getIntegerProperty(String key){ public static int getIntegerProperty(String key){
return Integer.parseInt(getStringProperty(key)); return Integer.parseInt(getStringProperty(key));
} }
public static boolean getBooleanProperty(String key) {
return Boolean.parseBoolean(getStringProperty(key));
}
public static boolean containsProperty(String key) {
return getStringProperty(key) != null;
}
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 (?, ?)");
stmt.setObject(1, key); stmt.setObject(1, key);
@ -190,6 +196,9 @@ public class HalContext {
dbConf.setProperty(key, value); dbConf.setProperty(key, value);
} }
public static DBConnection getDB(){ public static DBConnection getDB(){
return db; return db;
} }
@ -202,4 +211,5 @@ public class HalContext {
HalContext.db = db; HalContext.db = db;
} }
} }

View file

@ -1,7 +1,9 @@
package se.hal.plugin.netscan; package se.hal.plugin.netscan;
import se.hal.HalContext;
import se.hal.intf.*; import se.hal.intf.*;
import se.hal.struct.devicedata.SwitchEventData; import se.hal.struct.devicedata.SwitchEventData;
import zutil.InetUtil;
import zutil.log.LogUtil; import zutil.log.LogUtil;
import zutil.net.InetScanner; import zutil.net.InetScanner;
import zutil.net.InetScanner.InetScanListener; import zutil.net.InetScanner.InetScanListener;
@ -9,9 +11,10 @@ import zutil.osal.MultiCommandExecutor;
import java.io.IOException; import java.io.IOException;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -24,21 +27,25 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
public static Logger logger = LogUtil.getLogger(); public static Logger logger = LogUtil.getLogger();
private static final int NETWORK_SYNC_INTERVAL = 3 * 60 * 60 * 1000; // 3 hours private static final int NETWORK_SYNC_INTERVAL = 3 * 60 * 60 * 1000; // 3 hours
private static final int PING_INTERVAL = 10 * 1000; // 10 sec private static final int PING_INTERVAL = 10 * 1000; // 10 sec
private static final String PARAM_IPSCAN = "netscan.ipscan";
private ScheduledExecutorService executor; private ScheduledExecutorService executor;
private HalEventReportListener listener; private HalEventReportListener listener;
private ArrayList<LocalNetworkDevice> devices = new ArrayList<>(); /** A register and a cache of previous state **/
private HashMap<LocalNetworkDevice,SwitchEventData> devices = new HashMap<>();
@Override @Override
public boolean isAvailable() { public boolean isAvailable() {
return ! InetScanner.getLocalInet4Address().isEmpty(); return ! InetUtil.getLocalInet4Address().isEmpty();
} }
@Override @Override
public void initialize() throws Exception { public void initialize() throws Exception {
executor = Executors.newScheduledThreadPool(1); executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(NetScanController.this, 10_000, PING_INTERVAL, TimeUnit.MILLISECONDS);
if (!HalContext.containsProperty(PARAM_IPSCAN) || HalContext.getBooleanProperty(PARAM_IPSCAN)) {
executor.scheduleAtFixedRate(new Runnable() { executor.scheduleAtFixedRate(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -46,25 +53,28 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
logger.fine("Starting network scan..."); logger.fine("Starting network scan...");
InetScanner scanner = new InetScanner(); InetScanner scanner = new InetScanner();
scanner.setListener(NetScanController.this); scanner.setListener(NetScanController.this);
scanner.scan(InetScanner.getLocalInet4Address().get(0)); scanner.scan(InetUtil.getLocalInet4Address().get(0));
logger.fine("Network scan done"); logger.fine("Network scan done");
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}, 30_000, NETWORK_SYNC_INTERVAL, TimeUnit.MILLISECONDS); }, 30_000, NETWORK_SYNC_INTERVAL, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(NetScanController.this, 10_001, PING_INTERVAL, TimeUnit.MILLISECONDS); }
} }
@Override @Override
public void run() { public void run() {
try(MultiCommandExecutor executor = new MultiCommandExecutor();){ try(MultiCommandExecutor executor = new MultiCommandExecutor();){
for (int i = 0; i < devices.size(); i++) { for (Map.Entry<LocalNetworkDevice,SwitchEventData> entry : devices.entrySet()) {
LocalNetworkDevice device = devices.get(i);
if (listener != null) { if (listener != null) {
logger.fine("Pinging ip: "+device.getHost()); boolean online = InetScanner.isReachable(entry.getKey().getHost(), executor);
boolean online = InetScanner.isReachable(device.getHost(), executor); if (entry.getValue() == null || entry.getValue().isOn() == online) {
listener.reportReceived(device, new SwitchEventData(online, System.currentTimeMillis())); entry.setValue(
new SwitchEventData(online, System.currentTimeMillis()));
logger.fine("IP "+entry.getKey().getHost() +" state has changed to "+ entry.getValue());
listener.reportReceived(entry.getKey(), entry.getValue());
}
} }
} }
} catch (IOException e) { } catch (IOException e) {
@ -85,7 +95,7 @@ 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 LocalNetworkDevice)
devices.add((LocalNetworkDevice) event); devices.put((LocalNetworkDevice) event, null);
} }
@Override @Override
public void deregister(HalEventConfig event) { public void deregister(HalEventConfig event) {