Added event config page
Former-commit-id: 96256bf1c1c7c4ac2f7a851fc816910cf5b79bb1
This commit is contained in:
parent
2c98cd897d
commit
068e19db22
5 changed files with 305 additions and 4 deletions
|
|
@ -52,7 +52,7 @@ public class HalServer {
|
|||
for(Sensor sensor : Sensor.getLocalSensors(db)){
|
||||
ControllerManager.getInstance().register(sensor);
|
||||
}
|
||||
for(Event event : Event.getEvents(db)){
|
||||
for(Event event : Event.getLocalEvents(db)){
|
||||
ControllerManager.getInstance().register(event);
|
||||
}
|
||||
|
||||
|
|
|
|||
95
src/se/hal/page/EventConfigHttpPage.java
Executable file
95
src/se/hal/page/EventConfigHttpPage.java
Executable file
|
|
@ -0,0 +1,95 @@
|
|||
package se.hal.page;
|
||||
|
||||
import se.hal.ControllerManager;
|
||||
import se.hal.HalContext;
|
||||
import se.hal.intf.HalHttpPage;
|
||||
import se.hal.struct.Event;
|
||||
import se.hal.struct.Sensor;
|
||||
import se.hal.struct.User;
|
||||
import zutil.db.DBConnection;
|
||||
import zutil.io.file.FileUtil;
|
||||
import zutil.parser.Templator;
|
||||
import zutil.ui.Configurator;
|
||||
import zutil.ui.Configurator.ConfigurationParam;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class EventConfigHttpPage extends HalHttpPage {
|
||||
private static final String TEMPLATE = "web-resource/event_config.tmpl";
|
||||
|
||||
private class EventDataParams{
|
||||
public Class clazz;
|
||||
public ConfigurationParam[] params;
|
||||
}
|
||||
private EventDataParams[] eventConfigurations;
|
||||
|
||||
|
||||
public EventConfigHttpPage() {
|
||||
super("Configuration", "event_config");
|
||||
|
||||
eventConfigurations = new EventDataParams[
|
||||
ControllerManager.getInstance().getAvailableSensors().size()];
|
||||
int i=0;
|
||||
for(Class c : ControllerManager.getInstance().getAvailableEvents()){
|
||||
eventConfigurations[i] = new EventDataParams();
|
||||
eventConfigurations[i].clazz = c;
|
||||
eventConfigurations[i].params = Configurator.getConfiguration(c);
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Templator httpRespond(
|
||||
Map<String, Object> session,
|
||||
Map<String, String> cookie,
|
||||
Map<String, String> request)
|
||||
throws Exception{
|
||||
|
||||
DBConnection db = HalContext.getDB();
|
||||
User localUser = User.getLocalUser(db);
|
||||
|
||||
// Save new input
|
||||
if(request.containsKey("action")){
|
||||
int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1);
|
||||
Event event;
|
||||
switch(request.get("action")) {
|
||||
// Local Sensors
|
||||
case "create_local_event":
|
||||
event = new Event();
|
||||
event.setName(request.get("name"));
|
||||
event.setType(request.get("type"));
|
||||
//sensor.setConfig(request.get("config"));
|
||||
event.setUser(localUser);
|
||||
event.save(db);
|
||||
case "modify_local_event":
|
||||
event = Event.getEvent(db, id);
|
||||
if(event != null){
|
||||
event.setName(request.get("name"));
|
||||
event.setType(request.get("type"));
|
||||
//sensor.setConfig(request.get("config"));
|
||||
event.setUser(localUser);
|
||||
event.save(db);
|
||||
}
|
||||
break;
|
||||
case "remove_local_event":
|
||||
event = Event.getEvent(db, id);
|
||||
if(event != null)
|
||||
event.delete(db);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Output
|
||||
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));
|
||||
tmpl.set("user", localUser);
|
||||
tmpl.set("localEvent", Event.getLocalEvents(db));
|
||||
tmpl.set("localEventConf", eventConfigurations);
|
||||
|
||||
|
||||
tmpl.set("availableSensors", ControllerManager.getInstance().getAvailableSensors());
|
||||
|
||||
return tmpl;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -33,12 +33,16 @@ public class Event extends DBBean{
|
|||
private transient HalEvent eventData;
|
||||
|
||||
// User configuration
|
||||
@DBColumn("user_id")
|
||||
private User user;
|
||||
|
||||
|
||||
public static Event getEvent(DBConnection db, long id) throws SQLException{
|
||||
return DBBean.load(db, Event.class, id);
|
||||
}
|
||||
|
||||
public static List<Event> getEvents(DBConnection db) throws SQLException {
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT * FROM event" );
|
||||
public static List<Event> getLocalEvents(DBConnection db) throws SQLException {
|
||||
PreparedStatement stmt = db.getPreparedStatement( "SELECT event.* FROM event,user WHERE user.external == 0 AND user.id == event.user_id" );
|
||||
return DBConnection.exec(stmt, DBBeanSQLResultHandler.createList(Event.class, db) );
|
||||
}
|
||||
|
||||
|
|
@ -80,6 +84,34 @@ public class Event extends DBBean{
|
|||
}
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getType() {
|
||||
return type;
|
||||
}
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
public String getConfig() {
|
||||
return config;
|
||||
}
|
||||
public void setConfig(String config) {
|
||||
this.config = config;
|
||||
this.eventData = null; // invalidate current sensor data object
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
}
|
||||
public void setUser(User user) {
|
||||
this.user = user;
|
||||
}
|
||||
|
||||
|
||||
public Class<? extends HalEventController> getController(){
|
||||
return getEventData().getEventController();
|
||||
}
|
||||
|
|
|
|||
174
web-resource/event_config.tmpl
Executable file
174
web-resource/event_config.tmpl
Executable file
|
|
@ -0,0 +1,174 @@
|
|||
<h1 class="page-header">Event Configuration</h1>
|
||||
|
||||
<div class="col-md-12">
|
||||
<div class="panel panel-default drop-shadow">
|
||||
<div class="panel-heading">Local Events</div>
|
||||
<div class="panel-body">
|
||||
<p>This is a local list of events connected to this node.</p>
|
||||
|
||||
<table class="table table-hover table-condensed">
|
||||
<thead>
|
||||
<th>Name</th>
|
||||
<th>Type</th>
|
||||
<th>Public</th>
|
||||
<th>Configuration</th>
|
||||
<th>
|
||||
<button class="btn btn-default btn-xs pull-right" data-toggle="modal"
|
||||
data-target="#eventModal">
|
||||
<span class="glyphicon glyphicon-plus"></span>
|
||||
</button>
|
||||
</th>
|
||||
</thead>
|
||||
{{#localEvents}}
|
||||
<tr>
|
||||
<td>{{.name}}</td>
|
||||
<td>{{.type}}</td>
|
||||
<td>{{.sync}}</td>
|
||||
<td>{{.config}}</td>
|
||||
<td>
|
||||
<form method="POST">
|
||||
<input type="hidden" name="action" value="remove_local_event">
|
||||
<input type="hidden" name="id" value="{{.getId()}}">
|
||||
|
||||
<div class="btn-toolbar pull-right">
|
||||
<button type="button" class="btn btn-default btn-xs" data-toggle="modal"
|
||||
data-target="#eventModal"
|
||||
data-id="{{.getId()}}"
|
||||
data-name="{{.name}}"
|
||||
data-type="{{.type}}"
|
||||
data-sync="{{.sync}}"
|
||||
data-config="{{.config}}">
|
||||
<span class="glyphicon glyphicon-pencil"></span>
|
||||
</button>
|
||||
|
||||
<button type="submit" class="btn btn-danger btn-xs">
|
||||
<span class="glyphicon glyphicon-trash"></span>
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
{{/localEvents}}
|
||||
</table>
|
||||
|
||||
<br>
|
||||
<p>Events that has been automatically detected.</p>
|
||||
<table class="table table-hover table-condensed">
|
||||
<thead>
|
||||
<th>Type</th>
|
||||
<th>Configuration</th>
|
||||
</thead>
|
||||
{{#detectedEvents}}
|
||||
<tr>
|
||||
<td>{{.getClass().getName()}}</td>
|
||||
<td>{{.}}</td>
|
||||
<td>
|
||||
<div class="btn-toolbar pull-right">
|
||||
<button type="button" class="btn btn-default btn-xs" data-toggle="modal"
|
||||
data-target="#eventModal"
|
||||
data-type="{{.type}}"
|
||||
data-config="{{.config}}">
|
||||
<span class="glyphicon glyphicon-plus"></span>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{{/detectedEvents}}
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<!------------- MODALS --------------->
|
||||
<script>
|
||||
var eventDataConf = {};
|
||||
|
||||
$(function(){
|
||||
// initialize event modal things
|
||||
$("#event-data-conf-template div").each(function(){
|
||||
eventDataConf[$(this).attr("id")] = $(this).html();
|
||||
});
|
||||
$("#eventModal select[name=type]").change(function(){
|
||||
// Update dynamic inputs
|
||||
$("#eventModal #event-data-conf").html(eventDataConf[$(this).val()]);
|
||||
});
|
||||
// click event
|
||||
$("#eventModal").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_local_event");
|
||||
modal.find("input[name=id]").val(button.data("id"));
|
||||
modal.find("input[name=name]").val(button.data("name"));
|
||||
modal.find("select[name=type]").val(button.data("type"));
|
||||
modal.find("input[name=config]").val(button.data("config"));
|
||||
}
|
||||
else{ // create
|
||||
modal.find("input[name=action]").val("create_local_event");
|
||||
modal.find("input[name=id]").val(-1);
|
||||
}
|
||||
// Update dynamic inputs
|
||||
modal.find("select[name=type]").change();
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
<div class="modal fade" id="eventModal" tabindex="-1">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
<h4 class="modal-title" id="exampleModalLabel">External User</h4>
|
||||
</div>
|
||||
<form method="POST">
|
||||
<div class="modal-body">
|
||||
<input type="hidden" id="action" name="action" value="">
|
||||
<input type="hidden" id="id" name="id">
|
||||
<div class="form-group">
|
||||
<label class="control-label">Name:</label>
|
||||
<input type="text" class="form-control" name="name">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="control-label">Type:</label>
|
||||
<select class="form-control" name="type">
|
||||
{{#availableEvents}}
|
||||
<option>{{.getName()}}</option>
|
||||
{{/availableEvents}}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="control-label">Public:</label>
|
||||
<input type="checkbox" class="form-control" name="sync" value="true">
|
||||
</div>
|
||||
|
||||
<hr>
|
||||
<div id="event-data-conf">
|
||||
<!-- Dynamic form -->
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="reset" class="btn btn-default" data-dismiss="modal">Cancel</button>
|
||||
<button type="submit" class="btn btn-primary">Save</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div id="event-data-conf-template" class="hidden">
|
||||
{{#localEventConf}}
|
||||
<div id="{{.clazz.getName()}}">
|
||||
{{#.params}}
|
||||
<div class="form-group">
|
||||
<label class="control-label">{{.getNiceName()}}:</label>
|
||||
<input type="text" class="form-control" name="{{.getName()}}">
|
||||
</div>
|
||||
{{/.params}}
|
||||
</div>
|
||||
{{/localEventConf}}
|
||||
</div>
|
||||
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
<h1 class="page-header">Configuration</h1>
|
||||
<h1 class="page-header">Sensor Configuration</h1>
|
||||
|
||||
<div class="col-md-12">
|
||||
<div class="panel panel-default drop-shadow">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue