<file_sep>package com.example.transmissoriotparacegos.providers

import com.example.transmissoriotparacegos.models.IoTDevice
import com.google.gson.GsonBuilder
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import rx.Observable

class IoTApi(val ip: String) {
    var service: IoTServiceDef
    init {
        val logging = HttpLoggingInterceptor()
        logging.level = HttpLoggingInterceptor.Level.BODY
        val httpClient = OkHttpClient.Builder()
        httpClient.addInterceptor(logging)
        val gson = GsonBuilder().setLenient().create()
        val retrofit = Retrofit.Builder().apply {
            baseUrl("$ip/")
            addCallAdapterFactory(RxJavaCallAdapterFactory.create())
            addConverterFactory((GsonConverterFactory.create(gson)))
            client(httpClient.build())
        }.build()
        service = retrofit.create<IoTServiceDef>(IoTServiceDef::class.java)
    }

    fun getVariaveis() : Observable<IoTDevice> {
        return service.getVariaveis()
            .map {
                IoTDevice(it.nome, it.descricao, it.capacidades)
            }
    }
}<file_sep>package com.example.transmissoriotparacegos.providers

import com.example.transmissoriotparacegos.models.IoTDevice
import retrofit2.http.GET
import rx.Observable

interface IoTServiceDef {
    @GET("/variavel")
    fun getVariaveis(): Observable<IoTDevice>
}<file_sep>package com.example.transmissoriotparacegos.ui.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.transmissoriotparacegos.models.IoTDevice
import com.example.transmissoriotparacegos.providers.IoTApi
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers

class HomeViewModel(val ip: String) : ViewModel() {
    private val api = IoTApi(ip)
    lateinit var iotDevice: MutableLiveData<IoTDevice>
    init {
        api.getVariaveis()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                iotDevice.postValue(it)
            }
    }
}
