1
0
Fork 0

implement getting the light status

master
Florian Schrofner 2022-02-14 21:38:34 +01:00
parent bc70fcfa44
commit b3e391bcfc
5 changed files with 50 additions and 13 deletions

View File

@ -28,6 +28,13 @@ kotlin {
}
}
}
targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
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

View File

@ -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<ElgatoLightStatus>("http://" + lightAddress + ":" + (port ?: 9123) + createPath(LIGHT_ENDPOINT))
return status.toLightStatus()
}
}
private fun createPath(pathElements: List<String>): 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<ElgatoLight>,
@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) }

View File

@ -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(

View File

@ -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<LightRepository> { ElgatoLightRepository() }
single<LightRepository> { ElgatoLightRepository(get()) }
}
val networkModule = module {
single<HttpClient> { HttpClient(CIO) }
single<HttpClient> { HttpClient(CIO){
install(JsonFeature)
}}
}
val mainModule = listOf(

View File

@ -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,9 +59,11 @@ class GetCommand(
private val targetLamp: String by argument(ARG_TARGET_LAMP)
override fun run() {
runBlocking {
val status = lightRepository.getLightStatus(targetLamp)
echo(status)
}
}
}
class ApplyCommand: CliktCommand(name = "apply", help = "Applies the given configuration to the specified light"){