2019-03-12 22:06:04 +01:00
/ * *
* TheCitadelofRicks
*
* Copyright 2019 < seil0 @mosad . xyz >
*
* 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
2019-03-13 19:12:05 +01:00
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
2019-03-12 22:06:04 +01:00
import org.mosad.thecitadelofricks.hsoparser.CourseListParser
import org.mosad.thecitadelofricks.hsoparser.MensaParser
import org.mosad.thecitadelofricks.hsoparser.TimetableParser
2019-03-13 19:12:05 +01:00
import org.slf4j.Logger
import org.slf4j.LoggerFactory
2019-03-12 22:06:04 +01:00
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
2019-03-13 19:12:05 +01:00
import java.net.HttpURLConnection
import java.net.URL
import java.time.LocalDateTime
2019-03-13 15:13:53 +01:00
import java.util.*
2019-03-15 23:57:31 +01:00
import kotlin.collections.ArrayList
2019-05-23 14:56:16 +02:00
import kotlin.collections.HashSet
2019-03-13 15:13:53 +01:00
import kotlin.concurrent.scheduleAtFixedRate
2019-03-12 22:06:04 +01:00
@RestController
class APIController {
2019-05-25 19:37:04 +02:00
// TODO clean up and move stuff to a CacheController
2019-03-13 19:12:05 +01:00
// Controller stuff
var logger : Logger = LoggerFactory . getLogger ( APIController :: class . java )
2019-03-18 19:11:20 +01:00
private var requestCount = 0
2019-03-13 19:12:05 +01:00
private val startTime = System . currentTimeMillis ( ) / 1000
2019-05-25 19:37:04 +02:00
private val softwareVersion = " 1.1.0 "
private val apiVersion = " 1.1.0 "
2019-03-13 15:13:53 +01:00
2019-03-13 19:12:05 +01:00
// hso parser links (hardcoded)
2019-03-12 22:06:04 +01:00
private val mensaLink = " https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/ "
private val mensaName = " Offenburg "
2019-03-13 19:12:05 +01:00
// cache objects
2019-03-12 22:06:04 +01:00
private var coursesLinkList = ArrayList < Course > ( )
private var coursesLastUpdate : Long = 0
2019-03-15 23:57:31 +01:00
private var timetableList = ArrayList < TimetableCourseWeek > ( ) // this list contains all timetables
2019-03-12 22:06:04 +01:00
private var mensaCurrentWeek = MensaWeek ( )
private var mensaNextWeek = MensaWeek ( )
private var mensaLastUpdate : Long = 0
init {
2019-03-13 15:13:53 +01:00
initUpdates ( ) // without this 5-10 seconds after startup the response will be empty
2019-03-18 16:13:13 +01:00
val currentTime = System . currentTimeMillis ( )
val delay24h = ( 86400000 - ( ( currentTime + 3600000 ) % 86400000 ) ) + 60000
val delay3h = ( 10800000 - ( ( currentTime + 3600000 ) % 10800000 ) ) + 60000
2019-03-13 15:13:53 +01:00
// update courses every 24 hours (time in ms)
2019-03-18 16:13:13 +01:00
Timer ( ) . scheduleAtFixedRate ( delay24h , 86400000 ) {
2019-03-13 15:13:53 +01:00
asyncUpdateCourses ( )
}
2019-03-13 19:12:05 +01:00
// update courses every 3 hours (time in ms)
2019-03-18 16:13:13 +01:00
Timer ( ) . scheduleAtFixedRate ( delay3h , 10800000 ) {
2019-03-13 15:13:53 +01:00
asyncUpdateMensa ( )
}
2019-03-13 19:12:05 +01:00
// update all already existing timetables every 3 hours (time in ms)
2019-03-18 16:13:13 +01:00
Timer ( ) . scheduleAtFixedRate ( delay3h , 10800000 ) {
2019-03-13 19:12:05 +01:00
asyncUpdateTimetables ( )
}
2019-03-12 22:06:04 +01:00
}
2019-05-25 19:37:04 +02:00
@Deprecated ( " courses is replaced by courseList " , replaceWith = ReplaceWith ( " courseList() " ) )
2019-03-12 22:06:04 +01:00
@RequestMapping ( " /courses " )
fun courses ( ) : CoursesList {
2019-05-23 14:56:16 +02:00
return courseList ( )
}
@RequestMapping ( " /courseList " )
fun courseList ( ) : CoursesList {
logger . info ( " courseList request at ${LocalDateTime.now()} ! " )
2019-03-18 19:11:20 +01:00
requestCount ++
2019-03-16 00:00:34 +01:00
return CoursesList ( CoursesMeta ( coursesLastUpdate , coursesLinkList . size ) , coursesLinkList )
2019-03-12 22:06:04 +01:00
}
@RequestMapping ( " /mensamenu " )
fun mensamenu ( ) : Mensa {
2019-03-13 19:12:05 +01:00
logger . info ( " mensamenu request at ${LocalDateTime.now()} ! " )
2019-03-18 19:11:20 +01:00
requestCount ++
2019-03-16 00:00:34 +01:00
return Mensa ( MensaMeta ( mensaLastUpdate , mensaName ) , mensaCurrentWeek , mensaNextWeek )
2019-03-12 22:06:04 +01:00
}
@RequestMapping ( " /timetable " )
2019-03-15 23:57:31 +01:00
fun timetable (
@RequestParam ( value = " courseName " , defaultValue = " AI4 " ) courseName : String ,
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : TimetableCourseWeek {
2019-03-13 19:12:05 +01:00
logger . info ( " timetable request at ${LocalDateTime.now()} ! " )
2019-03-18 19:11:20 +01:00
requestCount ++
2019-05-23 14:56:16 +02:00
return getTimetable ( courseName , week )
}
@RequestMapping ( " /lessonSubjectList " )
fun lessonSubjectList (
@RequestParam ( value = " courseName " , defaultValue = " AI4 " ) courseName : String ,
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : HashSet < String > {
val lessonSubjectList = ArrayList < String > ( )
logger . info ( " lessonSubjectList request at ${LocalDateTime.now()} ! " )
requestCount ++
// get every lesson subject for the given week
val flatMap = getTimetable ( courseName , week ) . timetable . days . flatMap { it . timeslots . asIterable ( ) }
flatMap . forEach {
it . stream ( ) . filter { x -> x . lessonSubject . isNotEmpty ( ) } . findAny ( ) . ifPresent { x -> lessonSubjectList . add ( x . lessonSubject ) }
}
return HashSet ( lessonSubjectList )
}
@RequestMapping ( " /lesson " )
fun lesson (
@RequestParam ( value = " courseName " , defaultValue = " AI4 " ) courseName : String ,
@RequestParam ( value = " lessonSubject " , defaultValue = " Mathematik 4 " ) lessonSubject : String ,
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : ArrayList < Lesson > {
val lessonList = ArrayList < Lesson > ( )
// get all lessons from the weeks timetable
val flatMap = getTimetable ( courseName , week ) . timetable . days . flatMap { it . timeslots . asIterable ( ) }
flatMap . forEach {
it . forEach { lesson ->
if ( lesson . lessonSubject . contains ( lessonSubject ) ) {
lessonList . add ( lesson )
}
}
//it.stream().filter { x -> x.lessonSubject.contains(lessonSubject) }.findAny().ifPresent { x -> println("${x.lessonSubject}, ${x.lessonTeacher}") }
}
return lessonList
2019-03-12 22:06:04 +01:00
}
2019-03-13 15:13:53 +01:00
@RequestMapping ( " /status " )
fun status ( ) : Status {
2019-03-13 19:12:05 +01:00
val currentTime = System . currentTimeMillis ( ) / 1000
val minutes = ( currentTime - startTime ) % 3600 / 60
val hours = ( currentTime - startTime ) % 86400 / 3600
val days = ( currentTime - startTime ) / 86400
2019-03-13 15:13:53 +01:00
var hsoCode = 999
var swfrCode = 999
2019-03-13 19:12:05 +01:00
logger . info ( " status request at ${LocalDateTime.now()} ! " )
2019-03-13 15:13:53 +01:00
try {
val hsoURL = URL ( " https://www.hs-offenburg.de/ " )
2019-05-25 19:37:04 +02:00
val swfrURL = URL ( " https://www.swfr.de/ " )
2019-03-13 15:13:53 +01:00
var connection = hsoURL . openConnection ( ) as HttpURLConnection
connection . requestMethod = " HEAD "
2019-05-25 19:37:04 +02:00
connection . connectTimeout = 15000
2019-03-13 15:13:53 +01:00
hsoCode = connection . responseCode
2019-05-25 19:37:04 +02:00
2019-03-13 15:13:53 +01:00
connection = swfrURL . openConnection ( ) as HttpURLConnection
2019-05-25 19:37:04 +02:00
connection . connectTimeout = 15000
2019-03-13 15:13:53 +01:00
swfrCode = connection . responseCode
} catch ( e : Exception ) {
2019-03-13 19:12:05 +01:00
logger . error ( " Error while fetching url response codes! " , e )
2019-03-13 15:13:53 +01:00
}
2019-03-13 19:12:05 +01:00
return Status (
LocalDateTime . now ( ) ,
" $days days, $hours : $minutes " ,
apiVersion ,
2019-03-18 16:13:13 +01:00
softwareVersion ,
2019-03-18 19:11:20 +01:00
requestCount ,
2019-03-13 19:12:05 +01:00
Date ( coursesLastUpdate * 1000 ) ,
Date ( mensaLastUpdate * 1000 ) ,
hsoCode ,
swfrCode
)
2019-03-13 15:13:53 +01:00
}
2019-03-12 22:06:04 +01:00
/ * *
2019-03-13 19:12:05 +01:00
* this function updates the courses list
2019-03-12 22:06:04 +01:00
* during the update process the old data will be returned for a API request
* /
2019-03-13 15:13:53 +01:00
private fun asyncUpdateCourses ( ) = GlobalScope . launch {
coursesLinkList = CourseListParser ( ) . getCourseLinks ( )
coursesLastUpdate = System . currentTimeMillis ( ) / 1000
2019-03-13 19:12:05 +01:00
logger . info ( " updated courses successful at ${Date(coursesLastUpdate * 1000)} " )
2019-03-12 22:06:04 +01:00
}
/ * *
2019-03-13 19:12:05 +01:00
* this function updates the mensa menu list
2019-03-12 22:06:04 +01:00
* during the update process the old data will be returned for a API request
* /
2019-03-13 15:13:53 +01:00
private fun asyncUpdateMensa ( ) = GlobalScope . launch {
mensaCurrentWeek = MensaParser ( ) . getMensaMenu ( mensaLink )
mensaNextWeek = MensaParser ( ) . getMensaMenu ( MensaParser ( ) . getMenuLinkNextWeek ( mensaLink ) )
mensaLastUpdate = System . currentTimeMillis ( ) / 1000
2019-03-13 19:12:05 +01:00
logger . info ( " updated mensamenu successful at ${Date(mensaLastUpdate * 1000)} " )
}
/ * *
* this function updates all existing timetables
* during the update process the old data will be returned for a API request
* /
private fun asyncUpdateTimetables ( ) = GlobalScope . launch {
timetableList . forEach { timetableCourse ->
2019-03-15 23:57:31 +01:00
val updateURL = timetableCourse . meta . link
timetableCourse . timetable = TimetableParser ( ) . getTimeTable ( updateURL )
timetableCourse . meta . updateTime = System . currentTimeMillis ( ) / 1000
2019-03-13 19:12:05 +01:00
}
logger . info ( " updated ${timetableList.size} timetables successful! " )
2019-03-12 22:06:04 +01:00
}
/ * *
* this function checks if we need to update the timetable for a given course and if so does it
* during the update process the old data will be returned for a API request
* update if the last update was 6 hours ago
* /
2019-03-15 23:57:31 +01:00
private fun checkTimetableCourse ( courseName : String , week : Int ) = runBlocking {
2019-03-12 22:06:04 +01:00
val currentTime = System . currentTimeMillis ( ) / 1000
2019-03-15 23:57:31 +01:00
var timetable = TimetableWeek ( )
2019-03-12 22:06:04 +01:00
// check if the timetable already exists and is up to date
2019-03-15 23:57:31 +01:00
val result = timetableList . stream ( ) . filter { x -> x . meta . courseName == courseName && x . meta . week == week } . findAny ( ) . orElse ( null )
2019-03-12 22:06:04 +01:00
2019-03-13 19:12:05 +01:00
when ( result ) {
2019-03-12 22:06:04 +01:00
// there is no such course yet, create one
2019-03-13 19:12:05 +01:00
null -> {
2019-03-12 22:06:04 +01:00
val courseLink = coursesLinkList . stream ( ) . filter { x -> x . courseName == courseName } . findFirst ( ) . orElse ( null ) . courseLink
2019-03-15 23:57:31 +01:00
val timetableMeta = TimetableCourseMeta ( currentTime , courseName , week , courseLink . replace ( " week=0 " , " week= $week " ) )
2019-03-12 22:06:04 +01:00
2019-03-15 23:57:31 +01:00
val jobTimetable = GlobalScope . async {
timetable = TimetableParser ( ) . getTimeTable ( timetableMeta . link )
2019-03-12 22:06:04 +01:00
}
2019-03-15 23:57:31 +01:00
jobTimetable . await ( )
2019-03-12 22:06:04 +01:00
2019-03-15 23:57:31 +01:00
timetableList . add ( TimetableCourseWeek ( timetableMeta , timetable ) )
logger . info ( " added new timetable for $courseName , week $week " )
2019-03-12 22:06:04 +01:00
}
}
}
2019-05-23 14:56:16 +02:00
private fun getTimetable ( courseName : String , week : Int ) : TimetableCourseWeek {
checkTimetableCourse ( courseName , week ) // check if we need to update and perform the update if so
return timetableList . stream ( ) . filter { x -> x . meta . courseName == courseName && x . meta . week == week } . findAny ( ) . orElse ( null )
}
2019-03-12 22:06:04 +01:00
private fun initUpdates ( ) = runBlocking {
// get all courses on startup
val jobCourseUpdate = GlobalScope . async {
coursesLinkList = CourseListParser ( ) . getCourseLinks ( )
coursesLastUpdate = System . currentTimeMillis ( ) / 1000
}
// get the current and next weeks mensa menus
val jobCurrentMensa = GlobalScope . async {
mensaCurrentWeek = MensaParser ( ) . getMensaMenu ( mensaLink )
}
val jobNextMensa = GlobalScope . async {
mensaCurrentWeek = MensaParser ( ) . getMensaMenu ( mensaLink )
mensaLastUpdate = System . currentTimeMillis ( ) / 1000
}
jobCourseUpdate . await ( )
jobCurrentMensa . await ( )
jobNextMensa . await ( )
2019-03-13 19:12:05 +01:00
logger . info ( " init updates successful " )
2019-03-12 22:06:04 +01:00
}
}