This is the home of the new hsoApp!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

152 lines
5.7 KiB

/**
* ProjectLaogai
*
* Copyright 2019-2020 <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.seil0.projectlaogai.controller
import android.content.Context
import android.util.Log
import com.google.gson.GsonBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
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
import org.mosad.seil0.projectlaogai.hsoparser.CoursesList
import org.mosad.seil0.projectlaogai.hsoparser.MensaMenu
import org.mosad.seil0.projectlaogai.hsoparser.TimetableCourseWeek
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.net.URL
class TCoRAPIController {
companion object {
private const val className = "TCoRAPIController"
private const val tcorBaseURL = "https://tcor.mosad.xyz"
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getCoursesList(context: Context) = GlobalScope.launch {
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())
// write the json object to a file
withContext(Dispatchers.IO) {
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)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(CoursesList()))
writer.close()
}
}
}
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getMensa(context: Context) = GlobalScope.launch {
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())
// write the json object to a file
withContext(Dispatchers.IO) {
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)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(MensaMenu()))
writer.close()
}
}
}
/**
* get the json object from tcor api and write it as file (cache)
*/
fun getTimetable(courseName: String, week: Int, context: Context) = GlobalScope.launch {
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())
// write the json object to a file
withContext(Dispatchers.IO) {
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)
// write a empty file, since it is loaded later
withContext(Dispatchers.IO) {
val writer = BufferedWriter(FileWriter(file))
writer.write(GsonBuilder().create().toJson(TimetableCourseWeek()))
writer.close()
}
}
}
}
}

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close