From 9349d32c7cd8278efb9ee5985a9f7418e2dcb3a0 Mon Sep 17 00:00:00 2001 From: JUNGGWAN KIM Date: Tue, 24 Sep 2024 17:27:35 +0900 Subject: [PATCH] weather realm --- .../rasel/lunar/launcher/model/WeatherInfo.kt | 66 +++++++++++++- .../launcher/workers/OpenWeatherGetter.kt | 88 ++++++++++--------- .../rasel/lunar/launcher/workers/WorkersDb.kt | 10 ++- 3 files changed, 117 insertions(+), 47 deletions(-) diff --git a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfo.kt b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfo.kt index 63783344..bf249180 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfo.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/model/WeatherInfo.kt @@ -5,8 +5,9 @@ import com.google.gson.annotations.SerializedName import io.realm.kotlin.ext.realmListOf import io.realm.kotlin.types.RealmList import io.realm.kotlin.types.RealmObject +import io.realm.kotlin.types.annotations.Ignore - class WeatherInfo : RealmObject { +class WeatherInfo : RealmObject { constructor() constructor(coord: Location?, weather: Array?, main: Main?) { this.coord = coord @@ -20,11 +21,50 @@ import io.realm.kotlin.types.RealmObject @SerializedName("coord") var coord: Location? = null + @Ignore @SerializedName("weather") - var weather: RealmList? = realmListOf() + var weather: ArrayList? = arrayListOf() + + var weathers: RealmList = realmListOf() + + @SerializedName("base") + var base: String? = null @SerializedName("main") var main: Main? = null +fun filled() { + weather?.let { + weathers.addAll(it) + } + +} + + @SerializedName("visibility") + var visibility: String? = null + + @SerializedName("wind") + var wind: Wind? = null + + @SerializedName("clouds") + var clouds: Clouds? = null + + @SerializedName("dt") + var dt: String? = null + + @SerializedName("sys") + var sys: Sys? = null + + @SerializedName("timezone") + var timezone: String? = null + + @SerializedName("id") + var id: String? = null + + @SerializedName("name") + var name: String? = null + + @SerializedName("cod") + var cod: String? = null } class Location : RealmObject { @@ -32,14 +72,14 @@ class Location : RealmObject { var lat: String? = null } -class Weather : RealmObject{ +class Weather : RealmObject { var id: String? = null var main: String? = null var description: String? = null var icon: String? = null } -class Main : RealmObject { +class Main : RealmObject { var temp: String? = null var feels_like: String? = null var temp_min: String? = null @@ -48,4 +88,22 @@ class Main : RealmObject { var humidity: String? = null var sea_level: String? = null var grnd_level: String? = null +} + +class Wind: RealmObject { + var speed: String? = null + var deg: String? = null + var gust: String? = null +} + +class Clouds: RealmObject { + var all: String? = null +} + +class Sys: RealmObject { + var type: String? = null + var id: String? = null + var country: String? = null + var sunrise: String? = null + var sunset: String? = null } \ No newline at end of file diff --git a/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt b/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt index 3ceb302e..d7b4080b 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/workers/OpenWeatherGetter.kt @@ -10,6 +10,7 @@ import com.google.android.gms.tasks.CancellationTokenSource import io.realm.kotlin.Realm import io.realm.kotlin.RealmConfiguration import io.realm.kotlin.UpdatePolicy +import io.realm.kotlin.ext.query import io.realm.kotlin.types.TypedRealmObject import rasel.lunar.launcher.model.WeatherInfo import rasel.lunar.launcher.utils.BLog @@ -20,45 +21,43 @@ import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory import retrofit2.create import retrofit2.http.GET +import retrofit2.http.Path +import retrofit2.http.Query import kotlin.reflect.KClass -class OpenWeatherGetter : BaseGetter { +class OpenWeatherGetter(context: Context, workerParams: WorkerParameters) : BaseGetter(context, workerParams) { companion object { val TAG = "OpenWeatherGetter" } - constructor(context: Context, workerParams: WorkerParameters) : super(context, workerParams) {} - + ////////////////////////////////////////// + val KEY = "87cd0810b7e4b4debd31a6ef98b98154" // openWeatherMap api key + var alt: Double? = null // 경도 + var lat: Double? = null // 위도 + ////////////////////////////////////////// override fun realWork(): Result { BLog.LOGE("${TAG} realWork() ") - - //위치 정보 {위 경도 가져오자} - getLocation() - - //87cd0810b7e4b4debd31a6ef98b98154 - //{https://home.openweathermap.org/api 에서 정보를 조회 하자} - getWeather() - - // 정형화된 정보를 취드하여 realm db에 저장 하자 - val config = RealmConfiguration.create( - schema = setOf(OpenWeatherGetter::class) as Set> - ) - val realm = Realm.open(config) - - realm.close() + // 위치 위,경도 가져오자 + setLocation() return Result.success() } @SuppressLint("MissingPermission") - fun getLocation() { + fun setLocation() { val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this.applicationContext) fusedLocationProviderClient.getCurrentLocation(Priority.PRIORITY_HIGH_ACCURACY,CancellationTokenSource().token) .addOnSuccessListener{ success: Location? -> success?.let { BLog.LOGE("Location >>> $it") - BLog.LOGE("Location altitude >>> ${it.altitude}") + BLog.LOGE("Location altitude >>> ${it.longitude}") + alt = it.longitude BLog.LOGE("Location latitude >>> ${it.latitude}") + lat = it.latitude + + //{https://home.openweathermap.org/api 에서 정보를 조회 하자} + getWeather() + } }.addOnFailureListener{ BLog.LOGE("Location error >>> $it") @@ -71,37 +70,42 @@ class OpenWeatherGetter : BaseGetter { .baseUrl("https://api.openweathermap.org") .addConverterFactory(GsonConverterFactory.create()) .build() - val service = retro.create() - - val call = service.getPosts() - - call?.enqueue(object: Callback { - override fun onResponse(call: Call, response: Response) { - BLog.LOGE("Location error >>> $response") - - // 받아온 결과를 메모리에 올려놓고 처리할 클래스가 필요해 - response.body()?.let {w-> + BLog.LOGE("Location lat >>> ${lat}") + BLog.LOGE("Location alt >>> ${alt}") + if (lat != null && alt != null) { + val call = service.getPosts(lat!!, alt!!, KEY) + call?.execute()?.let {response -> + response.body()?.let { w -> BLog.LOGE("Location error >>> ${w.coord}}") WorkersDb.getRealm().writeBlocking { - copyToRealm(w, UpdatePolicy.ALL) + w.filled().run { + copyToRealm(w, UpdatePolicy.ALL) + } } + BLog.LOGE("db에 저장된 weatherInfo >>> ${WorkersDb.getRealm().query().first().find()?.base}") } - - } - - override fun onFailure(call: Call, t: Throwable) { - BLog.LOGE("Location error >>> $t") - } - - - }) +// call?.enqueue(object : Callback { +// override fun onResponse( +// call: Call, +// response: Response +// ) { +// BLog.LOGE("Location error >>> $response") +// // Realm을 이용해서 db에 저장 +// +// } +// +// override fun onFailure(call: Call, t: Throwable) { +// BLog.LOGE("Location error >>> $t") +// } +// }) + } } } interface RestrofitService { - @GET("/data/2.5/weather?lat=44.34&lon=10.99&appid=87cd0810b7e4b4debd31a6ef98b98154") - fun getPosts(): Call? + @GET("/data/2.5/weather") + fun getPosts(@Query("lat") lat: Double, @Query("lon") alt: Double, @Query("appid") key: String): Call? } diff --git a/app/src/main/kotlin/rasel/lunar/launcher/workers/WorkersDb.kt b/app/src/main/kotlin/rasel/lunar/launcher/workers/WorkersDb.kt index 80627d73..4cdeee1c 100644 --- a/app/src/main/kotlin/rasel/lunar/launcher/workers/WorkersDb.kt +++ b/app/src/main/kotlin/rasel/lunar/launcher/workers/WorkersDb.kt @@ -14,15 +14,22 @@ import io.realm.kotlin.types.annotations.PrimaryKey import rasel.lunar.launcher.apps.SimpleContact import rasel.lunar.launcher.model.AppInfo import rasel.lunar.launcher.model.BotCommandEentitie +import rasel.lunar.launcher.model.Clouds import rasel.lunar.launcher.model.CurrentPlayItem +import rasel.lunar.launcher.model.Location +import rasel.lunar.launcher.model.Main import rasel.lunar.launcher.model.NotificationItem import rasel.lunar.launcher.model.RssData import rasel.lunar.launcher.model.RssDataInterface +import rasel.lunar.launcher.model.Sys import rasel.lunar.launcher.model.TelegramBotUpdate import rasel.lunar.launcher.model.TelegramChat import rasel.lunar.launcher.model.TelegramData import rasel.lunar.launcher.model.TelegramFrom import rasel.lunar.launcher.model.TelegramMessage +import rasel.lunar.launcher.model.Weather +import rasel.lunar.launcher.model.WeatherInfo +import rasel.lunar.launcher.model.Wind import kotlin.reflect.KClass object WorkersDb { @@ -30,7 +37,8 @@ object WorkersDb { val clazz : Set> = setOf(RssData::class, NotificationItem::class, AppInfo::class,SimpleContact::class, RecentCall::class, RecentSms::class, CurrentPlayItem::class, - TelegramBotUpdate::class, TelegramData::class, TelegramMessage::class, TelegramChat::class, BotCommandEentitie::class, TelegramFrom::class,) + TelegramBotUpdate::class, TelegramData::class, TelegramMessage::class, TelegramChat::class, BotCommandEentitie::class, TelegramFrom::class, WeatherInfo::class, Main::class, Weather::class, Location::class, Wind::class, Clouds::class, Sys::class + ) val schemaVersion : Long = 0L