Some updates to ip scanner

This commit is contained in:
Ziver Koc 2016-10-03 17:48:00 +02:00
parent e7b3c4ba5a
commit b30fcba967
3 changed files with 19 additions and 22 deletions

View file

@ -6,36 +6,34 @@ import se.hal.intf.HalEventData;
import se.hal.struct.devicedata.SwitchEventData; import se.hal.struct.devicedata.SwitchEventData;
import zutil.ui.Configurator; import zutil.ui.Configurator;
import java.net.InetAddress;
/** /**
* Created by Ziver on 2016-10-02. * Created by Ziver on 2016-10-02.
*/ */
public class NetworkDevice implements HalEventConfig { public class LocalNetworkDevice implements HalEventConfig {
@Configurator.Configurable("IP Address") @Configurator.Configurable("IP Address")
private String ip; private String host;
public NetworkDevice() { } public LocalNetworkDevice() { }
public NetworkDevice(String hostAddress) { public LocalNetworkDevice(String hostAddress) {
this.ip = hostAddress; this.host = hostAddress;
} }
public String getIp() { public String getHost() {
return ip; return host;
} }
@Override @Override
public String toString(){ public String toString(){
return "IP: "+ip; return "Host: "+ host;
} }
@Override @Override
public boolean equals(Object obj){ public boolean equals(Object obj){
if (obj instanceof NetworkDevice) if (obj instanceof LocalNetworkDevice)
return ip != null && ip.equals(((NetworkDevice) obj).ip); return host != null && host.equals(((LocalNetworkDevice) obj).host);
return false; return false;
} }

View file

@ -25,7 +25,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
private ScheduledExecutorService executor; private ScheduledExecutorService executor;
private HalEventReportListener listener; private HalEventReportListener listener;
private ArrayList<NetworkDevice> devices = new ArrayList<>(); private ArrayList<LocalNetworkDevice> devices = new ArrayList<>();
@ -56,13 +56,12 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
@Override @Override
public void run() { public void run() {
try{ try(MultiCommandExecutor executor = new MultiCommandExecutor();){
MultiCommandExecutor executor = new MultiCommandExecutor();
for (int i = 0; i < devices.size(); i++) { for (int i = 0; i < devices.size(); i++) {
NetworkDevice device = devices.get(i); LocalNetworkDevice device = devices.get(i);
if (listener != null) { if (listener != null) {
logger.fine("Pinging ip: "+device.getIp()); logger.fine("Pinging ip: "+device.getHost());
boolean online = InetScanner.isReachable(InetAddress.getByName(device.getIp()), executor); boolean online = InetScanner.isReachable(device.getHost(), executor);
listener.reportReceived(device, new SwitchEventData(online, System.currentTimeMillis())); listener.reportReceived(device, new SwitchEventData(online, System.currentTimeMillis()));
} }
} }
@ -75,7 +74,7 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
logger.fine("Detected ip: "+ip.getHostAddress()); logger.fine("Detected ip: "+ip.getHostAddress());
if (listener != null) if (listener != null)
listener.reportReceived( listener.reportReceived(
new NetworkDevice(ip.getHostAddress()), new LocalNetworkDevice(ip.getHostAddress()),
new SwitchEventData(true, System.currentTimeMillis())); new SwitchEventData(true, System.currentTimeMillis()));
} }
@ -83,8 +82,8 @@ public class NetScanController implements HalEventController, HalAutoScannableCo
@Override @Override
public void register(HalEventConfig event) { public void register(HalEventConfig event) {
if (event instanceof NetworkDevice) if (event instanceof LocalNetworkDevice)
devices.add((NetworkDevice) event); devices.add((LocalNetworkDevice) event);
} }
@Override @Override
public void deregister(HalEventConfig event) { public void deregister(HalEventConfig event) {

2
src/se/hal/plugin/netscan/plugin.json Normal file → Executable file
View file

@ -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.NetworkDevice"} {"se.hal.intf.HalEventConfig": "se.hal.plugin.netscan.LocalNetworkDevice"}
] ]
} }