correctly configure new devices

edit data in db correctly
response correctly to requests
This commit is contained in:
lukas-heiligenbrunner 2020-01-16 21:12:05 +01:00
parent b9ba0dbcef
commit 23f75cae4f
3 changed files with 105 additions and 77 deletions

View File

@ -56,7 +56,7 @@ public class MqttService {
//device not configured yet
tramsmitMessage(message + ",-1");
} else {
checkDatabase(Integer.parseInt(message));
checkDatabase(cityid, Integer.parseInt(message));
}
} else {
//new device
@ -80,14 +80,30 @@ public class MqttService {
}
}
public void checkDatabase(int deviceid) {
ResultSet result = db.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=" + deviceid);
public void checkDatabase(int citywastezoneid, int deviceid) {
int wastetype = -1;
ResultSet set2 = db.executeQuery("SELECT * FROM cities WHERE `id`='" + citywastezoneid + "'");
try {
set2.last();
if (set2.getRow() != 1) {
//error
} else {
String typ = set2.getString("wastetype");
wastetype = getIntTyp(typ);
}
} catch (SQLException e) {
e.printStackTrace();
}
ResultSet result = db.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=" + citywastezoneid);
try {
result.last();
if (result.getRow() == 0) {
//if not found in db --> send zero
Log.debug("not found in db");
tramsmitMessage(deviceid + "," + "Plastic" + "," + 0);
tramsmitMessage(deviceid + "," + wastetype + "," + 0);
} else {
Log.debug(result.getString("pickupdate"));
@ -100,13 +116,12 @@ public class MqttService {
if (timestamp == timestampnow || timestamp == timestampnow + 86400000) { // 86400000 == one day
// valid time
// TODO: 12.01.20 read right waste type from db and replace below
tramsmitMessage(deviceid + "," + getIntTyp("Plastic") + "," + 1);
tramsmitMessage(deviceid + "," + wastetype + "," + 1);
Log.debug("valid time");
return;
}
} while (result.next());
tramsmitMessage(deviceid + "," + getIntTyp("Plastic") + "," + 0); //transmit zero if not returned before
tramsmitMessage(deviceid + "," + wastetype + "," + 0); //transmit zero if not returned before
}
} catch (SQLException | ParseException e) {
e.printStackTrace();

View File

@ -75,14 +75,14 @@ public class DeviceRequest extends PostRequest {
case "getzones":
set = jdcb.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC");
Log.debug(set.toString());
sb.append("{\"data\":[");
sb.append("{");
try {
int prev = 42;
while (set.next()) {
if (prev == set.getInt("zone")) {
} else {
sb.append("{\"zone\":\"" + set.getInt("zone") + "\"}");
sb.append("\"" + set.getInt("zone") + "\":\"" + set.getInt("zone") + "\"");
if (!set.isLast()) {
sb.append(",");
}
@ -92,21 +92,19 @@ public class DeviceRequest extends PostRequest {
} catch (SQLException e) {
e.printStackTrace();
}
sb.append("]");
sb.append(",\"query\":\"ok\"");
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());
sb.append("{\"data\":[");
sb.append("{");
try {
String prev = "42";
while (set.next()) {
if (prev == set.getString("wastetype")) {
} else {
sb.append("{\"wastetype\":\"" + set.getString("wastetype") + "\"}");
sb.append("\"" + set.getString("wastetype") + "\":\"" + set.getString("wastetype") + "\"");
if (!set.isLast()) {
sb.append(",");
}
@ -116,10 +114,25 @@ public class DeviceRequest extends PostRequest {
} catch (SQLException e) {
e.printStackTrace();
}
sb.append("]");
sb.append(",\"query\":\"ok\"");
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") + "'");
try {
set.last();
if (set.getRow() != 1) {
//error
} 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") + "'");
}
} catch (SQLException e) {
e.printStackTrace();
}
//'action=savetodb&cityname=' + cityname + '&zonename=' + zone+'&wastetype='+wastetype+'&devicename='+devicename+'&devicelocation='+devicelocation
break;
}
return sb.toString();
}

View File

@ -22,6 +22,11 @@ $(document).ready(function () {
}
$(".configuredevicebutton").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({
@ -46,50 +51,67 @@ $(document).ready(function () {
if (result.value) {
console.log(result.value);
const answers = JSON.stringify(result.value);
cityname = result.value[2];
devicename = result.value[0];
devicelocation = result.value[1];
Swal.mixin({
input: 'text',
confirmButtonText: 'Next →',
showCancelButton: true,
progressSteps: ['1']
}).queue([
{
title: 'City',
text: 'Select your City',
input: 'select',
inputOptions: {
'SRB': 'Serbia', // How do I dynamically set value?
'UKR': 'Ukraine',
'HRV': 'Croatia'
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);
]).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];
Swal.mixin({
input: 'text',
confirmButtonText: 'Next →',
showCancelButton: true,
progressSteps: ['1']
}).queue([
{
title: 'City',
text: 'Select your City',
input: 'select',
inputOptions: {
'SRB': 'Serbia', // How do I dynamically set value?
'UKR': 'Ukraine',
'HRV': 'Croatia'
}
}
]).then((result) => {
if (result.value) {
console.log(result.value);
}
});
}
$.post('/senddata/Devicedata', 'action=savetodb&deviceid=' + id + '&cityname=' + cityname + '&zonename=' + zone + '&wastetype=' + wastetype + '&devicename=' + devicename + '&devicelocation=' + devicelocation, 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. ')
});
}
});
}
});
});
}
});
});
}
});
});
@ -97,28 +119,6 @@ $(document).ready(function () {
console.log("click..." + id);
});
/*
,{
title: 'Zone',
text: 'Select the Waste Zone',
input: 'select',
inputOptions: {
'SRB': 'Serbia', // How do I dynamically set value?
'UKR': 'Ukraine',
'HRV': 'Croatia'
}
},{
title: 'Type',
text: 'Select the Waste type',
input: 'select',
inputOptions: {
'SRB': 'Serbia', // How do I dynamically set value?
'UKR': 'Ukraine',
'HRV': 'Croatia'
}
}
*/
var test = $('#table-devices').DataTable();
}, 'json');