diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt b/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt index d169846..d5d07a1 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/APIController.kt @@ -29,12 +29,18 @@ import org.mosad.thecitadelofricks.hsoparser.TimetableParser import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController -import java.util.ArrayList +import java.lang.Exception +import java.net.* +import java.util.* +import kotlin.concurrent.scheduleAtFixedRate +import java.time.LocalDateTime @RestController class APIController { + //private val logger = LoggerFactory.getLogger(DemoApplication::class.java) + private val mensaLink = "https://www.swfr.de/de/essen-trinken/speiseplaene/mensa-offenburg/" private val mensaName = "Offenburg" @@ -48,20 +54,29 @@ class APIController { private var mensaLastUpdate: Long = 0 init { - initUpdates() + 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() + } + + // update courses every 6 hours (time in ms) + Timer().scheduleAtFixedRate(0, 21600000) { + asyncUpdateMensa() + } } @RequestMapping("/courses") fun courses(): CoursesList { println("courses request at " + System.currentTimeMillis() / 1000 + "!") - updateCoursesAsync() // check if we need to update and perform the update if so return CoursesList(CoursesMeta(coursesLinkList.size, coursesLastUpdate), coursesLinkList) } @RequestMapping("/mensamenu") fun mensamenu(): Mensa { println("mensamenu request at " + System.currentTimeMillis() / 1000 + "!") - updateMensa() // check if we need to update and perform the update if so return Mensa(MensaMeta(mensaName, mensaLastUpdate), mensaCurrentWeek, mensaNextWeek) } @@ -72,20 +87,37 @@ class APIController { return timetableList.stream().filter { x -> x.meta.courseName == courseName }.findAny().orElse(null) } + @RequestMapping("/status") + fun status(): Status { + println("status request at " + System.currentTimeMillis() / 1000 + "!") + var hsoCode = 999 + var swfrCode = 999 + + 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) { + // do some error handling + } + + return Status(LocalDateTime.now(), Date(coursesLastUpdate * 1000), Date(mensaLastUpdate * 1000), hsoCode, swfrCode) + } + /** * checks if we need to update the courses list and if so does it async * during the update process the old data will be returned for a API request * update if the last update was 24 hours ago */ - private fun updateCoursesAsync() = GlobalScope.launch { - val currentTime = System.currentTimeMillis() / 1000 - if ((currentTime - coursesLastUpdate) > 86400) { - coursesLinkList = CourseListParser().getCourseLinks() - coursesLastUpdate = currentTime - println("updated courses successful at " + System.currentTimeMillis() / 1000) - } else { - println("courses are up to date!") - } + private fun asyncUpdateCourses() = GlobalScope.launch { + coursesLinkList = CourseListParser().getCourseLinks() + coursesLastUpdate = System.currentTimeMillis() / 1000 + + println("updated courses successful at $coursesLastUpdate") } /** @@ -93,16 +125,12 @@ class APIController { * during the update process the old data will be returned for a API request * update if the last update was 6 hours ago */ - private fun updateMensa() = GlobalScope.launch { - val currentTime = System.currentTimeMillis() / 1000 - if ((currentTime - coursesLastUpdate) > 21600) { - mensaCurrentWeek = MensaParser().getMensaMenu(mensaLink) - mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaLink)) - mensaLastUpdate = currentTime - println("updated mensamenu successful at " + System.currentTimeMillis() / 1000) - } else { - println("mensamenu is up to date!") - } + private fun asyncUpdateMensa() = GlobalScope.launch { + mensaCurrentWeek = MensaParser().getMensaMenu(mensaLink) + mensaNextWeek = MensaParser().getMensaMenu(MensaParser().getMenuLinkNextWeek(mensaLink)) + mensaLastUpdate = System.currentTimeMillis() / 1000 + + println("updated mensamenu successful at $mensaLastUpdate") } /** diff --git a/src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt b/src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt index 464b0e4..f76466b 100644 --- a/src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt +++ b/src/main/kotlin/org/mosad/thecitadelofricks/DataTypes.kt @@ -22,6 +22,9 @@ package org.mosad.thecitadelofricks +import java.time.LocalDateTime +import java.util.* + // data classes for the course part data class Course(val courseName: String, val courseLink: String) @@ -34,7 +37,7 @@ data class Meal(val day: String, val heading: String, val parts: ArrayList) -data class MensaWeek(val days: Array = Array(7) { Meals(ArrayList()) }) +data class MensaWeek(val days: Array = Array(7) { Meals(ArrayList()) }) data class MensaMeta(val mensaName: String, val time: Long) @@ -49,4 +52,8 @@ data class TimetableWeek(val days: Array = Array(6) { TimetableDay data class TimetableMeta(val courseName: String, val courseLink: String, var time: Long) -data class TimetableCourse(val meta: TimetableMeta, var currentWeek: TimetableWeek, var nextWeek: TimetableWeek) \ No newline at end of file +data class TimetableCourse(val meta: TimetableMeta, var currentWeek: TimetableWeek, var nextWeek: TimetableWeek) + + +// data classes for the status part +data class Status(val time: LocalDateTime, val coursesLastUpdate: Date, val mensaLastUpdate: Date, val hsoResponseCode: Int, val swfrResponseCode: Int) \ No newline at end of file