Introduced AbstractController

This commit is contained in:
Ziver Koc 2020-07-23 23:44:40 +02:00
parent 267eda7afd
commit d759ebcd77
8 changed files with 103 additions and 68 deletions

View file

@ -34,6 +34,25 @@
</div>
</div>
<div class="col-md-12">
<div class="panel panel-default drop-shadow">
<div class="panel-heading">Active Controllers</div>
<div class="panel-body">
<table class="table table-hover table-condensed">
<thead>
<th class="col-md-3">Name</th>
</thead>
{{#controllers}}
<tr>
<td>{{.getClass().getName()}}</td>
</tr>
{{/controllers}}
</table>
</div>
</div>
</div>
<script>
$(function (){
$(".toggle-switch").on("switchChange.bootstrapSwitch", function (event, state) {

View file

@ -1,5 +1,6 @@
package se.hal;
import com.google.common.collect.Lists;
import se.hal.intf.*;
import se.hal.struct.Event;
import se.hal.struct.Sensor;
@ -49,8 +50,7 @@ public class ControllerManager implements HalSensorReportListener,
/** A map of all instantiated controllers **/
private HashMap<Class,Object> controllerMap = new HashMap<>();
private HashMap<Class,HalAbstractController> controllerMap = new HashMap<>();
/////////////////////////////// SENSORS ///////////////////////////////////
@ -332,6 +332,13 @@ public class ControllerManager implements HalSensorReportListener,
/////////////////////////////// GENERAL ///////////////////////////////////
/**
* @return all instantiated controllers.
*/
public List<HalAbstractController> getControllers() {
return new ArrayList(controllerMap.values());
}
@Override
public void preConfigurationAction(Configurator configurator, Object obj) {
if(obj instanceof HalSensorConfig) {
@ -368,38 +375,40 @@ public class ControllerManager implements HalSensorReportListener,
}
}
private <T> T getControllerInstance(Class<T> c){
Object controller;
if (controllerMap.containsKey(c))
private <T extends HalAbstractController> T getControllerInstance(Class<T> c){
HalAbstractController controller;
if (controllerMap.containsKey(c)) {
controller = controllerMap.get(c);
else {
// Instantiate controller
} else {
try {
// Instantiate controller
controller = c.newInstance();
if (controller instanceof HalAutoScannableController &&
! ((HalAutoScannableController)controller).isAvailable()) {
logger.warning("Controller is not ready: "+c.getName());
! ((HalAutoScannableController) controller).isAvailable()) {
logger.warning("Controller is not ready: " + c.getName());
return null;
}
logger.info("Instantiating new controller: " + c.getName());
controller.initialize();
if(controller instanceof HalSensorController) {
((HalSensorController) controller).setListener(this);
((HalSensorController) controller).initialize();
}
if(controller instanceof HalEventController) {
((HalEventController) controller).setListener(this);
if( ! (controller instanceof HalSensorController))
((HalEventController) controller).initialize();
}
controllerMap.put(c, controller);
} catch (Exception e){
logger.log(Level.SEVERE, "Unable to instantiate controller: "+c.getName(), e);
logger.log(Level.SEVERE, "Unable to instantiate controller: " + c.getName(), e);
return null;
}
}
return (T)controller;
return (T) controller;
}
private void removeControllerIfEmpty(Object controller){
@ -414,7 +423,7 @@ public class ControllerManager implements HalSensorReportListener,
if(size < 0){
// Remove controller as it has no more registered sensors
logger.info("Closing controller as it has no more registered devices: "+controller.getClass().getName());
logger.info("Closing controller as it has no more registered devices: " + controller.getClass().getName());
controllerMap.remove(controller.getClass());
if(controller instanceof HalSensorController)
@ -425,8 +434,6 @@ public class ControllerManager implements HalSensorReportListener,
}
public static void initialize(PluginManager pluginManager){
ControllerManager manager = new ControllerManager();
@ -440,10 +447,12 @@ public class ControllerManager implements HalSensorReportListener,
manager.addAvailableEvent(it.next());
}
for (Iterator<Class<? extends HalAutoScannableController>> it=
pluginManager.getClassIterator(HalAutoScannableController.class);
for (Iterator<Class<? extends HalAutoScannableController>> it = pluginManager.getClassIterator(HalAutoScannableController.class);
it.hasNext(); ){
manager.getControllerInstance(it.next()); // Instantiate controller
Class controller = it.next();
if (controller.isAssignableFrom(HalAbstractController.class))
manager.getControllerInstance(controller); // Instantiate controller
}
instance = manager;
@ -453,5 +462,4 @@ public class ControllerManager implements HalSensorReportListener,
public static ControllerManager getInstance(){
return instance;
}
}

View file

@ -172,7 +172,7 @@ public class HalContext {
public static HashMap<String,String> getProperties() {
public static Map<String,String> getProperties() {
HashMap map = new HashMap();
map.putAll(fileConf);
map.putAll(dbConf);

View file

@ -0,0 +1,49 @@
/*
* 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.intf;
public interface HalAbstractController {
/**
* The framework might create dummy objects so any type of
* resource initialization should be handled in this method
* and not in the constructor.
*/
void initialize() throws Exception;
/**
* @return the number of registered devices.
*/
int size();
/**
* Close any resources associated with this controller.
* This method could be called multiple times, first time
* should be handled as normal any subsequent calls should be ignored.
*/
void close();
}

View file

@ -1,13 +1,6 @@
package se.hal.intf;
public interface HalEventController {
/**
* The framework might create dummy objects so any type of
* resource initialization should be handled in this method
* and not in the constructor.
*/
void initialize() throws Exception;
public interface HalEventController extends HalAbstractController {
/**
* Will register an event type to be handled by this controller
@ -26,21 +19,8 @@ public interface HalEventController {
*/
void send(HalEventConfig eventConfig, HalEventData eventData);
/**
* @return the number of registered objects
*/
int size();
/**
* Set a listener that will receive all reports from the the registered Events
*/
void setListener(HalEventReportListener listener);
/**
* Close any resources associated with this controller.
* This method could be called multiple times, first time
* should be handled as normal any subsequent calls should be ignored.
*/
void close();
}

View file

@ -1,13 +1,6 @@
package se.hal.intf;
public interface HalSensorController {
/**
* The framework might create dummy objects so any type of
* resource initialization should be handled in this method
* and not in the constructor.
*/
void initialize() throws Exception;
public interface HalSensorController extends HalAbstractController {
/**
* Will register a sensor type to be handled by this controller
@ -20,21 +13,8 @@ public interface HalSensorController {
*/
void deregister(HalSensorConfig sensorConfig);
/**
* @return the number of registered objects
*/
int size();
/**
* Set a listener that will receive all reports from the the registered Sensors
*/
void setListener(HalSensorReportListener listener);
/**
* Close any resources associated with this controller.
* This method could be called multiple times, first time
* should be handled as normal any subsequent calls should be ignored.
*/
void close();
}

View file

@ -1,5 +1,6 @@
package se.hal.page;
import se.hal.ControllerManager;
import se.hal.HalContext;
import se.hal.HalServer;
import se.hal.intf.HalWebPage;
@ -43,7 +44,7 @@ public class PluginConfigWebPage extends HalWebPage {
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
tmpl.set("plugins", HalServer.getPlugins());
tmpl.set("controllers", ControllerManager.getInstance().getControllers());
return tmpl;
}
}

View file

@ -24,9 +24,7 @@ public class PropertyConfigWebPage extends HalWebPage {
Map<String, String> request)
throws Exception{
DBConnection db = HalContext.getDB();
HashMap properties = HalContext.getProperties();
Map<String,String> properties = HalContext.getProperties();
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
tmpl.set("properties", properties.entrySet());