From 6451ede58acb3453d20eddeee659d24e6fdd05a1 Mon Sep 17 00:00:00 2001 From: Florian Schrofner Date: Mon, 14 Feb 2022 22:18:58 +0100 Subject: [PATCH] start implementation of set command --- .../fi/schro/data/ElgatoLightRepository.kt | 42 ++++++++++++++++--- .../kotlin/fi/schro/data/LightRepository.kt | 4 +- src/nativeMain/kotlin/fi/schro/di/Modules.kt | 2 +- .../kotlin/fi/schro/ui/HappyCatCommand.kt | 15 ++++++- 4 files changed, 53 insertions(+), 10 deletions(-) diff --git a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt b/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt index 357b7f1..7f41b41 100644 --- a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt +++ b/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt @@ -3,13 +3,15 @@ package fi.schro.data import fi.schro.ui.LightPowerState import io.ktor.client.* import io.ktor.client.request.* +import io.ktor.client.statement.* +import io.ktor.http.* import io.ktor.utils.io.core.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import platform.posix.stat import kotlin.math.roundToInt 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" @@ -19,12 +21,19 @@ class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH) override suspend fun setLightStatus(lightAddress: String, port: Int?, status: LightStatus) { - TODO("Not yet implemented") + val elgatoStatus = ElgatoLightStatus.fromLightStatus(status) + + httpClient.use { + val response: HttpResponse = it.put(createUrl(lightAddress, port, LIGHT_ENDPOINT)){ + contentType(ContentType.Application.Json) + body = elgatoStatus + } + } } override suspend fun getLightStatus(lightAddress: String, port: Int?): LightStatus { httpClient.use { - val status = httpClient.get("http://" + lightAddress + ":" + (port ?: 9123) + createPath(LIGHT_ENDPOINT)) + val status = it.get(createUrl(lightAddress, port, LIGHT_ENDPOINT)) return status.toLightStatus() } } @@ -32,6 +41,10 @@ class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository private fun createPath(pathElements: List): String { return pathElements.joinToString(separator = PATH_SEPARATOR, prefix = PATH_SEPARATOR) } + + private fun createUrl(lightAddress: String, port: Int?, endpoint: List): String { + return "http://" + lightAddress + ":" + (port ?: 9123) + createPath(endpoint) + } } @Serializable @@ -42,6 +55,14 @@ data class ElgatoLightStatus( fun toLightStatus(): LightStatus { return lights.first().toLightStatus() } + + companion object { + fun fromLightStatus(status: LightStatus): ElgatoLightStatus { + return ElgatoLightStatus(listOf( + ElgatoLight.fromLightStatus(status) + )) + } + } } @Serializable @@ -52,15 +73,26 @@ data class ElgatoLight( ){ fun toLightStatus(): LightStatus { return LightStatus( - powerStatus = on?.let { LightPowerState.fromInt(it) }, + powerState = on?.let { LightPowerState.fromInt(it) }, brightness = brightness, temperature = temperature?.let { convertElgatoTemperatureToKelvin(it) } ) } + + companion object { + fun fromLightStatus(status: LightStatus): ElgatoLight { + return ElgatoLight( + on = status.powerState?.intValue, + brightness = status.brightness, + temperature = status.temperature?.let { convertKelvinToElgatoTemperature(it) } + ) + } + } } private fun convertKelvinToElgatoTemperature(kelvinTemperature: Int): Int { - TODO("todo") + //TODO: implement real conversion + return 319 } private fun convertElgatoTemperatureToKelvin(elgatoTemperature: Int): Int { diff --git a/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt b/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt index 64f9d26..7fdc0aa 100644 --- a/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt +++ b/src/nativeMain/kotlin/fi/schro/data/LightRepository.kt @@ -8,13 +8,13 @@ interface LightRepository { } data class LightStatus( - val powerStatus: LightPowerState?, + val powerState: LightPowerState?, val brightness: Int?, val temperature: Int? ){ override fun toString(): String { val stringList = mutableListOf() - powerStatus?.let { stringList.add("status: ${powerStatus.stringValue}") } + powerState?.let { stringList.add("status: ${powerState.stringValue}") } brightness?.let { stringList.add("brightness: $brightness") } temperature?.let { stringList.add("temperature: $temperature") } return stringList.joinToString(separator = "\n") diff --git a/src/nativeMain/kotlin/fi/schro/di/Modules.kt b/src/nativeMain/kotlin/fi/schro/di/Modules.kt index 6893ee3..4004e94 100644 --- a/src/nativeMain/kotlin/fi/schro/di/Modules.kt +++ b/src/nativeMain/kotlin/fi/schro/di/Modules.kt @@ -12,7 +12,7 @@ import org.koin.dsl.module val commandModule = module { single{ ApplyCommand() } - single{ SetCommand() } + single{ SetCommand(get()) } single{ GetCommand(get()) } } diff --git a/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt b/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt index 047b9fa..031f00c 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 fi.schro.data.LightStatus import kotlinx.coroutines.runBlocking import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -31,7 +32,9 @@ class HappyCatCommand: CliktCommand(), KoinComponent { override fun run() = Unit } -class SetCommand: CliktCommand(name = "set", help = "Sets the defined values to the specified light"){ +class SetCommand( + private val lightRepository: LightRepository +): CliktCommand(name = "set", help = "Sets the defined values to the specified light"){ private val targetLamp: String by argument(ARG_TARGET_LAMP) private val brightness: Int? by option( "-b", @@ -49,7 +52,15 @@ class SetCommand: CliktCommand(name = "set", help = "Sets the defined values to override fun run() { - echo("Values set") + val status = LightStatus( + powerState = powerState, + brightness = brightness, + temperature = temperature + ) + + runBlocking { + lightRepository.setLightStatus(lightAddress = targetLamp, status = status) + } } }