setup koin, add light repository, start network setup
This commit is contained in:
		
							parent
							
								
									6a3e65fdd0
								
							
						
					
					
						commit
						bc70fcfa44
					
				
					 7 changed files with 194 additions and 3 deletions
				
			
		| 
						 | 
					@ -1,5 +1,6 @@
 | 
				
			||||||
plugins {
 | 
					plugins {
 | 
				
			||||||
    kotlin("multiplatform") version "1.6.10"
 | 
					    kotlin("multiplatform") version "1.6.10"
 | 
				
			||||||
 | 
					    kotlin("plugin.serialization") version "1.6.10"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
group = "fi.schro"
 | 
					group = "fi.schro"
 | 
				
			||||||
| 
						 | 
					@ -32,6 +33,9 @@ kotlin {
 | 
				
			||||||
            dependencies {
 | 
					            dependencies {
 | 
				
			||||||
                implementation("io.insert-koin:koin-core:3.1.5")
 | 
					                implementation("io.insert-koin:koin-core:3.1.5")
 | 
				
			||||||
                implementation("com.github.ajalt.clikt:clikt:3.4.0")
 | 
					                implementation("com.github.ajalt.clikt:clikt:3.4.0")
 | 
				
			||||||
 | 
					                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")
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        val nativeTest by getting
 | 
					        val nativeTest by getting
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,4 @@
 | 
				
			||||||
kotlin.code.style=official
 | 
					kotlin.code.style=official
 | 
				
			||||||
kotlin.mpp.enableGranularSourceSetsMetadata=true
 | 
					kotlin.mpp.enableGranularSourceSetsMetadata=true
 | 
				
			||||||
kotlin.native.enableDependencyPropagation=false
 | 
					kotlin.native.enableDependencyPropagation=false
 | 
				
			||||||
 | 
					kotlin.native.cacheKind.linuxX64=none
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,14 @@
 | 
				
			||||||
package fi.schro
 | 
					package fi.schro
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fun main() {
 | 
					import fi.schro.di.mainModule
 | 
				
			||||||
    println("Hello, Kotlin/Native!")
 | 
					import fi.schro.ui.HappyCatCommand
 | 
				
			||||||
 | 
					import org.koin.core.context.startKoin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fun main(args: Array<String>) {
 | 
				
			||||||
 | 
					    startKoin {
 | 
				
			||||||
 | 
					        modules(mainModule)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    HappyCatCommand()
 | 
				
			||||||
 | 
					        .main(args)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
							
								
								
									
										46
									
								
								src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,46 @@
 | 
				
			||||||
 | 
					package fi.schro.data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import fi.schro.ui.LightPowerState
 | 
				
			||||||
 | 
					import kotlinx.serialization.SerialName
 | 
				
			||||||
 | 
					import kotlinx.serialization.Serializable
 | 
				
			||||||
 | 
					import kotlin.math.roundToInt
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ElgatoLightRepository: LightRepository {
 | 
				
			||||||
 | 
					    override fun setLightStatus(lightAddress: String, status: LightStatus) {
 | 
				
			||||||
 | 
					        TODO("Not yet implemented")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun getLightStatus(lightAddress: String): LightStatus {
 | 
				
			||||||
 | 
					        TODO("Not yet implemented")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Serializable
 | 
				
			||||||
 | 
					data class ElgatoLightStatus(
 | 
				
			||||||
 | 
					    @SerialName("lights") val lights: List<ElgatoLight>,
 | 
				
			||||||
 | 
					    @SerialName("numberOfLights") val numberOfLights: Int = lights.size
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Serializable
 | 
				
			||||||
 | 
					data class ElgatoLight(
 | 
				
			||||||
 | 
					    @SerialName("on") val on: Int? = null,
 | 
				
			||||||
 | 
					    @SerialName("brightness") val brightness: Int? = null,
 | 
				
			||||||
 | 
					    @SerialName("temperature") val temperature: Int? = null
 | 
				
			||||||
 | 
					){
 | 
				
			||||||
 | 
					    fun toLightStatus(){
 | 
				
			||||||
 | 
					        LightStatus(
 | 
				
			||||||
 | 
					            powerStatus = on?.let { LightPowerState.fromInt(it) },
 | 
				
			||||||
 | 
					            brightness = brightness,
 | 
				
			||||||
 | 
					            temperature = temperature?.let { convertElgatoTemperatureToKelvin(it) }
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private fun convertKelvinToElgatoTemperature(kelvinTemperature: Int): Int {
 | 
				
			||||||
 | 
					    TODO("todo")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					private fun convertElgatoTemperatureToKelvin(elgatoTemperature: Int): Int {
 | 
				
			||||||
 | 
					    //based on: https://github.com/justinforlenza/keylight-control/blob/main/src/keylight.js
 | 
				
			||||||
 | 
					    return (((-4100*elgatoTemperature) / 201f) + 1993300/201f).roundToInt()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										22
									
								
								src/nativeMain/kotlin/fi/schro/data/LightRepository.kt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								src/nativeMain/kotlin/fi/schro/data/LightRepository.kt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,22 @@
 | 
				
			||||||
 | 
					package fi.schro.data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import fi.schro.ui.LightPowerState
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					interface LightRepository {
 | 
				
			||||||
 | 
					    fun setLightStatus(lightAddress: String, status: LightStatus)
 | 
				
			||||||
 | 
					    fun getLightStatus(lightAddress: String): LightStatus
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					data class LightStatus(
 | 
				
			||||||
 | 
					    val powerStatus: LightPowerState?,
 | 
				
			||||||
 | 
					    val brightness: Int?,
 | 
				
			||||||
 | 
					    val temperature: Int?
 | 
				
			||||||
 | 
					){
 | 
				
			||||||
 | 
					    override fun toString(): String {
 | 
				
			||||||
 | 
					        val stringList = mutableListOf<String>()
 | 
				
			||||||
 | 
					        powerStatus?.let { stringList.add("status: ${powerStatus.stringValue}") }
 | 
				
			||||||
 | 
					        brightness?.let { stringList.add("brightness: $brightness") }
 | 
				
			||||||
 | 
					        temperature?.let { stringList.add("temperature: $temperature") }
 | 
				
			||||||
 | 
					        return stringList.joinToString(separator = "\n")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1 +1,30 @@
 | 
				
			||||||
package fi.schro.di
 | 
					package fi.schro.di
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import fi.schro.data.ElgatoLightRepository
 | 
				
			||||||
 | 
					import fi.schro.data.LightRepository
 | 
				
			||||||
 | 
					import fi.schro.ui.ApplyCommand
 | 
				
			||||||
 | 
					import fi.schro.ui.GetCommand
 | 
				
			||||||
 | 
					import fi.schro.ui.SetCommand
 | 
				
			||||||
 | 
					import io.ktor.client.*
 | 
				
			||||||
 | 
					import io.ktor.client.engine.cio.*
 | 
				
			||||||
 | 
					import org.koin.dsl.module
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					val commandModule = module {
 | 
				
			||||||
 | 
					    single{ ApplyCommand() }
 | 
				
			||||||
 | 
					    single{ SetCommand() }
 | 
				
			||||||
 | 
					    single{ GetCommand(get()) }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					val dataModule = module {
 | 
				
			||||||
 | 
					    single<LightRepository> { ElgatoLightRepository() }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					val networkModule = module {
 | 
				
			||||||
 | 
					    single<HttpClient> { HttpClient(CIO) }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					val mainModule = listOf(
 | 
				
			||||||
 | 
					    commandModule,
 | 
				
			||||||
 | 
					    dataModule,
 | 
				
			||||||
 | 
					    networkModule
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
| 
						 | 
					@ -1,4 +1,84 @@
 | 
				
			||||||
package fi.schro.ui
 | 
					package fi.schro.ui
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class HappyCatCommand {
 | 
					import com.github.ajalt.clikt.core.CliktCommand
 | 
				
			||||||
 | 
					import com.github.ajalt.clikt.core.subcommands
 | 
				
			||||||
 | 
					import com.github.ajalt.clikt.parameters.arguments.argument
 | 
				
			||||||
 | 
					import com.github.ajalt.clikt.parameters.options.check
 | 
				
			||||||
 | 
					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 org.koin.core.component.KoinComponent
 | 
				
			||||||
 | 
					import org.koin.core.component.inject
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const val ARG_TARGET_LAMP = "TARGET_LAMP"
 | 
				
			||||||
 | 
					const val ARG_CONFIGURATION_FILE = "CONFIGURATION_FILE"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class HappyCatCommand: CliktCommand(), KoinComponent {
 | 
				
			||||||
 | 
					    private val applyCommand: ApplyCommand by inject()
 | 
				
			||||||
 | 
					    private val getCommand: GetCommand by inject()
 | 
				
			||||||
 | 
					    private val setCommand: SetCommand by inject()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    init {
 | 
				
			||||||
 | 
					        subcommands(
 | 
				
			||||||
 | 
					            applyCommand,
 | 
				
			||||||
 | 
					            getCommand,
 | 
				
			||||||
 | 
					            setCommand
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun run() = Unit
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class SetCommand: 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",
 | 
				
			||||||
 | 
					        "--brightness",
 | 
				
			||||||
 | 
					        help = "The brightness to be set in percent of the maximum"
 | 
				
			||||||
 | 
					    ).int().check("Value must be between 0 and 100") {
 | 
				
			||||||
 | 
					        it in 0..100
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    private val temperature: Int? by option("-t", "--temperature", help = "The temperature to be set in Kelvin").int()
 | 
				
			||||||
 | 
					        .check("Value must be between 1,000 and 10,000") {
 | 
				
			||||||
 | 
					            it in 1000..10000
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private val powerState: LightPowerState? by option("-p", "--powerstate", help = "The state to be set").enum<LightPowerState>()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun run() {
 | 
				
			||||||
 | 
					        echo("Values set")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class GetCommand(
 | 
				
			||||||
 | 
					    private val lightRepository: LightRepository
 | 
				
			||||||
 | 
					) : CliktCommand(name = "get", help = "Gets and prints the current setting of the specified light") {
 | 
				
			||||||
 | 
					    private val targetLamp: String by argument(ARG_TARGET_LAMP)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun run() {
 | 
				
			||||||
 | 
					        val status = lightRepository.getLightStatus(targetLamp)
 | 
				
			||||||
 | 
					        echo(status)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ApplyCommand: CliktCommand(name = "apply", help = "Applies the given configuration to the specified light"){
 | 
				
			||||||
 | 
					    private val configurationFile: String by argument(ARG_CONFIGURATION_FILE)
 | 
				
			||||||
 | 
					    private val targetLamp: String by argument(ARG_TARGET_LAMP)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    override fun run() {
 | 
				
			||||||
 | 
					        echo("Configuration applied")
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					enum class LightPowerState(val stringValue: String, val intValue: Int) {
 | 
				
			||||||
 | 
					    ON("ON", 1),
 | 
				
			||||||
 | 
					    OFF("OFF", 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    companion object {
 | 
				
			||||||
 | 
					        fun fromInt(intValue: Int): LightPowerState? {
 | 
				
			||||||
 | 
					            return values().firstOrNull { it.intValue == intValue }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue