Skip to content

Commit

Permalink
Skipping scene setup if it is the current scene. Add also put as meth…
Browse files Browse the repository at this point in the history
…od for controlling a scene.
  • Loading branch information
sknull committed Apr 2, 2024
1 parent e3c2433 commit 1ffe922
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.visualdigits.kotlin.klanglicht.rest.scenes.controller
import de.visualdigits.kotlin.klanglicht.rest.scenes.service.ScenesService
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
Expand All @@ -16,10 +17,12 @@ class ScenesRestController(
@GetMapping("sceneNames")
fun hybrid(): Set<String> = scenesService.sceneNames()


@PostMapping("control")
@PostMapping("control")
fun controlPost(@RequestParam(value = "name") name: String) = scenesService.executeScene(name)

@PutMapping("control")
fun controlPut(@RequestParam(value = "name") name: String) = scenesService.executeScene(name)

@GetMapping("control")
fun controlGet(@RequestParam(value = "name") name: String) = scenesService.executeScene(name)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,49 @@ import org.springframework.stereotype.Service

@Service
class ScenesService(
var shellyService: ShellyService,
var lightmanagerService: LightmanagerService,
val hybridStageService: HybridStageService,
val yamahaAvantageService: YamahaAvantageService,
val configHolder: ConfigHolder
private var shellyService: ShellyService,
private var lightmanagerService: LightmanagerService,
private val hybridStageService: HybridStageService,
private val yamahaAvantageService: YamahaAvantageService,
private val configHolder: ConfigHolder
) {

private val log = LoggerFactory.getLogger(javaClass)

fun executeScene(name: String) {
val lmScene = configHolder.scenes().scenesMap.get(name)
lmScene
?.let { s ->
log.info("Executing scene '$name'...")
s.actions.forEach { action ->
log.info(" Executing action '$action'...")
when (action) {
is LMActionLmAir ->
lightmanagerService.controlIndex(index = action.sceneIndex)
private var previousSceneName: String = ""

is LMActionShelly ->
shellyService.power(ids = action.ids, turnOn = action.turnOn)
fun executeScene(sceneName: String) {
if (sceneName != previousSceneName) {
val lmScene = configHolder.scenes().scenesMap[sceneName]
lmScene
?.let { s ->
log.info("Executing scene '$sceneName'...")
s.actions.forEach { action ->
log.info(" Executing action '$action'...")
when (action) {
is LMActionLmAir ->
lightmanagerService.controlIndex(index = action.sceneIndex)

is LMActionHybrid ->
hybridStageService.hexColor(ids = action.ids, hexColors = action.hexColors, gains = action.gains)
is LMActionShelly ->
shellyService.power(ids = action.ids, turnOn = action.turnOn)

is LMActionLmYamahaAvantage -> {
when (action.command) {
"surroundProgram" -> yamahaAvantageService.setSurroundProgram(program = action.program)
is LMActionHybrid ->
hybridStageService.hexColor(ids = action.ids, hexColors = action.hexColors, gains = action.gains)

is LMActionLmYamahaAvantage -> {
when (action.command) {
"surroundProgram" -> yamahaAvantageService.setSurroundProgram(program = action.program)
}
}
}

is LMActionPause -> action.duration?.let { Thread.sleep(it) }
is LMActionPause -> action.duration?.let { Thread.sleep(it) }
}
}
}
}?:also {
log.info("No scene with name '$name'")
}?:also {
log.info("No scene with name '$sceneName'")
}
} else {
log.info("Scene '$sceneName' already set - skipping")
}
}

Expand Down

0 comments on commit 1ffe922

Please sign in to comment.