From fa16d1206b03db7965475fbf4b061b9658c933af Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Sat, 18 Jan 2020 08:42:08 +0100 Subject: [PATCH] waste zone/type/city info on device page --- .../db/{JDCB.java => JDBC.java} | 14 +-- .../db/PostgreSQLConnector.java | 37 ------ src/java/com/wasteinformationserver/main.java | 6 +- .../mqtt/MqttService.java | 6 +- .../website/datarequests/DataRequest.java | 26 ++-- .../website/datarequests/DeviceRequest.java | 118 +++++++++++------- .../website/datarequests/NewDateRequest.java | 16 +-- .../website/datarequests/RegisterRequest.java | 6 +- .../datarequests/login/LoginRequest.java | 10 +- src/resources/wwwroot/dashboard.html | 2 +- src/resources/wwwroot/device.html | 4 +- src/resources/wwwroot/js/device.js | 98 +++++++++++---- 12 files changed, 191 insertions(+), 152 deletions(-) rename src/java/com/wasteinformationserver/db/{JDCB.java => JDBC.java} (86%) delete mode 100755 src/java/com/wasteinformationserver/db/PostgreSQLConnector.java diff --git a/src/java/com/wasteinformationserver/db/JDCB.java b/src/java/com/wasteinformationserver/db/JDBC.java similarity index 86% rename from src/java/com/wasteinformationserver/db/JDCB.java rename to src/java/com/wasteinformationserver/db/JDBC.java index 2af0c98..4a5f700 100644 --- a/src/java/com/wasteinformationserver/db/JDCB.java +++ b/src/java/com/wasteinformationserver/db/JDBC.java @@ -5,10 +5,10 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -public class JDCB { +public class JDBC { static Connection conn; - static JDCB jdcb; + static JDBC JDBC; static boolean loggedin = false; static String usernamec; @@ -21,19 +21,19 @@ public class JDCB { usernamec = username; passwordc = password; dbnamec = dbname; - jdcb = new JDCB(username, password, dbname, ip, port); + JDBC = new JDBC(username, password, dbname, ip, port); } - private JDCB(String username, String password, String dbname, String ip, int port) throws IOException { + private JDBC(String username, String password, String dbname, String ip, int port) throws IOException { logintodb(username, password, dbname, ip, port); } - public static JDCB getInstance() throws IOException { + public static JDBC getInstance() throws IOException { if (loggedin) { - return jdcb; + return JDBC; } else { logintodb(usernamec, passwordc, dbnamec, ipc, portc); - return jdcb; + return JDBC; } } diff --git a/src/java/com/wasteinformationserver/db/PostgreSQLConnector.java b/src/java/com/wasteinformationserver/db/PostgreSQLConnector.java deleted file mode 100755 index 6dbe33d..0000000 --- a/src/java/com/wasteinformationserver/db/PostgreSQLConnector.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.wasteinformationserver.db; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.SQLException; - -public class PostgreSQLConnector extends Database { - - static { - try { - Class.forName("org.postgresql.Driver").newInstance(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - public PostgreSQLConnector(String user, String password, String host, int port, String dbName) { - super(user, password, host, port, dbName); - } - - @Override - public Connection getConnection() { - Connection con = null; - - try { - con = DriverManager.getConnection( - "jdbc:postgresql://" + host + ":" + port + "/" + dbName, - user, - password); - - } catch (SQLException e) { - e.printStackTrace(); - } - - return con; - } -} diff --git a/src/java/com/wasteinformationserver/main.java b/src/java/com/wasteinformationserver/main.java index ffcf1ca..0db24cf 100644 --- a/src/java/com/wasteinformationserver/main.java +++ b/src/java/com/wasteinformationserver/main.java @@ -2,7 +2,7 @@ package com.wasteinformationserver; import com.wasteinformationserver.basicutils.Info; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import com.wasteinformationserver.mqtt.MqttService; import com.wasteinformationserver.website.Webserver; @@ -32,8 +32,8 @@ public class main { //initial connect to db Log.message("initial login to db"); try { - JDCB.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306); - //JDCB.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306); + //JDBC.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306); + JDBC.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306); } catch (IOException e) { //e.printStackTrace(); Log.error("no connection to db"); diff --git a/src/java/com/wasteinformationserver/mqtt/MqttService.java b/src/java/com/wasteinformationserver/mqtt/MqttService.java index 9720dca..97213ba 100644 --- a/src/java/com/wasteinformationserver/mqtt/MqttService.java +++ b/src/java/com/wasteinformationserver/mqtt/MqttService.java @@ -1,7 +1,7 @@ package com.wasteinformationserver.mqtt; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import org.eclipse.paho.client.mqttv3.*; import java.io.IOException; @@ -14,12 +14,12 @@ import java.util.Date; public class MqttService { private MqttClient client = null; private String serveruri; - JDCB db; + JDBC db; public MqttService(String serverurl, String port) { serveruri = "tcp://" + serverurl + ":" + port; try { - db = JDCB.getInstance(); + db = JDBC.getInstance(); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java b/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java index a35ed2e..a04f37d 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/DataRequest.java @@ -2,7 +2,7 @@ package com.wasteinformationserver.website.datarequests; import com.wasteinformationserver.basicutils.Info; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import com.wasteinformationserver.website.basicrequest.PostRequest; import java.io.IOException; @@ -20,9 +20,9 @@ public class DataRequest extends PostRequest { ResultSet set = null; int status = -1; - JDCB jdcb; + JDBC jdbc; try { - jdcb = JDCB.getInstance(); + jdbc = JDBC.getInstance(); } catch (IOException e) { Log.error("no connection to db"); return "{\"query\" : \"nodbconn\"}"; @@ -35,7 +35,7 @@ public class DataRequest extends PostRequest { // check if wastezone and wasteregion already exists Log.debug(params.get("cityname") + params.get("wastetype") + params.get("wastezone")); - set = jdcb.executeQuery("select * from `cities` where `name`='" + params.get("cityname") + "' AND `wastetype`='" + params.get("wastetype") + "' AND `zone`='" + params.get("wastezone") + "'"); + set = jdbc.executeQuery("select * from `cities` where `name`='" + params.get("cityname") + "' AND `wastetype`='" + params.get("wastetype") + "' AND `zone`='" + params.get("wastezone") + "'"); int size = 0; try { if (set != null) { @@ -48,7 +48,7 @@ public class DataRequest extends PostRequest { if (size == 0) { //doesnt exist try { - status = jdcb.executeUpdate("INSERT INTO `cities`(`userid`, `name`, `wastetype`, `zone`) VALUES ('0','" + params.get("cityname") + "','" + params.get("wastetype") + "','" + params.get("wastezone") + "');"); + status = jdbc.executeUpdate("INSERT INTO `cities`(`userid`, `name`, `wastetype`, `zone`) VALUES ('0','" + params.get("cityname") + "','" + params.get("wastetype") + "','" + params.get("wastezone") + "');"); } catch (SQLException e) { e.printStackTrace(); } @@ -71,7 +71,7 @@ public class DataRequest extends PostRequest { sb.append("}"); break; case "getAllCities": - set = jdcb.executeQuery("select * from cities"); + set = jdbc.executeQuery("select * from cities"); Log.debug(set.toString()); sb.append("{\"data\":["); try { @@ -95,7 +95,7 @@ public class DataRequest extends PostRequest { //DELETE FROM `cities` WHERE `id`=0 sb.append("{"); try { - status = jdcb.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id") + "'"); + status = jdbc.executeUpdate("DELETE FROM `cities` WHERE `id`='" + params.get("id") + "'"); if (status == 1) { //success sb.append("\"status\" : \"success\""); @@ -117,7 +117,7 @@ public class DataRequest extends PostRequest { break; case "getAllDates": - set = jdcb.executeQuery("SELECT pickupdates.id,pickupdates.pickupdate,cities.userid,cities.name,cities.wastetype,cities.zone " + + set = jdbc.executeQuery("SELECT pickupdates.id,pickupdates.pickupdate,cities.userid,cities.name,cities.wastetype,cities.zone " + "FROM `pickupdates` INNER JOIN `cities` ON pickupdates.citywastezoneid = cities.id"); sb.append("{\"data\":["); try { @@ -141,7 +141,7 @@ public class DataRequest extends PostRequest { case "deletedate": sb.append("{"); try { - status = jdcb.executeUpdate("DELETE FROM `pickupdates` WHERE `id`='" + params.get("id") + "'"); + status = jdbc.executeUpdate("DELETE FROM `pickupdates` WHERE `id`='" + params.get("id") + "'"); if (status == 1) { //success sb.append("\"status\" : \"success\""); @@ -176,7 +176,7 @@ public class DataRequest extends PostRequest { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); String time = sdf.format(date); - set = jdcb.executeQuery("SELECT * FROM `pickupdates` WHERE `pickupdate` BETWEEN '0000-12-27' AND '"+time+"'"); + set = jdbc.executeQuery("SELECT * FROM `pickupdates` WHERE `pickupdate` BETWEEN '0000-12-27' AND '"+time+"'"); set.last(); sb.append("\"finshedcollections\":\"" + set.getRow() + "\""); @@ -185,15 +185,15 @@ public class DataRequest extends PostRequest { date = new Date(date.getTime()+1 * 24 * 60 * 60 * 1000); time = sdf.format(date); - set = jdcb.executeQuery("SELECT * FROM `pickupdates` WHERE `pickupdate` BETWEEN '"+time+"' AND '2222-12-27'"); + set = jdbc.executeQuery("SELECT * FROM `pickupdates` WHERE `pickupdate` BETWEEN '"+time+"' AND '2222-12-27'"); set.last(); sb.append(",\"futurecollections\":\"" + set.getRow() + "\""); - set = jdcb.executeQuery("select * from pickupdates"); + set = jdbc.executeQuery("select * from pickupdates"); set.last(); sb.append(",\"collectionnumber\":\"" + set.getRow() + "\""); - set = jdcb.executeQuery("select * from `cities`"); + set = jdbc.executeQuery("select * from `cities`"); set.last(); sb.append(",\"citynumber\":\"" + set.getRow() + "\""); } catch (SQLException e) { diff --git a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java index 6b87a5b..de6e071 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java @@ -1,7 +1,7 @@ package com.wasteinformationserver.website.datarequests; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import com.wasteinformationserver.website.basicrequest.PostRequest; import java.io.IOException; @@ -13,9 +13,9 @@ public class DeviceRequest extends PostRequest { @Override public String request(HashMap params) { - JDCB jdcb = null; + JDBC jdbc = null; try { - jdcb = JDCB.getInstance(); + jdbc = jdbc.getInstance(); } catch (IOException e) { e.printStackTrace(); } @@ -23,48 +23,68 @@ public class DeviceRequest extends PostRequest { StringBuilder sb = new StringBuilder(); switch (params.get("action")) { case "getdevices": - ResultSet set = jdcb.executeQuery("SELECT * from devices"); + ResultSet setunconfigured = jdbc.executeQuery("SELECT * FROM `devices` WHERE `CityID`=-1"); + ResultSet setconfigured = jdbc.executeQuery("SELECT * FROM `devices` INNER JOIN `cities` ON devices.CityID = cities.id"); + sb.append("{\"data\":["); try { - while (set.next()) { - int deviceid = set.getInt("DeviceID"); - int cityid = set.getInt("CityID"); - if (cityid == -1) { - //not configured - sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\"}"); - } else { - String devicename = set.getString("DeviceName"); - String devicelocation = set.getString("DeviceLocation"); + setconfigured.last(); + int configsize = setconfigured.getRow(); + System.out.println(configsize); + setconfigured.first(); + setconfigured.previous(); - sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\",\"devicename\":\"" + devicename + "\",\"devicelocation\":\"" + devicelocation + "\"}"); - } + while (setunconfigured.next()) { + int deviceid = setunconfigured.getInt("DeviceID"); + int cityid = setunconfigured.getInt("CityID"); - if (!set.isLast()) { + sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\"}"); + + if (!(setunconfigured.isLast() && configsize==0)) { sb.append(","); } } + + while (setconfigured.next()) { + int deviceid = setconfigured.getInt("DeviceID"); + int cityid = setconfigured.getInt("CityID"); + + String devicename = setconfigured.getString("DeviceName"); + String devicelocation = setconfigured.getString("DeviceLocation"); + + String cityname = setconfigured.getString("name"); + String wastetype = setconfigured.getString("wastetype"); + String zone = setconfigured.getString("zone"); + + sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\",\"devicename\":\"" + devicename + "\",\"devicelocation\":\"" + devicelocation + "\",\"cityname\":\"" + cityname + "\",\"wastetype\":\"" + wastetype + "\",\"zone\":\"" + zone + "\"}"); + + if (!setconfigured.isLast()) { + sb.append(","); + } + } sb.append("]}"); + System.out.println(sb.toString()); } catch (SQLException e) { e.printStackTrace(); } break; case "getCitynames": - set = jdcb.executeQuery("select * from cities"); - Log.debug(set.toString()); + setunconfigured = jdbc.executeQuery("select * from cities"); + Log.debug(setunconfigured.toString()); sb.append("{"); try { String prev = ""; - while (set.next()) { - if (prev.equals(set.getString("name"))) { + while (setunconfigured.next()) { + if (prev.equals(setunconfigured.getString("name"))) { } else { - if (!set.isFirst()) { + if (!setunconfigured.isFirst()) { sb.append(","); } - sb.append("\"" + set.getString("name") + "\":\"" + set.getString("name") + "\""); + sb.append("\"" + setunconfigured.getString("name") + "\":\"" + setunconfigured.getString("name") + "\""); } - prev = set.getString("name"); + prev = setunconfigured.getString("name"); } } catch (SQLException e) { e.printStackTrace(); @@ -73,21 +93,21 @@ public class DeviceRequest extends PostRequest { Log.debug(sb.toString()); break; case "getzones": - set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC"); - Log.debug(set.toString()); + setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC"); + Log.debug(setunconfigured.toString()); sb.append("{"); try { int prev = 42; - while (set.next()) { - if (prev == set.getInt("zone")) { + while (setunconfigured.next()) { + if (prev == setunconfigured.getInt("zone")) { } else { - sb.append("\"" + set.getInt("zone") + "\":\"" + set.getInt("zone") + "\""); - if (!set.isLast()) { + sb.append("\"" + setunconfigured.getInt("zone") + "\":\"" + setunconfigured.getInt("zone") + "\""); + if (!setunconfigured.isLast()) { sb.append(","); } } - prev = set.getInt("zone"); + prev = setunconfigured.getInt("zone"); } } catch (SQLException e) { e.printStackTrace(); @@ -95,21 +115,21 @@ public class DeviceRequest extends PostRequest { sb.append("}"); break; case "gettypes": - set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC"); - Log.debug(set.toString()); + setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC"); + Log.debug(setunconfigured.toString()); sb.append("{"); try { String prev = "42"; - while (set.next()) { - if (prev == set.getString("wastetype")) { + while (setunconfigured.next()) { + if (prev == setunconfigured.getString("wastetype")) { } else { - sb.append("\"" + set.getString("wastetype") + "\":\"" + set.getString("wastetype") + "\""); - if (!set.isLast()) { + sb.append("\"" + setunconfigured.getString("wastetype") + "\":\"" + setunconfigured.getString("wastetype") + "\""); + if (!setunconfigured.isLast()) { sb.append(","); } } - prev = set.getString("wastetype"); + prev = setunconfigured.getString("wastetype"); } } catch (SQLException e) { e.printStackTrace(); @@ -117,21 +137,27 @@ public class DeviceRequest extends PostRequest { sb.append("}"); break; case "savetodb": - set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'"); + setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'"); try { - set.last(); - if (set.getRow() != 1) { - //error + setunconfigured.last(); + if (setunconfigured.getRow() != 1) { + // TODO: 17.01.20 error handling } else { - int id = set.getInt("id"); - jdcb.executeUpdate("UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'"); + int id = setunconfigured.getInt("id"); + jdbc.executeUpdate("UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'"); + sb.append("{\"success\":\"true\"}"); } } catch (SQLException e) { e.printStackTrace(); } - - - //'action=savetodb&cityname=' + cityname + '&zonename=' + zone+'&wastetype='+wastetype+'&devicename='+devicename+'&devicelocation='+devicelocation + break; + case "deleteDevice": + try { + int res = jdbc.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params.get("id") + "'"); + } catch (SQLException e) { + e.printStackTrace(); + } + sb.append("{\"status\":\"success\"}"); break; } return sb.toString(); diff --git a/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java b/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java index 0babe42..ed39f71 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/NewDateRequest.java @@ -1,7 +1,7 @@ package com.wasteinformationserver.website.datarequests; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import com.wasteinformationserver.website.basicrequest.PostRequest; import java.io.IOException; @@ -13,17 +13,17 @@ public class NewDateRequest extends PostRequest { @Override public String request(HashMap params) { StringBuilder sb = new StringBuilder(); - JDCB jdcb; + JDBC jdbc; ResultSet set; try { - jdcb = JDCB.getInstance(); + jdbc = JDBC.getInstance(); } catch (IOException e) { Log.error("no connection to db"); return "{\"query\" : \"nodbconn\"}"; } switch (params.get("action")) { case "getCitynames": - set = jdcb.executeQuery("select * from cities"); + set = jdbc.executeQuery("select * from cities"); Log.debug(set.toString()); sb.append("{\"data\":["); try { @@ -48,7 +48,7 @@ public class NewDateRequest extends PostRequest { Log.debug(sb.toString()); break; case "getzones": - set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC"); + set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC"); Log.debug(set.toString()); sb.append("{\"data\":["); try { @@ -72,7 +72,7 @@ public class NewDateRequest extends PostRequest { sb.append("}"); break; case "gettypes": - set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='"+params.get("zonename")+"' ORDER BY zone ASC"); + set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='"+params.get("zonename")+"' ORDER BY zone ASC"); Log.debug(set.toString()); sb.append("{\"data\":["); try { @@ -98,13 +98,13 @@ public class NewDateRequest extends PostRequest { case "newdate": sb.append("{"); Log.debug(params); - set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zone") + "' AND `wastetype`='" + params.get("wastetype") + "'"); + set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zone") + "' AND `wastetype`='" + params.get("wastetype") + "'"); try { set.last(); if (set.getRow() == 1) { Log.debug(set.getInt("id")); - int status = jdcb.executeUpdate("INSERT INTO `pickupdates`(`citywastezoneid`, `pickupdate`) VALUES ('" + set.getInt("id") + "','" + params.get("date") + "')"); + int status = jdbc.executeUpdate("INSERT INTO `pickupdates`(`citywastezoneid`, `pickupdate`) VALUES ('" + set.getInt("id") + "','" + params.get("date") + "')"); if (status == 1) { sb.append("\"status\" : \"success\""); } else { diff --git a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java index 66985f5..6e203af 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java @@ -1,7 +1,7 @@ package com.wasteinformationserver.website.datarequests; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import com.wasteinformationserver.website.HttpTools; import com.wasteinformationserver.website.basicrequest.PostRequest; @@ -16,9 +16,9 @@ public class RegisterRequest extends PostRequest { String passhash = HttpTools.StringToMD5(params.get("password")); - JDCB myjd = null; + JDBC myjd = null; try { - myjd = JDCB.getInstance(); + myjd = JDBC.getInstance(); } catch (IOException e) { e.printStackTrace(); } diff --git a/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java b/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java index 1739434..3dc8e02 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/login/LoginRequest.java @@ -1,7 +1,7 @@ package com.wasteinformationserver.website.datarequests.login; import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDCB; +import com.wasteinformationserver.db.JDBC; import com.wasteinformationserver.website.HttpTools; import com.wasteinformationserver.website.basicrequest.PostRequest; @@ -19,16 +19,16 @@ public class LoginRequest extends PostRequest { String password = params.get("password"); String username = params.get("username"); - JDCB jdcb; + JDBC jdbc; try { - jdcb = JDCB.getInstance(); + jdbc = JDBC.getInstance(); } catch (IOException e) { Log.error("no connection to db"); return "{\"status\" : \"nodbconn\"}"; } - ResultSet s = jdcb.executeQuery("select * from user where username ='" + username + "'"); - ; + ResultSet s = jdbc.executeQuery("select * from user where username ='" + username + "'"); + //new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation").executeQuery("select * from user where username ='" + username + "'"); Log.debug("successfully logged in to db"); String response = "{\"accept\": false}"; diff --git a/src/resources/wwwroot/dashboard.html b/src/resources/wwwroot/dashboard.html index 54d50ef..d2d8e1d 100644 --- a/src/resources/wwwroot/dashboard.html +++ b/src/resources/wwwroot/dashboard.html @@ -210,7 +210,7 @@

- Alle abholdaten + All pickupdats

diff --git a/src/resources/wwwroot/device.html b/src/resources/wwwroot/device.html index b06d960..1414bc0 100644 --- a/src/resources/wwwroot/device.html +++ b/src/resources/wwwroot/device.html @@ -141,7 +141,7 @@
-

150

+

TODO

Todo

@@ -156,7 +156,7 @@
-

42

+

TODO

Devices

diff --git a/src/resources/wwwroot/js/device.js b/src/resources/wwwroot/js/device.js index 8757f08..152b943 100644 --- a/src/resources/wwwroot/js/device.js +++ b/src/resources/wwwroot/js/device.js @@ -1,25 +1,79 @@ $(document).ready(function () { + var devicetable = null; + reloadDevices(); - $.post('/senddata/Devicedata', 'action=getdevices', function (data) { - - console.log(data); - for (var i = 0; i < data.data.length; i++) { - var id = data.data[i].deviceid; - var cityid = data.data[i].cityid; - - if (cityid == -1) { - $("#devices-tablebody").append("" + id + "new Device"); - } else { - var devicename = data.data[i].devicename; - var devicelocation = data.data[i].devicelocation; - - $("#devices-tablebody").append("" + id + "" + devicename + "" + devicelocation + "" + cityid + "DEL"); + function reloadDevices() { + $.post('/senddata/Devicedata', 'action=getdevices', function (data) { + if (devicetable != null) { + devicetable.destroy(); } - console.log(); - //devices-tablebody + console.log(data); - } + $('#devices-tablebody').html(""); + $(".delbtn").off(); + + for (var i = 0; i < data.data.length; i++) { + var id = data.data[i].deviceid; + var cityid = data.data[i].cityid; + + + if (cityid == -1) { + $("#devices-tablebody").append("" + id + "new Device"); + } else { + var devicename = data.data[i].devicename; + var devicelocation = data.data[i].devicelocation; + + var cityname = data.data[i].cityname; + var cityzone = data.data[i].zone; + var wastetype = data.data[i].wastetype; + + //todo load right names from db + $("#devices-tablebody").append("" + id + "" + devicename + "" + devicelocation + "" + cityname + "/" + wastetype + "/" + cityzone + ""); + + } + } + + addDeleteButton(); + addConfigDialog(); + devicetable = $('#table-devices').DataTable(); + }, 'json'); + } + + function addDeleteButton() { + $(".delbtn").click(function (event) { + var id = event.target.getAttribute("dataid"); + console.log("clicked btn data " + id); + $.post('/senddata/Devicedata', 'action=deleteDevice&id=' + id, function (data) { + console.log(data); + if (data.status == "success") { + Swal.fire({ + type: "success", + title: 'Successfully deleted city!', + html: 'This alert closes automatically.', + timer: 1000, + }).then((result) => { + console.log('Popup closed. ') + + }); + reloadDevices(); + } else if (data.status == "dependenciesnotdeleted") { + Swal.fire({ + type: "warning", + title: 'This city is a dependency of a date', + html: 'Do you want do delete it anyway with all dependencies?', + }).then((result) => { + console.log('Popup closed. ') + + }); + //todo set yes no button here + } + + }, "json"); + }); + } + + function addConfigDialog() { $(".configuredevicebutton").click(function (event) { var id = event.target.getAttribute("deviceid"); var cityname; @@ -99,19 +153,17 @@ $(document).ready(function () { html: 'This alert closes automatically.', timer: 1000, }).then((result) => { - console.log('Popup closed. ') - + console.log('Popup closed. '); + reloadDevices(); }); } }); } }); }); - } }); }); - } }); }); @@ -119,9 +171,7 @@ $(document).ready(function () { console.log("click..." + id); }); - var test = $('#table-devices').DataTable(); - }, 'json'); - + } });