Added AlertAction and fixed enum selection issues on the frontend

This commit is contained in:
Ziver Koc 2023-01-06 00:16:45 +01:00
parent 0efa7320e3
commit b5ac492414
7 changed files with 91 additions and 28 deletions

View file

@ -170,7 +170,7 @@
"min",
"hour",
"day",
"week",
"week"
]
},
"in": "query",

View file

@ -153,8 +153,9 @@ function initDynamicModalForm(modalId, formTemplateId = null, templateID = null)
if (formTemplateId != null) {
dynamicConf[formTemplateId] = [];
$("#" + templateID + " div").each(function(){
dynamicConf[formTemplateId][$(this).attr("id")] = $(this).html();
dynamicConf[formTemplateId][$(this).prop("id")] = $(this).html();
});
// Update dynamic inputs
$("#" + modalId + " select[name=type]").change(function(){
$("#" + modalId + " #" + formTemplateId).html(dynamicConf[formTemplateId][$(this).val()]);
@ -170,26 +171,36 @@ function initDynamicModalForm(modalId, formTemplateId = null, templateID = null)
if (formTemplateId != null)
modal.find("#" + formTemplateId).empty(); // clear form div
// select dynamic form
var selector = modal.find("select[name=type]");
selector.val(button.data("type"));
selector.change(); // Update dynamic inputs
// set dynamic form data
$.each(button.attr(), function(fieldName, value) {
if(fieldName.startsWith("data-")) {
fieldName = fieldName.substring(5);
fieldName = fieldName.substring(5); // remove prefix data-
// case insensitive search
input = modal.find("input").filter(function() {
return this.name.toLowerCase() == fieldName;
var input = modal.find("input, select").filter(function() {
if (this.name.toLowerCase() == fieldName) {
if (this.type == "hidden" && modal.find("input[type=checkbox][name=" + fieldName + "]") != null)
return false; // Workaround for the default(false) boolean input
return true;
}
return false;
});
if (input.attr("type") == "checkbox") { // special handling for checkboxes
input.attr("value", "true");
if (value=="true") input.attr("checked", "true");
else input.removeAttr("checked");
// Add default false value as a unchecked checkbox is not included in the post
input.parent().prepend("<input type='hidden' name='" + input.attr("name") + "' value='false' />");
} else {
input.val(value);
if (input != null) {
if (input.prop("type") == "checkbox") { // special handling for checkboxes
input.prop("value", "true");
input.prop("checked", value == "true");
if (modal.find("input[type=hidden][name=" + fieldName + "]") == null) {
// Add default false value as a unchecked checkbox is not included in the post
input.parent().prepend("<input type='hidden' name='" + input.prop("name") + "' value='false' />");
}
} else {
input.val(value);
if (input.prop("tagName") == "SELECT")
input.change(); // required for select elements to update properly
}
}
}
});

View file

@ -14,15 +14,23 @@
<div class="panel panel-default drop-shadow">
<div class="panel-heading clearfix" style="padding: 4px 15px;">
<b class="panel-title">{{.getName()}}</b>
<div class="pull-right">
<button type="button" class="btn btn-default btn-xs" data-toggle="modal" style="padding: 1px 20px;"
data-target="#flowModal"
data-flow-id="{{.getId()}}"
data-enabled="{{.isEnabled()}}"
data-name="{{.getName()}}" >
<span class="glyphicon glyphicon-pencil"></span>
</button>
</div>
<form method="POST">
<input type="hidden" name="flow-id" value="{{.getId()}}">
<div class="btn-toolbar pull-right">
<button type="submit" class="btn btn-primary btn-xs" name="action" value="execute_flow">
<small class="glyphicon glyphicon-play"></small>
</button>
<button type="button" class="btn btn-default btn-xs" data-toggle="modal" style="padding: 1px 20px;"
data-target="#flowModal"
data-flow-id="{{.getId()}}"
data-enabled="{{.isEnabled()}}"
data-name="{{.getName()}}" >
<span class="glyphicon glyphicon-pencil"></span>
</button>
</div>
</form>
</div>
<div class="panel-body {{^.isEnabled()}}disabled{{/.isEnabled()}}" style="padding-bottom: 0px;">
<table class="table table-condensed" style="margin-bottom: 0px;">

View file

@ -0,0 +1,36 @@
package se.hal.action;
import se.hal.intf.HalAction;
import se.hal.page.HalAlertManager;
import zutil.log.LogUtil;
import zutil.ui.UserMessageManager.MessageTTL;
import zutil.ui.conf.Configurator;
import java.util.logging.Logger;
import static zutil.ui.UserMessageManager.*;
/**
* Action that will alert users with a message
*/
public class AlertAction implements HalAction {
private static final Logger logger = LogUtil.getLogger();
@Configurator.Configurable("Alert Severity")
private MessageLevel severity = MessageLevel.INFO;
@Configurator.Configurable("Alert Message")
private MessageTTL ttl = MessageTTL.ONE_VIEW;
@Configurator.Configurable("Alert Message")
private String message = "";
@Override
public void execute() {
HalAlertManager.getInstance().addAlert(new UserMessage(severity, message, ttl));
}
public String toString(){
return "Send Alert: " + severity + ": " + message;
}
}

View file

@ -64,17 +64,21 @@ public class TriggerWebPage extends HalWebPage {
break;
case "modify_flow":
logger.info("Modifying flow: " + flow.getName());
logger.info("Modifying flow(id: " + flow.getId() + "): " + flow.getName());
flow.setEnabled("on".equals(request.get("enabled")));
flow.setName(request.get("name"));
flow.save(db);
break;
case "remove_flow":
logger.info("Removing flow: " + flow.getName());
logger.info("Removing flow(id: " + flow.getId() + "): " + flow.getName());
flow.delete(db);
break;
case "execute_flow":
flow.execute();
break;
// Triggers
case "create_trigger":
if (flow == null) {

View file

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

View file

@ -114,6 +114,9 @@ public class TriggerFlow extends DBBean {
public void execute(){
if (!enabled)
return;
logger.info("Executing flow(id: " + getId() + "): " + getName());
for (Action action : actionList){
action.execute();
}