removed checked calls for JDBC and added connection check in JDBC class itself
This commit is contained in:
parent
aa023aeb95
commit
022ae044ab
@ -1,4 +1,7 @@
|
||||
package com.wasteinformationserver.db;
|
||||
|
||||
import com.wasteinformationserver.basicutils.Log;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
@ -53,14 +56,15 @@ public class JDBC {
|
||||
* @return JDBC object of this
|
||||
* @throws IOException
|
||||
*/
|
||||
public static JDBC getInstance() throws IOException {
|
||||
if (loggedin) {
|
||||
return JDBC;
|
||||
} else {
|
||||
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
|
||||
return JDBC;
|
||||
public static JDBC getInstance() {
|
||||
if (!loggedin) {
|
||||
try {
|
||||
logintodb(usernamec, passwordc, dbnamec, ipc, portc);
|
||||
} catch (IOException e) {
|
||||
Log.Log.error("no connetion to db - retrying in 5min");
|
||||
}
|
||||
}
|
||||
|
||||
return JDBC;
|
||||
}
|
||||
|
||||
private static void logintodb(String username, String password, String dbname, String ip, int port) throws IOException {
|
||||
@ -76,6 +80,7 @@ public class JDBC {
|
||||
loggedin = true;
|
||||
} catch (SQLException e) {
|
||||
throw new IOException("No connection to database");
|
||||
// todo reconnect every 5mins or something
|
||||
}
|
||||
|
||||
}
|
||||
@ -108,4 +113,8 @@ public class JDBC {
|
||||
|
||||
return stmt.executeUpdate();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return loggedin;
|
||||
}
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ import java.util.*
|
||||
* @author Gregor Dutzler
|
||||
*/
|
||||
class MqttService(serverurl: String, port: String) {
|
||||
private var client: MqttClient? = null
|
||||
private val serveruri: String
|
||||
private var db: JDBC? = null
|
||||
private val serveruri: String = "tcp://$serverurl:$port"
|
||||
private var client: MqttClient = MqttClient(serveruri, "JavaSample42")
|
||||
private var db: JDBC = JDBC.getInstance()
|
||||
|
||||
/**
|
||||
* init mqtt service
|
||||
@ -32,8 +32,7 @@ class MqttService(serverurl: String, port: String) {
|
||||
* @param port mqtt server port
|
||||
*/
|
||||
init {
|
||||
serveruri = "tcp://$serverurl:$port"
|
||||
connect()
|
||||
connectToDb()
|
||||
}
|
||||
|
||||
/**
|
||||
@ -44,26 +43,27 @@ class MqttService(serverurl: String, port: String) {
|
||||
client = MqttClient(serveruri, "JavaSample42")
|
||||
val connOpts = MqttConnectOptions()
|
||||
connOpts.isCleanSession = true
|
||||
client!!.connect(connOpts)
|
||||
client!!.setCallback(object : MqttCallback {
|
||||
client.connect(connOpts)
|
||||
client.setCallback(object : MqttCallback {
|
||||
override fun connectionLost(throwable: Throwable) {
|
||||
error("connection lost")
|
||||
connect()
|
||||
connectToDb()
|
||||
}
|
||||
|
||||
override fun messageArrived(s: String, mqttMessage: MqttMessage) {
|
||||
val deviceid = String(mqttMessage.payload)
|
||||
message("received Request from PCB")
|
||||
val res = db!!.executeQuery("SELECT * from devices WHERE DeviceID=$deviceid")
|
||||
val res = db.executeQuery("SELECT * from devices WHERE DeviceID=$deviceid")
|
||||
try {
|
||||
res.last()
|
||||
if (res.row != 0) { //existing device
|
||||
res.first()
|
||||
val devicecities = db!!.executeQuery("SELECT * from device_city WHERE DeviceID='$deviceid'")
|
||||
val devicecities = db.executeQuery("SELECT * from device_city WHERE DeviceID='$deviceid'")
|
||||
devicecities.last()
|
||||
if (devicecities.row == 0) { //not configured
|
||||
tramsmitMessage("$deviceid,-1")
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
devicecities.first()
|
||||
devicecities.previous()
|
||||
|
||||
@ -72,8 +72,9 @@ class MqttService(serverurl: String, port: String) {
|
||||
checkDatabase(cityid, deviceid.toInt())
|
||||
}
|
||||
}
|
||||
} else { //new device
|
||||
db!!.executeUpdate("INSERT INTO devices (DeviceID) VALUES ($deviceid)")
|
||||
}
|
||||
else { //new device
|
||||
db.executeUpdate("INSERT INTO devices (DeviceID) VALUES ($deviceid)")
|
||||
info("new device registered to server")
|
||||
tramsmitMessage("$deviceid,-1")
|
||||
}
|
||||
@ -84,7 +85,7 @@ class MqttService(serverurl: String, port: String) {
|
||||
|
||||
override fun deliveryComplete(iMqttDeliveryToken: IMqttDeliveryToken) {}
|
||||
})
|
||||
client!!.subscribe("TopicIn")
|
||||
client.subscribe("TopicIn")
|
||||
} catch (e: MqttException) {
|
||||
error("Connection to the Broker failed")
|
||||
}
|
||||
@ -92,24 +93,26 @@ class MqttService(serverurl: String, port: String) {
|
||||
|
||||
private fun checkDatabase(citywastezoneid: Int, deviceid: Int) {
|
||||
var wastetype = -1
|
||||
val set2 = db!!.executeQuery("SELECT * FROM cities WHERE `id`='$citywastezoneid'")
|
||||
val set2 = db.executeQuery("SELECT * FROM cities WHERE `id`='$citywastezoneid'")
|
||||
try {
|
||||
set2.last()
|
||||
if (set2.row != 1) { //error
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
val typ = set2.getString("wastetype")
|
||||
wastetype = getIntTyp(typ)
|
||||
}
|
||||
} catch (e: SQLException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
val result = db!!.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=$citywastezoneid")
|
||||
val result = db.executeQuery("SELECT pickupdates.pickupdate FROM pickupdates WHERE pickupdates.citywastezoneid=$citywastezoneid")
|
||||
try {
|
||||
result.last()
|
||||
if (result.row == 0) { //if not found in db --> send zero
|
||||
debug("not found in db")
|
||||
tramsmitMessage("$deviceid,$wastetype,0")
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
debug(result.getString("pickupdate"))
|
||||
result.first()
|
||||
do {
|
||||
@ -138,41 +141,33 @@ class MqttService(serverurl: String, port: String) {
|
||||
val message = MqttMessage(temp.toByteArray())
|
||||
message.qos = 2
|
||||
try {
|
||||
client!!.publish("TopicOut", message)
|
||||
client.publish("TopicOut", message)
|
||||
} catch (e: MqttException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getTyp(number: Int): String? {
|
||||
if (number == 1) {
|
||||
return "Plastic"
|
||||
} else if (number == 2) {
|
||||
return "Metal"
|
||||
} else if (number == 3) {
|
||||
return "Residual waste"
|
||||
} else if (number == 4) {
|
||||
return "Biowaste"
|
||||
return when (number) {
|
||||
1 -> "Plastic"
|
||||
2 -> "Metal"
|
||||
3 -> "Residual waste"
|
||||
4 -> "Biowaste"
|
||||
else -> null
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
private fun getIntTyp(temp: String): Int {
|
||||
var number = 0
|
||||
when (temp) {
|
||||
"Plastic" -> number = 1
|
||||
"Metal" -> number = 2
|
||||
"Residual waste" -> number = 3
|
||||
"Biowaste" -> number = 4
|
||||
return when (temp) {
|
||||
"Plastic" -> 1
|
||||
"Metal" -> 2
|
||||
"Residual waste" -> 3
|
||||
"Biowaste" -> 4
|
||||
else -> 0
|
||||
}
|
||||
return number
|
||||
}
|
||||
|
||||
private fun connect() {
|
||||
try {
|
||||
db = JDBC.getInstance()
|
||||
} catch (e: IOException) {
|
||||
error("no connetion to db")
|
||||
}
|
||||
private fun connectToDb() {
|
||||
db = JDBC.getInstance()
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ import com.wasteinformationserver.basicutils.Log.Log.error
|
||||
import com.wasteinformationserver.basicutils.Log.Log.warning
|
||||
import com.wasteinformationserver.db.JDBC
|
||||
import com.wasteinformationserver.website.basicrequest.PostRequest
|
||||
import java.io.IOException
|
||||
import java.sql.ResultSet
|
||||
import java.sql.SQLException
|
||||
import java.sql.SQLIntegrityConstraintViolationException
|
||||
@ -18,12 +17,12 @@ class DataRequest : PostRequest() {
|
||||
val sb = StringBuilder()
|
||||
var set: ResultSet?
|
||||
var status = -1
|
||||
val jdbc: JDBC = try {
|
||||
JDBC.getInstance()
|
||||
} catch (e: IOException) {
|
||||
val jdbc: JDBC = JDBC.getInstance()
|
||||
if (!jdbc.isConnected) {
|
||||
error("no connection to db")
|
||||
return "{\"query\" : \"nodbconn\"}"
|
||||
}
|
||||
|
||||
when (params["action"]) {
|
||||
"newCity" -> {
|
||||
sb.append("{")
|
||||
|
@ -4,21 +4,20 @@ import com.wasteinformationserver.basicutils.Log.Log.debug
|
||||
import com.wasteinformationserver.basicutils.Log.Log.error
|
||||
import com.wasteinformationserver.db.JDBC
|
||||
import com.wasteinformationserver.website.basicrequest.PostRequest
|
||||
import java.io.IOException
|
||||
import java.sql.ResultSet
|
||||
import java.sql.SQLException
|
||||
import java.util.*
|
||||
|
||||
class DeviceRequest : PostRequest() {
|
||||
override fun request(params: HashMap<String, String>): String {
|
||||
var jdbc: JDBC? = null
|
||||
try {
|
||||
jdbc = JDBC.getInstance()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
val jdbc = JDBC.getInstance()
|
||||
if (!jdbc.isConnected) {
|
||||
error("no connection to db")
|
||||
return "{\"query\" : \"nodbconn\"}"
|
||||
}
|
||||
|
||||
val sb = StringBuilder()
|
||||
var deviceset: ResultSet
|
||||
val deviceset: ResultSet
|
||||
when (params["action"]) {
|
||||
"getdevices" -> {
|
||||
deviceset = jdbc!!.executeQuery("SELECT * FROM `devices")
|
||||
@ -135,7 +134,7 @@ class DeviceRequest : PostRequest() {
|
||||
}
|
||||
"deleteDevice" -> {
|
||||
try {
|
||||
jdbc!!.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params["id"] + "'")
|
||||
jdbc.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params["id"] + "'")
|
||||
jdbc.executeUpdate("DELETE FROM device_city WHERE `DeviceID`='" + params["id"] + "'")
|
||||
} catch (e: SQLException) {
|
||||
e.printStackTrace()
|
||||
@ -145,7 +144,7 @@ class DeviceRequest : PostRequest() {
|
||||
"addtodb" -> {
|
||||
var cityid = -1
|
||||
try {
|
||||
val device = jdbc!!.executeQuery("SELECT * FROM cities WHERE name='" + params["cityname"] + "' AND wastetype='" + params["wastetype"] + "' AND zone='" + params["zonename"] + "'")
|
||||
val device = jdbc.executeQuery("SELECT * FROM cities WHERE name='" + params["cityname"] + "' AND wastetype='" + params["wastetype"] + "' AND zone='" + params["zonename"] + "'")
|
||||
device.first()
|
||||
cityid = device.getInt("id")
|
||||
jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params["deviceid"] + "', '" + cityid + "');")
|
||||
@ -156,11 +155,11 @@ class DeviceRequest : PostRequest() {
|
||||
}
|
||||
"getheader" -> {
|
||||
try {
|
||||
var numberset = jdbc!!.executeQuery("SELECT * FROM devices")
|
||||
var numberset = jdbc.executeQuery("SELECT * FROM devices")
|
||||
numberset.last()
|
||||
val devicenr = numberset.row
|
||||
|
||||
numberset = jdbc!!.executeQuery("SELECT * FROM devices WHERE CityID=-1")
|
||||
numberset = jdbc.executeQuery("SELECT * FROM devices WHERE CityID=-1")
|
||||
numberset.last()
|
||||
val unconfigureddevices = numberset.row
|
||||
|
||||
|
@ -4,7 +4,6 @@ import com.wasteinformationserver.basicutils.Log;
|
||||
import com.wasteinformationserver.db.JDBC;
|
||||
import com.wasteinformationserver.website.basicrequest.PostRequest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
@ -13,14 +12,14 @@ public class NewDateRequest extends PostRequest {
|
||||
@Override
|
||||
public String request(HashMap<String, String> params) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
JDBC jdbc;
|
||||
ResultSet set;
|
||||
try {
|
||||
jdbc = JDBC.getInstance();
|
||||
} catch (IOException e) {
|
||||
|
||||
JDBC jdbc = JDBC.getInstance();
|
||||
if (!jdbc.isConnected()) {
|
||||
Log.Log.error("no connection to db");
|
||||
return "{\"query\" : \"nodbconn\"}";
|
||||
}
|
||||
|
||||
switch (params.get("action")) {
|
||||
case "getCitynames":
|
||||
set = jdbc.executeQuery("select * from cities");
|
||||
@ -72,7 +71,7 @@ public class NewDateRequest extends PostRequest {
|
||||
sb.append("}");
|
||||
break;
|
||||
case "gettypes":
|
||||
set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='"+params.get("zonename")+"' ORDER BY zone ASC");
|
||||
set = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC");
|
||||
Log.Log.debug(set.toString());
|
||||
sb.append("{\"data\":[");
|
||||
try {
|
||||
|
Loading…
Reference in New Issue
Block a user