From a550ccee77a6cfc2eb5a8f059e21ca8481d86c2a Mon Sep 17 00:00:00 2001 From: pranshulgg Date: Thu, 24 Sep 2026 21:25:50 +0530 Subject: [PATCH] add kmi api, repo, and json # --- .../network/sources/weather/kmi/KmiApi.kt | 15 +--- .../sources/weather/kmi/KmiRepository.kt | 80 +++++++++++++++++++ .../weather/kmi/json/KmiWeatherJson.kt | 72 +++++++++++++++++ 3 files changed, 156 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiRepository.kt create mode 100644 app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/json/KmiWeatherJson.kt diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiApi.kt b/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiApi.kt index a1dd68e64..2c201da76 100644 --- a/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiApi.kt +++ b/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiApi.kt @@ -1,22 +1,15 @@ package com.pranshulgg.weather_master_app.core.network.sources.weather.kmi -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.JmaApi -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.alerts.json.JmaWarningJson -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.json.JmaAmedasCurrentJson -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.json.JmaAmedasStationJson -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.json.JmaAreaJson -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.json.JmaForecastBlockJson -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.json.JmaHourlyJson -import com.pranshulgg.weather_master_app.core.network.sources.weather.jma.json.JmaWeekAreaEntryJson +import com.pranshulgg.weather_master_app.core.network.sources.weather.kmi.json.KmiWeatherJson import okhttp3.OkHttpClient +import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import retrofit2.http.GET -import retrofit2.http.Path import retrofit2.http.Query import java.util.concurrent.TimeUnit - +// Source - https://github.com/jdejaegh/irm-kmi-api interface KmiApi { @GET("appv4/?s=getForecasts") @@ -24,7 +17,7 @@ interface KmiApi { @Query("lat") lat: Double, @Query("long") lon: Double, @Query("k") key: String, - ) + ): Response companion object { diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiRepository.kt b/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiRepository.kt new file mode 100644 index 000000000..0a1397096 --- /dev/null +++ b/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/KmiRepository.kt @@ -0,0 +1,80 @@ +package com.pranshulgg.weather_master_app.core.network.sources.weather.kmi + +import com.pranshulgg.weather_master_app.core.model.domain.location.Location +import com.pranshulgg.weather_master_app.core.model.domain.weather.Weather +import com.pranshulgg.weather_master_app.core.model.sources.Source +import com.pranshulgg.weather_master_app.core.model.weather.FinishedWeatherResult +import com.pranshulgg.weather_master_app.core.model.weather.WeatherDataPack +import com.pranshulgg.weather_master_app.core.network.sources.weather.mgm.MgmApi +import com.pranshulgg.weather_master_app.data.local.dao.alerts.AlertsDao +import com.pranshulgg.weather_master_app.data.local.dao.location.LocationKeysDao +import com.pranshulgg.weather_master_app.data.local.dao.weather.ApiKeysDao +import com.pranshulgg.weather_master_app.data.local.dao.weather.WeatherContextDao +import com.pranshulgg.weather_master_app.data.local.dao.weather.WeatherDao +import com.pranshulgg.weather_master_app.data.repository.capability.WeatherCapability +import com.pranshulgg.weather_master_app.data.repository.data.BaseRepository +import com.pranshulgg.weather_master_app.data.repository.weather.CacheModel +import java.security.MessageDigest +import java.time.LocalDate +import java.time.format.DateTimeFormatter +import java.util.Locale +import javax.inject.Inject + +// Source - https://github.com/jdejaegh/irm-kmi-api + +class KmiRepository @Inject constructor( + val dao: WeatherContextDao, + val weatherDao: WeatherDao, + val api: KmiApi, + val apiKeysDao: ApiKeysDao, + val locationKeysDao: LocationKeysDao, + val alertsDao: AlertsDao +) : BaseRepository() { + override val weatherSource = Source.KMI + override val alertSource = Source.KMI + override val airQualitySource = Source.NONE + + override val providesAlerts = true + + + override fun weatherCapability(): WeatherCapability? { + return object : WeatherCapability { + override suspend fun fetchAndProcess( + location: Location, + isManualRefresh: Boolean, + isForceRefresh: Boolean, + cacheModel: CacheModel + ): WeatherDataPack { + val response = api.fetchWeather( + lat = location.latitude, + lon = location.longitude, + key = generateIrmKmiKey() + ) + + + } + + override suspend fun saveToDb(data: WeatherDataPack, cacheModel: CacheModel) { + TODO("Not yet implemented") + } + + override fun finishedResult(data: Weather): FinishedWeatherResult { + TODO("Not yet implemented") + } + } + } + +} + +private fun generateIrmKmiKey(): String { + val date = LocalDate.now().format( + DateTimeFormatter.ofPattern("dd/MM/yyyy", Locale.ROOT) + ) + + val input = "r9EnW374jkJ9acc;getForecasts;$date" + + return MessageDigest + .getInstance("MD5") + .digest(input.toByteArray(Charsets.UTF_8)) + .joinToString("") { "%02x".format(it) } +} diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/json/KmiWeatherJson.kt b/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/json/KmiWeatherJson.kt new file mode 100644 index 000000000..ad775dd5b --- /dev/null +++ b/app/src/main/java/com/pranshulgg/weather_master_app/core/network/sources/weather/kmi/json/KmiWeatherJson.kt @@ -0,0 +1,72 @@ +package com.pranshulgg.weather_master_app.core.network.sources.weather.kmi.json + +import com.google.gson.annotations.SerializedName + +data class KmiWeatherJson( + val obs: KmiCurrentWeatherJson, + @SerializedName("for") val forecast: KmiForecastJson +) + +data class KmiCurrentWeatherJson( + val temp: Double?, + val ww: Double?, +) + +data class KmiForecastJson( + val daily: List, + val hourly: List, + val warning: List +) + +data class KmiDayJson( + val period: String?, + val tempMin: Double?, + val tempMax: Double?, + val ww1: Double?, + val ww2: Double?, + @SerializedName("ddText") val windDirectionText: KmiDayWindDirectionJson, + val wind: KmiDayWindJson, + val precipChance: Double?, + val precipQuantity: String? +) + +data class KmiDayWindDirectionJson( + val en: String? +) + +data class KmiDayWindJson( + val speed: Double? +) + +data class KmiHourJson( + val hour: String?, + val temp: Double?, + val ww: Double?, + val precipChance: String?, + val precipQuantity: Double?, + val pressure: Double?, + val windSpeedKm: Double?, + val windDirectionText: KmiHourWindDirectionJson, + val dateShow: String?, +) + +data class KmiHourWindDirectionJson( + val en: String? +) + +data class KmiWarningJson( + val warningType: KmiWarningTypeJson, + val warningLevel: Double?, + val fromTimestamp: String?, + val toTimestamp: String?, + val text: KmiWarningTextJson +) + + +data class KmiWarningTypeJson( + val en: String? +) + +data class KmiWarningTextJson( + val en: String? +) \ No newline at end of file