Small code cleanup

This commit is contained in:
Ziver Koc 2019-11-11 19:09:54 +01:00 committed by Ziver Koc
parent 5d21a0cc05
commit efebd4c5ec
10 changed files with 51 additions and 34 deletions

View file

@ -68,7 +68,7 @@ public class EventConfigHttpPage extends HalHttpPage {
case "modify_local_event":
event = Event.getEvent(db, id);
if (event != null){
if (event != null) {
event.setName(request.get("name"));
event.setType(request.get("type"));
event.setUser(localUser);

View file

@ -11,16 +11,20 @@ import se.hal.struct.devicedata.SwitchEventData;
import se.hal.util.DeviceNameComparator;
import se.hal.util.HistoryDataListSqlResult;
import se.hal.util.HistoryDataListSqlResult.HistoryData;
import zutil.ObjectUtil;
import zutil.db.DBConnection;
import zutil.io.file.FileUtil;
import zutil.log.LogUtil;
import zutil.parser.Templator;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class EventOverviewHttpPage extends HalHttpPage {
private static final Logger logger = LogUtil.getLogger();
private static final int HISTORY_LIMIT = 200;
private static final String OVERVIEW_TEMPLATE = "resource/web/event_overview.tmpl";
private static final String DETAIL_TEMPLATE = "resource/web/event_detail.tmpl";
@ -40,8 +44,8 @@ public class EventOverviewHttpPage extends HalHttpPage {
DBConnection db = HalContext.getDB();
if(request.containsKey("action")){
int id = (request.containsKey("action_id") ? Integer.parseInt(request.get("action_id")) : -1);
if (request.containsKey("action")) {
int id = (ObjectUtil.isEmpty(request.get("action_id")) ? -1 : Integer.parseInt(request.get("action_id")));
// change event data
SwitchEventData eventData = new SwitchEventData();
@ -55,10 +59,10 @@ public class EventOverviewHttpPage extends HalHttpPage {
ControllerManager.getInstance().send(event);
}
int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1);
int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));
// Save new input
if(!request.containsKey("action") && id >= 0){
if (!request.containsKey("action") && id >= 0) {
Event event = Event.getEvent(db, id);
// get history data

View file

@ -35,24 +35,25 @@ public class HalAlertManager implements HttpPage {
private HalAlertManager(){}
public String getUrl(){
return "/"+PAGE_NAME;
return "/" + PAGE_NAME;
}
public void addAlert(HalAlert alert){
public void addAlert(HalAlert alert) {
alerts.remove(alert); // We don't want to flood the user with duplicate alerts
alerts.add(alert);
}
public Templator generateAlerts(){
public Templator generateAlerts() {
try {
// clone alert list and update ttl of alerts
List<HalAlert> alertsClone = new ArrayList<>(alerts.size());
for(Iterator<HalAlert> it = alerts.iterator(); it.hasNext(); ){
for(Iterator<HalAlert> it = alerts.iterator(); it.hasNext(); ) {
HalAlert alert = it.next();
alertsClone.add(alert);
alert.ttl--;
if(alert.ttl <= 0) { // if alert is to old, remove it
if (alert.ttl <= 0) { // if alert is to old, remove it
logger.fine("Alert dismissed with end of life, alert id: "+ alert.id);
it.remove();
}
@ -62,7 +63,7 @@ public class HalAlertManager implements HttpPage {
tmpl.set("serviceUrl", getUrl());
tmpl.set("alerts", alertsClone);
return tmpl;
}catch (IOException e){
} catch (IOException e) {
logger.log(Level.SEVERE, null, e);
}
return null;
@ -75,14 +76,14 @@ public class HalAlertManager implements HttpPage {
Map<String, String> cookie,
Map<String, String> request) throws IOException {
if (request.containsKey("action")){
if (request.get("action").equals("dismiss")){
if (request.containsKey("action")) {
if (request.get("action").equals("dismiss")) {
// parse alert id
int id = Integer.parseInt(request.get("id"));
// Find alert
for(Iterator<HalAlert> it = alerts.iterator(); it.hasNext(); ){
for(Iterator<HalAlert> it = alerts.iterator(); it.hasNext(); ) {
HalAlert alert = it.next();
if(alert.getId() == id) {
if (alert.getId() == id) {
logger.fine("User dismissed alert id: "+ id);
it.remove();
break;
@ -102,7 +103,7 @@ public class HalAlertManager implements HttpPage {
}
public static class HalAlert{
public static class HalAlert {
private static int nextId = 0;
private int id;
@ -141,7 +142,7 @@ public class HalAlertManager implements HttpPage {
return msg;
}
public void setTTL(AlertTTL ttl){
public void setTTL(AlertTTL ttl) {
switch (ttl){
case ONE_VIEW: this.ttl = 1; break;
case DISMISSED: this.ttl = Integer.MAX_VALUE; break;
@ -152,7 +153,7 @@ public class HalAlertManager implements HttpPage {
}
@Override
public boolean equals(Object obj){
public boolean equals(Object obj) {
if (obj instanceof HalAlert)
return level == ((HalAlert) obj).level &&
title.equals(((HalAlert) obj).title);

View file

@ -28,6 +28,7 @@ public class MapJsonPage extends HalJsonPage {
Map<String, String> request) throws Exception {
DBConnection db = HalContext.getDB();
DataNode root = new DataNode(DataNode.DataType.Map);
if ("getdata".equals(request.get("action"))) {
getDeviceNode(db, root);
} else if ("save".equals(request.get("action"))) {

View file

@ -20,15 +20,13 @@ public class PCOverviewHttpPage extends HalHttpPage {
}
@Override
public Templator httpRespond(
public Templator httpRespond (
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request)
throws Exception {
DBConnection db = HalContext.getDB();
List<User> users = User.getUsers(db);
List<Sensor> sensors = Sensor.getSensors(db);
Templator tmpl = new Templator(FileUtil.find(TEMPLATE));

View file

@ -44,7 +44,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
// Save new input
if(request.containsKey("action")){
int id = (!ObjectUtil.isEmpty(request.get("id")) ? Integer.parseInt(request.get("id")) : -1);
int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));
Sensor sensor;
User user;
@ -59,9 +59,11 @@ public class SensorConfigHttpPage extends HalHttpPage {
sensor.getDeviceConfigurator().setValues(request).applyConfiguration();
sensor.save(db);
ControllerManager.getInstance().register(sensor);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully created new sensor: "+sensor.getName(), AlertTTL.ONE_VIEW));
break;
case "modify_local_sensor":
sensor = Sensor.getSensor(db, id);
if(sensor != null){
@ -70,6 +72,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
sensor.setSynced(Boolean.parseBoolean(request.get("sync")));
sensor.getDeviceConfigurator().setValues(request).applyConfiguration();
sensor.save(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully saved sensor: "+sensor.getName(), AlertTTL.ONE_VIEW));
} else {
@ -77,11 +80,13 @@ public class SensorConfigHttpPage extends HalHttpPage {
AlertLevel.ERROR, "Unknown sensor id: "+id, AlertTTL.ONE_VIEW));
}
break;
case "remove_local_sensor":
sensor = Sensor.getSensor(db, id);
if(sensor != null) {
ControllerManager.getInstance().deregister(sensor);
sensor.delete(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully deleted sensor: "+sensor.getName(), AlertTTL.ONE_VIEW));
} else {
@ -101,6 +106,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
user.setPort(Integer.parseInt(request.get("port")));
user.setExternal(true);
user.save(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully created new external user with host: "+user.getHostname(), AlertTTL.ONE_VIEW));
break;
@ -110,6 +116,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
user.setHostname(request.get("hostname"));
user.setPort(Integer.parseInt(request.get("port")));
user.save(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully saved external user with host: "+user.getHostname(), AlertTTL.ONE_VIEW));
} else {
@ -121,6 +128,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
user = User.getUser(db, id);
if (user != null) {
user.delete(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully deleted user with host: "+user.getHostname(), AlertTTL.ONE_VIEW));
} else {
@ -135,6 +143,7 @@ public class SensorConfigHttpPage extends HalHttpPage {
if(sensor != null){
sensor.setSynced(Boolean.parseBoolean(request.get("sync")));
sensor.save(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully saved external sensor: "+sensor.getName(), AlertTTL.ONE_VIEW));
} else {
@ -153,7 +162,6 @@ public class SensorConfigHttpPage extends HalHttpPage {
tmpl.set("detectedSensors", ControllerManager.getInstance().getDetectedSensors());
tmpl.set("extUsers", User.getExternalUsers(db));
tmpl.set("extSensor", Sensor.getExternalSensors(db));
tmpl.set("availableSensors", ControllerManager.getInstance().getAvailableSensors());
return tmpl;

View file

@ -112,7 +112,7 @@ public class SensorJsonPage extends HalJsonPage {
DataNode timestampNode = new DataNode(DataNode.DataType.List);
DataNode dataNode = new DataNode(DataNode.DataType.List);
// end timestamp
if (endTime != UTCTimeUtility.INFINITY){
if (endTime != UTCTimeUtility.INFINITY) {
timestampNode.add(System.currentTimeMillis() - endTime);
dataNode.add((String)null);
}

View file

@ -7,6 +7,7 @@ import se.hal.struct.Sensor;
import se.hal.util.DeviceNameComparator;
import se.hal.util.HistoryDataListSqlResult;
import se.hal.util.HistoryDataListSqlResult.HistoryData;
import zutil.ObjectUtil;
import zutil.db.DBConnection;
import zutil.io.file.FileUtil;
import zutil.parser.Templator;
@ -35,10 +36,10 @@ public class SensorOverviewHttpPage extends HalHttpPage {
throws Exception{
DBConnection db = HalContext.getDB();
int id = (request.containsKey("id") ? Integer.parseInt(request.get("id")) : -1);
int id = (ObjectUtil.isEmpty(request.get("id")) ? -1 : Integer.parseInt(request.get("id")));
// Save new input
if(id >= 0){
if (id >= 0) {
Sensor sensor = Sensor.getSensor(db, id);
// get history data
@ -52,8 +53,7 @@ public class SensorOverviewHttpPage extends HalHttpPage {
tmpl.set("sensor", sensor);
tmpl.set("history", history);
return tmpl;
}
else {
} else {
Sensor[] sensors = Sensor.getLocalSensors(db).toArray(new Sensor[0]);
Arrays.sort(sensors, DeviceNameComparator.getInstance());

View file

@ -23,7 +23,7 @@ public class TriggerHttpPage extends HalHttpPage {
private ArrayList<ClassConfigurationData> actionConfigurators;
public TriggerHttpPage(){
public TriggerHttpPage() {
super("trigger");
super.getRootNav().createSubNav("Events").createSubNav(this.getId(), "Triggers");
@ -37,14 +37,14 @@ public class TriggerHttpPage extends HalHttpPage {
@Override
public Templator httpRespond(
public Templator httpRespond (
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request)
throws Exception{
throws Exception {
DBConnection db = HalContext.getDB();
if(request.containsKey("action")){
if (request.containsKey("action")) {
TriggerFlow flow = null;
if (request.containsKey("flow-id") && !request.get("flow-id").isEmpty())
flow = TriggerFlow.getTriggerFlow(db, Integer.parseInt(request.get("flow-id")));
@ -62,11 +62,13 @@ public class TriggerHttpPage extends HalHttpPage {
flow = new TriggerFlow();
flow.save(db);
break;
case "modify_flow":
flow.setEnabled("on".equals(request.get("enabled")));
flow.setName(request.get("name"));
flow.save(db);
break;
case "remove_flow":
flow.delete(db);
break;
@ -87,6 +89,7 @@ public class TriggerHttpPage extends HalHttpPage {
trigger.getObjectConfigurator().setValues(request).applyConfiguration();
trigger.save(db); // will save all sub beans also
break;
case "remove_trigger":
if (flow == null)
flow = TriggerFlow.getTriggerFlow(db, trigger);
@ -110,6 +113,7 @@ public class TriggerHttpPage extends HalHttpPage {
action.getObjectConfigurator().setValues(request).applyConfiguration();
action.save(db); // will save all sub beans also
break;
case "remove_action":
if (flow == null)
flow = TriggerFlow.getTriggerFlow(db, action);

View file

@ -27,13 +27,13 @@ public class UserConfigHttpPage extends HalHttpPage {
Map<String, Object> session,
Map<String, String> cookie,
Map<String, String> request)
throws Exception{
throws Exception {
DBConnection db = HalContext.getDB();
User localUser = User.getLocalUser(db);
// Save new input
if(request.containsKey("action")){
if (request.containsKey("action")) {
User user;
switch(request.get("action")) {
// Local User
@ -46,6 +46,7 @@ public class UserConfigHttpPage extends HalHttpPage {
localUser.setEmail(request.get("email"));
localUser.setAddress(request.get("address"));
localUser.save(db);
HalAlertManager.getInstance().addAlert(new HalAlert(
AlertLevel.SUCCESS, "Successfully saved profile changes", AlertTTL.ONE_VIEW));
break;