added on disk cache for the timetables
All checks were successful
continuous-integration/drone/push Build is passing

* update spring-boot 2.1.9 -> 2.1.10
This commit is contained in:
2019-11-14 18:43:00 +01:00
parent dd064d63af
commit 697f5e3167
5 changed files with 89 additions and 64 deletions

View File

@ -22,22 +22,31 @@
package org.mosad.thecitadelofricks.controller
import com.google.gson.Gson
import com.google.gson.JsonParser
import org.mosad.thecitadelofricks.TimetableCourseWeek
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.*
import java.util.*
class StartupController {
private val logger: Logger = LoggerFactory.getLogger(CacheController::class.java)
private val userHome = System.getProperty("user.home")
private val tcorHome = "$userHome/.tcor"
private val dirTcorHome = File(tcorHome)
private val dirTcorCache = File("$tcorHome/cache")
private val fileConfig = File("$tcorHome/config.xml")
companion object {
val userHome: String = System.getProperty("user.home")
val tcorHome = "$userHome/.tcor"
val dirTcorHome = File(tcorHome)
val dirTcorCache = File("$tcorHome/cache")
val fileConfig = File("$tcorHome/config.xml")
var cachetAPIKey = "0"
var cachetBaseURL = "https://status.mosad.xyz"
var courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
var mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
var mensaName = "Offenburg"
}
init {
// if the tcor directory doesn't exist, create it
@ -56,8 +65,14 @@ class StartupController {
} else {
createConfig()
}
// TODO read cached timetable files, as they are not initially cached
readCachedTimetables()
}
/**
* load the config stored in the config.xml file
*/
private fun loadConfig() = try {
val properties = Properties()
properties.loadFromXML(FileInputStream(fileConfig))
@ -90,6 +105,9 @@ class StartupController {
logger.error("error while loading config", ex)
}
/**
* create an initial config file
*/
private fun createConfig() = try {
val properties = Properties()
@ -105,32 +123,24 @@ class StartupController {
logger.error("error while creating config", ex)
}
companion object {
private var cachetAPIKey = "0"
private var cachetBaseURL = "https://status.mosad.xyz"
private var courseListURL = "https://www.hs-offenburg.de/studium/vorlesungsplaene/"
private var mensaMenuURL = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/"
private var mensaName = "Offenburg"
/**
* read all previously cached timetables
*/
private fun readCachedTimetables() {
dirTcorCache.walkTopDown().forEach {
if (it.isFile && it.name.endsWith(".json")) {
try {
val fileReader = FileReader(it)
val bufferedReader = BufferedReader(fileReader)
val timetableObject = JsonParser.parseString(bufferedReader.readLine()).asJsonObject
fun getCachetAPIKey(): String {
return cachetAPIKey
CacheController.timetableList.add(Gson().fromJson(timetableObject, TimetableCourseWeek().javaClass))
bufferedReader.close()
fileReader.close()
} catch (ex: Exception) {
logger.error("error while reading cache", ex)
}
}
}
fun getCachetBaseURL(): String {
return cachetBaseURL
}
fun getCourseListURL(): String {
return courseListURL
}
fun getMensaMenuURL(): String {
return mensaMenuURL
}
fun getMensaName(): String {
return mensaName
}
}
}