Moved the smaller plugins to separate folders.

This commit is contained in:
Ziver Koc 2020-06-22 22:26:53 +02:00
parent fc11ae264f
commit edd5d0d083
18 changed files with 233 additions and 74 deletions

View file

@ -3,6 +3,9 @@
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-mqtt.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />
@ -13,7 +16,6 @@
<property name="compileDir" value="${buildRoot}/production" />
<property name="compileTestDir" value="${buildRoot}/test" />
<property name="releaseDir" value="${buildRoot}/release" />
<property name="releaseJar" value="hal-mqtt.jar" />
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
<!-- ________________________ TARGETS ________________________ -->

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hal MQTT Plugin" >
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-netscan.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />
<property name="testDir" value="${root}/test" />
<property name="libDir" value="${root}/lib" />
<property name="buildRoot" value="${root}/build" />
<property name="compileDir" value="${buildRoot}/production" />
<property name="compileTestDir" value="${buildRoot}/test" />
<property name="releaseDir" value="${buildRoot}/release" />
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
<!-- ________________________ TARGETS ________________________ -->
<import file="../../build_plugin.xml"/>
</project>

View file

@ -0,0 +1,130 @@
package se.hal.plugin.netscan;
import se.hal.HalContext;
import se.hal.intf.*;
import se.hal.struct.devicedata.SwitchEventData;
import zutil.InetUtil;
import zutil.log.LogUtil;
import zutil.net.InetScanner;
import zutil.net.InetScanner.InetScanListener;
import zutil.osal.MultiCommandExecutor;
import java.net.InetAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
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 static final String PARAM_IPSCAN = "netscan.ipscan";
private ScheduledExecutorService executor;
private HalEventReportListener listener;
/** A register and a cache of previous state **/
private HashMap<NetworkDevice,SwitchEventData> devices = new HashMap<>();
@Override
public boolean isAvailable() {
return ! InetUtil.getLocalInet4Address().isEmpty();
}
@Override
public void initialize() throws Exception {
executor = Executors.newScheduledThreadPool(2);
executor.scheduleAtFixedRate(NetScanController.this, 10_000, PING_INTERVAL, TimeUnit.MILLISECONDS);
if (HalContext.getBooleanProperty(PARAM_IPSCAN, true)) {
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
logger.fine("Starting network scan...");
InetScanner scanner = new InetScanner();
scanner.setListener(NetScanController.this);
scanner.scan(InetUtil.getLocalInet4Address().get(0));
logger.fine("Network scan done");
} catch (Exception e) {
logger.log(Level.SEVERE, null, e);
}
}
}, 30_000, NETWORK_SYNC_INTERVAL, TimeUnit.MILLISECONDS);
}
}
@Override
public void run() {
try(MultiCommandExecutor executor = new MultiCommandExecutor()){
for (Map.Entry<NetworkDevice,SwitchEventData> entry : devices.entrySet()) {
NetworkDevice device = entry.getKey();
SwitchEventData prevData = entry.getValue();
if (listener != null) {
// We ping two times to increase reliability
boolean ping = false;
ping |= InetScanner.isReachable(device.getHost(), executor);
if (!ping)
ping |= InetScanner.isReachable(device.getHost(), executor);
// Should we report?
if (prevData == null || prevData.isOn() != ping) {
SwitchEventData newData = new SwitchEventData(ping, System.currentTimeMillis());
entry.setValue(newData);
logger.fine("IP "+device.getHost() +" state has changed to "+ newData.isOn());
listener.reportReceived(device, newData);
}
}
}
} catch (Exception e) {
logger.log(Level.SEVERE, null, e);
}
}
@Override
public void foundInetAddress(InetAddress ip) {
logger.fine("Auto 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.put((NetworkDevice) event, null);
}
@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;
}
}
}

View file

@ -0,0 +1,45 @@
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;
public class NetworkDevice implements HalEventConfig {
@Configurator.Configurable("IP Address")
private String host;
public NetworkDevice() { }
public NetworkDevice(String hostAddress) {
this.host = hostAddress;
}
public String getHost() {
return host;
}
@Override
public String toString(){
return "Host: "+ host;
}
@Override
public boolean equals(Object obj){
if (obj instanceof NetworkDevice)
return host != null && host.equals(((NetworkDevice) obj).host);
return false;
}
@Override
public Class<? extends HalEventController> getEventControllerClass() {
return NetScanController.class;
}
@Override
public Class<? extends HalEventData> getEventDataClass() {
return SwitchEventData.class;
}
}

View file

@ -0,0 +1,8 @@
{
"version": 1.0,
"name": "Hal-Network Scanner",
"interfaces": [
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.netscan.NetScanController"},
{"se.hal.intf.HalEventConfig": "se.hal.plugin.netscan.NetworkDevice"}
]
}

View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project name="Hal MQTT Plugin" >
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-nutups.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />
<property name="testDir" value="${root}/test" />
<property name="libDir" value="${root}/lib" />
<property name="buildRoot" value="${root}/build" />
<property name="compileDir" value="${buildRoot}/production" />
<property name="compileTestDir" value="${buildRoot}/test" />
<property name="releaseDir" value="${buildRoot}/release" />
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
<!-- ________________________ TARGETS ________________________ -->
<import file="../../build_plugin.xml"/>
</project>

View file

