2019-03-17 18:12:36 +01:00
/ * *
* ProjectLaogai
*
2020-01-15 15:00:05 +01:00
* Copyright 2019 - 2020 < seil0 @mosad . xyz >
2019-03-17 18:12:36 +01:00
*
* 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.seil0.projectlaogai.controller
import android.content.Context
2019-08-25 22:45:23 +02:00
import android.util.Log
2020-05-29 16:56:15 +02:00
import com.google.gson.Gson
2020-02-27 12:09:24 +01:00
import com.google.gson.GsonBuilder
2020-06-11 21:41:05 +02:00
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
2020-05-29 16:56:15 +02:00
import kotlinx.coroutines.*
2019-03-17 18:12:36 +01:00
import org.json.JSONObject
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.coursesCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.mensaCacheTime
import org.mosad.seil0.projectlaogai.controller.PreferencesController.Companion.timetableCacheTime
2020-06-07 23:41:49 +02:00
import org.mosad.seil0.projectlaogai.util.CoursesList
import org.mosad.seil0.projectlaogai.util.Lesson
import org.mosad.seil0.projectlaogai.util.MensaMenu
import org.mosad.seil0.projectlaogai.util.TimetableCourseWeek
2019-06-26 15:00:43 +02:00
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
2019-03-17 18:12:36 +01:00
import java.net.URL
2019-06-26 15:00:43 +02:00
class TCoRAPIController {
2019-03-17 18:12:36 +01:00
2019-04-03 19:21:51 +02:00
companion object {
2019-08-25 22:45:23 +02:00
private const val className = " TCoRAPIController "
private const val tcorBaseURL = " https://tcor.mosad.xyz "
2019-06-26 15:00:43 +02:00
/ * *
2020-05-29 16:56:15 +02:00
* 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 ) .
2019-06-26 15:00:43 +02:00
* /
2020-06-05 19:17:49 +02:00
fun getCoursesList ( context : Context ) : Job {
2020-02-27 12:09:24 +01:00
val url = URL ( " $tcorBaseURL /courseList " )
val file = File ( context . filesDir , " courses.json " )
2019-06-26 15:00:43 +02:00
2020-06-05 19:17:49 +02:00
return GlobalScope . launch ( Dispatchers . IO ) {
try {
// read data from the API
val coursesObject = JSONObject ( url . readText ( ) )
2019-06-26 15:00:43 +02:00
2020-06-05 19:17:49 +02:00
// write the json object to a file
2020-02-04 19:58:07 +01:00
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( coursesObject . toString ( ) )
writer . close ( )
2019-08-25 22:45:23 +02:00
2020-06-05 19:17:49 +02:00
// update cache time
coursesCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /courseList " , ex )
2019-06-26 15:00:43 +02:00
2020-06-05 19:17:49 +02:00
// write a empty file, since it is loaded later
2020-02-27 12:09:24 +01:00
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( GsonBuilder ( ) . create ( ) . toJson ( CoursesList ( ) ) )
writer . close ( )
}
}
2019-06-26 15:00:43 +02:00
}
2019-04-03 19:21:51 +02:00
/ * *
2020-05-29 16:56:15 +02:00
* Get this and next weeks mensa menus from the tcor API .
* Read the json object from tcor api and write it as file ( cache ) .
2019-04-03 19:21:51 +02:00
* /
2020-06-05 19:17:49 +02:00
fun getMensaMenu ( context : Context ) : Job {
2020-02-27 12:09:24 +01:00
val url = URL ( " $tcorBaseURL /mensamenu " )
val file = File ( context . filesDir , " mensa.json " )
2019-08-25 22:45:23 +02:00
2020-06-05 19:17:49 +02:00
return GlobalScope . launch ( Dispatchers . IO ) {
try {
// read data from the API
val mensaObject = JSONObject ( url . readText ( ) )
2019-04-03 19:21:51 +02:00
2020-06-05 19:17:49 +02:00
// write the json object to a file
2020-02-04 19:58:07 +01:00
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( mensaObject . toString ( ) )
writer . close ( )
2019-04-03 19:21:51 +02:00
2020-06-05 19:17:49 +02:00
// update cache time
mensaCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /mensamenu " , ex )
2020-02-27 12:09:24 +01:00
2020-06-05 19:17:49 +02:00
// write a empty file, since it is loaded later
2020-02-27 12:09:24 +01:00
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( GsonBuilder ( ) . create ( ) . toJson ( MensaMenu ( ) ) )
writer . close ( )
}
2019-08-25 22:45:23 +02:00
}
2019-04-03 19:21:51 +02:00
}
2019-04-06 14:13:01 +02:00
/ * *
2020-05-29 16:56:15 +02:00
* Get the timetable for @param courseName at week @param week
* Read the json object from tcor api and write it as file ( cache ) .
2019-04-06 14:13:01 +02:00
* /
2020-06-05 19:17:49 +02:00
fun getTimetable ( courseName : String , week : Int , context : Context ) : Job {
2020-06-06 20:56:45 +02:00
val url = URL ( " $tcorBaseURL /timetable?course= $courseName &week= $week " )
2020-02-27 12:09:24 +01:00
val file = File ( context . filesDir , " timetable- $courseName - $week .json " )
2019-08-25 22:45:23 +02:00
2020-06-05 19:17:49 +02:00
return GlobalScope . launch ( Dispatchers . IO ) {
try {
// read data from the API
val timetableObject = JSONObject ( url . readText ( ) )
2019-04-06 14:13:01 +02:00
2020-06-05 19:17:49 +02:00
// write the json object to a file
2020-02-04 19:58:07 +01:00
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( timetableObject . toString ( ) )
writer . close ( )
2019-04-06 14:13:01 +02:00
2020-06-05 19:17:49 +02:00
// update cache time
timetableCacheTime = System . currentTimeMillis ( ) / 1000
PreferencesController . save ( context )
} catch ( ex : Exception ) {
Log . e ( className , " failed to get /timetable " , ex )
2020-02-27 12:09:24 +01:00
2020-06-05 19:17:49 +02:00
// write a empty file, since it is loaded later
2020-02-27 12:09:24 +01:00
val writer = BufferedWriter ( FileWriter ( file ) )
writer . write ( GsonBuilder ( ) . create ( ) . toJson ( TimetableCourseWeek ( ) ) )
writer . close ( )
}
2019-08-25 22:45:23 +02:00
}
2019-04-06 14:13:01 +02:00
}
2020-05-29 16:56:15 +02:00
/ * *
2020-06-05 19:17:49 +02:00
* Get all lessons for a course at one week ( async )
* @param courseName the course name
* @param week the week to look up
2020-05-29 16:56:15 +02:00
* /
2020-06-05 16:12:53 +02:00
fun getLessonSubjectListAsync ( courseName : String , week : Int ) : Deferred < ArrayList < String > > {
2020-06-06 20:56:45 +02:00
val url = URL ( " $tcorBaseURL /subjectList?course= $courseName &week= $week " )
2020-05-29 16:56:15 +02:00
return GlobalScope . async {
2020-06-05 16:12:53 +02:00
Gson ( ) . fromJson ( url . readText ( ) , ArrayList < String > ( ) :: class . java )
2020-05-29 16:56:15 +02:00
}
}
2020-06-05 19:17:49 +02:00
/ * *
* Get all occurrences of a lesson for a course at one week
* @param courseName the course name
2020-06-06 20:56:45 +02:00
* @param subject the subject to search for
2020-06-05 19:17:49 +02:00
* @param week the week to look up
* /
2020-06-11 21:41:05 +02:00
fun getLessons ( courseName : String , subject : String , week : Int ) : ArrayList < Lesson > {
2020-06-06 20:56:45 +02:00
val url = URL ( " $tcorBaseURL /lessons?course= $courseName &subject= $subject &week= $week " )
2020-06-11 21:41:05 +02:00
var array : ArrayList < Lesson >
runBlocking {
withContext ( Dispatchers . Default ) {
array = Gson ( ) . fromJson (
JsonParser . parseString ( url . readText ( ) ) . asJsonArray ,
object : TypeToken < ArrayList < Lesson > > ( ) { } . type
)
}
2020-06-05 19:17:49 +02:00
}
2020-06-11 21:41:05 +02:00
return array
2020-06-05 19:17:49 +02:00
}
2019-04-03 19:21:51 +02:00
}
2019-03-17 18:12:36 +01:00
}