enable multiple cities per device
applied db entries on add btn click
This commit is contained in:
parent
c590b612b5
commit
d34b43f61a
@ -42,27 +42,29 @@ public class MqttService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void messageArrived(String s, MqttMessage mqttMessage) {
|
public void messageArrived(String s, MqttMessage mqttMessage) {
|
||||||
String message = new String(mqttMessage.getPayload());
|
String deviceid = new String(mqttMessage.getPayload());
|
||||||
Log.message("received Request from PCB");
|
Log.message("received Request from PCB");
|
||||||
|
|
||||||
ResultSet res = db.executeQuery("SELECT * from devices WHERE DeviceID=" + message);
|
ResultSet res = db.executeQuery("SELECT * from devices WHERE DeviceID=" + deviceid);
|
||||||
try {
|
try {
|
||||||
res.last();
|
res.last();
|
||||||
if (res.getRow() != 0) {
|
if (res.getRow() != 0) {
|
||||||
//existing device
|
//existing device
|
||||||
res.first();
|
res.first();
|
||||||
|
|
||||||
|
// TODO: 23.01.20 --> check device_city db and foreach all cities
|
||||||
int cityid = res.getInt("CityID");
|
int cityid = res.getInt("CityID");
|
||||||
if (cityid == -1) {
|
if (cityid == -1) {
|
||||||
//device not configured yet
|
//device not configured yet
|
||||||
tramsmitMessage(message + ",-1");
|
tramsmitMessage(deviceid + ",-1");
|
||||||
} else {
|
} else {
|
||||||
checkDatabase(cityid, Integer.parseInt(message));
|
checkDatabase(cityid, Integer.parseInt(deviceid));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//new device
|
//new device
|
||||||
db.executeUpdate("INSERT INTO devices (DeviceID) VALUES (" + message + ")");
|
db.executeUpdate("INSERT INTO devices (DeviceID) VALUES (" + deviceid + ")");
|
||||||
Log.info("new device registered to server");
|
Log.info("new device registered to server");
|
||||||
tramsmitMessage(message + ",-1");
|
tramsmitMessage(deviceid + ",-1");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -23,41 +23,37 @@ public class DeviceRequest extends PostRequest {
|
|||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
switch (params.get("action")) {
|
switch (params.get("action")) {
|
||||||
case "getdevices":
|
case "getdevices":
|
||||||
ResultSet setunconfigured = jdbc.executeQuery("SELECT * FROM `devices` WHERE `CityID`=-1");
|
ResultSet deviceset = jdbc.executeQuery("SELECT * FROM `devices");
|
||||||
ResultSet setconfigured = jdbc.executeQuery("SELECT * FROM `devices` INNER JOIN `cities` ON devices.CityID = cities.id");
|
|
||||||
|
|
||||||
sb.append("{\"data\":[");
|
sb.append("{\"data\":[");
|
||||||
try {
|
try {
|
||||||
setconfigured.last();
|
while (deviceset.next()) {
|
||||||
int configsize = setconfigured.getRow();
|
int deviceid = deviceset.getInt("DeviceID");
|
||||||
setconfigured.first();
|
int cityid = deviceset.getInt("CityID");
|
||||||
setconfigured.previous();
|
|
||||||
|
|
||||||
while (setunconfigured.next()) {
|
if (cityid == -1) {
|
||||||
int deviceid = setunconfigured.getInt("DeviceID");
|
sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"cityid\":\"").append(cityid).append("\"}");
|
||||||
int cityid = setunconfigured.getInt("CityID");
|
} else {
|
||||||
|
String devicename = deviceset.getString("DeviceName");
|
||||||
|
String devicelocation = deviceset.getString("DeviceLocation");
|
||||||
|
|
||||||
sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"cityid\":\"").append(cityid).append("\"}");
|
sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"devicename\":\"").append(devicename).append("\",\"devicelocation\":\"").append(devicelocation).append("\",\"devices\":[");
|
||||||
|
|
||||||
if (!(setunconfigured.isLast() && configsize == 0)) {
|
ResultSet devicecities = jdbc.executeQuery("SELECT * FROM `device_city` INNER JOIN `cities` ON device_city.CityID=cities.id WHERE `DeviceID`='" + deviceid + "'");
|
||||||
sb.append(",");
|
while (devicecities.next()) {
|
||||||
|
int cityidd = devicecities.getInt("id");
|
||||||
|
String cityname = devicecities.getString("name");
|
||||||
|
String wastetype = devicecities.getString("wastetype");
|
||||||
|
String zone = devicecities.getString("zone");
|
||||||
|
|
||||||
|
sb.append("{\"cityid\":\"").append(cityidd).append("\",\"cityname\":\"").append(cityname).append("\",\"wastetype\":\"").append(wastetype).append("\",\"zone\":\"").append(zone).append("\"}");
|
||||||
|
if (!(devicecities.isLast())) {
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.append("]}");
|
||||||
}
|
}
|
||||||
}
|
if (!(deviceset.isLast())) {
|
||||||
|
|
||||||
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\":\"").append(deviceid).append("\",\"cityid\":\"").append(cityid).append("\",\"devicename\":\"").append(devicename).append("\",\"devicelocation\":\"").append(devicelocation).append("\",\"cityname\":\"").append(cityname).append("\",\"wastetype\":\"").append(wastetype).append("\",\"zone\":\"").append(zone).append("\"}");
|
|
||||||
|
|
||||||
if (!setconfigured.isLast()) {
|
|
||||||
sb.append(",");
|
sb.append(",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,19 +64,19 @@ public class DeviceRequest extends PostRequest {
|
|||||||
|
|
||||||
break;
|
break;
|
||||||
case "getCitynames":
|
case "getCitynames":
|
||||||
setunconfigured = jdbc.executeQuery("select * from cities");
|
deviceset = jdbc.executeQuery("select * from cities");
|
||||||
Log.debug(setunconfigured.toString());
|
Log.debug(deviceset.toString());
|
||||||
sb.append("{");
|
sb.append("{");
|
||||||
try {
|
try {
|
||||||
String prev = "";
|
String prev = "";
|
||||||
while (setunconfigured.next()) {
|
while (deviceset.next()) {
|
||||||
if (!prev.equals(setunconfigured.getString("name"))) {
|
if (!prev.equals(deviceset.getString("name"))) {
|
||||||
if (!setunconfigured.isFirst()) {
|
if (!deviceset.isFirst()) {
|
||||||
sb.append(",");
|
sb.append(",");
|
||||||
}
|
}
|
||||||
sb.append("\"").append(setunconfigured.getString("name")).append("\":\"").append(setunconfigured.getString("name")).append("\"");
|
sb.append("\"").append(deviceset.getString("name")).append("\":\"").append(deviceset.getString("name")).append("\"");
|
||||||
}
|
}
|
||||||
prev = setunconfigured.getString("name");
|
prev = deviceset.getString("name");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -89,19 +85,19 @@ public class DeviceRequest extends PostRequest {
|
|||||||
Log.debug(sb.toString());
|
Log.debug(sb.toString());
|
||||||
break;
|
break;
|
||||||
case "getzones":
|
case "getzones":
|
||||||
setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC");
|
deviceset = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC");
|
||||||
Log.debug(setunconfigured.toString());
|
Log.debug(deviceset.toString());
|
||||||
sb.append("{");
|
sb.append("{");
|
||||||
try {
|
try {
|
||||||
int prev = 42;
|
int prev = 42;
|
||||||
while (setunconfigured.next()) {
|
while (deviceset.next()) {
|
||||||
if (prev != setunconfigured.getInt("zone")) {
|
if (prev != deviceset.getInt("zone")) {
|
||||||
sb.append("\"").append(setunconfigured.getInt("zone")).append("\":\"").append(setunconfigured.getInt("zone")).append("\"");
|
sb.append("\"").append(deviceset.getInt("zone")).append("\":\"").append(deviceset.getInt("zone")).append("\"");
|
||||||
if (!setunconfigured.isLast()) {
|
if (!deviceset.isLast()) {
|
||||||
sb.append(",");
|
sb.append(",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prev = setunconfigured.getInt("zone");
|
prev = deviceset.getInt("zone");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -109,19 +105,19 @@ public class DeviceRequest extends PostRequest {
|
|||||||
sb.append("}");
|
sb.append("}");
|
||||||
break;
|
break;
|
||||||
case "gettypes":
|
case "gettypes":
|
||||||
setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC");
|
deviceset = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC");
|
||||||
Log.debug(setunconfigured.toString());
|
Log.debug(deviceset.toString());
|
||||||
sb.append("{");
|
sb.append("{");
|
||||||
try {
|
try {
|
||||||
String prev = "42";
|
String prev = "42";
|
||||||
while (setunconfigured.next()) {
|
while (deviceset.next()) {
|
||||||
if (!prev.equals(setunconfigured.getString("wastetype"))) {
|
if (!prev.equals(deviceset.getString("wastetype"))) {
|
||||||
sb.append("\"" + setunconfigured.getString("wastetype") + "\":\"" + setunconfigured.getString("wastetype") + "\"");
|
sb.append("\"" + deviceset.getString("wastetype") + "\":\"" + deviceset.getString("wastetype") + "\"");
|
||||||
if (!setunconfigured.isLast()) {
|
if (!deviceset.isLast()) {
|
||||||
sb.append(",");
|
sb.append(",");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prev = setunconfigured.getString("wastetype");
|
prev = deviceset.getString("wastetype");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@ -129,14 +125,17 @@ public class DeviceRequest extends PostRequest {
|
|||||||
sb.append("}");
|
sb.append("}");
|
||||||
break;
|
break;
|
||||||
case "savetodb":
|
case "savetodb":
|
||||||
setunconfigured = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'");
|
|
||||||
try {
|
try {
|
||||||
setunconfigured.last();
|
ResultSet cityset = jdbc.executeQuery("SELECT id from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'");
|
||||||
if (setunconfigured.getRow() != 1) {
|
cityset.last();
|
||||||
|
if (cityset.getRow() != 1) {
|
||||||
// TODO: 17.01.20 error handling
|
// TODO: 17.01.20 error handling
|
||||||
} else {
|
} else {
|
||||||
int id = setunconfigured.getInt("id");
|
int cityid = cityset.getInt("id");
|
||||||
jdbc.executeUpdate("UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'");
|
System.out.println(cityid);
|
||||||
|
|
||||||
|
jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params.get("deviceid") + "', '" + cityid + "');");
|
||||||
|
jdbc.executeUpdate("UPDATE devices SET `CityID`='0',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'");
|
||||||
sb.append("{\"success\":\"true\"}");
|
sb.append("{\"success\":\"true\"}");
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@ -146,17 +145,38 @@ public class DeviceRequest extends PostRequest {
|
|||||||
case "deleteDevice":
|
case "deleteDevice":
|
||||||
try {
|
try {
|
||||||
jdbc.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params.get("id") + "'");
|
jdbc.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params.get("id") + "'");
|
||||||
|
jdbc.executeUpdate("DELETE FROM device_city WHERE `DeviceID`='" + params.get("id") + "'");
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
sb.append("{\"status\":\"success\"}");
|
sb.append("{\"status\":\"success\"}");
|
||||||
break;
|
break;
|
||||||
case "getDeviceNumber":
|
case "getDeviceNumber":
|
||||||
// TODO: 18.01.20
|
try {
|
||||||
|
ResultSet numberset = jdbc.executeQuery("SELECT * FROM devices");
|
||||||
|
numberset.last();
|
||||||
|
int devicenr = numberset.getRow();
|
||||||
|
|
||||||
|
sb.append("{\"devicenr\":\"" + devicenr + "\"}");
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case "addtodb":
|
case "addtodb":
|
||||||
// TODO: 18.01.20
|
System.out.println(params);
|
||||||
//ResultSet seti = jdbc.executeUpdate("UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'"); ");
|
int cityid = -1;
|
||||||
|
try {
|
||||||
|
ResultSet device = jdbc.executeQuery("SELECT * FROM cities WHERE name='" + params.get("cityname") + "' AND wastetype='" + params.get("wastetype") + "' AND zone='" + params.get("zonename") + "'");
|
||||||
|
device.first();
|
||||||
|
cityid = device.getInt("id");
|
||||||
|
System.out.println(cityid);
|
||||||
|
jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params.get("deviceid") + "', '" + cityid + "');");
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sb.append("{\"success\":true}");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
|
@ -24,11 +24,18 @@ $(document).ready(function () {
|
|||||||
var devicename = data.data[i].devicename;
|
var devicename = data.data[i].devicename;
|
||||||
var devicelocation = data.data[i].devicelocation;
|
var devicelocation = data.data[i].devicelocation;
|
||||||
|
|
||||||
var cityname = data.data[i].cityname;
|
var row = "<tr><td>" + id + "</td><td>" + devicename + "</td><td>" + devicelocation + "</td><td>";
|
||||||
var cityzone = data.data[i].zone;
|
|
||||||
var wastetype = data.data[i].wastetype;
|
|
||||||
|
|
||||||
$("#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>");
|
for (var n = 0; n < data.data[i].devices.length; n++) {
|
||||||
|
var cityname = data.data[i].devices[n].cityname;
|
||||||
|
var cityzone = data.data[i].devices[n].zone;
|
||||||
|
var wastetype = data.data[i].devices[n].wastetype;
|
||||||
|
row += cityname + "/" + wastetype + "/" + cityzone + " </br>";
|
||||||
|
}
|
||||||
|
|
||||||
|
row += "</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>";
|
||||||
|
|
||||||
|
$("#devices-tablebody").append(row);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,7 +49,7 @@ $(document).ready(function () {
|
|||||||
|
|
||||||
function addAddButton() {
|
function addAddButton() {
|
||||||
$('.addbtn').click(function (event) {
|
$('.addbtn').click(function (event) {
|
||||||
var id = event.target.getAttribute("deviceid");
|
var id = event.target.getAttribute("dataid");
|
||||||
var cityname;
|
var cityname;
|
||||||
var zone;
|
var zone;
|
||||||
var wastetype;
|
var wastetype;
|
||||||
@ -110,7 +117,7 @@ $(document).ready(function () {
|
|||||||
Swal.fire({
|
Swal.fire({
|
||||||
type: "success",
|
type: "success",
|
||||||
title: 'Successfully configured!',
|
title: 'Successfully configured!',
|
||||||
html: 'This alert closes automatically.',
|
html: 'This alert closes added.',
|
||||||
timer: 1000,
|
timer: 1000,
|
||||||
}).then((result) => {
|
}).then((result) => {
|
||||||
console.log('Popup closed. ');
|
console.log('Popup closed. ');
|
||||||
|
Loading…
Reference in New Issue
Block a user