Skip to content

Házi Feladat #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/src/main/kotlin/WeatherApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,11 @@ class WeatherApp

fun main() {
runApplication<WeatherApp>()

val latitudeCoord = 47.4984
val longitudeCoord = 19.0404
//val latitudeCoord = 47.58446085308398
//val longitudeCoord = 18.877541667963346
WeatherCalculate.printDayAverageTemperaturesAt( latitudeCoord, longitudeCoord)
}

37 changes: 37 additions & 0 deletions app/src/main/kotlin/WeatherCalculate.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package hu.vanio.kotlin.feladat.ms

typealias StringToDoubles = MutableMap<String, MutableList<Double> >
class WeatherCalculate()
{
companion object {
fun recordData(meteoData: MeteoData) : StringToDoubles {
var dayTemperatures : StringToDoubles = mutableMapOf()
for (i in 0 until meteoData.hourly.time.size) {
val day = meteoData.hourly.time[i].substring(0 .. 9)
dayTemperatures.getOrPut(day, ::mutableListOf).add(meteoData.hourly.temperature_2m[i])
}
return dayTemperatures
}
fun printDayAverages(dayTemperatures : StringToDoubles) {
println("Day average temperatures:")
dayTemperatures.forEach { entry ->
println("${entry.key}: ${entry.value.average()}")
}
}
fun getDayAverages(dayTemperatures : StringToDoubles) : MutableMap<String, Double> {
var ret : MutableMap<String, Double> = mutableMapOf()
dayTemperatures.forEach { entry ->
ret.put(entry.key, entry.value.average())
}
return ret
}
fun printDayAverageTemperaturesAt(latitudeCoord : Double, longitudeCoord : Double) {
val meteoUrl = "https://api.open-meteo.com/v1/forecast?latitude=$latitudeCoord&longitude=$longitudeCoord&hourly=temperature_2m&timezone=auto"
val meteoData = getMeteoDataFromUrl(meteoUrl)
if (meteoData.hourly.time.isNotEmpty()) {
val dayTemps = WeatherCalculate.recordData(meteoData)
WeatherCalculate.printDayAverages(dayTemps)
}
}
}
}
72 changes: 72 additions & 0 deletions app/src/main/kotlin/WeatherDatas.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package hu.vanio.kotlin.feladat.ms

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.URL

data class MeteoData(
val latitude: Double = 0.0,
val longitude: Double = 0.0,
val generationtime_ms: Double = 0.0,
val utc_offset_seconds : Int = 0,
val timezone: String = "",
val timezone_abbreviation: String = "",
val elevation : Double= 0.0,
val hourly_units : HourlyUnits = HourlyUnits(),
val hourly : Hourly = Hourly(),
)
data class HourlyUnits(
val time: String = "",
val temperature_2m: String = "",
)
data class Hourly(
val time: List<String> = mutableListOf(),
val temperature_2m: List<Double> = mutableListOf(),
)
fun getJsonDataFromUrl(url: String): String {
val connection = URL(url).openConnection()
val reader = BufferedReader(InputStreamReader(connection.getInputStream()))
val jsonData = StringBuilder()

var line: String?
while (reader.readLine().also { line = it } != null) {
jsonData.append(line)
}
reader.close()

return jsonData.toString()
}
fun getMeteoDataFromJson(jsonStr: String): MeteoData {
try {
return jacksonObjectMapper().readValue(jsonStr)
}
catch (e1 : com.fasterxml.jackson.core.JsonParseException)
{
println("Wrong formed JSON: " + e1.toString())
return MeteoData()
}
catch (e2: com.fasterxml.jackson.databind.exc.MismatchedInputException)
{
println("Wrong formed JSON: " + e2.toString())
return MeteoData()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ez elfedi a hibát. Kívülről úgy látszik, hogy sikeresen lefutott, csak nem volt adat.

}
}
fun getMeteoDataFromUrl(url: String): MeteoData {
try
{
val jsonData = getJsonDataFromUrl(url)
//val jsonData = getIt()
return jacksonObjectMapper().readValue(jsonData)
}
catch (e : java.net.UnknownHostException)
{
println("Wrong URL: " + e.toString())
return MeteoData()
}
catch (e2: Exception) {
println("Serious error: " + e2.toString())
return MeteoData()
}
}
Loading