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

View File

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

View File

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