Added some more dummy sensors and improved the architecture

This commit is contained in:
Ziver Koc 2020-12-08 23:14:36 +01:00
parent f4515f86c8
commit a968c1a91e
6 changed files with 111 additions and 22 deletions

View file

@ -1,7 +1,6 @@
package se.hal.plugin.dummy; package se.hal.plugin.dummy;
import se.hal.intf.*; import se.hal.intf.*;
import se.hal.struct.devicedata.TemperatureSensorData;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -9,7 +8,7 @@ import java.util.concurrent.*;
public class DummyController implements HalSensorController, HalEventController, Runnable { public class DummyController implements HalSensorController, HalEventController, Runnable {
private List registeredDevices = new ArrayList(); private List<DummyDevice> registeredDevices = new ArrayList();
private ScheduledExecutorService executor; private ScheduledExecutorService executor;
private HalSensorReportListener sensorListener; private HalSensorReportListener sensorListener;
private HalEventReportListener eventListener; private HalEventReportListener eventListener;
@ -28,15 +27,15 @@ public class DummyController implements HalSensorController, HalEventController,
@Override @Override
public void run() { public void run() {
if (registeredDevices != null) { if (registeredDevices != null) {
for (Object device : registeredDevices) { for (DummyDevice device : registeredDevices) {
if (sensorListener != null && device instanceof DummyTemperatureSensor) { if (sensorListener != null) {
sensorListener.reportReceived( HalDeviceData data = device.generateData();
(HalSensorConfig) device,
new TemperatureSensorData( if (data instanceof HalSensorData) {
(int)(Math.random()*30), sensorListener.reportReceived((HalSensorConfig) device, (HalSensorData) data);
System.currentTimeMillis() } else if (data instanceof HalEventData) {
) eventListener.reportReceived((HalEventConfig) device, (HalEventData) data);
); }
} }
} }
} }
@ -44,15 +43,15 @@ public class DummyController implements HalSensorController, HalEventController,
@Override @Override
public void register(HalSensorConfig sensorConfig) { public void register(HalSensorConfig sensorConfig) {
if (sensorConfig instanceof DummyTemperatureSensor) { if (sensorConfig instanceof DummyDevice) {
registeredDevices.add(sensorConfig); registeredDevices.add((DummyDevice) sensorConfig);
} }
} }
@Override @Override
public void register(HalEventConfig eventConfig) { public void register(HalEventConfig eventConfig) {
if (eventConfig instanceof DummySwitchEvent) { if (eventConfig instanceof DummyDevice) {
registeredDevices.add(eventConfig); registeredDevices.add((DummyDevice) eventConfig);
} }
} }

View file

@ -0,0 +1,32 @@
/*
* 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.dummy;
import se.hal.intf.HalDeviceData;
public interface DummyDevice {
public HalDeviceData generateData();
}

View file

@ -0,0 +1,43 @@
package se.hal.plugin.dummy;
import se.hal.intf.HalDeviceData;
import se.hal.intf.HalSensorConfig;
import se.hal.intf.HalSensorController;
import se.hal.intf.HalSensorData;
import se.hal.struct.devicedata.HumiditySensorData;
import se.hal.struct.devicedata.TemperatureSensorData;
public class DummyHumiditySensor implements DummyDevice, HalSensorConfig {
@Override
public HalDeviceData generateData() {
return new HumiditySensorData(
(int) (Math.random() * 100),
System.currentTimeMillis()
);
}
@Override
public long getDataInterval() {
return 60*1000; // 1 min
}
@Override
public AggregationMethod getAggregationMethod() {
return AggregationMethod.AVERAGE;
}
@Override
public Class<? extends HalSensorController> getSensorControllerClass() {
return DummyController.class;
}
@Override
public Class<? extends HalSensorData> getSensorDataClass() {
return HumiditySensorData.class;
}
}

View file

@ -1,13 +1,20 @@
package se.hal.plugin.dummy; package se.hal.plugin.dummy;
import se.hal.intf.*; import se.hal.intf.*;
import se.hal.struct.devicedata.HumiditySensorData;
import se.hal.struct.devicedata.SwitchEventData; import se.hal.struct.devicedata.SwitchEventData;
import se.hal.struct.devicedata.TemperatureSensorData; import se.hal.struct.devicedata.TemperatureSensorData;
public class DummySwitchEvent implements HalEventConfig { public class DummySwitchEvent implements DummyDevice, HalEventConfig {
public DummySwitchEvent() { } @Override
public HalDeviceData generateData() {
return new SwitchEventData(
(int) (Math.random() * 10) < 5,
System.currentTimeMillis()
);
}
@Override @Override

View file

@ -1,20 +1,27 @@
package se.hal.plugin.dummy; package se.hal.plugin.dummy;
import se.hal.intf.HalDeviceData;
import se.hal.intf.HalSensorConfig; import se.hal.intf.HalSensorConfig;
import se.hal.intf.HalSensorController; import se.hal.intf.HalSensorController;
import se.hal.intf.HalSensorData; import se.hal.intf.HalSensorData;
import se.hal.struct.devicedata.TemperatureSensorData; import se.hal.struct.devicedata.TemperatureSensorData;
public class DummyTemperatureSensor implements HalSensorConfig { public class DummyTemperatureSensor implements DummyDevice, HalSensorConfig {
public DummyTemperatureSensor() { } @Override
public HalDeviceData generateData() {
return new TemperatureSensorData(
(int) (Math.random() * 30),
System.currentTimeMillis()
);
}
@Override @Override
public long getDataInterval() { public long getDataInterval() {
return 60*1000; // 1 min return 60 * 1000; // 1 min
} }
@Override @Override

View file

@ -3,7 +3,8 @@
"name": "Hal-Dummy Devices", "name": "Hal-Dummy Devices",
"description": "Dummy devices with simulated data, can be used for demo or testing purposes.", "description": "Dummy devices with simulated data, can be used for demo or testing purposes.",
"interfaces": [ "interfaces": [
{"se.hal.intf.HalSensorConfig": "se.hal.plugin.dummy.DummyTemperatureSensor"}, {"se.hal.intf.HalSensorConfig": "se.hal.plugin.dummy.DummyHumiditySensor"},
{"se.hal.intf.HalEventConfig": "se.hal.plugin.dummy.DummySwitchEvent"} {"se.hal.intf.HalEventConfig": "se.hal.plugin.dummy.DummySwitchEvent"},
{"se.hal.intf.HalSensorConfig": "se.hal.plugin.dummy.DummyTemperatureSensor"}
] ]
} }