From efebd4c5ec9befcabdcc97985d638c7d005d5793 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 11 Nov 2019 19:09:54 +0100 Subject: [PATCH] Small code cleanup --- src/se/hal/page/EventConfigHttpPage.java | 2 +- src/se/hal/page/EventOverviewHttpPage.java | 12 ++++++--- src/se/hal/page/HalAlertManager.java | 27 +++++++++++---------- src/se/hal/page/MapJsonPage.java | 1 + src/se/hal/page/PCOverviewHttpPage.java | 4 +-- src/se/hal/page/SensorConfigHttpPage.java | 12 +++++++-- src/se/hal/page/SensorJsonPage.java | 2 +- src/se/hal/page/SensorOverviewHttpPage.java | 8 +++--- src/se/hal/page/TriggerHttpPage.java | 12 ++++++--- src/se/hal/page/UserConfigHttpPage.java | 5 ++-- 10 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/se/hal/page/EventConfigHttpPage.java b/src/se/hal/page/EventConfigHttpPage.java index ff5bb779..4c449612 100755 --- a/src/se/hal/page/EventConfigHttpPage.java +++ b/src/se/hal/page/EventConfigHttpPage.java @@ -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); diff --git a/src/se/hal/page/EventOverviewHttpPage.java b/src/se/hal/page/EventOverviewHttpPage.java index 860691cc..ad8f1153 100755 --- a/src/se/hal/page/EventOverviewHttpPage.java +++ b/src/se/hal/page/EventOverviewHttpPage.java @@ -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 diff --git a/src/se/hal/page/HalAlertManager.java b/src/se/hal/page/HalAlertManager.java index 3152fe1e..189eda26 100755 --- a/src/se/hal/page/HalAlertManager.java +++ b/src/se/hal/page/HalAlertManager.java @@ -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 alertsClone = new ArrayList<>(alerts.size()); - for(Iterator it = alerts.iterator(); it.hasNext(); ){ + for(Iterator 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 cookie, Map 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 it = alerts.iterator(); it.hasNext(); ){ + for(Iterator 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); diff --git a/src/se/hal/page/MapJsonPage.java b/src/se/hal/page/MapJsonPage.java index ff785405..c3db8856 100755 --- a/src/se/hal/page/MapJsonPage.java +++ b/src/se/hal/page/MapJsonPage.java @@ -28,6 +28,7 @@ public class MapJsonPage extends HalJsonPage { Map 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"))) { diff --git a/src/se/hal/page/PCOverviewHttpPage.java b/src/se/hal/page/PCOverviewHttpPage.java index 86a2a094..255cd276 100755 --- a/src/se/hal/page/PCOverviewHttpPage.java +++ b/src/se/hal/page/PCOverviewHttpPage.java @@ -20,15 +20,13 @@ public class PCOverviewHttpPage extends HalHttpPage { } @Override - public Templator httpRespond( + public Templator httpRespond ( Map session, Map cookie, Map request) throws Exception { DBConnection db = HalContext.getDB(); - - List users = User.getUsers(db); List sensors = Sensor.getSensors(db); Templator tmpl = new Templator(FileUtil.find(TEMPLATE)); diff --git a/src/se/hal/page/SensorConfigHttpPage.java b/src/se/hal/page/SensorConfigHttpPage.java index 66732d7c..a29bc79f 100755 --- a/src/se/hal/page/SensorConfigHttpPage.java +++ b/src/se/hal/page/SensorConfigHttpPage.java @@ -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; diff --git a/src/se/hal/page/SensorJsonPage.java b/src/se/hal/page/SensorJsonPage.java index 8d7b0790..1b22aff1 100755 --- a/src/se/hal/page/SensorJsonPage.java +++ b/src/se/hal/page/SensorJsonPage.java @@ -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); } diff --git a/src/se/hal/page/SensorOverviewHttpPage.java b/src/se/hal/page/SensorOverviewHttpPage.java index acc2606a..0ee1b63c 100755 --- a/src/se/hal/page/SensorOverviewHttpPage.java +++ b/src/se/hal/page/SensorOverviewHttpPage.java @@ -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()); diff --git a/src/se/hal/page/TriggerHttpPage.java b/src/se/hal/page/TriggerHttpPage.java index 183231ca..37a81788 100755 --- a/src/se/hal/page/TriggerHttpPage.java +++ b/src/se/hal/page/TriggerHttpPage.java @@ -23,7 +23,7 @@ public class TriggerHttpPage extends HalHttpPage { private ArrayList 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 session, Map cookie, Map 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); diff --git a/src/se/hal/page/UserConfigHttpPage.java b/src/se/hal/page/UserConfigHttpPage.java index 283121a5..f0b23bcd 100755 --- a/src/se/hal/page/UserConfigHttpPage.java +++ b/src/se/hal/page/UserConfigHttpPage.java @@ -27,13 +27,13 @@ public class UserConfigHttpPage extends HalHttpPage { Map session, Map cookie, Map 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;