start implementation of set command
parent
b3e391bcfc
commit
6451ede58a
|
@ -3,13 +3,15 @@ package fi.schro.data
|
||||||
import fi.schro.ui.LightPowerState
|
import fi.schro.ui.LightPowerState
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
|
import io.ktor.client.statement.*
|
||||||
|
import io.ktor.http.*
|
||||||
import io.ktor.utils.io.core.*
|
import io.ktor.utils.io.core.*
|
||||||
import kotlinx.serialization.SerialName
|
import kotlinx.serialization.SerialName
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
|
import platform.posix.stat
|
||||||
import kotlin.math.roundToInt
|
import kotlin.math.roundToInt
|
||||||
|
|
||||||
class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository {
|
class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository {
|
||||||
//TODO: add light endpoint constant
|
|
||||||
private val PATH_SEPARATOR = "/"
|
private val PATH_SEPARATOR = "/"
|
||||||
private val DEFAULT_PATH = "elgato"
|
private val DEFAULT_PATH = "elgato"
|
||||||
private val LIGHT_PATH = "lights"
|
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)
|
val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH)
|
||||||
|
|
||||||
override suspend fun setLightStatus(lightAddress: String, port: Int?, status: LightStatus) {
|
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 {
|
override suspend fun getLightStatus(lightAddress: String, port: Int?): LightStatus {
|
||||||
httpClient.use {
|
httpClient.use {
|
||||||
val status = httpClient.get<ElgatoLightStatus>("http://" + lightAddress + ":" + (port ?: 9123) + createPath(LIGHT_ENDPOINT))
|
val status = it.get<ElgatoLightStatus>(createUrl(lightAddress, port, LIGHT_ENDPOINT))
|
||||||
return status.toLightStatus()
|
return status.toLightStatus()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,6 +41,10 @@ class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository
|
||||||
private fun createPath(pathElements: List<String>): String {
|
private fun createPath(pathElements: List<String>): String {
|
||||||
return pathElements.joinToString(separator = PATH_SEPARATOR, prefix = PATH_SEPARATOR)
|
return pathElements.joinToString(separator = PATH_SEPARATOR, prefix = PATH_SEPARATOR)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun createUrl(lightAddress: String, port: Int?, endpoint: List<String>): String {
|
||||||
|
return "http://" + lightAddress + ":" + (port ?: 9123) + createPath(endpoint)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
@ -42,6 +55,14 @@ data class ElgatoLightStatus(
|
||||||
fun toLightStatus(): LightStatus {
|
fun toLightStatus(): LightStatus {
|
||||||
return lights.first().toLightStatus()
|
return lights.first().toLightStatus()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
fun fromLightStatus(status: LightStatus): ElgatoLightStatus {
|
||||||
|
return ElgatoLightStatus(listOf(
|
||||||
|
ElgatoLight.fromLightStatus(status)
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
|
@ -52,15 +73,26 @@ data class ElgatoLight(
|
||||||
){
|
){
|
||||||
fun toLightStatus(): LightStatus {
|
fun toLightStatus(): LightStatus {
|
||||||
return LightStatus(
|
return LightStatus(
|
||||||
powerStatus = on?.let { LightPowerState.fromInt(it) },
|
powerState = on?.let { LightPowerState.fromInt(it) },
|
||||||
brightness = brightness,
|
brightness = brightness,
|
||||||
temperature = temperature?.let { convertElgatoTemperatureToKelvin(it) }
|
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 {
|
private fun convertKelvinToElgatoTemperature(kelvinTemperature: Int): Int {
|
||||||
TODO("todo")
|
//TODO: implement real conversion
|
||||||
|
return 319
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun convertElgatoTemperatureToKelvin(elgatoTemperature: Int): Int {
|
private fun convertElgatoTemperatureToKelvin(elgatoTemperature: Int): Int {
|
||||||
|
|
|
@ -8,13 +8,13 @@ interface LightRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
data class LightStatus(
|
data class LightStatus(
|
||||||
val powerStatus: LightPowerState?,
|
val powerState: LightPowerState?,
|
||||||
val brightness: Int?,
|
val brightness: Int?,
|
||||||
val temperature: Int?
|
val temperature: Int?
|
||||||
){
|
){
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
val stringList = mutableListOf<String>()
|
val stringList = mutableListOf<String>()
|
||||||
powerStatus?.let { stringList.add("status: ${powerStatus.stringValue}") }
|
powerState?.let { stringList.add("status: ${powerState.stringValue}") }
|
||||||
brightness?.let { stringList.add("brightness: $brightness") }
|
brightness?.let { stringList.add("brightness: $brightness") }
|
||||||
temperature?.let { stringList.add("temperature: $temperature") }
|
temperature?.let { stringList.add("temperature: $temperature") }
|
||||||
return stringList.joinToString(separator = "\n")
|
return stringList.joinToString(separator = "\n")
|
||||||
|
|
|
@ -12,7 +12,7 @@ import org.koin.dsl.module
|
||||||
|
|
||||||
val commandModule = module {
|
val commandModule = module {
|
||||||
single{ ApplyCommand() }
|
single{ ApplyCommand() }
|
||||||
single{ SetCommand() }
|
single{ SetCommand(get()) }
|
||||||
single{ GetCommand(get()) }
|
single{ GetCommand(get()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.enum
|
||||||
import com.github.ajalt.clikt.parameters.types.int
|
import com.github.ajalt.clikt.parameters.types.int
|
||||||
import fi.schro.data.LightRepository
|
import fi.schro.data.LightRepository
|
||||||
|
import fi.schro.data.LightStatus
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import org.koin.core.component.KoinComponent
|
import org.koin.core.component.KoinComponent
|
||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
|
@ -31,7 +32,9 @@ class HappyCatCommand: CliktCommand(), KoinComponent {
|
||||||
override fun run() = Unit
|
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 targetLamp: String by argument(ARG_TARGET_LAMP)
|
||||||
private val brightness: Int? by option(
|
private val brightness: Int? by option(
|
||||||
"-b",
|
"-b",
|
||||||
|
@ -49,7 +52,15 @@ class SetCommand: CliktCommand(name = "set", help = "Sets the defined values to
|
||||||
|
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
echo("Values set")
|
val status = LightStatus(
|
||||||
|
powerState = powerState,
|
||||||
|
brightness = brightness,
|
||||||
|
temperature = temperature
|
||||||
|
)
|
||||||
|
|
||||||
|
runBlocking {
|
||||||
|
lightRepository.setLightStatus(lightAddress = targetLamp, status = status)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue