2019-10-28 18:39:44 +01:00
/ * *
* TheCitadelofRicks
*
2020-01-15 13:42:40 +01:00
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
2019-10-28 18:39:44 +01:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program ; if not , write to the Free Software
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston ,
* MA 02110 - 1301 , USA .
*
* /
package org.mosad.thecitadelofricks.controller
2019-11-14 18:43:00 +01:00
import com.google.gson.Gson
import com.google.gson.JsonParser
import org.mosad.thecitadelofricks.TimetableCourseWeek
2019-10-28 18:39:44 +01:00
import org.slf4j.Logger
import org.slf4j.LoggerFactory
2019-11-14 18:43:00 +01:00
import java.io.*
2019-10-28 18:39:44 +01:00
import java.util.*
class StartupController {
private val logger : Logger = LoggerFactory . getLogger ( CacheController :: class . java )
2019-11-14 18:43:00 +01:00
companion object {
2020-01-15 13:42:40 +01:00
val userHome : String = System . getProperty ( " user.dir " )
val tcorHome = " $userHome /tcor "
2019-11-14 18:43:00 +01:00
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/ "
2023-09-25 15:33:42 +02:00
val roomListURL = " https://www.hs-offenburg.de/die-hochschule/organisation/infos-services/raumbelegungen "
2022-12-19 20:30:53 +01:00
var mensaMenuURL = " https://www.swfr.de/essen/mensen-cafes-speiseplaene/mensa-offenburg "
2019-11-14 18:43:00 +01:00
var mensaName = " Offenburg "
}
2019-10-28 18:39:44 +01:00
init {
// if the tcor directory doesn't exist, create it
if ( ! dirTcorHome . exists ( ) ) {
dirTcorHome . mkdir ( )
}
// if the cache directory doesn't exist, create it
if ( ! dirTcorCache . exists ( ) ) {
dirTcorCache . mkdir ( )
}
// check if the config file exist, if so load it
if ( fileConfig . exists ( ) ) {
loadConfig ( )
} else {
createConfig ( )
}
2019-11-14 18:43:00 +01:00
2019-11-21 14:58:34 +01:00
// read cached timetable files, as they are not initially cached
2019-11-14 18:43:00 +01:00
readCachedTimetables ( )
2019-10-28 18:39:44 +01:00
}
2019-11-14 18:43:00 +01:00
/ * *
* load the config stored in the config . xml file
* /
2019-10-28 18:39:44 +01:00
private fun loadConfig ( ) = try {
val properties = Properties ( )
properties . loadFromXML ( FileInputStream ( fileConfig ) )
2022-12-19 20:26:45 +01:00
try {
cachetAPIKey = properties . getProperty ( " cachetAPIKey " )
} catch ( _ : Exception ) { }
2019-10-28 18:39:44 +01:00
2022-12-19 20:26:45 +01:00
try {
cachetBaseURL = properties . getProperty ( " cachetBaseURL " )
} catch ( _ : Exception ) { }
2019-10-28 18:39:44 +01:00
2022-12-19 20:26:45 +01:00
try {
mensaMenuURL = properties . getProperty ( " mensaMenuURL " )
} catch ( _ : Exception ) { }
2019-10-28 18:39:44 +01:00
2022-12-19 20:26:45 +01:00
try {
mensaName = properties . getProperty ( " mensaName " )
} catch ( _ : Exception ) { }
2019-10-28 18:39:44 +01:00
} catch ( ex : Exception ) {
logger . error ( " error while loading config " , ex )
}
2019-11-14 18:43:00 +01:00
/ * *
* create an initial config file
* /
2019-10-28 18:39:44 +01:00
private fun createConfig ( ) = try {
val properties = Properties ( )
2022-12-19 20:26:45 +01:00
properties . setProperty ( " cachetAPIKey " , cachetAPIKey )
properties . setProperty ( " cachetBaseURL " , cachetBaseURL )
properties . setProperty ( " mensaMenuURL " , mensaMenuURL )
properties . setProperty ( " mensaName " , mensaName )
2019-10-28 18:39:44 +01:00
val outputStream = FileOutputStream ( fileConfig )
properties . storeToXML ( outputStream , " tcor configuration " )
outputStream . close ( )
} catch ( ex : Exception ) {
logger . error ( " error while creating config " , ex )
}
2019-11-14 18:43:00 +01:00
/ * *
* read all previously cached timetables
* /
private fun readCachedTimetables ( ) {
dirTcorCache . walkTopDown ( ) . forEach {
if ( it . isFile && it . name . endsWith ( " .json " ) ) {
2020-02-16 15:18:52 +01:00
val fileReader = FileReader ( it )
val bufferedReader = BufferedReader ( fileReader )
2019-11-14 18:43:00 +01:00
try {
val timetableObject = JsonParser . parseString ( bufferedReader . readLine ( ) ) . asJsonObject
2020-06-06 23:07:23 +02:00
val timetable = Gson ( ) . fromJson ( timetableObject , TimetableCourseWeek ( ) . javaClass )
2021-10-13 16:39:01 +02:00
CacheController . timetableList [ " ${timetable.meta.courseName} - ${timetable.meta.weekIndex} " ] =
timetable
2019-11-14 18:43:00 +01:00
} catch ( ex : Exception ) {
logger . error ( " error while reading cache " , ex )
2020-02-16 15:18:52 +01:00
} finally {
bufferedReader . close ( )
fileReader . close ( )
2019-11-14 18:43:00 +01:00
}
}
2019-10-28 18:39:44 +01:00
}
}
2021-10-11 10:18:42 +02:00
}