-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial WebSocket Implementation (#1884)
* Initial setup of WebSockets! * Got some good sets of table tennis going. * Move to a more kotlin friendly way to lock. * Functional get config call. * Remove testing function. * Linting. * Migrate get config calls to websockets! * Working retries. * Get services now as websocket request. * Remove unused service call via api. * Fix issue with widget not prompting the correct items. * Migrate to websocket get states. * ktlint. * Review Comments.
- Loading branch information
Showing
26 changed files
with
434 additions
and
229 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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
89 changes: 89 additions & 0 deletions
89
common/src/main/java/io/homeassistant/companion/android/common/data/HomeAssistantApis.kt
This file contains 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,89 @@ | ||
package io.homeassistant.companion.android.common.data | ||
|
||
import android.os.Build | ||
import android.webkit.CookieManager | ||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.PropertyNamingStrategy | ||
import com.fasterxml.jackson.module.kotlin.registerKotlinModule | ||
import io.homeassistant.companion.android.common.BuildConfig | ||
import io.homeassistant.companion.android.common.data.url.UrlRepository | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.jackson.JacksonConverterFactory | ||
import java.util.concurrent.TimeUnit | ||
import javax.inject.Inject | ||
|
||
class HomeAssistantApis @Inject constructor(private val urlRepository: UrlRepository) { | ||
companion object { | ||
private const val LOCAL_HOST = "http://localhost/" | ||
private const val USER_AGENT = "User-Agent" | ||
private const val USER_AGENT_STRING = "HomeAssistant/Android" | ||
|
||
private val CALL_TIMEOUT = 30L | ||
private val READ_TIMEOUT = 30L | ||
} | ||
|
||
private fun configureOkHttpClient(builder: OkHttpClient.Builder): OkHttpClient.Builder { | ||
if (BuildConfig.DEBUG) { | ||
builder.addInterceptor( | ||
HttpLoggingInterceptor().apply { | ||
level = HttpLoggingInterceptor.Level.BODY | ||
} | ||
) | ||
} | ||
builder.addInterceptor { | ||
return@addInterceptor if (it.request().url.toString().contains(LOCAL_HOST)) { | ||
val newRequest = runBlocking { | ||
it.request().newBuilder() | ||
.url( | ||
it.request().url.toString() | ||
.replace(LOCAL_HOST, urlRepository.getUrl().toString()) | ||
) | ||
.header( | ||
USER_AGENT, | ||
"$USER_AGENT_STRING ${Build.MODEL} ${BuildConfig.VERSION_NAME}" | ||
) | ||
.build() | ||
} | ||
it.proceed(newRequest) | ||
} else { | ||
it.proceed(it.request()) | ||
} | ||
} | ||
// Only deal with cookies when on non wear device and for now I don't have a better | ||
// way to determine if we are really on wear os.... | ||
// TODO: Please fix me. | ||
var cookieManager: CookieManager? = null | ||
try { | ||
cookieManager = CookieManager.getInstance() | ||
} catch (e: Exception) { | ||
// Noop | ||
} | ||
if (cookieManager != null) { | ||
builder.cookieJar(CookieJarCookieManagerShim()) | ||
} | ||
builder.callTimeout(CALL_TIMEOUT, TimeUnit.SECONDS) | ||
builder.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) | ||
|
||
return builder | ||
} | ||
|
||
val retrofit: Retrofit = Retrofit | ||
.Builder() | ||
.addConverterFactory( | ||
JacksonConverterFactory.create( | ||
ObjectMapper() | ||
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) | ||
.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE) | ||
.registerKotlinModule() | ||
) | ||
) | ||
.client(configureOkHttpClient(OkHttpClient.Builder()).build()) | ||
.baseUrl(LOCAL_HOST) | ||
.build() | ||
|
||
val okHttpClient = configureOkHttpClient(OkHttpClient.Builder()).build() | ||
} |
82 changes: 0 additions & 82 deletions
82
common/src/main/java/io/homeassistant/companion/android/common/data/HomeAssistantRetrofit.kt
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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
Oops, something went wrong.