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-05-28 12:49:25 +02:00
import org.mosad.thecitadelofricks.CacheController.Companion.courseList
import org.mosad.thecitadelofricks.CacheController.Companion.mensaMenu
2019-06-03 12:20:01 +02:00
import org.mosad.thecitadelofricks.CacheController.Companion.timetableList
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-12 22:06:04 +01:00
@RestController
class APIController {
2019-05-28 12:49:25 +02:00
private val logger : Logger = LoggerFactory . getLogger ( APIController :: class . java )
private val cache = CacheController ( )
2019-03-12 22:06:04 +01:00
2019-10-05 17:17:26 +02:00
private val apiVersion = " 1.1.3 "
private val softwareVersion = " 1.1.5 "
2019-05-28 12:49:25 +02:00
private val startTime = System . currentTimeMillis ( ) / 1000
2019-03-13 19:12:05 +01:00
2019-10-05 17:17:26 +02:00
private var totalRequests = 0
private var mensaMenuRequests = 0
private var timetableRequests = ArrayList < TimetableCounter > ( )
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 " )
2019-05-28 12:49:25 +02:00
fun courses ( ) : CourseList {
2019-05-23 14:56:16 +02:00
return courseList ( )
}
@RequestMapping ( " /courseList " )
2019-05-28 12:49:25 +02:00
fun courseList ( ) : CourseList {
2019-05-23 14:56:16 +02:00
logger . info ( " courseList request at ${LocalDateTime.now()} ! " )
2019-10-05 17:17:26 +02:00
totalRequests ++
return courseList
2019-03-12 22:06:04 +01:00
}
@RequestMapping ( " /mensamenu " )
2019-05-28 12:49:25 +02:00
fun mensamenu ( ) : MensaMenu {
2019-03-13 19:12:05 +01:00
logger . info ( " mensamenu request at ${LocalDateTime.now()} ! " )
2019-10-05 17:17:26 +02:00
mensaMenuRequests ++
totalRequests ++
return mensaMenu
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-10-05 17:17:26 +02:00
updateTimetableRequests ( courseName )
totalRequests ++
2019-05-28 12:49:25 +02:00
return cache . getTimetable ( courseName , week )
2019-05-23 14:56:16 +02:00
}
@RequestMapping ( " /lessonSubjectList " )
fun lessonSubjectList (
@RequestParam ( value = " courseName " , defaultValue = " AI4 " ) courseName : String ,
@RequestParam ( value = " week " , defaultValue = " 0 " ) week : Int
) : HashSet < String > {
logger . info ( " lessonSubjectList request at ${LocalDateTime.now()} ! " )
2019-10-05 17:17:26 +02:00
totalRequests ++
2019-06-02 16:55:36 +02:00
return getLessonSubjectList ( courseName , week )
}
2019-10-05 17:17:26 +02:00
@RequestMapping ( " /lessons " )
2019-06-02 16:55:36 +02:00
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 > {
logger . info ( " lesson request at ${LocalDateTime.now()} ! " )
2019-10-05 17:17:26 +02:00
totalRequests ++
2019-06-02 16:55:36 +02:00
return getLesson ( courseName , lessonSubject , week )
}
@RequestMapping ( " /status " )
fun status ( ) : Status {
return getStatus ( )
}
// non direct api functions
2019-10-05 17:17:26 +02:00
/ * *
* get every explicit lesson in a week
* @param courseName the name of the course to be requested
* @param weekIndex request week number ( current week = 0 )
* @return a HashSet of explicit lessons for one week
* /
private fun getLessonSubjectList ( courseName : String , weekIndex : Int ) : HashSet < String > {
2019-06-02 16:55:36 +02:00
val lessonSubjectList = ArrayList < String > ( )
2019-05-23 14:56:16 +02:00
// get every lesson subject for the given week
2019-10-05 17:17:26 +02:00
val flatMap = cache . getTimetable ( courseName , weekIndex ) . timetable . days . flatMap { it . timeslots . asIterable ( ) }
2019-05-23 14:56:16 +02:00
flatMap . forEach {
it . stream ( ) . filter { x -> x . lessonSubject . isNotEmpty ( ) } . findAny ( ) . ifPresent { x -> lessonSubjectList . add ( x . lessonSubject ) }
}
return HashSet ( lessonSubjectList )
}
2019-10-05 17:17:26 +02:00
/ * *
* get every explicit lesson in a week
* @param courseName the name of the course to be requested
* @param lessonSubject the lesson subject to be requested
* @param weekIndex request week number ( current week = 0 )
* @return a ArrayList of every lesson with lessonSubject for one week
* /
private fun getLesson ( courseName : String , lessonSubject : String , weekIndex : Int ) : ArrayList < Lesson > {
2019-05-23 14:56:16 +02:00
val lessonList = ArrayList < Lesson > ( )
// get all lessons from the weeks timetable
2019-10-05 17:17:26 +02:00
val flatMap = cache . getTimetable ( courseName , weekIndex ) . timetable . days . flatMap { it . timeslots . asIterable ( ) }
2019-05-23 14:56:16 +02:00
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-10-05 17:17:26 +02:00
/ * *
* if a timetable is requested update the request counter
* /
private fun updateTimetableRequests ( courseName : String ) {
timetableRequests . stream ( ) . filter { it . courseName == courseName } . findFirst ( ) . ifPresentOrElse ( {
it . requests ++
} , {
timetableRequests . add ( TimetableCounter ( courseName , 1 ) )
} )
}
2019-06-02 16:55:36 +02:00
private fun getStatus ( ) : 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-06-03 12:20:01 +02:00
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-10-05 17:17:26 +02:00
totalRequests ,
mensaMenuRequests ,
timetableRequests ,
timetableList . size ,
2019-05-28 12:49:25 +02:00
Date ( courseList . meta . updateTime * 1000 ) ,
Date ( mensaMenu . meta . updateTime * 1000 ) ,
2019-03-13 19:12:05 +01:00
hsoCode ,
swfrCode
)
2019-03-13 15:13:53 +01:00
}
2019-03-12 22:06:04 +01:00
}