Merge remote-tracking branch 'origin/master'

This commit is contained in:
Ziver Koc 2016-10-04 21:57:05 +02:00
commit bf303604bd
4 changed files with 49 additions and 27 deletions

View file

@ -5,7 +5,9 @@ share that data between friends. It is developed to be very extensible so future
Sensors and other input devices can be supported. Sensors and other input devices can be supported.
Currently supported devices: Currently supported devices:
- **Tellstick** - **Network Scanner**, IP scanner to detect devices on local network
- **NUT**, Linux UPS daemon
- **Tellstick**, Supported devices:
- NexaSelfLearning - NexaSelfLearning
- Oregon0x1A2D - Oregon0x1A2D
- **Raspberry Pi**, GPIO connected sensors - **Raspberry Pi**, GPIO connected sensors

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,47 +27,54 @@ 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(new Runnable() { executor.scheduleAtFixedRate(NetScanController.this, 10_000, PING_INTERVAL, TimeUnit.MILLISECONDS);
@Override if (!HalContext.containsProperty(PARAM_IPSCAN) || HalContext.getBooleanProperty(PARAM_IPSCAN)) {
public void run() { executor.scheduleAtFixedRate(new Runnable() {
try { @Override
logger.fine("Starting network scan..."); public void run() {
InetScanner scanner = new InetScanner(); try {
scanner.setListener(NetScanController.this); logger.fine("Starting network scan...");
scanner.scan(InetScanner.getLocalInet4Address().get(0)); InetScanner scanner = new InetScanner();
logger.fine("Network scan done"); scanner.setListener(NetScanController.this);
} catch (Exception e) { scanner.scan(InetUtil.getLocalInet4Address().get(0));
e.printStackTrace(); logger.fine("Network scan done");
} catch (Exception e) {
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) {