weather realm

This commit is contained in:
JUNGGWAN KIM 2024-09-24 17:27:35 +09:00
parent 248e0ada31
commit 9349d32c7c
3 changed files with 117 additions and 47 deletions

View File

@ -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<Weather>?, 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<Weather>? = realmListOf()
var weather: ArrayList<Weather>? = arrayListOf()
var weathers: RealmList<Weather> = 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
}

View File

@ -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<KClass<out TypedRealmObject>>
)
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<RestrofitService>()
val call = service.getPosts()
call?.enqueue(object: Callback<WeatherInfo?> {
override fun onResponse(call: Call<WeatherInfo?>, response: Response<WeatherInfo?>) {
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<WeatherInfo>().first().find()?.base}")
}
}
override fun onFailure(call: Call<WeatherInfo?>, t: Throwable) {
BLog.LOGE("Location error >>> $t")
}
})
// call?.enqueue(object : Callback<WeatherInfo?> {
// override fun onResponse(
// call: Call<WeatherInfo?>,
// response: Response<WeatherInfo?>
// ) {
// BLog.LOGE("Location error >>> $response")
// // Realm을 이용해서 db에 저장
//
// }
//
// override fun onFailure(call: Call<WeatherInfo?>, 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<WeatherInfo?>?
@GET("/data/2.5/weather")
fun getPosts(@Query("lat") lat: Double, @Query("lon") alt: Double, @Query("appid") key: String): Call<WeatherInfo?>?
}

View File

@ -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<KClass<out BaseRealmObject>> = 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