-
Notifications
You must be signed in to change notification settings - Fork 12
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
varjasz
wants to merge
1
commit into
vanioinformatika:master
Choose a base branch
from
imkuqm:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Házi Feladat #10
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
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() | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.