Implementation of gui alerts (not dissmissable yet). issue 9

Former-commit-id: 3184c884854a7db76467ab6e7310ce7dea951c39
This commit is contained in:
Ziver Koc 2016-01-20 17:05:18 +01:00
parent efa9e0b264
commit 86bf95d417
5 changed files with 146 additions and 2 deletions

View file

@ -8,6 +8,7 @@ import se.hal.deamon.PCDataSynchronizationDaemon;
import se.hal.intf.HalDaemon; import se.hal.intf.HalDaemon;
import se.hal.intf.HalHttpPage; import se.hal.intf.HalHttpPage;
import se.hal.page.*; import se.hal.page.*;
import se.hal.page.HalAlertManager.*;
import se.hal.struct.Event; import se.hal.struct.Event;
import se.hal.struct.Sensor; import se.hal.struct.Sensor;
import zutil.db.DBConnection; import zutil.db.DBConnection;
@ -39,13 +40,16 @@ public class HalServer {
LogUtil.setFormatter("zutil", formatter); LogUtil.setFormatter("zutil", formatter);
LogUtil.setGlobalFormatter(formatter); LogUtil.setGlobalFormatter(formatter);
// init Managers
HalContext.initialize();
ControllerManager.initialize();
HalAlertManager.initialize();
// init DB and other configurations // init DB and other configurations
HalContext.initialize();
DBConnection db = HalContext.getDB(); DBConnection db = HalContext.getDB();
// Init sensors,events and controllers // Init sensors,events and controllers
ControllerManager.initialize();
for(Sensor sensor : Sensor.getLocalSensors(db)){ for(Sensor sensor : Sensor.getLocalSensors(db)){
ControllerManager.getInstance().register(sensor); ControllerManager.getInstance().register(sensor);
} }
@ -69,6 +73,9 @@ public class HalServer {
// init http server // init http server
HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.ERROR, "error test"));
HalAlertManager.getInstance().addAlert(new HalAlert(AlertLevel.INFO, "info test"));
HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors")); HalHttpPage.getRootNav().addSubNav(new HalNavigation("sensors", "Sensors"));
HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events")); HalHttpPage.getRootNav().addSubNav(new HalNavigation("events", "Events"));
pages = new HalHttpPage[]{ pages = new HalHttpPage[]{

View file

@ -1,6 +1,7 @@
package se.hal.intf; package se.hal.intf;
import se.hal.HalContext; import se.hal.HalContext;
import se.hal.page.HalAlertManager;
import se.hal.page.HalNavigation; import se.hal.page.HalNavigation;
import se.hal.struct.User; import se.hal.struct.User;
import zutil.db.DBConnection; import zutil.db.DBConnection;
@ -53,6 +54,7 @@ public abstract class HalHttpPage implements HttpPage{
tmpl.set("nav", nav.getNavBreadcrumb().get(1)); tmpl.set("nav", nav.getNavBreadcrumb().get(1));
tmpl.set("rootNav", rootNav); tmpl.set("rootNav", rootNav);
tmpl.set("userNav", userNav); tmpl.set("userNav", userNav);
tmpl.set("alerts", HalAlertManager.getInstance().generateAlerts());
tmpl.set("content", httpRespond(session, cookie, request)); tmpl.set("content", httpRespond(session, cookie, request));
out.print(tmpl.compile()); out.print(tmpl.compile());

View file

@ -0,0 +1,104 @@
package se.hal.page;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.net.http.HttpHeaderParser;
import zutil.net.http.HttpPage;
import zutil.net.http.HttpPrintStream;
import zutil.parser.Templator;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Created by ezivkoc on 2016-01-20.
*/
public class HalAlertManager implements HttpPage {
private static final Logger logger = LogUtil.getLogger();
private static final String TMPL_PATH = "web-resource/alerts.tmpl";
private static final String PAGE_NAME = "alert";
private static HalAlertManager instance;
public enum AlertLevel{
ERROR,
WARNING,
SUCCESS,
INFO
}
private List<HalAlert> alerts = new LinkedList<>();
private HalAlertManager(){}
public void addAlert(HalAlert alert){
alerts.add(alert);
}
public Templator generateAlerts(){
try {
Templator tmpl = new Templator(FileUtil.find(TMPL_PATH));
tmpl.set("alerts", alerts);
return tmpl;
}catch (IOException e){
logger.log(Level.SEVERE, null, e);
}
return null;
}
@Override
public void respond(HttpPrintStream out,
HttpHeaderParser client_info,
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request) throws IOException {
}
public static void initialize(){
instance = new HalAlertManager();
}
public static HalAlertManager getInstance(){
return instance;
}
public static class HalAlert{
private static int nextId = 0;
private int id;
private AlertLevel level;
private String msg;
public HalAlert(AlertLevel level, String msg) {
this.id = nextId++;
this.level = level;
this.msg = msg;
}
public int getId() {
return id;
}
public AlertLevel getLevel() {
return level;
}
public boolean isError(){ return level == AlertLevel.ERROR; }
public boolean isWarning(){ return level == AlertLevel.WARNING; }
public boolean isSuccess(){ return level == AlertLevel.SUCCESS; }
public boolean isInfo(){ return level == AlertLevel.INFO; }
public String getMessage() {
return msg;
}
}
}

30
web-resource/alerts.tmpl Executable file
View file

@ -0,0 +1,30 @@
{{#alerts}}
{{#.isError()}}
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<span class="glyphicon glyphicon-minus-sign" aria-hidden="true"></span>
<strong>Error:</strong> {{.getMessage()}}
</div>
{{/.isError()}}
{{#.isWarning()}}
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<span class="glyphicon glyphicon-warning-sign" aria-hidden="true"></span>
<strong>Warning:</strong> {{.getMessage()}}
</div>
{{/.isWarning()}}
{{#.isSuccess()}}
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<span class="glyphicon glyphicon-ok-circle" aria-hidden="true"></span>
<strong>Success:</strong> {{.getMessage()}}
</div>
{{/.isSuccess()}}
{{#.isInfo()}}
<div class="alert alert-info alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<span class="glyphicon glyphicon-info-sign" aria-hidden="true"></span>
<strong>Info:</strong> {{.getMessage()}}
</div>
{{/.isInfo()}}
{{/alerts}}

View file

@ -79,6 +79,7 @@
</div> </div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main"> <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
{{alerts}}
{{content}} {{content}}
</div> </div>