1
0
Fork 0

add daemon command, remove some echoes

master
Florian Schrofner 2022-02-20 17:09:25 +01:00
parent fa1e4eb966
commit bb31d3b274
4 changed files with 40 additions and 7 deletions

View File

@ -23,8 +23,6 @@ class ConfigurationRepositoryImpl(
val statusToApply = determineCurrentStatus(configuration) val statusToApply = determineCurrentStatus(configuration)
statusToApply?.let { newStatus -> statusToApply?.let { newStatus ->
echo("applying new status:")
echo(newStatus.toString())
applyLightStatus(lightAddress, port, newStatus) applyLightStatus(lightAddress, port, newStatus)
} }
} }
@ -47,6 +45,8 @@ class ConfigurationRepositoryImpl(
private suspend fun applyLightStatus(lightAddress: String, port: Int?, status: LightStatus){ private suspend fun applyLightStatus(lightAddress: String, port: Int?, status: LightStatus){
val currentStatus = lightRepository.getLightStatus(lightAddress) val currentStatus = lightRepository.getLightStatus(lightAddress)
currentStatus.getNecessaryChanges(status)?.let { statusUpdate -> currentStatus.getNecessaryChanges(status)?.let { statusUpdate ->
echo("applying status changes:")
echo(statusUpdate)
lightRepository.setLightStatus(lightAddress, port, statusUpdate) lightRepository.setLightStatus(lightAddress, port, statusUpdate)
} }
} }

View File

@ -8,14 +8,21 @@ 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 org.koin.core.component.KoinComponent
import org.koin.core.component.get
import kotlin.math.roundToInt import kotlin.math.roundToInt
class ElgatoLightRepository(private val httpClient: HttpClient): LightRepository { class ElgatoLightRepository: LightRepository, KoinComponent {
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"
private val ACCESSORY_INFO_PATH = "accessory-info" 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 LIGHT_ENDPOINT = listOf(DEFAULT_PATH, LIGHT_PATH)
val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH) val ACCESSORY_INFO_ENDPOINT = listOf(DEFAULT_PATH, ACCESSORY_INFO_PATH)

View File

@ -5,6 +5,7 @@ import fi.schro.data.ConfigurationRepositoryImpl
import fi.schro.data.ElgatoLightRepository import fi.schro.data.ElgatoLightRepository
import fi.schro.data.LightRepository import fi.schro.data.LightRepository
import fi.schro.ui.ApplyCommand import fi.schro.ui.ApplyCommand
import fi.schro.ui.DaemonCommand
import fi.schro.ui.GetCommand import fi.schro.ui.GetCommand
import fi.schro.ui.SetCommand import fi.schro.ui.SetCommand
import io.ktor.client.* import io.ktor.client.*
@ -14,17 +15,19 @@ import org.koin.dsl.module
val commandModule = module { val commandModule = module {
single{ ApplyCommand(get()) } single{ ApplyCommand(get()) }
single{ DaemonCommand(get()) }
single{ SetCommand(get()) } single{ SetCommand(get()) }
single{ GetCommand(get()) } single{ GetCommand(get()) }
} }
val dataModule = module { val dataModule = module {
single<LightRepository> { ElgatoLightRepository(get()) } single<LightRepository> { ElgatoLightRepository() }
single<ConfigurationRepository> { ConfigurationRepositoryImpl(get()) } single<ConfigurationRepository> { ConfigurationRepositoryImpl(get()) }
} }
val networkModule = module { val networkModule = module {
single<HttpClient> { HttpClient(CIO){ //HttpClient has to be recreated every time it is used
factory<HttpClient> { HttpClient(CIO){
install(JsonFeature) install(JsonFeature)
}} }}
} }

View File

@ -10,7 +10,7 @@ import com.github.ajalt.clikt.parameters.types.int
import fi.schro.data.ConfigurationRepository import fi.schro.data.ConfigurationRepository
import fi.schro.data.LightRepository import fi.schro.data.LightRepository
import fi.schro.data.LightStatus import fi.schro.data.LightStatus
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.*
import org.koin.core.component.KoinComponent import org.koin.core.component.KoinComponent
import org.koin.core.component.inject 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 { class HappyCatCommand: CliktCommand(name = "hc", help = "A commandline utility to control your elgato keylight"), KoinComponent {
private val applyCommand: ApplyCommand by inject() private val applyCommand: ApplyCommand by inject()
private val daemonCommand: DaemonCommand by inject()
private val getCommand: GetCommand by inject() private val getCommand: GetCommand by inject()
private val setCommand: SetCommand by inject() private val setCommand: SetCommand by inject()
init { init {
subcommands( subcommands(
applyCommand, applyCommand,
daemonCommand,
getCommand, getCommand,
setCommand setCommand
) )
@ -80,7 +82,7 @@ class GetCommand(
class ApplyCommand( class ApplyCommand(
private val configurationRepository: ConfigurationRepository 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 configurationFile: String by argument(ARG_CONFIGURATION_FILE)
private val targetLamp: String by argument(ARG_TARGET_LAMP) 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) { enum class LightPowerStatus(val stringValue: String, val intValue: Int) {
ON("ON", 1), ON("ON", 1),
OFF("OFF", 0); OFF("OFF", 0);