Implemented network scanner
This commit is contained in:
parent
990db6a74a
commit
0a9bca2804
12 changed files with 188 additions and 22 deletions
|
|
@ -1,7 +1,116 @@
|
|||
package se.hal.plugin.netscan;
|
||||
|
||||
import se.hal.intf.*;
|
||||
import se.hal.struct.devicedata.SwitchEventData;
|
||||
import zutil.log.LogUtil;
|
||||
import zutil.net.InetScanner;
|
||||
import zutil.net.InetScanner.InetScanListener;
|
||||
import zutil.osal.MultiCommandExecutor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2016-09-30.
|
||||
*/
|
||||
public class NetScanController {
|
||||
public class NetScanController implements HalEventController, HalAutoScannableController, InetScanListener, Runnable{
|
||||
public static Logger logger = LogUtil.getLogger();
|
||||
private static final int NETWORK_SYNC_INTERVAL = 3 * 60 * 60 * 1000; // 3 hours
|
||||
private static final int PING_INTERVAL = 10 * 1000; // 10 sec
|
||||
|
||||
private ScheduledExecutorService executor;
|
||||
private HalEventReportListener listener;
|
||||
private ArrayList<NetworkDevice> devices = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isAvailable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() throws Exception {
|
||||
executor = Executors.newScheduledThreadPool(1);
|
||||
executor.scheduleAtFixedRate(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
logger.fine("Starting network scan...");
|
||||
InetScanner scanner = new InetScanner();
|
||||
scanner.setListener(NetScanController.this);
|
||||
scanner.scan(InetAddress.getLocalHost());
|
||||
logger.fine("Network scan done");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 30_000, NETWORK_SYNC_INTERVAL, TimeUnit.MILLISECONDS);
|
||||
executor.scheduleAtFixedRate(NetScanController.this, 10_001, PING_INTERVAL, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try{
|
||||
MultiCommandExecutor executor = new MultiCommandExecutor();
|
||||
for (int i = 0; i < devices.size(); i++) {
|
||||
NetworkDevice device = devices.get(i);
|
||||
if (listener != null) {
|
||||
logger.fine("Pinging ip: "+device.getIp());
|
||||
boolean online = InetScanner.isReachable(InetAddress.getByName(device.getIp()), executor);
|
||||
listener.reportReceived(device, new SwitchEventData(online, System.currentTimeMillis()));
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void foundInetAddress(InetAddress ip) {
|
||||
logger.fine("Detected ip: "+ip.getHostAddress());
|
||||
if (listener != null)
|
||||
listener.reportReceived(
|
||||
new NetworkDevice(ip.getHostAddress()),
|
||||
new SwitchEventData(true, System.currentTimeMillis()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void register(HalEventConfig event) {
|
||||
if (event instanceof NetworkDevice)
|
||||
devices.add((NetworkDevice) event);
|
||||
}
|
||||
@Override
|
||||
public void deregister(HalEventConfig event) {
|
||||
devices.remove(event);
|
||||
}
|
||||
@Override
|
||||
public int size() {
|
||||
return devices.size();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void send(HalEventConfig eventConfig, HalEventData eventData) { }
|
||||
|
||||
|
||||
@Override
|
||||
public void setListener(HalEventReportListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (executor != null){
|
||||
executor.shutdown();
|
||||
executor = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
50
src/se/hal/plugin/netscan/NetworkDevice.java
Normal file
50
src/se/hal/plugin/netscan/NetworkDevice.java
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
package se.hal.plugin.netscan;
|
||||
|
||||
import se.hal.intf.HalEventConfig;
|
||||
import se.hal.intf.HalEventController;
|
||||
import se.hal.intf.HalEventData;
|
||||
import se.hal.struct.devicedata.SwitchEventData;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
||||
/**
|
||||
* Created by Ziver on 2016-10-02.
|
||||
*/
|
||||
public class NetworkDevice implements HalEventConfig {
|
||||
|
||||
@Configurator.Configurable("IP Address")
|
||||
private String ip;
|
||||
|
||||
|
||||
|
||||
public NetworkDevice() { }
|
||||
public NetworkDevice(String hostAddress) {
|
||||
this.ip = hostAddress;
|
||||
}
|
||||
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return "IP: "+ip;
|
||||
}
|
||||
@Override
|
||||
public boolean equals(Object obj){
|
||||
if (obj instanceof NetworkDevice)
|
||||
return ip != null && ip.equals(((NetworkDevice) obj).ip);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<? extends HalEventController> getEventControllerClass() {
|
||||
return NetScanController.class;
|
||||
}
|
||||
@Override
|
||||
public Class<? extends HalEventData> getEventDataClass() {
|
||||
return SwitchEventData.class;
|
||||
}
|
||||
}
|
||||
8
src/se/hal/plugin/netscan/plugin.json
Normal file
8
src/se/hal/plugin/netscan/plugin.json
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"version": 1.0,
|
||||
"name": "Network Scanner",
|
||||
"interfaces": [
|
||||
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.netscan.NetScanController"},
|
||||
{"se.hal.intf.HalEventConfig": "se.hal.plugin.netscan.NetworkDevice"}
|
||||
]
|
||||
}
|
||||
|
|
@ -57,8 +57,8 @@ public class RPiDS18B20 implements RPiSensor, Runnable {
|
|||
controller.sendDataReport(
|
||||
new RPiTemperatureSensor(w1Address),
|
||||
new TemperatureSensorData(
|
||||
System.currentTimeMillis(),
|
||||
device.getTemperature(TemperatureScale.CELSIUS)
|
||||
device.getTemperature(TemperatureScale.CELSIUS),
|
||||
System.currentTimeMillis()
|
||||
));
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -163,7 +163,7 @@ public class NexaSelfLearningProtocol extends TellstickProtocol {
|
|||
*/
|
||||
list.add(new TellstickDecodedEntry(
|
||||
new NexaSelfLearning(struct.house, struct.group, struct.unit),
|
||||
new SwitchEventData(struct.enable)
|
||||
new SwitchEventData(struct.enable, System.currentTimeMillis())
|
||||
));
|
||||
/* }*/
|
||||
return list;
|
||||
|
|
|
|||
|
|
@ -67,6 +67,7 @@ public class Oregon0x1A2DProtocol extends TellstickProtocol {
|
|||
|
||||
|
||||
// Create return objects
|
||||
long timestamp = System.currentTimeMillis();
|
||||
boolean humidityFound=false, temperatureFound=false;
|
||||
ArrayList<TellstickDecodedEntry> list = new ArrayList<>();
|
||||
for (Oregon0x1A2D device : TellstickSerialComm.getInstance().getRegisteredDevices(Oregon0x1A2D.class)) {
|
||||
|
|
@ -78,20 +79,20 @@ public class Oregon0x1A2DProtocol extends TellstickProtocol {
|
|||
sensorType = OregonSensorType.POWER;
|
||||
switch (sensorType){
|
||||
case HUMIDITY:
|
||||
dataObj = new HumiditySensorData(humidity);
|
||||
dataObj = new HumiditySensorData(humidity, timestamp);
|
||||
humidityFound = true;
|
||||
break;
|
||||
case LIGHT:
|
||||
dataObj = new LightSensorData(temperature);
|
||||
dataObj = new LightSensorData(temperature, timestamp);
|
||||
temperatureFound = true;
|
||||
break;
|
||||
case TEMPERATURE:
|
||||
dataObj = new TemperatureSensorData(temperature);
|
||||
dataObj = new TemperatureSensorData(temperature, timestamp);
|
||||
temperatureFound = true;
|
||||
break;
|
||||
default:
|
||||
case POWER:
|
||||
dataObj = new PowerConsumptionSensorData(temperature);
|
||||
dataObj = new PowerConsumptionSensorData(temperature, timestamp);
|
||||
temperatureFound = true;
|
||||
break;
|
||||
|
||||
|
|
@ -102,11 +103,11 @@ public class Oregon0x1A2DProtocol extends TellstickProtocol {
|
|||
if (!temperatureFound)
|
||||
list.add(new TellstickDecodedEntry(
|
||||
new Oregon0x1A2D(address, OregonSensorType.TEMPERATURE),
|
||||
new TemperatureSensorData(temperature)));
|
||||
new TemperatureSensorData(temperature, timestamp)));
|
||||
if (!humidityFound && humidity>0.0)
|
||||
list.add(new TellstickDecodedEntry(
|
||||
new Oregon0x1A2D(address, OregonSensorType.HUMIDITY),
|
||||
new HumiditySensorData(humidity)));
|
||||
new HumiditySensorData(humidity, timestamp)));
|
||||
|
||||
return list;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,8 +33,9 @@ public class DimmerEventData extends HalEventData {
|
|||
|
||||
|
||||
public DimmerEventData() { }
|
||||
public DimmerEventData(double dimmValue) {
|
||||
public DimmerEventData(double dimmValue, long timestamp) {
|
||||
this.dimmValue = dimmValue;
|
||||
this.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,8 +12,9 @@ public class HumiditySensorData extends HalSensorData {
|
|||
|
||||
|
||||
public HumiditySensorData() { }
|
||||
public HumiditySensorData(double humidity) {
|
||||
public HumiditySensorData(double humidity, long timestamp) {
|
||||
this.humidity = humidity;
|
||||
this.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -11,8 +11,9 @@ public class LightSensorData extends HalSensorData {
|
|||
|
||||
|
||||
public LightSensorData(){}
|
||||
public LightSensorData(double lux){
|
||||
public LightSensorData(double lux, long timestamp){
|
||||
this.lux = lux;
|
||||
this.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -12,11 +12,8 @@ public class PowerConsumptionSensorData extends HalSensorData {
|
|||
|
||||
|
||||
public PowerConsumptionSensorData() { }
|
||||
public PowerConsumptionSensorData(double wattHours) {
|
||||
public PowerConsumptionSensorData(double wattHours, long timestamp) {
|
||||
this.wattHours = wattHours;
|
||||
}
|
||||
public PowerConsumptionSensorData(long timestamp, double wattHours) {
|
||||
this(wattHours);
|
||||
super.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,8 +33,9 @@ public class SwitchEventData extends HalEventData {
|
|||
|
||||
|
||||
public SwitchEventData() { }
|
||||
public SwitchEventData(boolean enabled) {
|
||||
public SwitchEventData(boolean enabled, long timestamp) {
|
||||
this.enabled = enabled;
|
||||
this.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
public void turnOn(){
|
||||
|
|
|
|||
|
|
@ -11,11 +11,8 @@ public class TemperatureSensorData extends HalSensorData {
|
|||
|
||||
|
||||
public TemperatureSensorData(){}
|
||||
public TemperatureSensorData(double temperature){
|
||||
public TemperatureSensorData(double temperature, long timestamp){
|
||||
this.temperature = temperature;
|
||||
}
|
||||
public TemperatureSensorData(long timestamp, double temperature){
|
||||
this(temperature);
|
||||
super.setTimestamp(timestamp);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue