getter setter

all parameters in storage class
This commit is contained in:
lukas-heiligenbrunner 2020-03-27 10:33:14 +01:00
parent cded7c7d27
commit 3818b6d1c7

View File

@ -15,32 +15,135 @@ import java.util.*
*/
class Storage {
companion object {
val obj = Storage()
private val obj = Storage()
fun getInstance(): Storage {
return obj;
}
}
var mqttServer: String = "";
private var mqttServer: String = ""
get() = field
set(value) {
field = value
}
private var mqttPort: Int = -1
get() = field
set(value) {
field = value
}
private var dbName: String = ""
get() = field
set(value) {
field = value
}
private var dbUser: String = ""
get() = field
set(value) {
field = value
}
private var dbPassword: String = ""
get() = field
set(value) {
field = value
}
private var dbPort: Int = -1
get() = field
set(value) {
field = value
}
private val prop = Properties()
/**
* init config file
*/
fun init() {
val prop = Properties()
try {
// try to load existing config file
val inp = FileInputStream("settings.prop")
prop.load(inp)
mqttServer = prop.get("mqttserver") as String
} catch (ee: FileNotFoundException) {
// file not generated yet
prop.store(FileOutputStream("settings.prop"), "")
store()
Log.info("new Settings config file generated")
} catch (e: IOException) {
e.printStackTrace()
}
}
/**
* store data to storage file
*/
fun store() {
prop["mqttserver"] = mqttServer
prop["mqttport"] = mqttPort
prop["dbname"] = dbName
prop["dbuser"] = dbUser
prop["dbpass"] = dbPassword
prop["dbport"] = dbPort
prop.store(FileOutputStream("settings.prop"), "")
}
/**
* check if all needed properties are set up correctly
*/
fun isEveryThingDefined(): Boolean {
return (mqttServer != "" &&
mqttPort != 0 &&
dbName != "" &&
dbUser != "" &&
dbPassword != "" &&
dbPort != -1)
}
/**
* is the mqttservername defined?
*/
fun isMqttServerDefined(): Boolean {
return (mqttServer != "")
}
/**
* is the mqttserver port defined?
*/
fun isMqttPortDefined(): Boolean {
return (mqttPort != -1)
}
/**
* is the dbname defined?
*/
fun isDBNameDefined(): Boolean {
return (dbName != "")
}
/**
* is the dbport defined?
*/
fun isDBPortDefined(): Boolean {
return (dbPort != -1)
}
/**
* is the dbpassword defined?
*/
fun isDBPasswdDefined(): Boolean {
return (dbPassword != "")
}
/**
* is the dbusername defined?
*/
fun isDBUsernameDefined(): Boolean {
return (dbUser != "")
}
}