@ -32,6 +32,7 @@ import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime
import org.mosad.seil0.projectlaogai.hsoparser.CoursesList
import org.mosad.seil0.projectlaogai.hsoparser.Lesson
import org.mosad.seil0.projectlaogai.hsoparser.MensaMenu
import org.mosad.seil0.projectlaogai.hsoparser.TimetableCourseWeek
import java.io.BufferedWriter
@ -49,29 +50,27 @@ class TCoRAPIController {
* Get a array of all currently available courses at the tcor API .
* Read the json object from tcor api and write it as file ( cache ) .
* /
fun getCoursesList ( context : Context ) = GlobalScope . launch {
fun getCoursesList ( context : Context ) : Job {
val url = URL ( " $tcorBaseURL /courseList " )
val file = File ( context . filesDir , " courses.json " )
try {
// read data from the API
val coursesObject = JSONObject ( url . readText ( ) ) //JSONObject(inReader.readLine())
return GlobalScope . launch ( Dispatchers . IO ) {
try {
// read data from the API
val coursesObject = JSONObject ( url . readText ( ) )
// write the json object to a file
withContext ( Dispatchers . IO ) {
// write the json object to a file
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( coursesObject . toString ( ) )
writer . close ( )
}
// update cache time
coursesCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /courseList " , ex )
// update cache time
coursesCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /courseList " , ex )
// write a empty file, since it is loaded later
withContext ( Dispatchers . IO ) {
// write a empty file, since it is loaded later
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( GsonBuilder ( ) . create ( ) . toJson ( CoursesList ( ) ) )
writer . close ( )
@ -83,29 +82,27 @@ class TCoRAPIController {
* Get this and next weeks mensa menus from the tcor API .
* Read the json object from tcor api and write it as file ( cache ) .
* /
fun getMensaMenu ( context : Context ) = GlobalScope . launch {
fun getMensaMenu ( context : Context ) : Job {
val url = URL ( " $tcorBaseURL /mensamenu " )
val file = File ( context . filesDir , " mensa.json " )
try {
// read data from the API
val mensaObject = JSONObject ( url . readText ( ) ) //JSONObject(inReader.readLine())
return GlobalScope . launch ( Dispatchers . IO ) {
try {
// read data from the API
val mensaObject = JSONObject ( url . readText ( ) )
// write the json object to a file
withContext ( Dispatchers . IO ) {
// write the json object to a file
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( mensaObject . toString ( ) )
writer . close ( )
}
// update cache time
mensaCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /mensamenu " , ex )
// update cache time
mensaCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /mensamenu " , ex )
// write a empty file, since it is loaded later
withContext ( Dispatchers . IO ) {
// write a empty file, since it is loaded later
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( GsonBuilder ( ) . create ( ) . toJson ( MensaMenu ( ) ) )
writer . close ( )
@ -118,29 +115,27 @@ class TCoRAPIController {
* Get the timetable for @param courseName at week @param week
* Read the json object from tcor api and write it as file ( cache ) .
* /
fun getTimetable ( courseName : String , week : Int , context : Context ) = GlobalScope . launch {
fun getTimetable ( courseName : String , week : Int , context : Context ) : Job {
val url = URL ( " $tcorBaseURL /timetable?courseName= $courseName &week= $week " )
val file = File ( context . filesDir , " timetable- $courseName - $week .json " )
try {
// read data from the API
val timetableObject = JSONObject ( url . readText ( ) ) //JSONObject(inReader.readLine())
return GlobalScope . launch ( Dispatchers . IO ) {
try {
// read data from the API
val timetableObject = JSONObject ( url . readText ( ) )
// write the json object to a file
withContext ( Dispatchers . IO ) {
// write the json object to a file
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( timetableObject . toString ( ) )
writer . close ( )
}
// update cache time
timetableCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /timetable " , ex )
// update cache time
timetableCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /timetable " , ex )
// write a empty file, since it is loaded later
withContext ( Dispatchers . IO ) {
// write a empty file, since it is loaded later
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( GsonBuilder ( ) . create ( ) . toJson ( TimetableCourseWeek ( ) ) )
writer . close ( )
@ -150,7 +145,9 @@ class TCoRAPIController {
}
/ * *
* TODO
* Get all lessons for a course at one week ( async )
* @param courseName the course name
* @param week the week to look up
* /
fun getLessonSubjectListAsync ( courseName : String , week : Int ) : Deferred < ArrayList < String > > {
val url = URL ( " $tcorBaseURL /lessonSubjectList?courseName= $courseName &week= $week " )
@ -159,6 +156,20 @@ class TCoRAPIController {
Gson ( ) . fromJson ( url . readText ( ) , ArrayList < String > ( ) :: class . java )
}
}
/ * *
* Get all occurrences of a lesson for a course at one week
* @param courseName the course name
* @param lessonSubject the subject to search for
* @param week the week to look up
* /
fun getLessonsAsync ( courseName : String , lessonSubject : String , week : Int ) : Deferred < ArrayList < Lesson > > {
val url = URL ( " $tcorBaseURL /lessons?courseName= $courseName &lessonSubject= $lessonSubject &week= $week " )
return GlobalScope . async {
Gson ( ) . fromJson ( url . readText ( ) , ArrayList < Lesson > ( ) :: class . java )
}
}
}
}