Implemented instantiatiation of triggers and actions. issue 6

This commit is contained in:
Ziver Koc 2017-02-08 17:20:41 +01:00
parent 9fa687081a
commit 36f2509d94
8 changed files with 100 additions and 56 deletions

View file

@ -94,7 +94,7 @@
<target name="build-dependencies">
<mkdir dir="${buildDir}" />
<get src="http://ci.koc.se/jenkins/job/Zutil/131/artifact/build/release/Zutil.jar"
<get src="http://ci.koc.se/jenkins/job/Zutil/136/artifact/build/release/Zutil.jar"
dest="${libDir}" verbose="true" usetimestamp="true"/>
</target>

View file

@ -20,7 +20,7 @@
<th class="col-md-5 text-center">
Triggers
<button class="btn btn-default btn-xs pull-right" data-toggle="modal"
data-target="#triggerModal">
data-target="#triggerModal" data-action="create_trigger" data-flow-id="{{.getId()}}">
<span class="glyphicon glyphicon-plus"></span>
</button>
</th>
@ -28,7 +28,7 @@
<th class="col-md-6 text-center">
Actions
<button class="btn btn-default btn-xs pull-right" data-toggle="modal"
data-target="#actionModal">
data-target="#actionModal" data-action="create_action" data-flow-id="{{.getId()}}">
<span class="glyphicon glyphicon-plus"></span>
</button>
</th>
@ -38,24 +38,14 @@
<td>
<!-- TRIGGERS -->
<table class="table table-hover table-condensed table-borderless">
{{#.getTriggers()}}
<tr><td>
<div class="panel panel-default drop-shadow"><div class="panel-body">
<span class="glyphicon glyphicon-time"></span>
test trigger
</div></div>
</td></tr>
<tr><td>
<div class="panel panel-default drop-shadow"><div class="panel-body">
<span class="glyphicon glyphicon-time"></span>
test trigger
</div></div>
</td></tr>
<tr><td>
<div class="panel panel-default drop-shadow"><div class="panel-body">
<span class="glyphicon glyphicon-time"></span>
test trigger
{{.}}
</div></div>
</td></tr>
{{/.getTriggers()}}
</table>
<!-- /TRIGGERS -->
</td>
@ -66,24 +56,14 @@
<td>
<!-- ACTIONS -->
<table class="table table-hover table-condensed table-borderless">
{{#.getActions()}}
<tr><td>
<div class="panel panel-default drop-shadow"><div class="panel-body">
<span class="glyphicon glyphicon-play-circle"></span>
test action
</div></div>
</td></tr>
<tr><td>
<div class="panel panel-default drop-shadow"><div class="panel-body">
<span class="glyphicon glyphicon-play-circle"></span>
test action
</div></div>
</td></tr>
<tr><td>
<div class="panel panel-default drop-shadow"><div class="panel-body">
<span class="glyphicon glyphicon-play-circle"></span>
test action
{{.}}
</div></div>
</td></tr>
{{/.getActions()}}
</table>
<!-- /ACTIONS -->
</td>
@ -121,14 +101,8 @@
$("#"+name+"Modal").on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var modal = $(this);
modal.find("input[type=text]").val(""); // Reset all inputs
if(button.data("id") >= 0){ // edit
modal.find("input[name=action]").val("modify_"+name);
}
else{ // create
modal.find("input[name=action]").val("create_"+name);
modal.find("input[name=id]").val(-1);
}
modal.find("input").val(""); // Reset all inputs
// set dynamic form data
modal.find("select[name=type]").val(button.data("type"));
modal.find("select[name=type]").change(); // Update dynamic inputs
@ -155,8 +129,9 @@
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" id="action" name="action" value="">
<input type="hidden" id="id" name="id">
<input type="hidden" name="action" value="">
<input type="hidden" name="flow-id">
<input type="hidden" name="trigger-id">
<div class="form-group">
<label class="control-label">Type:</label>
<select class="form-control" name="type">
@ -190,8 +165,9 @@
</div>
<form method="POST">
<div class="modal-body">
<input type="hidden" id="action" name="action" value="">
<input type="hidden" id="id" name="id">
<input type="hidden" name="action" value="">
<input type="hidden" name="flow_id">
<input type="hidden" name="action_id">
<div class="form-group">
<label class="control-label">Type:</label>
<select class="form-control" name="type">
@ -236,7 +212,7 @@
{{/triggerConf}}
</div>
<div id="sensor-data-conf-template" class="hidden">
<div id="action-data-conf-template" class="hidden">
{{#actionConf}}
<div id="{{.clazz.getName()}}">
{{#.params}}

View file

@ -46,14 +46,14 @@ public class TriggerHttpPage extends HalHttpPage {
if(request.containsKey("action")){
TriggerFlow flow = null;
if (request.containsKey("flow_id"))
flow = TriggerFlow.getTriggerFlow(db, Integer.parseInt(request.get("flow_id")));
if (request.containsKey("flow-id") && !request.get("flow-id").isEmpty())
flow = TriggerFlow.getTriggerFlow(db, Integer.parseInt(request.get("flow-id")));
Trigger trigger = null;
if (request.containsKey("trigger_id"))
trigger = Trigger.getTrigger(db, Integer.parseInt(request.get("trigger_id")));
if (request.containsKey("trigger-id") && !request.get("trigger-id").isEmpty())
trigger = Trigger.getTrigger(db, Integer.parseInt(request.get("trigger-id")));
Action action = null;
if (request.containsKey("action_id"))
action = Action.getAction(db, Integer.parseInt(request.get("action_id")));
if (request.containsKey("action-id") && !request.get("action-id").isEmpty())
action = Action.getAction(db, Integer.parseInt(request.get("action-id")));
switch(request.get("action")) {
@ -68,10 +68,17 @@ public class TriggerHttpPage extends HalHttpPage {
// Triggers
case "create_trigger":
//TODO: trigger = new HalTrigger();
if (flow == null){
HalAlertManager.getInstance().addAlert(new HalAlertManager.HalAlert(
HalAlertManager.AlertLevel.ERROR, "Invalid flow id", HalAlertManager.AlertTTL.ONE_VIEW));
break;
}
trigger = new Trigger();
flow.addTrigger(trigger);
/* FALLTHROUGH */
case "modify_trigger":
// TODO: save attrib
trigger.setObjectClass(request.get("type"));
trigger.getObjectConfigurator().setValues(request).applyConfiguration();
trigger.save(db);
break;
case "remove_trigger":
@ -81,10 +88,17 @@ public class TriggerHttpPage extends HalHttpPage {
// Triggers
case "create_action":
//TODO: action = new HalAction();
if (flow == null){
HalAlertManager.getInstance().addAlert(new HalAlertManager.HalAlert(
HalAlertManager.AlertLevel.ERROR, "Invalid flow id", HalAlertManager.AlertTTL.ONE_VIEW));
break;
}
action = new Action();
flow.addAction(action);
/* FALLTHROUGH */
case "modify_action":
// TODO: save attrib
action.setObjectClass(request.get("type"));
action.getObjectConfigurator().setValues(request).applyConfiguration();
action.save(db);
break;
case "remove_action":

View file

@ -20,6 +20,7 @@
{"se.hal.intf.HalTrigger": "se.hal.trigger.DateTimeTrigger"},
{"se.hal.intf.HalTrigger": "se.hal.trigger.TimerTrigger"}
{"se.hal.intf.HalTrigger": "se.hal.trigger.TimerTrigger"},
{"se.hal.intf.HalAction": "se.hal.trigger.action.SendEventAction"}
]
}

View file

@ -7,6 +7,7 @@ import zutil.db.bean.DBBean;
import zutil.db.bean.DBBeanObjectDSO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
@ -21,7 +22,7 @@ public class Action extends DBBeanObjectDSO<HalAction>{
}
public static List<Action> getActions(DBConnection db, TriggerFlow flow) {
// TODO:
return null;
return new ArrayList<>();
}

View file

@ -6,6 +6,7 @@ import zutil.db.bean.DBBean;
import zutil.db.bean.DBBeanObjectDSO;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
@ -22,7 +23,7 @@ public class Trigger extends DBBeanObjectDSO<HalTrigger>{
}
public static List<Trigger> getTriggers(DBConnection db, TriggerFlow flow) {
// Todo:
return null;
return new ArrayList<>();
}

View file

@ -21,6 +21,6 @@ public class TimerTrigger implements HalTrigger {
@Override
public void reset() {
timer = new Timer(timerTime * 1000);
timer = new Timer(timerTime * 1000).start();
}
}

View file

@ -0,0 +1,51 @@
package se.hal.trigger.action;
import se.hal.ControllerManager;
import se.hal.HalContext;
import se.hal.intf.HalAction;
import se.hal.intf.HalEventData;
import se.hal.struct.Event;
import zutil.db.DBConnection;
import zutil.log.LogUtil;
import zutil.ui.Configurator;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
*/
public class SendEventAction implements HalAction {
private static final Logger logger = LogUtil.getLogger();
@Configurator.Configurable(order = 1, value = "Event Device ID")
private int eventId;
@Configurator.Configurable(order = 1, value = "Data to Send")
private double data;
@Override
public void execute() {
try {
DBConnection db = HalContext.getDB();
Event event = Event.getEvent(db, eventId);
if (event != null) {
HalEventData dataObj = event.getDeviceConfig().getEventDataClass().newInstance();
dataObj.setData(data);
event.setDeviceData(dataObj);
// Send
ControllerManager.getInstance().send(event);
}
else
logger.warning("Unable to find event with id: "+ eventId);
} catch (Exception e) {
logger.log(Level.SEVERE, null, e);
}
}
public String toString(){
return "Send event: "+ eventId +" with data: "+ data;
}
}