Some more backend ui code for triggers. issue 6

This commit is contained in:
Ziver Koc 2017-01-27 17:38:39 +01:00
parent 115bf6efac
commit 8d9354d845
4 changed files with 96 additions and 39 deletions

View file

@ -1,10 +1,12 @@
package se.hal.intf;
import se.hal.struct.TriggerFlow;
import se.hal.struct.dso.ActionDSO;
import zutil.db.DBConnection;
import zutil.db.bean.DBBean;
import java.sql.SQLException;
import java.util.List;
/**
* Defines a action that will be executed
@ -18,6 +20,10 @@ public abstract class HalAction{
dso.getObject().dso = dso;
return dso.getObject();
}
public static List<HalAction> getActions(DBConnection db, TriggerFlow flow) {
// TODO:
return null;
}
public Long getId(){
@ -31,6 +37,9 @@ public abstract class HalAction{
dso.save(db);
}
public void delete(DBConnection db) throws SQLException {
dso.delete(db);
}
/**

View file

@ -1,10 +1,12 @@
package se.hal.intf;
import se.hal.struct.TriggerFlow;
import se.hal.struct.dso.TriggerDSO;
import zutil.db.DBConnection;
import zutil.db.bean.DBBean;
import java.sql.SQLException;
import java.util.List;
/**
* A class that declares a trigger/condition that
@ -19,6 +21,9 @@ public abstract class HalTrigger{
dso.getObject().dso = dso;
return dso.getObject();
}
public static List<HalTrigger> getTriggers(DBConnection db, TriggerFlow flow) {
return null;
}
public Long getId(){
@ -32,6 +37,11 @@ public abstract class HalTrigger{
dso.save(db);
}
public void delete(DBConnection db) throws SQLException {
dso.delete(db);
}
/**
* Evaluates if this trigger has passed. If the trigger is
@ -44,4 +54,5 @@ public abstract class HalTrigger{
* Reset the evaluation to false.
*/
public abstract void reset();
}

View file

@ -1,31 +1,39 @@
package se.hal.page;
import se.hal.ControllerManager;
import se.hal.HalContext;
import se.hal.TriggerManager;
import se.hal.intf.HalAction;
import se.hal.intf.HalHttpPage;
import se.hal.struct.Event;
import se.hal.intf.HalTrigger;
import se.hal.struct.ClassConfigurationData;
import se.hal.struct.TriggerFlow;
import se.hal.struct.devicedata.SwitchEventData;
import se.hal.util.HistoryDataListSqlResult;
import se.hal.util.HistoryDataListSqlResult.HistoryData;
import zutil.db.DBConnection;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
public class TriggerHttpPage extends HalHttpPage {
private static final String TEMPLATE = "resource/web/trigger.tmpl";
private ArrayList<ClassConfigurationData> triggerConfigurators;
private ArrayList<ClassConfigurationData> actionConfigurators;
public TriggerHttpPage(){
super("trigger");
super.getRootNav().createSubNav("Events").createSubNav(this.getId(), "Triggers");
triggerConfigurators = new ArrayList<>();
for(Class c : TriggerManager.getInstance().getAvailableTriggers())
triggerConfigurators.add(new ClassConfigurationData(c));
actionConfigurators = new ArrayList<>();
for(Class c : TriggerManager.getInstance().getAvailableActions())
actionConfigurators.add(new ClassConfigurationData(c));
}
@Override
public Templator httpRespond(
Map<String, Object> session,
@ -35,17 +43,59 @@ public class TriggerHttpPage extends HalHttpPage {
DBConnection db = HalContext.getDB();
if(request.containsKey("action")){
TriggerFlow flow = null;
if (request.containsKey("flow_id"))
flow = TriggerFlow.getTriggerFlow(db, Integer.parseInt(request.get("flow_id")));
HalTrigger trigger = null;
if (request.containsKey("trigger_id"))
trigger = HalTrigger.getTrigger(db, Integer.parseInt(request.get("trigger_id")));
HalAction action = null;
if (request.containsKey("action_id"))
action = HalAction.getAction(db, Integer.parseInt(request.get("action_id")));
switch(request.get("action")) {
// Local Sensors
// Flows
case "create_flow":
TriggerFlow flow = new TriggerFlow();
flow = new TriggerFlow();
flow.save(db);
break;
case "remove_flow":
flow.delete(db);
break;
// Triggers
case "create_trigger":
//TODO: trigger = new HalTrigger();
flow.addTrigger(trigger);
case "modify_trigger":
// TODO: save attrib
trigger.save(db);
break;
case "remove_trigger":
flow.removeTrigger(trigger);
trigger.delete(db);
break;
// Triggers
case "create_action":
//TODO: action = new HalAction();
flow.addAction(action);
case "modify_action":
// TODO: save attrib
action.save(db);
break;
case "remove_action":
flow.removeAction(action);
action.delete(db);
break;
}
}
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
tmpl.set("triggerConfigurations", triggerConfigurators);
tmpl.set("actionConfigurations", actionConfigurators);
tmpl.set("availableTriggers", TriggerManager.getInstance().getAvailableTriggers());
tmpl.set("availableActions", TriggerManager.getInstance().getAvailableActions());
tmpl.set("flows", TriggerFlow.getTriggerFlows(db));

View file

@ -26,10 +26,8 @@ import java.util.logging.Logger;
public class TriggerFlow extends DBBean {
private static final Logger logger = LogUtil.getLogger();
private String triggers; // only used for flat DB storage
private transient ArrayList<HalTrigger> triggerList = new ArrayList<>();
private String actions; // only used for flat DB storage
private transient ArrayList<HalAction> actionList = new ArrayList<>();
private transient List<HalTrigger> triggerList = new ArrayList<>();
private transient List<HalAction> actionList = new ArrayList<>();
@ -37,50 +35,37 @@ public class TriggerFlow extends DBBean {
PreparedStatement stmt = db.getPreparedStatement("SELECT * FROM trigger_flow");
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(TriggerFlow.class, db));
}
@Override
public void save(DBConnection db) throws SQLException {
DataNode root = new DataNode(DataNode.DataType.List);
for (HalTrigger t : triggerList)
root.add(t.getId());
triggers = JSONWriter.toString(root);
for (HalAction a : actionList)
root.add(a.getId());
actions = JSONWriter.toString(root);
super.save(db);
public static TriggerFlow getTriggerFlow(DBConnection db, int id) throws SQLException {
return DBBean.load(db, TriggerFlow.class, id);
}
@Override
protected void postUpdateAction() {
DBConnection db = HalContext.getDB();
triggerList.clear();
for (DataNode tId : JSONParser.read(triggers))
try {
triggerList.add(HalTrigger.getTrigger(db, tId.getInt()));
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
}
triggerList = HalTrigger.getTriggers(db, this);
actionList.clear();
for (DataNode aId : JSONParser.read(actions))
try {
actionList.add(HalAction.getAction(db, aId.getInt()));
} catch (SQLException e) {
logger.log(Level.SEVERE, null, e);
}
actionList = HalAction.getActions(db, this);
}
public void addTrigger(HalTrigger trigger) {
triggerList.add(trigger);
}
public void removeTrigger(HalTrigger trigger) {
triggerList.remove(trigger);
}
public void addAction(HalAction action) {
actionList.add(action);
}
public void removeAction(HalAction action) {
actionList.remove(action);
}
/**
* @return true if any one of the triggerList evaluate to true,
@ -112,4 +97,6 @@ public class TriggerFlow extends DBBean {
trigger.reset();
}
}
}