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-03-13 15:13:53 +01:00
import kotlin.concurrent.scheduleAtFixedRate
2019-03-12 22:06:04 +01:00
@RestController
class APIController {
2019-03-13 19:12:05 +01:00
// Controller stuff
var logger : Logger = LoggerFactory . getLogger ( APIController :: class . java )
private val startTime = System . currentTimeMillis ( ) / 1000
private val apiVersion = " 1.0.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
// TODO we could use a first time delay
// update courses every 24 hours (time in ms)
Timer ( ) . scheduleAtFixedRate ( 0 , 86400000 ) {
asyncUpdateCourses ( )
}
2019-03-13 19:12:05 +01:00
// update courses every 3 hours (time in ms)
Timer ( ) . scheduleAtFixedRate ( 0 , 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)
Timer ( ) . scheduleAtFixedRate ( 0 , 10800000 ) {
asyncUpdateTimetables ( )
}
2019-03-12 22:06:04 +01:00
}
@RequestMapping ( " /courses " )
fun courses ( ) : CoursesList {
2019-03-13 19:12:05 +01:00
logger . info ( " courses request at ${LocalDateTime.now()} ! " )
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-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-15 23:57:31 +01:00
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
}
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/ " )
var connection = hsoURL . openConnection ( ) as HttpURLConnection
connection . requestMethod = " HEAD "
hsoCode = connection . responseCode
val swfrURL = URL ( " https://www.swfr.de/ " )
connection = swfrURL . openConnection ( ) as HttpURLConnection
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 ,
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
}
}
}
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
}
}