Skip to content

Commit

Permalink
Consolidating handler code
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Jan 4, 2024
1 parent 22e2096 commit 6378dc8
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ class HybridStageRestController {
@RequestParam(value = "hexColors") hexColors: String,
@RequestParam(value = "gains", required = false, defaultValue = "") gains: String,
@RequestParam(value = "transition", required = false, defaultValue = "1000") transitionDuration: Long,
@RequestParam(value = "turnOn", required = false, defaultValue = "true") turnOn: Boolean
@RequestParam(value = "turnOn", required = false, defaultValue = "true") turnOn: Boolean,
@RequestParam(value = "store", required = false, defaultValue = "true") store: Boolean
) {
hybridStageHandler?.hexColor(
ids = ids,
hexColors = hexColors,
gains = gains,
transitionDuration = transitionDuration
transitionDuration = transitionDuration,
turnOn = turnOn,
store = store
)
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package de.visualdigits.kotlin.klanglicht.rest.hybrid.handler

import de.visualdigits.kotlin.klanglicht.model.hybrid.HybridScene
import de.visualdigits.kotlin.klanglicht.model.shelly.client.ShellyClient
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class HybridStageHandler {

private val log: Logger = LoggerFactory.getLogger(javaClass)

@Autowired
val configHolder: ConfigHolder? = null

Expand All @@ -19,17 +24,61 @@ class HybridStageHandler {
* @param gains The comma separated list of gains (taken from stage setup if omitted).
* @param transitionDuration The fade duration in milli seconds.
* @param turnOn Determines if the device should be turned on.
* @param store Determines if the colors should be saved in the ConfigHolder.
*/
fun hexColor(
ids: String,
hexColors: String,
gains: String,
transitionDuration: Long
transitionDuration: Long,
turnOn: Boolean,
store: Boolean = true
) {
val nextScene = HybridScene(ids, hexColors, gains, preferences = configHolder?.preferences)
val nextScene = HybridScene(ids, hexColors, gains, turnOn.toString(), preferences = configHolder?.preferences)

configHolder?.currentScene?.fade(nextScene, transitionDuration, configHolder.preferences!!)

configHolder?.updateScene(nextScene)
if (store) {
configHolder?.updateScene(nextScene)
}
}

fun restoreColors(
ids: String,
transitionDuration: Long
) {
val lIds = ids
.split(",")
.filter { it.trim().isNotEmpty() }
.map { it.trim() }

lIds.forEach { id ->
configHolder?.getFadeable(id)?.write(configHolder.preferences!!, transitionDuration = transitionDuration)
}
}

fun gain(
ids: String,
gain: Int,
transitionDuration: Long
) {
val lIds = ids
.split(",")
.filter { it.trim().isNotEmpty() }
.map { it.trim() }
lIds.forEach { id ->
val sid = id.trim()
val shellyDevice = configHolder?.preferences?.getShellyDevice(sid)
if (shellyDevice != null) {
val ipAddress: String = shellyDevice.ipAddress
val lastColor = configHolder?.getFadeable(sid)
lastColor?.setGain(gain.toFloat())
try {
ShellyClient.setGain(ipAddress = ipAddress, gain = gain, transitionDuration = transitionDuration)
} catch (e: Exception) {
log.warn("Could not get gain for shelly at '$ipAddress'")
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.visualdigits.kotlin.klanglicht.rest.shelly.controller

import de.visualdigits.kotlin.klanglicht.rest.hybrid.handler.HybridStageHandler
import de.visualdigits.kotlin.klanglicht.rest.shelly.handler.ShellyHandler
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -17,6 +18,9 @@ class ShellyRestController {
@Autowired
var shellyHandler: ShellyHandler? = null

@Autowired
val hybridStageHandler: HybridStageHandler? = null

/**
* Sets the given scene or index on the connected lightmanager air.
*
Expand All @@ -31,6 +35,15 @@ class ShellyRestController {
shellyHandler?.control(sceneId, index)
}

@GetMapping("power")
fun power(
@RequestParam(value = "ids", required = false, defaultValue = "") ids: String,
@RequestParam(value = "turnOn", required = false, defaultValue = "true") turnOn: Boolean,
@RequestParam(value = "transition", required = false, defaultValue = "1000") transitionDuration: Long
) {
shellyHandler?.power(ids, turnOn, transitionDuration)
}

@GetMapping("hexColor")
fun hexColor(
@RequestParam(value = "ids", required = false, defaultValue = "") ids: String,
Expand All @@ -40,24 +53,15 @@ class ShellyRestController {
@RequestParam(value = "turnOn", required = false, defaultValue = "true") turnOn: Boolean,
@RequestParam(value = "store", required = false, defaultValue = "true") store: Boolean
) {
shellyHandler?.hexColors(ids, hexColors, gains, transitionDuration, turnOn, store)
hybridStageHandler?.hexColor(ids, hexColors, gains, transitionDuration, turnOn, store)
}

@GetMapping("restore")
fun restoreColors(
@RequestParam(value = "ids", required = false, defaultValue = "") ids: String,
@RequestParam(value = "transition", required = false, defaultValue = "1000") transitionDuration: Long
) {
shellyHandler?.restoreColors(ids, transitionDuration)
}

@GetMapping("power")
fun power(
@RequestParam(value = "ids", required = false, defaultValue = "") ids: String,
@RequestParam(value = "turnOn", required = false, defaultValue = "true") turnOn: Boolean,
@RequestParam(value = "transition", required = false, defaultValue = "1000") transitionDuration: Long
) {
shellyHandler?.power(ids, turnOn, transitionDuration)
hybridStageHandler?.restoreColors(ids, transitionDuration)
}

@GetMapping("gain")
Expand All @@ -66,6 +70,6 @@ class ShellyRestController {
@RequestParam(value = "gain", required = false, defaultValue = "") gain: Int,
@RequestParam(value = "transition", required = false, defaultValue = "1000") transitionDuration: Long
) {
shellyHandler?.gain(ids, gain, transitionDuration)
hybridStageHandler?.gain(ids, gain, transitionDuration)
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package de.visualdigits.kotlin.klanglicht.rest.shelly.handler

import de.visualdigits.kotlin.klanglicht.model.color.RGBColor
import de.visualdigits.kotlin.klanglicht.model.hybrid.HybridScene
import de.visualdigits.kotlin.klanglicht.model.shelly.ShellyDevice
import de.visualdigits.kotlin.klanglicht.model.shelly.client.ShellyClient
import de.visualdigits.kotlin.klanglicht.model.shelly.status.Status
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import de.visualdigits.kotlin.klanglicht.rest.lightmanager.feign.LightmanagerClient
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.runBlocking
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import kotlin.coroutines.coroutineContext

@Component
class ShellyHandler {
Expand Down Expand Up @@ -49,65 +44,6 @@ class ShellyHandler {
}
}

/**
* Set hex colors.
*
* @param ids The list of ids.
* @param hexColors The list of hex colors.
* @param gains The list of gains (taken from stage setup if omitted).
* @param transitionDuration The fade duration in milli seconds.
* @param turnOn Determines if the device should be turned on.
*/
fun hexColors(
ids: String,
hexColors: String,
gains: String,
transitionDuration: Long,
turnOn: Boolean,
store: Boolean = true
) {
val nextScene = HybridScene(ids, hexColors, gains, turnOn.toString(), preferences = configHolder?.preferences)

configHolder?.currentScene?.fade(nextScene, transitionDuration, configHolder.preferences!!)

if (store) {
configHolder?.updateScene(nextScene)
}
}

fun color(
ids: String,
red: Int,
green: Int,
blue: Int,
gains: String,
transitionDuration: Long,
turnOn: Boolean,
store: Boolean
) {
val nextScene = HybridScene(ids, RGBColor(red, green, blue).hex(), gains, turnOn.toString(), preferences = configHolder?.preferences)

configHolder?.currentScene?.fade(nextScene, transitionDuration, configHolder.preferences!!)

if (store) {
configHolder?.updateScene(nextScene)
}
}

fun restoreColors(
ids: String,
transitionDuration: Long
) {
val lIds = ids
.split(",")
.filter { it.trim().isNotEmpty() }
.map { it.trim() }

lIds.forEach { id ->
configHolder?.getFadeable(id)?.write(configHolder.preferences!!, transitionDuration = transitionDuration)
}
}

fun power(
ids: String,
turnOn: Boolean,
Expand Down Expand Up @@ -139,31 +75,6 @@ class ShellyHandler {
}
}

fun gain(
ids: String,
gain: Int,
transitionDuration: Long
) {
val lIds = ids
.split(",")
.filter { it.trim().isNotEmpty() }
.map { it.trim() }
lIds.forEach { id ->
val sid = id.trim()
val shellyDevice = configHolder?.preferences?.getShellyDevice(sid)
if (shellyDevice != null) {
val ipAddress: String = shellyDevice.ipAddress
val lastColor = configHolder?.getFadeable(sid)
lastColor?.setGain(gain.toFloat())
try {
ShellyClient.setGain(ipAddress = ipAddress, gain = gain, transitionDuration = transitionDuration)
} catch (e: Exception) {
log.warn("Could not get gain for shelly at '$ipAddress'")
}
}
}
}

fun currentPowers(): Map<String, Status> {
val powers: MutableMap<String, Status> = LinkedHashMap()
status().forEach { (device: ShellyDevice, status: Status) ->
Expand Down

0 comments on commit 6378dc8

Please sign in to comment.