correct error handling of no db and mqtt connection.
This commit is contained in:
parent
cf9069e489
commit
14eea02dcf
@ -25,6 +25,7 @@ fun main() {
|
||||
try {
|
||||
Thread.sleep(200)
|
||||
Log.warning("Shutting down ...")
|
||||
JDBC.getInstance().disconnect();
|
||||
//shutdown routine
|
||||
} catch (e: InterruptedException) {
|
||||
e.printStackTrace()
|
||||
@ -37,9 +38,11 @@ fun main() {
|
||||
//initial connect to db
|
||||
Log.message("initial login to db")
|
||||
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);
|
||||
} catch (e: IOException) { //e.printStackTrace();
|
||||
} catch (e: IOException) {
|
||||
Log.error("no connection to db")
|
||||
}
|
||||
|
||||
@ -52,6 +55,12 @@ fun main() {
|
||||
//startup 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()
|
||||
}else{
|
||||
Log.error("could't start mqtt service because of missing db connection!")
|
||||
}
|
||||
|
||||
}
|
@ -21,37 +21,43 @@ class Storage {
|
||||
}
|
||||
}
|
||||
|
||||
private var mqttServer: String = ""
|
||||
var mqttServer: String = ""
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
private var mqttPort: Int = -1
|
||||
var mqttPort: Int = -1
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
private var dbName: String = ""
|
||||
var dbName: String = ""
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
private var dbUser: String = ""
|
||||
var dbhost: String = ""
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
private var dbPassword: String = ""
|
||||
var dbUser: String = ""
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
private var dbPort: Int = -1
|
||||
var dbPassword: String = ""
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
}
|
||||
|
||||
var dbPort: Int = -1
|
||||
get() = field
|
||||
set(value) {
|
||||
field = value
|
||||
@ -70,6 +76,7 @@ class Storage {
|
||||
|
||||
mqttServer = prop["mqttserver"] as String
|
||||
mqttPort = (prop["mqttport"] as String).toInt()
|
||||
dbhost = prop["dbhost"] as String
|
||||
dbName = prop["dbname"] as String
|
||||
dbUser = prop["dbuser"] as String
|
||||
dbPassword = prop["dbpass"] as String
|
||||
@ -89,12 +96,13 @@ class Storage {
|
||||
fun store() {
|
||||
prop["mqttserver"] = mqttServer
|
||||
prop["mqttport"] = mqttPort.toString()
|
||||
prop["dbhost"] = dbhost
|
||||
prop["dbname"] = dbName
|
||||
prop["dbuser"] = dbUser
|
||||
prop["dbpass"] = dbPassword
|
||||
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
|
||||
*/
|
||||
fun isEveryThingDefined(): Boolean {
|
||||
return (mqttServer != "" &&
|
||||
mqttPort != 0 &&
|
||||
dbName != "" &&
|
||||
dbUser != "" &&
|
||||
dbPassword != "" &&
|
||||
dbPort != -1)
|
||||
return (isMqttServerDefined() &&
|
||||
isMqttPortDefined() &&
|
||||
isDBNameDefined() &&
|
||||
isDBUsernameDefined() &&
|
||||
isDBPasswdDefined() &&
|
||||
isDBPortDefined())
|
||||
}
|
||||
|
||||
|
||||
|
@ -82,7 +82,14 @@ public class JDBC {
|
||||
throw new IOException("No connection to database");
|
||||
// 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();
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
public static boolean isConnected() {
|
||||
return loggedin;
|
||||
}
|
||||
}
|
||||
|
@ -22,11 +22,12 @@ class DataRequest : PostRequest() {
|
||||
val sb = StringBuilder()
|
||||
var set: ResultSet?
|
||||
var status = -1
|
||||
val jdbc: JDBC = JDBC.getInstance()
|
||||
if (!jdbc.isConnected) {
|
||||
|
||||
if (!JDBC.isConnected()) {
|
||||
error("no connection to db")
|
||||
return "{\"query\" : \"nodbconn\"}"
|
||||
}
|
||||
val jdbc: JDBC = JDBC.getInstance()
|
||||
|
||||
when (params["action"]) {
|
||||
/**
|
||||
|
@ -15,11 +15,11 @@ import java.util.*
|
||||
*/
|
||||
class DeviceRequest : PostRequest() {
|
||||
override fun request(params: HashMap<String, String>): String {
|
||||
val jdbc = JDBC.getInstance()
|
||||
if (!jdbc.isConnected) {
|
||||
if (!JDBC.isConnected()) {
|
||||
error("no connection to db")
|
||||
return "{\"query\" : \"nodbconn\"}"
|
||||
}
|
||||
val jdbc: JDBC = JDBC.getInstance()
|
||||
|
||||
val sb = StringBuilder()
|
||||
val deviceset: ResultSet
|
||||
@ -28,7 +28,7 @@ class DeviceRequest : PostRequest() {
|
||||
* return all available devices
|
||||
*/
|
||||
"getdevices" -> {
|
||||
deviceset = jdbc!!.executeQuery("SELECT * FROM `devices")
|
||||
deviceset = jdbc.executeQuery("SELECT * FROM `devices")
|
||||
sb.append("{\"data\":[")
|
||||
try {
|
||||
while (deviceset.next()) {
|
||||
@ -67,7 +67,7 @@ class DeviceRequest : PostRequest() {
|
||||
* returns all available city names
|
||||
*/
|
||||
"getCitynames" -> {
|
||||
deviceset = jdbc!!.executeQuery("select * from cities")
|
||||
deviceset = jdbc.executeQuery("select * from cities")
|
||||
debug(deviceset.toString())
|
||||
sb.append("{")
|
||||
try {
|
||||
@ -91,7 +91,7 @@ class DeviceRequest : PostRequest() {
|
||||
* returns all available zones for specified city
|
||||
*/
|
||||
"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())
|
||||
sb.append("{")
|
||||
try {
|
||||
@ -114,7 +114,7 @@ class DeviceRequest : PostRequest() {
|
||||
* returns all available waste types for specified zone and city
|
||||
*/
|
||||
"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())
|
||||
sb.append("{")
|
||||
try {
|
||||
@ -137,7 +137,7 @@ class DeviceRequest : PostRequest() {
|
||||
* configure device and save infos to db
|
||||
*/
|
||||
"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()
|
||||
if (cityset.row != 1) {
|
||||
error("error saving device to db --> device multiply defined?")
|
||||
|
@ -17,11 +17,13 @@ class NewDateRequest : PostRequest() {
|
||||
override fun request(params: HashMap<String, String>): String {
|
||||
val sb = StringBuilder()
|
||||
val set: ResultSet
|
||||
val jdbc = JDBC.getInstance()
|
||||
if (!jdbc.isConnected) {
|
||||
|
||||
if (!JDBC.isConnected()) {
|
||||
error("no connection to db")
|
||||
return "{\"query\" : \"nodbconn\"}"
|
||||
}
|
||||
val jdbc: JDBC = JDBC.getInstance()
|
||||
|
||||
when (params["action"]) {
|
||||
"getCitynames" -> {
|
||||
set = jdbc.executeQuery("select * from cities")
|
||||
|
@ -25,8 +25,8 @@ class LoginRequest : PostRequest() {
|
||||
val username = params["username"]
|
||||
val jdbc: JDBC = try {
|
||||
JDBC.getInstance()
|
||||
} catch (e: IOException) {
|
||||
error("no connection to db")
|
||||
} catch (e: Exception) {
|
||||
error("no connection to db" + e.printStackTrace())
|
||||
return "{\"status\" : \"nodbconn\"}"
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user