Compare commits

...

2 Commits
master ... gson

Author SHA1 Message Date
a586c2308e reformat and todo 2020-02-14 16:43:29 +01:00
c7a657ce3c added gson to project 2020-02-14 16:39:17 +01:00
2 changed files with 35 additions and 11 deletions

View File

@ -30,6 +30,7 @@ dependencies {
compile group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.2.2' compile group: 'org.eclipse.paho', name: 'org.eclipse.paho.client.mqttv3', version: '1.2.2'
compile group: 'mysql',name:'mysql-connector-java',version: '8.0.18' compile group: 'mysql',name:'mysql-connector-java',version: '8.0.18'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
implementation 'com.google.code.gson:gson:2.8.6'
} }
task run (type: JavaExec){ task run (type: JavaExec){

View File

@ -1,5 +1,9 @@
package com.wasteinformationserver.website.datarequests package com.wasteinformationserver.website.datarequests
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.wasteinformationserver.basicutils.Log
import com.wasteinformationserver.basicutils.Log.Log.debug import com.wasteinformationserver.basicutils.Log.Log.debug
import com.wasteinformationserver.basicutils.Log.Log.error import com.wasteinformationserver.basicutils.Log.Log.error
import com.wasteinformationserver.db.JDBC import com.wasteinformationserver.db.JDBC
@ -19,39 +23,58 @@ class DeviceRequest : PostRequest() {
} }
val sb = StringBuilder() val sb = StringBuilder()
var deviceset: ResultSet var deviceset: ResultSet
val gson = Gson()
val elem = gson.toJsonTree(Object())
when (params["action"]) { when (params["action"]) {
"getdevices" -> { "getdevices" -> {
deviceset = jdbc!!.executeQuery("SELECT * FROM `devices") deviceset = jdbc!!.executeQuery("SELECT * FROM `devices")
elem.asJsonObject.add("data", JsonArray())
sb.append("{\"data\":[") sb.append("{\"data\":[")
try { try {
while (deviceset.next()) { while (deviceset.next()) {
val deviceid = deviceset.getInt("DeviceID") val deviceid = deviceset.getInt("DeviceID")
val cityid = deviceset.getInt("CityID") val cityid = deviceset.getInt("CityID")
if (cityid == -1) { if (cityid == -1) {
sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"cityid\":\"").append(cityid).append("\"}") val obj = JsonObject()
obj.addProperty("deviceid", deviceid)
obj.addProperty("cityid", cityid)
elem.asJsonObject.get("data").asJsonArray.add(obj)
} }
else { else {
val obj = JsonObject()
val devicename = deviceset.getString("DeviceName") val devicename = deviceset.getString("DeviceName")
val devicelocation = deviceset.getString("DeviceLocation") val devicelocation = deviceset.getString("DeviceLocation")
sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"devicename\":\"").append(devicename).append("\",\"devicelocation\":\"").append(devicelocation).append("\",\"devices\":[") obj.addProperty("deviceid", deviceid)
obj.addProperty("devicename", devicename)
obj.addProperty("devicelocation", devicelocation)
val locations = JsonArray()
obj.add("locations", locations)
elem.asJsonObject.get("data").asJsonArray.add(obj)
val devicecities = jdbc.executeQuery("SELECT * FROM `device_city` INNER JOIN `cities` ON device_city.CityID=cities.id WHERE `DeviceID`='$deviceid'") val devicecities = jdbc.executeQuery("SELECT * FROM `device_city` INNER JOIN `cities` ON device_city.CityID=cities.id WHERE `DeviceID`='$deviceid'")
while (devicecities.next()) { while (devicecities.next()) {
val cityidd = devicecities.getInt("id") val cityidd = devicecities.getInt("id")
val cityname = devicecities.getString("name") val cityname = devicecities.getString("name")
val wastetype = devicecities.getString("wastetype") val wastetype = devicecities.getString("wastetype")
val zone = devicecities.getString("zone") val 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) { val objc = JsonObject()
sb.append(",") objc.addProperty("cityid", cityidd)
objc.addProperty("cityname", cityname)
objc.addProperty("wastetype", wastetype)
objc.addProperty("zone", zone)
locations.add(objc)
// todo better var names
// todo implement in js and other methods
} }
} }
sb.append("]}")
} }
if (!deviceset.isLast) { Log.debug(gson.toJson(elem))
sb.append(",")
}
}
sb.append("]}")
} catch (e: SQLException) { } catch (e: SQLException) {
e.printStackTrace() e.printStackTrace()
} }