diff --git a/build.gradle.kts b/build.gradle.kts index b2c445c..542fb31 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -28,6 +28,13 @@ kotlin { } } } + + targets.withType { + binaries.all { + freeCompilerArgs += "-Xdisable-phases=EscapeAnalysis" + } + } + sourceSets { val nativeMain by getting { dependencies { @@ -36,6 +43,7 @@ kotlin { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2") implementation("io.ktor:ktor-client-core:1.6.7") implementation("io.ktor:ktor-client-cio:1.6.7") + implementation("io.ktor:ktor-client-serialization:1.6.7") } } val nativeTest by getting diff --git a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt b/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt index 945ab56..357b7f1 100644 --- a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt +++ b/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt @@ -1,17 +1,36 @@ package fi.schro.data import fi.schro.ui.LightPowerState +import io.ktor.client.* +import io.ktor.client.request.* +import io.ktor.utils.io.core.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlin.math.roundToInt -class ElgatoLightRepository: LightRepository { - override fun setLightStatus(lightAddress: String, status: LightStatus) { +class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository { + //TODO: add light endpoint constant + private val PATH_SEPARATOR = "/" + private val DEFAULT_PATH = "elgato" + private val LIGHT_PATH = "lights" + private val ACCESSORY_INFO_PATH = "accessory-info" + + val LIGHT_ENDPOINT = listOf(DEFAULT_PATH, LIGHT_PATH) + val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH) + + override suspend fun setLightStatus(lightAddress: String, port: Int?, status: LightStatus) { TODO("Not yet implemented") } - override fun getLightStatus(lightAddress: String): LightStatus { - TODO("Not yet implemented") + override suspend fun getLightStatus(lightAddress: String, port: Int?): LightStatus { + httpClient.use { + val status = httpClient.get("http://" + lightAddress + ":" + (port ?: 9123) + createPath(LIGHT_ENDPOINT)) + return status.toLightStatus() + } + } + + private fun createPath(pathElements: List): String { + return pathElements.joinToString(separator = PATH_SEPARATOR, prefix = PATH_SEPARATOR) } } @@ -19,7 +38,11 @@ class ElgatoLightRepository: LightRepository { data class ElgatoLightStatus( @SerialName("lights") val lights: List, @SerialName("numberOfLights") val numberOfLights: Int = lights.size -) +){ + fun toLightStatus(): LightStatus { + return lights.first().toLightStatus() + } +} @Serializable data class ElgatoLight( @@ -27,8 +50,8 @@ data class ElgatoLight( @SerialName("brightness") val brightness: Int? = null, @SerialName("temperature") val temperature: Int? = null ){ - fun toLightStatus(){ - LightStatus( + fun toLightStatus(): LightStatus { + return LightStatus( powerStatus = on?.let { LightPowerState.fromInt(it) }, brightness = brightness, temperature = temperature?.let { convertElgatoTemperatureToKelvin(it) } diff --git a/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt b/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt index c8b2cb5..64f9d26 100644 --- a/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt +++ b/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt @@ -3,8 +3,8 @@ package fi.schro.data import fi.schro.ui.LightPowerState interface LightRepository { - fun setLightStatus(lightAddress: String, status: LightStatus) - fun getLightStatus(lightAddress: String): LightStatus + suspend fun setLightStatus(lightAddress: String, port: Int? = null, status: LightStatus) + suspend fun getLightStatus(lightAddress: String, port: Int? = null): LightStatus } data class LightStatus( diff --git a/src/nativeMain/kotlin/fi/schro/di/Modules.kt b/src/nativeMain/kotlin/fi/schro/di/Modules.kt index 367a3ab..6893ee3 100644 --- a/src/nativeMain/kotlin/fi/schro/di/Modules.kt +++ b/src/nativeMain/kotlin/fi/schro/di/Modules.kt @@ -7,6 +7,7 @@ import fi.schro.ui.GetCommand import fi.schro.ui.SetCommand import io.ktor.client.* import io.ktor.client.engine.cio.* +import io.ktor.client.features.json.* import org.koin.dsl.module val commandModule = module { @@ -16,11 +17,13 @@ val commandModule = module { } val dataModule = module { - single { ElgatoLightRepository() } + single { ElgatoLightRepository(get()) } } val networkModule = module { - single { HttpClient(CIO) } + single { HttpClient(CIO){ + install(JsonFeature) + }} } val mainModule = listOf( diff --git a/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt b/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt index 33225ce..047b9fa 100644 --- a/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt +++ b/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt @@ -8,6 +8,7 @@ import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.types.enum import com.github.ajalt.clikt.parameters.types.int import fi.schro.data.LightRepository +import kotlinx.coroutines.runBlocking import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -58,8 +59,10 @@ class GetCommand( private val targetLamp: String by argument(ARG_TARGET_LAMP) override fun run() { - val status = lightRepository.getLightStatus(targetLamp) - echo(status) + runBlocking { + val status = lightRepository.getLightStatus(targetLamp) + echo(status) + } } }