From 7f62fc9becfde71d2aae437fa1166c8af5869f7b Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Sat, 18 Jan 2020 10:17:33 +0100 Subject: [PATCH] add button for adding new zone infos to device --- .../website/datarequests/DeviceRequest.java | 48 +++++++--- src/resources/wwwroot/device.html | 8 +- src/resources/wwwroot/js/device.js | 94 ++++++++++++++++++- 3 files changed, 129 insertions(+), 21 deletions(-) diff --git a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java index de6e071..1d914fe 100644 --- a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java +++ b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java @@ -30,7 +30,6 @@ public class DeviceRequest extends PostRequest { try { setconfigured.last(); int configsize = setconfigured.getRow(); - System.out.println(configsize); setconfigured.first(); setconfigured.previous(); @@ -40,30 +39,29 @@ public class DeviceRequest extends PostRequest { sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\"}"); - if (!(setunconfigured.isLast() && configsize==0)) { + if (!(setunconfigured.isLast() && configsize == 0)) { sb.append(","); } } - while (setconfigured.next()) { - int deviceid = setconfigured.getInt("DeviceID"); - int cityid = setconfigured.getInt("CityID"); + while (setconfigured.next()) { + int deviceid = setconfigured.getInt("DeviceID"); + int cityid = setconfigured.getInt("CityID"); - String devicename = setconfigured.getString("DeviceName"); - String devicelocation = setconfigured.getString("DeviceLocation"); + 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"); + 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 + "\"}"); + sb.append("{\"deviceid\":\"" + deviceid + "\",\"cityid\":\"" + cityid + "\",\"devicename\":\"" + devicename + "\",\"devicelocation\":\"" + devicelocation + "\",\"cityname\":\"" + cityname + "\",\"wastetype\":\"" + wastetype + "\",\"zone\":\"" + zone + "\"}"); - if (!setconfigured.isLast()) { - sb.append(","); - } + if (!setconfigured.isLast()) { + sb.append(","); } + } sb.append("]}"); - System.out.println(sb.toString()); } catch (SQLException e) { e.printStackTrace(); } @@ -159,6 +157,26 @@ public class DeviceRequest extends PostRequest { } sb.append("{\"status\":\"success\"}"); break; + case "getDeviceNumber": + // TODO: 18.01.20 + break; + case "addtodb": + // TODO: 18.01.20 + + setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'"); + try { + setunconfigured.last(); + if (setunconfigured.getRow() != 1) { + // TODO: 17.01.20 error handling + } else { + 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(); + } + break; } return sb.toString(); } diff --git a/src/resources/wwwroot/device.html b/src/resources/wwwroot/device.html index 1414bc0..3f04bf6 100644 --- a/src/resources/wwwroot/device.html +++ b/src/resources/wwwroot/device.html @@ -189,8 +189,8 @@ Device ID Devicename Devicelocation - Zone - X + WasteType + Action @@ -200,8 +200,8 @@ Device ID Devicename Devicelocation - Zone - X + WasteType + Action diff --git a/src/resources/wwwroot/js/device.js b/src/resources/wwwroot/js/device.js index 152b943..0c560be 100644 --- a/src/resources/wwwroot/js/device.js +++ b/src/resources/wwwroot/js/device.js @@ -28,18 +28,108 @@ $(document).ready(function () { 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 + ""); + $("#devices-tablebody").append("" + id + "" + devicename + "" + devicelocation + "" + cityname + "/" + wastetype + "/" + cityzone + ""); } } addDeleteButton(); + addAddButton(); addConfigDialog(); devicetable = $('#table-devices').DataTable(); }, 'json'); } + function addAddButton() { + $('.addbtn').click(function (event) { + var id = event.target.getAttribute("deviceid"); + var cityname; + var zone; + var wastetype; + var devicename; + var devicelocation; + + $.post('/senddata/Devicedata', 'action=getCitynames', function (data) { + Swal.mixin({ + input: 'text', + confirmButtonText: 'Next →', + showCancelButton: true, + progressSteps: ['1'] + }).queue([{ + title: 'City', + text: 'Select your City', + input: 'select', + inputOptions: data + } + ]).then((result) => { + if (result.value) { + console.log(result.value); + const answers = JSON.stringify(result.value); + cityname = result.value[0]; + + console.log("cityname=" + cityname); + $.post('/senddata/Devicedata', 'action=getzones&cityname=' + cityname, function (data) { + Swal.mixin({ + input: 'text', + confirmButtonText: 'Next →', + showCancelButton: true, + progressSteps: ['1'] + }).queue([ + { + title: 'City', + text: 'Select your City', + input: 'select', + inputOptions: data + } + ]).then((result) => { + if (result.value) { + console.log(result.value); + zone = result.value[0]; + $.post('/senddata/Devicedata', 'action=gettypes&cityname=' + cityname + '&zonename=' + zone, function (data) { + Swal.mixin({ + input: 'text', + confirmButtonText: 'Next →', + showCancelButton: true, + progressSteps: ['1'] + }).queue([ + { + title: 'City', + text: 'Select your City', + input: 'select', + inputOptions: data + } + ]).then((result) => { + if (result.value) { + console.log(result.value); + wastetype = result.value[0]; + + + //todo add to db + $.post('/senddata/Devicedata', 'action=addtodb&deviceid=' + id + '&cityname=' + cityname + '&zonename=' + zone + '&wastetype=' + wastetype, function (data) { + if (data.success) { + Swal.fire({ + type: "success", + title: 'Successfully configured!', + html: 'This alert closes automatically.', + timer: 1000, + }).then((result) => { + console.log('Popup closed. '); + reloadDevices(); + }); + } + }); + } + }); + }); + } + }); + }); + } + }); + }); + }); + } + function addDeleteButton() { $(".delbtn").click(function (event) { var id = event.target.getAttribute("dataid");