implement getting the light status
This commit is contained in:
		
							parent
							
								
									bc70fcfa44
								
							
						
					
					
						commit
						b3e391bcfc
					
				
					 5 changed files with 50 additions and 13 deletions
				
			
		| 
						 | 
					@ -28,6 +28,13 @@ kotlin {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget> {
 | 
				
			||||||
 | 
					        binaries.all {
 | 
				
			||||||
 | 
					            freeCompilerArgs += "-Xdisable-phases=EscapeAnalysis"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    sourceSets {
 | 
					    sourceSets {
 | 
				
			||||||
        val nativeMain by getting {
 | 
					        val nativeMain by getting {
 | 
				
			||||||
            dependencies {
 | 
					            dependencies {
 | 
				
			||||||
| 
						 | 
					@ -36,6 +43,7 @@ kotlin {
 | 
				
			||||||
                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
 | 
					                implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.2")
 | 
				
			||||||
                implementation("io.ktor:ktor-client-core:1.6.7")
 | 
					                implementation("io.ktor:ktor-client-core:1.6.7")
 | 
				
			||||||
                implementation("io.ktor:ktor-client-cio: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
 | 
					        val nativeTest by getting
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,17 +1,36 @@
 | 
				
			||||||
package fi.schro.data
 | 
					package fi.schro.data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import fi.schro.ui.LightPowerState
 | 
					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.SerialName
 | 
				
			||||||
import kotlinx.serialization.Serializable
 | 
					import kotlinx.serialization.Serializable
 | 
				
			||||||
import kotlin.math.roundToInt
 | 
					import kotlin.math.roundToInt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ElgatoLightRepository: LightRepository {
 | 
					class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository {
 | 
				
			||||||
    override fun setLightStatus(lightAddress: String, status: LightStatus) {
 | 
					    //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")
 | 
					        TODO("Not yet implemented")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun getLightStatus(lightAddress: String): LightStatus {
 | 
					    override suspend fun getLightStatus(lightAddress: String, port: Int?): LightStatus {
 | 
				
			||||||
        TODO("Not yet implemented")
 | 
					        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(
 | 
					data class ElgatoLightStatus(
 | 
				
			||||||
    @SerialName("lights") val lights: List<ElgatoLight>,
 | 
					    @SerialName("lights") val lights: List<ElgatoLight>,
 | 
				
			||||||
    @SerialName("numberOfLights") val numberOfLights: Int = lights.size
 | 
					    @SerialName("numberOfLights") val numberOfLights: Int = lights.size
 | 
				
			||||||
)
 | 
					){
 | 
				
			||||||
 | 
					    fun toLightStatus(): LightStatus {
 | 
				
			||||||
 | 
					        return lights.first().toLightStatus()
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Serializable
 | 
					@Serializable
 | 
				
			||||||
data class ElgatoLight(
 | 
					data class ElgatoLight(
 | 
				
			||||||
| 
						 | 
					@ -27,8 +50,8 @@ data class ElgatoLight(
 | 
				
			||||||
    @SerialName("brightness") val brightness: Int? = null,
 | 
					    @SerialName("brightness") val brightness: Int? = null,
 | 
				
			||||||
    @SerialName("temperature") val temperature: Int? = null
 | 
					    @SerialName("temperature") val temperature: Int? = null
 | 
				
			||||||
){
 | 
					){
 | 
				
			||||||
    fun toLightStatus(){
 | 
					    fun toLightStatus(): LightStatus {
 | 
				
			||||||
        LightStatus(
 | 
					        return LightStatus(
 | 
				
			||||||
            powerStatus = on?.let { LightPowerState.fromInt(it) },
 | 
					            powerStatus = on?.let { LightPowerState.fromInt(it) },
 | 
				
			||||||
            brightness = brightness,
 | 
					            brightness = brightness,
 | 
				
			||||||
            temperature = temperature?.let { convertElgatoTemperatureToKelvin(it) }
 | 
					            temperature = temperature?.let { convertElgatoTemperatureToKelvin(it) }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,8 +3,8 @@ package fi.schro.data
 | 
				
			||||||
import fi.schro.ui.LightPowerState
 | 
					import fi.schro.ui.LightPowerState
 | 
				
			||||||
 | 
					
 | 
				
			||||||
interface LightRepository {
 | 
					interface LightRepository {
 | 
				
			||||||
    fun setLightStatus(lightAddress: String, status: LightStatus)
 | 
					    suspend fun setLightStatus(lightAddress: String, port: Int? = null, status: LightStatus)
 | 
				
			||||||
    fun getLightStatus(lightAddress: String): LightStatus
 | 
					    suspend fun getLightStatus(lightAddress: String, port: Int? = null): LightStatus
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
data class LightStatus(
 | 
					data class LightStatus(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,6 +7,7 @@ import fi.schro.ui.GetCommand
 | 
				
			||||||
import fi.schro.ui.SetCommand
 | 
					import fi.schro.ui.SetCommand
 | 
				
			||||||
import io.ktor.client.*
 | 
					import io.ktor.client.*
 | 
				
			||||||
import io.ktor.client.engine.cio.*
 | 
					import io.ktor.client.engine.cio.*
 | 
				
			||||||
 | 
					import io.ktor.client.features.json.*
 | 
				
			||||||
import org.koin.dsl.module
 | 
					import org.koin.dsl.module
 | 
				
			||||||
 | 
					
 | 
				
			||||||
val commandModule = module {
 | 
					val commandModule = module {
 | 
				
			||||||
| 
						 | 
					@ -16,11 +17,13 @@ val commandModule = module {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
val dataModule = module {
 | 
					val dataModule = module {
 | 
				
			||||||
    single<LightRepository> { ElgatoLightRepository() }
 | 
					    single<LightRepository> { ElgatoLightRepository(get()) }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
val networkModule = module {
 | 
					val networkModule = module {
 | 
				
			||||||
    single<HttpClient> { HttpClient(CIO) }
 | 
					    single<HttpClient> { HttpClient(CIO){
 | 
				
			||||||
 | 
					        install(JsonFeature)
 | 
				
			||||||
 | 
					    }}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
val mainModule = listOf(
 | 
					val mainModule = listOf(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -58,9 +59,11 @@ class GetCommand(
 | 
				
			||||||
    private val targetLamp: String by argument(ARG_TARGET_LAMP)
 | 
					    private val targetLamp: String by argument(ARG_TARGET_LAMP)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    override fun run() {
 | 
					    override fun run() {
 | 
				
			||||||
 | 
					        runBlocking {
 | 
				
			||||||
            val status = lightRepository.getLightStatus(targetLamp)
 | 
					            val status = lightRepository.getLightStatus(targetLamp)
 | 
				
			||||||
            echo(status)
 | 
					            echo(status)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ApplyCommand: CliktCommand(name = "apply", help = "Applies the given configuration to the specified light"){
 | 
					class ApplyCommand: CliktCommand(name = "apply", help = "Applies the given configuration to the specified light"){
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue