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

View file

@ -1,15 +1,22 @@
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.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

View file

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