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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue