update coursesList and mensa every 24/6 hours & added status page
* the status page shows the latest updates and the http staus of the hso and swfr sites
This commit is contained in:
		| @ -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") | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|  | ||||
| @ -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<Strin | ||||
|  | ||||
| data class Meals(val meals: ArrayList<Meal>) | ||||
|  | ||||
| data class MensaWeek(val days: Array<Meals> = Array(7) { Meals(ArrayList<Meal>()) }) | ||||
| data class MensaWeek(val days: Array<Meals> = Array(7) { Meals(ArrayList()) }) | ||||
|  | ||||
| data class MensaMeta(val mensaName: String, val time: Long) | ||||
|  | ||||
| @ -49,4 +52,8 @@ data class TimetableWeek(val days: Array<TimetableDay> = 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) | ||||
| 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) | ||||
		Reference in New Issue
	
	Block a user