From bb31d3b274441566df9e2148b7f48cac2f23dead Mon Sep 17 00:00:00 2001 From: Florian Schrofner Date: Sun, 20 Feb 2022 17:09:25 +0100 Subject: [PATCH] add daemon command, remove some echoes --- .../fi/schro/data/ConfigurationRepository.kt | 4 +-- .../fi/schro/data/ElgatoLightRepository.kt | 9 ++++++- src/nativeMain/kotlin/fi/schro/di/Modules.kt | 7 +++-- .../kotlin/fi/schro/ui/HappyCatCommand.kt | 27 +++++++++++++++++-- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt b/src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt index 30b0a64..d097a08 100644 --- a/src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt +++ b/src/nativeMain/kotlin/fi/schro/data/ConfigurationRepository.kt @@ -23,8 +23,6 @@ class ConfigurationRepositoryImpl( val statusToApply = determineCurrentStatus(configuration) statusToApply?.let { newStatus -> - echo("applying new status:") - echo(newStatus.toString()) applyLightStatus(lightAddress, port, newStatus) } } @@ -47,6 +45,8 @@ class ConfigurationRepositoryImpl( private suspend fun applyLightStatus(lightAddress: String, port: Int?, status: LightStatus){ val currentStatus = lightRepository.getLightStatus(lightAddress) currentStatus.getNecessaryChanges(status)?.let { statusUpdate -> + echo("applying status changes:") + echo(statusUpdate) lightRepository.setLightStatus(lightAddress, port, statusUpdate) } } diff --git a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt b/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt index 7c0421b..72595e0 100644 --- a/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt +++ b/src/nativeMain/kotlin/fi/schro/data/ElgatoLightRepository.kt @@ -8,14 +8,21 @@ import io.ktor.http.* import io.ktor.utils.io.core.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable +import org.koin.core.component.KoinComponent +import org.koin.core.component.get import kotlin.math.roundToInt -class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository { +class ElgatoLightRepository: LightRepository, KoinComponent { private val PATH_SEPARATOR = "/" private val DEFAULT_PATH = "elgato" private val LIGHT_PATH = "lights" private val ACCESSORY_INFO_PATH = "accessory-info" + //needs a new http client for every request + private val httpClient: HttpClient get() { + return get() + } + val LIGHT_ENDPOINT = listOf(DEFAULT_PATH, LIGHT_PATH) val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH) diff --git a/src/nativeMain/kotlin/fi/schro/di/Modules.kt b/src/nativeMain/kotlin/fi/schro/di/Modules.kt index d25d2ca..c8822cd 100644 --- a/src/nativeMain/kotlin/fi/schro/di/Modules.kt +++ b/src/nativeMain/kotlin/fi/schro/di/Modules.kt @@ -5,6 +5,7 @@ import fi.schro.data.ConfigurationRepositoryImpl import fi.schro.data.ElgatoLightRepository import fi.schro.data.LightRepository import fi.schro.ui.ApplyCommand +import fi.schro.ui.DaemonCommand import fi.schro.ui.GetCommand import fi.schro.ui.SetCommand import io.ktor.client.* @@ -14,17 +15,19 @@ import org.koin.dsl.module val commandModule = module { single{ ApplyCommand(get()) } + single{ DaemonCommand(get()) } single{ SetCommand(get()) } single{ GetCommand(get()) } } val dataModule = module { - single { ElgatoLightRepository(get()) } + single { ElgatoLightRepository() } single { ConfigurationRepositoryImpl(get()) } } val networkModule = module { - single { HttpClient(CIO){ + //HttpClient has to be recreated every time it is used + factory { HttpClient(CIO){ install(JsonFeature) }} } diff --git a/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt b/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt index 521bb66..34cab40 100644 --- a/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt +++ b/src/nativeMain/kotlin/fi/schro/ui/HappyCatCommand.kt @@ -10,7 +10,7 @@ import com.github.ajalt.clikt.parameters.types.int import fi.schro.data.ConfigurationRepository import fi.schro.data.LightRepository import fi.schro.data.LightStatus -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.* import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -19,12 +19,14 @@ const val ARG_CONFIGURATION_FILE = "CONFIGURATION_FILE" class HappyCatCommand: CliktCommand(name = "hc", help = "A commandline utility to control your elgato keylight"), KoinComponent { private val applyCommand: ApplyCommand by inject() + private val daemonCommand: DaemonCommand by inject() private val getCommand: GetCommand by inject() private val setCommand: SetCommand by inject() init { subcommands( applyCommand, + daemonCommand, getCommand, setCommand ) @@ -80,7 +82,7 @@ class GetCommand( class ApplyCommand( private val configurationRepository: ConfigurationRepository -): CliktCommand(name = "apply", help = "Applies the given configuration to the specified light"){ +): CliktCommand(name = "apply", help = "Applies the currently valid configuration inside the configuration file to the specified light"){ private val configurationFile: String by argument(ARG_CONFIGURATION_FILE) private val targetLamp: String by argument(ARG_TARGET_LAMP) @@ -91,6 +93,27 @@ class ApplyCommand( } } +class DaemonCommand( + private val configurationRepository: ConfigurationRepository +): CliktCommand(name = "daemon", help = "Starts a daemon which applies the currently valid configuration inside the configuration file every minute"){ + private val configurationFile: String by argument(ARG_CONFIGURATION_FILE) + private val targetLamp: String by argument(ARG_TARGET_LAMP) + + override fun run() { + runBlocking { + while(isActive){ + //continue daemon even if applying configuration failed once + try { + configurationRepository.applyConfiguration(configurationFile, targetLamp) + } catch (exception: Exception){ + echo(exception) + } + delay(1000 * 60) + } + } + } +} + enum class LightPowerStatus(val stringValue: String, val intValue: Int) { ON("ON", 1), OFF("OFF", 0);