@ -23,10 +23,7 @@
package org.mosad.thecitadelofricks.controller
import com.google.gson.Gson
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.*
import org.mosad.thecitadelofricks.*
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
import org.mosad.thecitadelofricks.hsoparser.MensaParser
@ -39,6 +36,7 @@ import java.io.FileWriter
import java.util.*
import java.util.concurrent.Executors
import kotlin.collections.ArrayList
import kotlin.collections.HashMap
import kotlin.collections.HashSet
import kotlin.concurrent.scheduleAtFixedRate
@ -52,9 +50,9 @@ class CacheController {
companion object {
private val logger : Logger = LoggerFactory . getLogger ( CacheController :: class . java )
var courseList = CoursesList ( CoursesMeta ( 0 , 0 ) , ArrayList ( ) )
var courseList = CoursesList ( )
var mensaMenu = MensaMenu ( MensaMeta ( 0 , " " ) , MensaWeek ( ) , MensaWeek ( ) )
var timetableList = ArrayList< TimetableCourseWeek > ( ) // this list contains all timetables
var timetableList = HashMap< String , TimetableCourseWeek > ( ) // this list contains all timetables
/ * *
* get a timetable , since they may not be cached , we need to make sure it ' s cached , otherwise download
@ -63,30 +61,22 @@ class CacheController {
* @return timetable of the course ( Type : [ TimetableCourseWeek ] )
* /
fun getTimetable ( courseName : String , weekIndex : Int ) : TimetableCourseWeek = runBlocking {
val currentTime = System . currentTimeMillis ( ) / 1000
var timetable = TimetableWeek ( )
var weekNumberYear = 0
// check if the timetable already exists and is up to date
when ( timetableList . stream ( ) . filter { x -> x . meta . courseName == courseName && x . meta . weekIndex == weekIndex } . findAny ( ) . orElse ( null ) ) {
// there is no such course yet, create one
null -> {
val courseLink = courseList . courses . stream ( ) . filter { x -> x . courseName == courseName } . findFirst ( ) . orElse ( null ) . courseLink
val timetableLink = courseLink . replace ( " week=0 " , " week= $weekIndex " )
val jobTimetable = async {
timetable = TimetableParser ( ) . getTimeTable ( timetableLink )
weekNumberYear = TimetableParser ( ) . getWeekNumberYear ( timetableLink )
}
jobTimetable . await ( )
timetableList . add ( TimetableCourseWeek ( TimetableCourseMeta ( currentTime , courseName , weekIndex , weekNumberYear , timetableLink ) , timetable ) )
logger . info ( " added new timetable for $courseName , week $weekIndex " )
}
return @runBlocking timetableList . getOrPut ( " $courseName - $weekIndex " ) {
val timetableLink = courseList . courses [ courseName ]
?. courseLink
?. replace ( " week=0 " , " week= $weekIndex " ) ?: " "
val currentTime = System . currentTimeMillis ( ) / 1000
var timetable = TimetableWeek ( )
var weekNumberYear = 0
val timetableJobs = listOf (
async { timetable = TimetableParser ( ) . getTimeTable ( timetableLink ) } ,
async { weekNumberYear = TimetableParser ( ) . getWeekNumberYear ( timetableLink ) }
)
timetableJobs . awaitAll ( )
TimetableCourseWeek ( TimetableCourseMeta ( currentTime , courseName , weekIndex , weekNumberYear , timetableLink ) , timetable )
}
return @runBlocking timetableList . stream ( ) . filter { x -> x . meta . courseName == courseName && x . meta . weekIndex == weekIndex } . findAny ( ) . orElse ( null )
}
/ * *
@ -170,10 +160,10 @@ class CacheController {
try {
timetableList . forEach { timetableCourse ->
executor . execute {
timetableCourse . timetable = TimetableParser ( ) . getTimeTable ( timetableCours e. meta . link )
timetableCourse . meta . updateTime = System . currentTimeMillis ( ) / 1000
timetableCourse . value . timetable = TimetableParser ( ) . getTimeTable ( timetableCours e. valu e. meta . link )
timetableCourse . value . meta . updateTime = System . currentTimeMillis ( ) / 1000
saveTimetableToCache ( timetableCourse ) // save the updated timetable to the cache directory
saveTimetableToCache ( timetableCourse .value ) // save the updated timetable to the cache directory
}
}