add button for adding new zone infos to device

This commit is contained in:
lukas-heiligenbrunner 2020-01-18 10:17:33 +01:00
parent fa16d1206b
commit 7f62fc9bec
3 changed files with 129 additions and 21 deletions

View File

@ -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();
}

View File

@ -189,8 +189,8 @@
<th>Device ID</th>
<th>Devicename</th>
<th>Devicelocation</th>
<th>Zone</th>
<th>X</th>
<th>WasteType</th>
<th>Action</th>
</tr>
</thead>
<tbody id="devices-tablebody">
@ -200,8 +200,8 @@
<th>Device ID</th>
<th>Devicename</th>
<th>Devicelocation</th>
<th>Zone</th>
<th>X</th>
<th>WasteType</th>
<th>Action</th>
</tr>
</tfoot>
</table>

View File

@ -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("<tr><td>" + id + "</td><td>" + devicename + "</td><td>" + devicelocation + "</td><td>" + cityname + "/" + wastetype + "/" + cityzone + "</td><td><button dataid='" + id + "' type='button' class='delbtn btn btn-danger'>X</button></td></tr>");
$("#devices-tablebody").append("<tr><td>" + id + "</td><td>" + devicename + "</td><td>" + devicelocation + "</td><td>" + cityname + "/" + wastetype + "/" + cityzone + "</td><td><button dataid='" + id + "' type='button' class='delbtn btn btn-danger'>X</button><button dataid='" + id + "' type='button' class='addbtn btn btn-success'>ADD</button></td></tr>");
}
}
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 &rarr;',
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 &rarr;',
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 &rarr;',
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");