@ -0,0 +1,137 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.hal.plugin.nutups;
import se.hal.HalContext;
import se.hal.intf.HalAutoScannableController;
import se.hal.intf.HalSensorController;
import se.hal.intf.HalSensorConfig;
import se.hal.intf.HalSensorReportListener;
import zutil.log.LogUtil;
import zutil.osal.linux.app.NutUPSClient;
import java.util.HashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
public class NutUpsController implements HalSensorController, HalAutoScannableController, Runnable{
public static Logger logger = LogUtil.getLogger();
private static final int SYNC_INTERVAL = 60 * 1000;
private HashMap<String, NutUpsDevice> registeredDevices = new HashMap<>();
private NutUPSClient client;
private ScheduledExecutorService executor;
private HalSensorReportListener listener;
@Override
public boolean isAvailable() {
return HalContext.getStringProperty("nutups.host") != null;
}
@Override
public void initialize() throws Exception {
if (client == null) {
int port = NutUPSClient.DEFAULT_PORT;
if (HalContext.getStringProperty("nutups.port") != null)
port = Integer.parseInt(HalContext.getStringProperty("nutups.port"));
client = new NutUPSClient(HalContext.getStringProperty("nutups.host"), port);
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(this, 5000, SYNC_INTERVAL, TimeUnit.MILLISECONDS);
}
}
@Override
public void setListener(HalSensorReportListener listener) {
this.listener = listener;
}
@Override
public void run() {
try {
if (client != null && listener != null) {
for (NutUPSClient.UPSDevice ups : client.getUPSList()) {
NutUpsDevice device = registeredDevices.get(ups.getId());
if (device == null)
device = new NutUpsDevice(ups);
listener.reportReceived(device, device.read(ups));
}
}
} catch (Exception e){
logger.log(Level.SEVERE, "NutUps thread crashed", e);
}
}
@Override
public void close() {
client = null;
executor.shutdownNow();
}
@Override
public void register(HalSensorConfig sensor) {
registeredDevices.put(((NutUpsDevice) sensor).getUpsId(), (NutUpsDevice) sensor);
}
@Override
public void deregister(HalSensorConfig sensor) {
registeredDevices.remove(((NutUpsDevice) sensor).getUpsId());
}
@Override
public int size() {
return 0;
}
}

View file

@ -0,0 +1,112 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* The MIT License (MIT)
*
* Copyright (c) 2020 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.hal.plugin.nutups;
import se.hal.intf.HalSensorConfig;
import se.hal.intf.HalSensorController;
import se.hal.intf.HalSensorData;
import se.hal.struct.devicedata.PowerConsumptionSensorData;
import zutil.osal.linux.app.NutUPSClient;
import zutil.ui.Configurator;
public class NutUpsDevice implements HalSensorConfig{
@Configurator.Configurable("UPS id")
private String upsId;
public NutUpsDevice(){}
protected NutUpsDevice(NutUPSClient.UPSDevice ups){
this.upsId = ups.getId();
}
protected HalSensorData read(NutUPSClient.UPSDevice ups){
PowerConsumptionSensorData data = new PowerConsumptionSensorData();
data.setTimestamp(System.currentTimeMillis());
data.setData(ups.getPowerUsage() * 1/60.0); // convert watt min to watt hour
return data;
}
public String getUpsId(){
return upsId;
}
@Override
public long getDataInterval(){
return 60*1000; // 1 min
}
@Override
public boolean equals(Object obj){
if (obj instanceof NutUpsDevice)
return upsId != null && upsId.equals(((NutUpsDevice)obj).upsId);
return false;
}
public String toString(){
return "upsId: "+ upsId;
}
@Override
public AggregationMethod getAggregationMethod() {
return AggregationMethod.SUM;
}
@Override
public Class<? extends HalSensorController> getSensorControllerClass() {
return NutUpsController.class;
}
@Override
public Class<? extends HalSensorData> getSensorDataClass() {
return PowerConsumptionSensorData.class;
}
}

View file

@ -0,0 +1,8 @@
{
"version": 1.0,
"name": "Hal-NutUps",
"interfaces": [
{"se.hal.intf.HalAutoScannableController": "se.hal.plugin.nutups.NutUpsController"},
{"se.hal.intf.HalSensorConfig": "se.hal.plugin.nutups.NutUpsDevice"}
]
}

View file

@ -3,6 +3,9 @@
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-raspberry.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />

View file

@ -3,6 +3,9 @@
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-tellstick.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />
@ -13,7 +16,6 @@
<property name="compileDir" value="${buildRoot}/production" />
<property name="compileTestDir" value="${buildRoot}/test" />
<property name="releaseDir" value="${buildRoot}/release" />
<property name="releaseJar" value="hal-tellstick.jar" />
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
<!-- ________________________ TARGETS ________________________ -->

View file

@ -3,6 +3,9 @@
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-zigbee.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />
@ -13,7 +16,6 @@
<property name="compileDir" value="${buildRoot}/production" />
<property name="compileTestDir" value="${buildRoot}/test" />
<property name="releaseDir" value="${buildRoot}/release" />
<property name="releaseJar" value="hal-zigbee.jar" />
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
<!-- ________________________ TARGETS ________________________ -->

View file

@ -27,6 +27,9 @@
<!-- ________________________ PROPERTIES AND SETTINGS ________________________ -->
<!--plugin specific properties-->
<property name="releaseJar" value="hal-zwave.jar" />
<!--common properties-->
<property name="root" value="." />
<property name="srcDir" value="${root}/src" />
@ -37,7 +40,6 @@
<property name="compileDir" value="${buildRoot}/production" />
<property name="compileTestDir" value="${buildRoot}/test" />
<property name="releaseDir" value="${buildRoot}/release" />
<property name="releaseJar" value="hal-zwave.jar" />
<property name="reportsDir" value="../../${buildRoot}/reports" /> <!-- Use Hal reports folder -->
<!-- ________________________ TARGETS ________________________ -->