forked from github/szkolny
[APIv2/Vulcan] Fix timetable teams issue. Fix missing login data error.
This commit is contained in:
parent
67f98b08c6
commit
9866017f7e
@ -7,15 +7,14 @@ package pl.szczodrzynski.edziennik.api.v2.vulcan
|
|||||||
import pl.szczodrzynski.edziennik.App
|
import pl.szczodrzynski.edziennik.App
|
||||||
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_VULCAN_API
|
import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_VULCAN_API
|
||||||
import pl.szczodrzynski.edziennik.api.v2.models.Data
|
import pl.szczodrzynski.edziennik.api.v2.models.Data
|
||||||
import pl.szczodrzynski.edziennik.currentTimeUnix
|
|
||||||
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore
|
import pl.szczodrzynski.edziennik.data.db.modules.login.LoginStore
|
||||||
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
|
import pl.szczodrzynski.edziennik.data.db.modules.profiles.Profile
|
||||||
import pl.szczodrzynski.edziennik.isNotNullNorEmpty
|
import pl.szczodrzynski.edziennik.isNotNullNorEmpty
|
||||||
|
|
||||||
class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) {
|
class DataVulcan(app: App, profile: Profile?, loginStore: LoginStore) : Data(app, profile, loginStore) {
|
||||||
|
|
||||||
fun isApiLoginValid() = apiCertificateExpiryTime-30 > currentTimeUnix()
|
fun isApiLoginValid() = /*apiCertificateExpiryTime-30 > currentTimeUnix()
|
||||||
&& apiCertificateKey.isNotNullNorEmpty()
|
&&*/ apiCertificateKey.isNotNullNorEmpty()
|
||||||
&& apiCertificatePrivate.isNotNullNorEmpty()
|
&& apiCertificatePrivate.isNotNullNorEmpty()
|
||||||
&& symbol.isNotNullNorEmpty()
|
&& symbol.isNotNullNorEmpty()
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api
|
package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api
|
||||||
|
|
||||||
|
import androidx.core.util.set
|
||||||
import pl.szczodrzynski.edziennik.*
|
import pl.szczodrzynski.edziennik.*
|
||||||
import pl.szczodrzynski.edziennik.api.v2.Regexes
|
import pl.szczodrzynski.edziennik.api.v2.Regexes
|
||||||
import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_TIMETABLE
|
import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_TIMETABLE
|
||||||
@ -14,6 +15,7 @@ import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi
|
|||||||
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
|
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
|
||||||
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
|
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
|
||||||
import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject
|
import pl.szczodrzynski.edziennik.data.db.modules.subjects.Subject
|
||||||
|
import pl.szczodrzynski.edziennik.data.db.modules.teams.Team
|
||||||
import pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson
|
import pl.szczodrzynski.edziennik.data.db.modules.timetable.Lesson
|
||||||
import pl.szczodrzynski.edziennik.utils.Utils.crc16
|
import pl.szczodrzynski.edziennik.utils.Utils.crc16
|
||||||
import pl.szczodrzynski.edziennik.utils.Utils.d
|
import pl.szczodrzynski.edziennik.utils.Utils.d
|
||||||
@ -24,163 +26,180 @@ class VulcanApiTimetable(override val data: DataVulcan, val onSuccess: () -> Uni
|
|||||||
const val TAG = "VulcanApiTimetable"
|
const val TAG = "VulcanApiTimetable"
|
||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init { data.profile?.also { profile ->
|
||||||
data.profile?.also { profile ->
|
val currentWeekStart = Date.getToday().let { it.stepForward(0, 0, -it.weekDay) }
|
||||||
val currentWeekStart = Date.getToday().let { it.stepForward(0, 0, -it.weekDay) }
|
val getDate = data.arguments?.getString("weekStart") ?: currentWeekStart.stringY_m_d
|
||||||
val getDate = data.arguments?.getString("weekStart") ?: currentWeekStart.stringY_m_d
|
|
||||||
|
|
||||||
val weekStart = Date.fromY_m_d(getDate)
|
val weekStart = Date.fromY_m_d(getDate)
|
||||||
val weekEnd = weekStart.clone().stepForward(0, 0, 6)
|
val weekEnd = weekStart.clone().stepForward(0, 0, 6)
|
||||||
|
|
||||||
apiGet(TAG, VULCAN_API_ENDPOINT_TIMETABLE, parameters = mapOf(
|
apiGet(TAG, VULCAN_API_ENDPOINT_TIMETABLE, parameters = mapOf(
|
||||||
"DataPoczatkowa" to weekStart.stringY_m_d,
|
"DataPoczatkowa" to weekStart.stringY_m_d,
|
||||||
"DataKoncowa" to weekEnd.stringY_m_d,
|
"DataKoncowa" to weekEnd.stringY_m_d,
|
||||||
"IdUczen" to data.studentId,
|
"IdUczen" to data.studentId,
|
||||||
"IdOddzial" to data.studentClassId,
|
"IdOddzial" to data.studentClassId,
|
||||||
"IdOkresKlasyfikacyjny" to data.studentSemesterId
|
"IdOkresKlasyfikacyjny" to data.studentSemesterId
|
||||||
)) { json, _ ->
|
)) { json, _ ->
|
||||||
val dates: MutableSet<Int> = mutableSetOf()
|
val dates: MutableSet<Int> = mutableSetOf()
|
||||||
val lessons: MutableList<Lesson> = mutableListOf()
|
val lessons: MutableList<Lesson> = mutableListOf()
|
||||||
|
|
||||||
json.getJsonArray("Data")?.asJsonObjectList()?.forEach { lesson ->
|
json.getJsonArray("Data")?.asJsonObjectList()?.forEach { lesson ->
|
||||||
val lessonDate = Date.fromY_m_d(lesson.getString("DzienTekst"))
|
if (lesson.getBoolean("PlanUcznia") != true)
|
||||||
val lessonNumber = lesson.getInt("NumerLekcji")
|
return@forEach
|
||||||
val lessonRange = data.lessonRanges.singleOrNull { it.lessonNumber == lessonNumber }
|
val lessonDate = Date.fromY_m_d(lesson.getString("DzienTekst"))
|
||||||
val startTime = lessonRange?.startTime
|
val lessonNumber = lesson.getInt("NumerLekcji")
|
||||||
val endTime = lessonRange?.endTime
|
val lessonRange = data.lessonRanges.singleOrNull { it.lessonNumber == lessonNumber }
|
||||||
val teacherId = lesson.getLong("IdPracownik")
|
val startTime = lessonRange?.startTime
|
||||||
val teamId = data.studentClassId.toLong()
|
val endTime = lessonRange?.endTime
|
||||||
val classroom = lesson.getString("Classroom")
|
val teacherId = lesson.getLong("IdPracownik")
|
||||||
|
val classroom = lesson.getString("Sala")
|
||||||
|
|
||||||
val oldTeacherId = lesson.getLong("IdPracownikOld")
|
val oldTeacherId = lesson.getLong("IdPracownikOld")
|
||||||
|
|
||||||
val changeAnnotation = lesson.getString("AdnotacjaOZmianie") ?: ""
|
val changeAnnotation = lesson.getString("AdnotacjaOZmianie") ?: ""
|
||||||
val type = when {
|
val type = when {
|
||||||
changeAnnotation.startsWith("(przeniesiona z") -> Lesson.TYPE_SHIFTED_TARGET
|
changeAnnotation.startsWith("(przeniesiona z") -> Lesson.TYPE_SHIFTED_TARGET
|
||||||
changeAnnotation.startsWith("(przeniesiona na") -> Lesson.TYPE_SHIFTED_SOURCE
|
changeAnnotation.startsWith("(przeniesiona na") -> Lesson.TYPE_SHIFTED_SOURCE
|
||||||
changeAnnotation.startsWith("(zastępstwo") -> Lesson.TYPE_CHANGE
|
changeAnnotation.startsWith("(zastępstwo") -> Lesson.TYPE_CHANGE
|
||||||
lesson.getBoolean("PrzekreslonaNazwa") == true -> Lesson.TYPE_CANCELLED
|
lesson.getBoolean("PrzekreslonaNazwa") == true -> Lesson.TYPE_CANCELLED
|
||||||
else -> Lesson.TYPE_NORMAL
|
else -> Lesson.TYPE_NORMAL
|
||||||
|
}
|
||||||
|
|
||||||
|
val teamId = lesson.getString("PodzialSkrot")?.let { teamName ->
|
||||||
|
val name = "${data.teamClass?.name} $teamName"
|
||||||
|
val id = name.crc16().toLong()
|
||||||
|
var team = data.teamList.singleOrNull { it.name == name }
|
||||||
|
if (team == null) {
|
||||||
|
team = Team(
|
||||||
|
profileId,
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
Team.TYPE_VIRTUAL,
|
||||||
|
"${data.schoolName}:$name",
|
||||||
|
teacherId ?: oldTeacherId ?: -1
|
||||||
|
)
|
||||||
|
data.teamList[id] = team
|
||||||
}
|
}
|
||||||
|
team.id
|
||||||
|
} ?: data.studentClassId.toLong()
|
||||||
|
|
||||||
val subjectId = lesson.getLong("IdPrzedmiot")?.let {
|
val subjectId = lesson.getLong("IdPrzedmiot")?.let {
|
||||||
when (it) {
|
when (it) {
|
||||||
0L -> {
|
0L -> {
|
||||||
val subjectName = lesson.getString("PrzedmiotNazwa") ?: ""
|
val subjectName = lesson.getString("PrzedmiotNazwa") ?: ""
|
||||||
|
|
||||||
data.subjectList.singleOrNull { subject -> subject.longName == subjectName }?.id
|
data.subjectList.singleOrNull { subject -> subject.longName == subjectName }?.id
|
||||||
?: {
|
?: {
|
||||||
/**
|
/**
|
||||||
* CREATE A NEW SUBJECT IF IT DOESN'T EXIST
|
* CREATE A NEW SUBJECT IF IT DOESN'T EXIST
|
||||||
*/
|
*/
|
||||||
|
|
||||||
val subjectObject = Subject(
|
val subjectObject = Subject(
|
||||||
profileId,
|
profileId,
|
||||||
-1 * crc16(subjectName.toByteArray()).toLong(),
|
-1 * crc16(subjectName.toByteArray()).toLong(),
|
||||||
subjectName,
|
subjectName,
|
||||||
subjectName
|
subjectName
|
||||||
)
|
)
|
||||||
data.subjectList.put(subjectObject.id, subjectObject)
|
data.subjectList.put(subjectObject.id, subjectObject)
|
||||||
subjectObject.id
|
subjectObject.id
|
||||||
}.invoke()
|
}.invoke()
|
||||||
}
|
}
|
||||||
else -> it
|
else -> it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val id = lessonDate.combineWith(startTime) / 6L * 10L + (lesson.hashCode() and 0xFFFF)
|
||||||
|
|
||||||
|
val lessonObject = Lesson(profileId, id).apply {
|
||||||
|
this.type = type
|
||||||
|
|
||||||
|
when (type) {
|
||||||
|
Lesson.TYPE_NORMAL, Lesson.TYPE_CHANGE, Lesson.TYPE_SHIFTED_TARGET -> {
|
||||||
|
this.date = lessonDate
|
||||||
|
this.lessonNumber = lessonNumber
|
||||||
|
this.startTime = startTime
|
||||||
|
this.endTime = endTime
|
||||||
|
this.subjectId = subjectId
|
||||||
|
this.teacherId = teacherId
|
||||||
|
this.teamId = teamId
|
||||||
|
this.classroom = classroom
|
||||||
|
|
||||||
|
this.oldTeacherId = oldTeacherId
|
||||||
|
}
|
||||||
|
|
||||||
|
Lesson.TYPE_CANCELLED, Lesson.TYPE_SHIFTED_SOURCE -> {
|
||||||
|
this.oldDate = lessonDate
|
||||||
|
this.oldLessonNumber = lessonNumber
|
||||||
|
this.oldStartTime = startTime
|
||||||
|
this.oldEndTime = endTime
|
||||||
|
this.oldSubjectId = subjectId
|
||||||
|
this.oldTeacherId = teacherId
|
||||||
|
this.oldTeamId = teamId
|
||||||
|
this.oldClassroom = classroom
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val id = lessonDate.combineWith(startTime) / 6L * 10L + (lesson.hashCode() and 0xFFFF)
|
if (type == Lesson.TYPE_SHIFTED_SOURCE || type == Lesson.TYPE_SHIFTED_TARGET) {
|
||||||
|
val shift = Regexes.VULCAN_SHITFT_ANNOTATION.find(changeAnnotation)
|
||||||
|
val oldLessonNumber = shift?.get(2)?.toInt()
|
||||||
|
val oldLessonDate = shift?.get(3)?.let { Date.fromd_m_Y(it) }
|
||||||
|
|
||||||
val lessonObject = Lesson(profileId, id).apply {
|
val oldLessonRange = data.lessonRanges.singleOrNull { it.lessonNumber == oldLessonNumber }
|
||||||
this.type = type
|
val oldStartTime = oldLessonRange?.startTime
|
||||||
|
val oldEndTime = oldLessonRange?.endTime
|
||||||
|
|
||||||
when (type) {
|
when (type) {
|
||||||
Lesson.TYPE_NORMAL, Lesson.TYPE_CHANGE, Lesson.TYPE_SHIFTED_TARGET -> {
|
Lesson.TYPE_SHIFTED_SOURCE -> {
|
||||||
this.date = lessonDate
|
this.lessonNumber = oldLessonNumber
|
||||||
this.lessonNumber = lessonNumber
|
this.date = oldLessonDate
|
||||||
this.startTime = startTime
|
this.startTime = oldStartTime
|
||||||
this.endTime = endTime
|
this.endTime = oldEndTime
|
||||||
this.subjectId = subjectId
|
|
||||||
this.teacherId = teacherId
|
|
||||||
this.teamId = teamId
|
|
||||||
this.classroom = classroom
|
|
||||||
|
|
||||||
this.oldTeacherId = oldTeacherId
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Lesson.TYPE_CANCELLED, Lesson.TYPE_SHIFTED_SOURCE -> {
|
Lesson.TYPE_SHIFTED_TARGET -> {
|
||||||
this.oldDate = lessonDate
|
this.oldLessonNumber = oldLessonNumber
|
||||||
this.oldLessonNumber = lessonNumber
|
this.oldDate = oldLessonDate
|
||||||
this.oldStartTime = startTime
|
this.oldStartTime = oldStartTime
|
||||||
this.oldEndTime = endTime
|
this.oldEndTime = oldEndTime
|
||||||
this.oldSubjectId = subjectId
|
|
||||||
this.oldTeacherId = teacherId
|
|
||||||
this.oldTeamId = teamId
|
|
||||||
this.oldClassroom = classroom
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == Lesson.TYPE_SHIFTED_SOURCE || type == Lesson.TYPE_SHIFTED_TARGET) {
|
|
||||||
val shift = Regexes.VULCAN_SHITFT_ANNOTATION.find(changeAnnotation)
|
|
||||||
val oldLessonNumber = shift?.get(2)?.toInt()
|
|
||||||
val oldLessonDate = shift?.get(3)?.let { Date.fromd_m_Y(it) }
|
|
||||||
|
|
||||||
val oldLessonRange = data.lessonRanges.singleOrNull { it.lessonNumber == oldLessonNumber }
|
|
||||||
val oldStartTime = oldLessonRange?.startTime
|
|
||||||
val oldEndTime = oldLessonRange?.endTime
|
|
||||||
|
|
||||||
when (type) {
|
|
||||||
Lesson.TYPE_SHIFTED_SOURCE -> {
|
|
||||||
this.lessonNumber = oldLessonNumber
|
|
||||||
this.date = oldLessonDate
|
|
||||||
this.startTime = oldStartTime
|
|
||||||
this.endTime = oldEndTime
|
|
||||||
}
|
|
||||||
|
|
||||||
Lesson.TYPE_SHIFTED_TARGET -> {
|
|
||||||
this.oldLessonNumber = oldLessonNumber
|
|
||||||
this.oldDate = oldLessonDate
|
|
||||||
this.oldStartTime = oldStartTime
|
|
||||||
this.oldEndTime = oldEndTime
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type != Lesson.TYPE_NORMAL) {
|
|
||||||
data.metadataList.add(Metadata(
|
|
||||||
profileId,
|
|
||||||
Metadata.TYPE_LESSON_CHANGE,
|
|
||||||
id,
|
|
||||||
profile.empty,
|
|
||||||
profile.empty,
|
|
||||||
System.currentTimeMillis()
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
dates.add(lessonDate.value)
|
|
||||||
lessons.add(lessonObject)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val date: Date = weekStart.clone()
|
if (type != Lesson.TYPE_NORMAL) {
|
||||||
while (date <= weekEnd) {
|
data.metadataList.add(Metadata(
|
||||||
if (!dates.contains(date.value)) {
|
profileId,
|
||||||
lessons.add(Lesson(profileId, date.value.toLong()).apply {
|
Metadata.TYPE_LESSON_CHANGE,
|
||||||
this.type = Lesson.TYPE_NO_LESSONS
|
id,
|
||||||
this.date = date.clone()
|
profile.empty,
|
||||||
})
|
profile.empty,
|
||||||
}
|
System.currentTimeMillis()
|
||||||
|
))
|
||||||
date.stepForward(0, 0, 1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
d(TAG, "Clearing lessons between ${weekStart.stringY_m_d} and ${weekEnd.stringY_m_d} - timetable downloaded for $getDate")
|
dates.add(lessonDate.value)
|
||||||
|
lessons.add(lessonObject)
|
||||||
data.lessonNewList.addAll(lessons)
|
|
||||||
data.toRemove.add(DataRemoveModel.Timetable.between(weekStart, weekEnd))
|
|
||||||
|
|
||||||
data.setSyncNext(ENDPOINT_VULCAN_API_TIMETABLE, SYNC_ALWAYS)
|
|
||||||
onSuccess()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
val date: Date = weekStart.clone()
|
||||||
|
while (date <= weekEnd) {
|
||||||
|
if (!dates.contains(date.value)) {
|
||||||
|
lessons.add(Lesson(profileId, date.value.toLong()).apply {
|
||||||
|
this.type = Lesson.TYPE_NO_LESSONS
|
||||||
|
this.date = date.clone()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
date.stepForward(0, 0, 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
d(TAG, "Clearing lessons between ${weekStart.stringY_m_d} and ${weekEnd.stringY_m_d} - timetable downloaded for $getDate")
|
||||||
|
|
||||||
|
data.lessonNewList.addAll(lessons)
|
||||||
|
data.toRemove.add(DataRemoveModel.Timetable.between(weekStart, weekEnd))
|
||||||
|
|
||||||
|
data.setSyncNext(ENDPOINT_VULCAN_API_TIMETABLE, SYNC_ALWAYS)
|
||||||
|
onSuccess()
|
||||||
}
|
}
|
||||||
}
|
}}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user