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.
TheCitadelofRicks/src/main/kotlin/org/mosad/thecitadelofricks/controller/StatusController.kt

121 lines
4.0 KiB

/**
* TheCitadelofRicks
*
* Copyright 2019 <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.thecitadelofricks.controller
import org.mosad.thecitadelofricks.APIController.Companion.apiVersion
import org.mosad.thecitadelofricks.APIController.Companion.softwareVersion
import org.mosad.thecitadelofricks.APIController.Companion.startTime
import org.mosad.thecitadelofricks.Status
import org.mosad.thecitadelofricks.TimetableCounter
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.net.HttpURLConnection
import java.net.URL
import java.time.LocalDateTime
import java.util.*
import kotlin.collections.ArrayList
class StatusController {
companion object {
private val logger: Logger = LoggerFactory.getLogger(StatusController::class.java)
private var totalRequests = 0
private var mensaMenuRequests = 0
private var timetableRequests = ArrayList<TimetableCounter>()
fun updateTotalRequests() {
totalRequests++
}
fun updateMensaMenuRequests() {
mensaMenuRequests++
}
/**
* if a timetable is requested update the request counter
*/
fun updateTimetableRequests(courseName: String) {
timetableRequests.stream().filter { it.courseName == courseName }.findFirst().ifPresentOrElse({
it.requests++
}, {
timetableRequests.add(TimetableCounter(courseName, 1))
})
}
fun getTotalRequests(): Int {
return totalRequests
}
fun getMensaMenuRequests(): Int {
return mensaMenuRequests
}
fun getTimetableRequests(): ArrayList<TimetableCounter> {
return timetableRequests
}
fun getStatus(): Status {
val currentTime = System.currentTimeMillis() / 1000
val minutes = (currentTime - startTime) % 3600 / 60
val hours = (currentTime - startTime) % 86400 / 3600
val days = (currentTime - startTime) / 86400
var hsoCode = 999
var swfrCode = 999
try {
val hsoURL = URL("https://www.hs-offenburg.de/")
val swfrURL = URL("https://www.swfr.de/")
var connection = hsoURL.openConnection() as HttpURLConnection
connection.requestMethod = "HEAD"
connection.connectTimeout = 15000
hsoCode = connection.responseCode
connection = swfrURL.openConnection() as HttpURLConnection
connection.connectTimeout = 15000
swfrCode = connection.responseCode
} catch (e: Exception) {
logger.error("Error while fetching url response codes!", e)
}
return Status(
LocalDateTime.now(),
"$days days, $hours:$minutes",
apiVersion,
softwareVersion,
getTotalRequests(),
getMensaMenuRequests(),
getTimetableRequests(),
CacheController.timetableList.size,
Date(CacheController.courseList.meta.updateTime * 1000),
Date(CacheController.mensaMenu.meta.updateTime * 1000),
hsoCode,
swfrCode
)
}
}
}

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