correct error handling of no db and mqtt connection.

This commit is contained in:
lukas-heiligenbrunner 2020-04-15 16:53:58 +02:00
parent cf9069e489
commit 14eea02dcf
7 changed files with 58 additions and 31 deletions

View File

@ -25,6 +25,7 @@ fun main() {
try { try {
Thread.sleep(200) Thread.sleep(200)
Log.warning("Shutting down ...") Log.warning("Shutting down ...")
JDBC.getInstance().disconnect();
//shutdown routine //shutdown routine
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
e.printStackTrace() e.printStackTrace()
@ -37,9 +38,11 @@ fun main() {
//initial connect to db //initial connect to db
Log.message("initial login to db") Log.message("initial login to db")
try { try {
JDBC.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306) val stor = Storage.getInstance();
JDBC.init(stor.dbUser, stor.dbPassword, stor.dbName, stor.dbhost, stor.dbPort)
// JDBC.init("ingproject", "Kb9Dxklumt76ieq6", "ingproject", "db.power4future.at", 3306)
//JDBC.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306); //JDBC.init("users", "kOpaIJUjkgb9ur6S", "wasteinformation", "192.168.65.15", 3306);
} catch (e: IOException) { //e.printStackTrace(); } catch (e: IOException) {
Log.error("no connection to db") Log.error("no connection to db")
} }
@ -52,6 +55,12 @@ fun main() {
//startup mqtt service //startup mqtt service
Log.message("starting mqtt service") Log.message("starting mqtt service")
val m = MqttService("mqtt.heili.eu", "1883") if (JDBC.isConnected()) {
val m = MqttService(Storage.getInstance().mqttServer, Storage.getInstance().mqttPort.toString())
// val m = MqttService("mqtt.heili.eu", "1883")
m.startupService() m.startupService()
}else{
Log.error("could't start mqtt service because of missing db connection!")
}
} }

View File

@ -21,37 +21,43 @@ class Storage {
} }
} }
private var mqttServer: String = "" var mqttServer: String = ""
get() = field get() = field
set(value) { set(value) {
field = value field = value
} }
private var mqttPort: Int = -1 var mqttPort: Int = -1
get() = field get() = field
set(value) { set(value) {
field = value field = value
} }
private var dbName: String = "" var dbName: String = ""
get() = field get() = field
set(value) { set(value) {
field = value field = value
} }
private var dbUser: String = "" var dbhost: String = ""
get() = field get() = field
set(value) { set(value) {
field = value field = value
} }
private var dbPassword: String = "" var dbUser: String = ""
get() = field get() = field
set(value) { set(value) {
field = value field = value
} }
private var dbPort: Int = -1 var dbPassword: String = ""
get() = field
set(value) {
field = value
}
var dbPort: Int = -1
get() = field get() = field
set(value) { set(value) {
field = value field = value
@ -70,6 +76,7 @@ class Storage {
mqttServer = prop["mqttserver"] as String mqttServer = prop["mqttserver"] as String
mqttPort = (prop["mqttport"] as String).toInt() mqttPort = (prop["mqttport"] as String).toInt()
dbhost = prop["dbhost"] as String
dbName = prop["dbname"] as String dbName = prop["dbname"] as String
dbUser = prop["dbuser"] as String dbUser = prop["dbuser"] as String
dbPassword = prop["dbpass"] as String dbPassword = prop["dbpass"] as String
@ -89,12 +96,13 @@ class Storage {
fun store() { fun store() {
prop["mqttserver"] = mqttServer prop["mqttserver"] = mqttServer
prop["mqttport"] = mqttPort.toString() prop["mqttport"] = mqttPort.toString()
prop["dbhost"] = dbhost
prop["dbname"] = dbName prop["dbname"] = dbName
prop["dbuser"] = dbUser prop["dbuser"] = dbUser
prop["dbpass"] = dbPassword prop["dbpass"] = dbPassword
prop["dbport"] = dbPort.toString() prop["dbport"] = dbPort.toString()
prop.store(FileOutputStream("settings.prop"), "") prop.store(FileOutputStream("settings.prop"), "main config")
} }
/** /**
@ -102,12 +110,12 @@ class Storage {
* todo real check if connections can be established * todo real check if connections can be established
*/ */
fun isEveryThingDefined(): Boolean { fun isEveryThingDefined(): Boolean {
return (mqttServer != "" && return (isMqttServerDefined() &&
mqttPort != 0 && isMqttPortDefined() &&
dbName != "" && isDBNameDefined() &&
dbUser != "" && isDBUsernameDefined() &&
dbPassword != "" && isDBPasswdDefined() &&
dbPort != -1) isDBPortDefined())
} }

View File

@ -82,7 +82,14 @@ public class JDBC {
throw new IOException("No connection to database"); throw new IOException("No connection to database");
// todo reconnect every 5mins or something // todo reconnect every 5mins or something
} }
}
public void disconnect(){
try {
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
} }
/** /**
@ -114,7 +121,7 @@ public class JDBC {
return stmt.executeUpdate(); return stmt.executeUpdate();
} }
public boolean isConnected() { public static boolean isConnected() {
return loggedin; return loggedin;
} }
} }

View File

@ -22,11 +22,12 @@ class DataRequest : PostRequest() {
val sb = StringBuilder() val sb = StringBuilder()
var set: ResultSet? var set: ResultSet?
var status = -1 var status = -1
val jdbc: JDBC = JDBC.getInstance()
if (!jdbc.isConnected) { if (!JDBC.isConnected()) {
error("no connection to db") error("no connection to db")
return "{\"query\" : \"nodbconn\"}" return "{\"query\" : \"nodbconn\"}"
} }
val jdbc: JDBC = JDBC.getInstance()
when (params["action"]) { when (params["action"]) {
/** /**

View File

@ -15,11 +15,11 @@ import java.util.*
*/ */
class DeviceRequest : PostRequest() { class DeviceRequest : PostRequest() {
override fun request(params: HashMap<String, String>): String { override fun request(params: HashMap<String, String>): String {
val jdbc = JDBC.getInstance() if (!JDBC.isConnected()) {
if (!jdbc.isConnected) {
error("no connection to db") error("no connection to db")
return "{\"query\" : \"nodbconn\"}" return "{\"query\" : \"nodbconn\"}"
} }
val jdbc: JDBC = JDBC.getInstance()
val sb = StringBuilder() val sb = StringBuilder()
val deviceset: ResultSet val deviceset: ResultSet
@ -28,7 +28,7 @@ class DeviceRequest : PostRequest() {
* return all available devices * return all available devices
*/ */
"getdevices" -> { "getdevices" -> {
deviceset = jdbc!!.executeQuery("SELECT * FROM `devices") deviceset = jdbc.executeQuery("SELECT * FROM `devices")
sb.append("{\"data\":[") sb.append("{\"data\":[")
try { try {
while (deviceset.next()) { while (deviceset.next()) {
@ -67,7 +67,7 @@ class DeviceRequest : PostRequest() {
* returns all available city names * returns all available city names
*/ */
"getCitynames" -> { "getCitynames" -> {
deviceset = jdbc!!.executeQuery("select * from cities") deviceset = jdbc.executeQuery("select * from cities")
debug(deviceset.toString()) debug(deviceset.toString())
sb.append("{") sb.append("{")
try { try {
@ -91,7 +91,7 @@ class DeviceRequest : PostRequest() {
* returns all available zones for specified city * returns all available zones for specified city
*/ */
"getzones" -> { "getzones" -> {
deviceset = jdbc!!.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' ORDER BY zone ASC") deviceset = jdbc.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' ORDER BY zone ASC")
debug(deviceset.toString()) debug(deviceset.toString())
sb.append("{") sb.append("{")
try { try {
@ -114,7 +114,7 @@ class DeviceRequest : PostRequest() {
* returns all available waste types for specified zone and city * returns all available waste types for specified zone and city
*/ */
"gettypes" -> { "gettypes" -> {
deviceset = jdbc!!.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' ORDER BY zone ASC") deviceset = jdbc.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' ORDER BY zone ASC")
debug(deviceset.toString()) debug(deviceset.toString())
sb.append("{") sb.append("{")
try { try {
@ -137,7 +137,7 @@ class DeviceRequest : PostRequest() {
* configure device and save infos to db * configure device and save infos to db
*/ */
"savetodb" -> try { "savetodb" -> try {
val cityset = jdbc!!.executeQuery("SELECT id from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' AND `wastetype`='" + params["wastetype"] + "'") val cityset = jdbc.executeQuery("SELECT id from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' AND `wastetype`='" + params["wastetype"] + "'")
cityset.last() cityset.last()
if (cityset.row != 1) { if (cityset.row != 1) {
error("error saving device to db --> device multiply defined?") error("error saving device to db --> device multiply defined?")

View File

@ -17,11 +17,13 @@ class NewDateRequest : PostRequest() {
override fun request(params: HashMap<String, String>): String { override fun request(params: HashMap<String, String>): String {
val sb = StringBuilder() val sb = StringBuilder()
val set: ResultSet val set: ResultSet
val jdbc = JDBC.getInstance()
if (!jdbc.isConnected) { if (!JDBC.isConnected()) {
error("no connection to db") error("no connection to db")
return "{\"query\" : \"nodbconn\"}" return "{\"query\" : \"nodbconn\"}"
} }
val jdbc: JDBC = JDBC.getInstance()
when (params["action"]) { when (params["action"]) {
"getCitynames" -> { "getCitynames" -> {
set = jdbc.executeQuery("select * from cities") set = jdbc.executeQuery("select * from cities")

View File

@ -25,8 +25,8 @@ class LoginRequest : PostRequest() {
val username = params["username"] val username = params["username"]
val jdbc: JDBC = try { val jdbc: JDBC = try {
JDBC.getInstance() JDBC.getInstance()
} catch (e: IOException) { } catch (e: Exception) {
error("no connection to db") error("no connection to db" + e.printStackTrace())
return "{\"status\" : \"nodbconn\"}" return "{\"status\" : \"nodbconn\"}"
} }