Fixed device reconfiguration, not tested though
This commit is contained in:
parent
5a1be3c2e4
commit
edaa23f0f6
6 changed files with 79 additions and 77 deletions
|
|
@ -19,10 +19,9 @@ import java.util.logging.Logger;
|
|||
/**
|
||||
* This class manages all SensorController and EventController objects
|
||||
*/
|
||||
public class EventControllerManager extends HalAbstractControllerManager<HalEventController,Event,HalEventConfig> implements
|
||||
HalDeviceReportListener<HalEventConfig,HalEventData>,
|
||||
PreConfigurationActionListener,
|
||||
PostConfigurationActionListener {
|
||||
public class EventControllerManager extends HalAbstractControllerManager<HalEventController,Event,HalEventConfig>
|
||||
implements HalDeviceReportListener<HalEventConfig,HalEventData> {
|
||||
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static EventControllerManager instance;
|
||||
|
||||
|
|
@ -32,8 +31,6 @@ public class EventControllerManager extends HalAbstractControllerManager<HalEven
|
|||
private List<Event> registeredEvents = Collections.synchronizedList(new ArrayList<>());
|
||||
/** List of auto detected events **/
|
||||
private List<Event> detectedEvents = Collections.synchronizedList(new ArrayList<>());
|
||||
/** List of all registered events **/
|
||||
private List<Event> limboEvents = Collections.synchronizedList(new LinkedList<>());
|
||||
|
||||
|
||||
public void initialize(PluginManager pluginManager) {
|
||||
|
|
@ -205,28 +202,6 @@ public class EventControllerManager extends HalAbstractControllerManager<HalEven
|
|||
return controllerMap.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void preConfigurationAction(Configurator configurator, Object obj) {
|
||||
if (obj instanceof HalEventConfig) {
|
||||
Event event = HalDeviceUtil.findDevice((HalEventConfig) obj, registeredEvents);
|
||||
if (event != null){
|
||||
deregister(event);
|
||||
limboEvents.add(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postConfigurationAction(Configurator configurator, Object obj) {
|
||||
if (obj instanceof HalEventConfig) {
|
||||
Event event = HalDeviceUtil.findDevice((HalEventConfig) obj, limboEvents);
|
||||
if (event != null){
|
||||
register(event);
|
||||
limboEvents.remove(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static EventControllerManager getInstance(){
|
||||
return instance;
|
||||
|
|
|
|||
|
|
@ -1,15 +1,9 @@
|
|||
package se.hal;
|
||||
|
||||
|
||||
import se.hal.intf.HalAbstractControllerManager;
|
||||
import se.hal.intf.HalDaemon;
|
||||
import se.hal.intf.HalWebPage;
|
||||
import se.hal.intf.HalJsonPage;
|
||||
import se.hal.intf.*;
|
||||
import se.hal.page.*;
|
||||
import se.hal.struct.Event;
|
||||
import se.hal.struct.PluginConfig;
|
||||
import se.hal.struct.Sensor;
|
||||
import se.hal.struct.TriggerFlow;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.log.LogUtil;
|
||||
|
|
@ -150,6 +144,7 @@ public class HalServer {
|
|||
pluginConfig.save(db);
|
||||
}
|
||||
|
||||
|
||||
public static List<PluginData> getEnabledPlugins() {
|
||||
return pluginManager.toArray();
|
||||
}
|
||||
|
|
@ -158,20 +153,24 @@ public class HalServer {
|
|||
return pluginManager.toArrayAll();
|
||||
}
|
||||
|
||||
public static List<HalAbstractControllerManager> getControllerManagers() {
|
||||
return controllerManagers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param daemon registers the given daemon and starts execution of the Runnable.
|
||||
*/
|
||||
public static void registerDaemon(HalDaemon daemon){
|
||||
logger.info("Registering daemon: " + daemon.getClass());
|
||||
daemons.add(daemon);
|
||||
daemon.initiate(daemonExecutor);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param page registers the given page with the intranet Hal web server.
|
||||
*/
|
||||
public static void registerPage(HalWebPage page){
|
||||
pages.add(page);
|
||||
http.setPage(page.getId(), page);
|
||||
}
|
||||
|
||||
|
||||
public static List<HalAbstractControllerManager> getControllerManagers() {
|
||||
return controllerManagers;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,22 +19,18 @@ import java.util.logging.Logger;
|
|||
/**
|
||||
* This class manages all SensorController and EventController objects
|
||||
*/
|
||||
public class SensorControllerManager extends HalAbstractControllerManager<HalAbstractController, Sensor, HalSensorConfig> implements
|
||||
HalDeviceReportListener<HalSensorConfig, HalSensorData>,
|
||||
PreConfigurationActionListener,
|
||||
PostConfigurationActionListener {
|
||||
public class SensorControllerManager extends HalAbstractControllerManager<HalAbstractController, Sensor, HalSensorConfig>
|
||||
implements HalDeviceReportListener<HalSensorConfig, HalSensorData> {
|
||||
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
private static SensorControllerManager instance;
|
||||
|
||||
|
||||
/** All available sensor plugins **/
|
||||
private List<Class<? extends HalSensorConfig>> availableSensors = new ArrayList<>();
|
||||
/** List of all registered sensors **/
|
||||
private List<Sensor> registeredSensors = Collections.synchronizedList(new ArrayList<>());
|
||||
/** List of auto detected sensors **/
|
||||
private List<Sensor> detectedSensors = Collections.synchronizedList(new ArrayList<>());
|
||||
/** List of sensors that are currently being reconfigured **/
|
||||
private List<Sensor> limboSensors = Collections.synchronizedList(new LinkedList<>());
|
||||
|
||||
|
||||
@Override
|
||||
|
|
@ -95,7 +91,7 @@ public class SensorControllerManager extends HalAbstractControllerManager<HalAbs
|
|||
@Override
|
||||
public void deregister(Sensor sensor){
|
||||
if (sensor.getDeviceConfig() == null) {
|
||||
logger.warning("Sensor config is null: "+ sensor);
|
||||
logger.warning("Sensor config is null: " + sensor);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -192,29 +188,6 @@ public class SensorControllerManager extends HalAbstractControllerManager<HalAbs
|
|||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void preConfigurationAction(Configurator configurator, Object obj) {
|
||||
if (obj instanceof HalSensorConfig) {
|
||||
Sensor sensor = HalDeviceUtil.findDevice((HalSensorConfig) obj, registeredSensors);
|
||||
if (sensor != null){
|
||||
deregister(sensor);
|
||||
limboSensors.add(sensor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postConfigurationAction(Configurator configurator, Object obj) {
|
||||
if (obj instanceof HalSensorConfig) {
|
||||
Sensor sensor = HalDeviceUtil.findDevice((HalSensorConfig) obj, limboSensors);
|
||||
if (sensor != null){
|
||||
register(sensor);
|
||||
limboSensors.remove(sensor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static SensorControllerManager getInstance(){
|
||||
return instance;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import java.util.logging.Logger;
|
|||
/**
|
||||
*
|
||||
* @param <T> is the device controller class
|
||||
* @param <V> is the device class
|
||||
* @param <C> is the device configuration class
|
||||
*/
|
||||
public abstract class HalAbstractControllerManager<T extends HalAbstractController, V extends HalAbstractDevice, C extends HalDeviceConfig> {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package se.hal.intf;
|
|||
|
||||
import se.hal.HalContext;
|
||||
import se.hal.struct.User;
|
||||
import se.hal.util.HalDeviceChangeListener;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.db.bean.DBBean;
|
||||
import zutil.log.LogUtil;
|
||||
|
|
@ -55,9 +56,11 @@ public abstract class HalAbstractDevice<V extends HalAbstractDevice, C extends H
|
|||
public Configurator<C> getDeviceConfigurator() {
|
||||
C obj = getDeviceConfig();
|
||||
if (obj != null) {
|
||||
HalDeviceChangeListener<C> listener = new HalDeviceChangeListener<>();
|
||||
|
||||
Configurator<C> configurator = new Configurator<>(obj);
|
||||
//configurator.setPreConfigurationListener(ControllerManager.getInstance()); // TODO:
|
||||
//configurator.setPostConfigurationListener(ControllerManager.getInstance()); // TODO:
|
||||
configurator.setPreConfigurationListener(listener);
|
||||
configurator.setPostConfigurationListener(listener);
|
||||
return configurator;
|
||||
}
|
||||
return null;
|
||||
|
|
@ -71,16 +74,15 @@ public abstract class HalAbstractDevice<V extends HalAbstractDevice, C extends H
|
|||
applyConfig();
|
||||
deviceData = getLatestDeviceData(HalContext.getDB());
|
||||
} catch (Exception e) {
|
||||
logger.log(Level.SEVERE, "Unable instantiate DeviceConfig: " + type, e);
|
||||
logger.log(Level.SEVERE, "Unable instantiate HalDeviceConfig: " + type, e);
|
||||
}
|
||||
}
|
||||
return deviceConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will replace the current DeviceData.
|
||||
* And the current config will be applied on the new DeviceData.
|
||||
* DeviceData will be reset if the input is set as null.
|
||||
* Will replace the current device configuration.
|
||||
* The device configuration will be reset if the input is set as null.
|
||||
*/
|
||||
public void setDeviceConfig(C data) {
|
||||
if (data != null) {
|
||||
|
|
|
|||
52
hal-core/src/se/hal/util/HalDeviceChangeListener.java
Normal file
52
hal-core/src/se/hal/util/HalDeviceChangeListener.java
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
package se.hal.util;
|
||||
|
||||
import se.hal.HalServer;
|
||||
import se.hal.intf.HalAbstractControllerManager;
|
||||
import se.hal.intf.HalAbstractDevice;
|
||||
import se.hal.intf.HalDeviceConfig;
|
||||
import zutil.ui.Configurator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A listener implementation that will deregister a device and then re-register it on all interested managers.
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
public class HalDeviceChangeListener<C extends HalDeviceConfig> implements Configurator.PreConfigurationActionListener<C>, Configurator.PostConfigurationActionListener<C> {
|
||||
/** List of sensors that are currently being reconfigured **/
|
||||
private Map<HalAbstractDevice, List<HalAbstractControllerManager>> limboDevices = new HashMap<>();
|
||||
|
||||
|
||||
@Override
|
||||
public void preConfigurationAction(Configurator configurator, HalDeviceConfig deviceConfig) {
|
||||
List<HalAbstractControllerManager> managers = HalServer.getControllerManagers();
|
||||
|
||||
for (HalAbstractControllerManager manager : managers) {
|
||||
HalAbstractDevice device = HalDeviceUtil.findDevice(deviceConfig, manager.getRegisteredDevices());
|
||||
if (device != null) {
|
||||
manager.deregister(device);
|
||||
|
||||
if (!limboDevices.containsKey(device))
|
||||
limboDevices.put(device, new ArrayList<>(2));
|
||||
limboDevices.get(device).add(manager);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postConfigurationAction(Configurator configurator, HalDeviceConfig deviceConfig) {
|
||||
HalAbstractDevice device = HalDeviceUtil.findDevice(deviceConfig, new ArrayList<>(limboDevices.keySet()));
|
||||
List<HalAbstractControllerManager> managers = limboDevices.get(device);
|
||||
|
||||
if (managers != null) {
|
||||
for (HalAbstractControllerManager manager : managers) {
|
||||
manager.register(device);
|
||||
}
|
||||
|
||||
limboDevices.remove(device);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue