Skip to content

Commit

Permalink
Reinvent lightmanager scenes eqivalent to zones. Move hybrid scenes t…
Browse files Browse the repository at this point in the history
…o dedicated controller
  • Loading branch information
sknull committed Apr 20, 2024
1 parent ff4f793 commit 9370426
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.client

import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.json.LmAirProject
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.json.NType
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionHybrid
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionLmAir
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionLmYamahaAvantage
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionPause
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionShelly
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionUrl
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMMarker
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMMarkers
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMParams
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScene
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScenes
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMZones
import de.visualdigits.kotlin.util.get
import de.visualdigits.kotlin.util.post
Expand All @@ -18,33 +28,33 @@ class LightmanagerClient(
const val COLOR_OFF = "#91FFAA"
}

fun html(): String? {
return URL("$lightmanagerUrl/").get<String>()
fun html(): String {
return URL("$lightmanagerUrl/").get<String>()?:""
}

fun configXml(): String? {
return URL("$lightmanagerUrl/config.xml").get<String>()
fun configXml(): String {
return URL("$lightmanagerUrl/config.xml").get<String>()?:""
}

fun paramsJson(): String? {
return URL("$lightmanagerUrl/params.json").get<String>()
fun paramsJson(): String {
return URL("$lightmanagerUrl/params.json").get<String>()?:""
}

fun controlScene(sceneId: Int): String? {
return URL("$lightmanagerUrl/control?key=$sceneId").post<String>()
fun controlScene(sceneId: Int): String {
return URL("$lightmanagerUrl/control?key=$sceneId").post<String>()?:""
}

fun controlIndex(index: Int): String? {
return URL("$lightmanagerUrl/control?scene=$index").post<String>()
fun controlIndex(index: Int): String {
return URL("$lightmanagerUrl/control?scene=$index").post<String>()?:""
}

fun params(): LMParams {
return LMParams.load(paramsJson()!!)
return LMParams.load(paramsJson())
}

fun zones(): LMZones {
val markers: LMMarkers = markers()
val document = Jsoup.parse(html()!!)
val document = Jsoup.parse(html())
val setUpName = document.select("div[id=mytitle]").first()?.text()?:""
val zones = LMZones(setUpName)
document
Expand All @@ -64,9 +74,21 @@ class LightmanagerClient(
return actors
}

fun scenes(): LMScenes {
val document = Jsoup.parse(html())
val setupName = document.select("div[id=mytitle]").first()?.text()
val scenes = LMScenes(setupName)
document
.select("div[id=scenes]")
.first()
?.select("div[class=sbElement]")
?.forEach { elem -> scenes.add(LMScene(name = elem.child(0).text())) }
return scenes
}

fun markers(): LMMarkers {
val markerState: BooleanArray = params().markerState
val document = Jsoup.parse(html()!!)
val document = Jsoup.parse(html())
val setupName = document.select("div[id=mytitle]").first()?.text()
val markers = LMMarkers()
markers.name = setupName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,33 @@ class LMScenes(
override fun toString(): String {
return "$name\n" + scenes.toMap().map { e -> " ${e.key}\n ${e.value.scenes.joinToString("\n ")}" }.joinToString("\n")
}

fun add(scene: LMScene) {
scenesMap[scene.name!!] = scene
var group: String? = "common"
val attributes = LMNamedAttributes(scene.name, "group", "color")
if (attributes.matched()) {
val name = attributes.name
if (name?.isNotEmpty() == true) {
scene.name = name
}
val g = attributes["group"]
if (g.isNotEmpty()) {
group = g
}
scene.color = attributes["color"].split(",").map { it.trim() }
}
if ("hidden" != group) {
group
?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
?.let { name ->
var g = scenes[name]
if (g == null) {
g = LMSceneGroup(name)
scenes[name] = g
}
g.scenes.add(scene)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.visualdigits.kotlin.klanglicht.rest.hybrid.controller

import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html.LMHtmlScenes
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping

@Controller
@RequestMapping("/v1/hybrid/web")
class HybridStageWebController(
var configHolder: ConfigHolder,
) {

@GetMapping("/scenes", produces = ["application/xhtml+xml"])
fun scenes(model: Model): String {
model.addAttribute("theme", configHolder.preferences?.theme)
model.addAttribute("title", "Scenes")
val scenes = LMHtmlScenes(configHolder.scenes())
model.addAttribute("content", scenes.toHtml(configHolder))
return "pagetemplate"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ import org.springframework.web.bind.annotation.RequestMapping
@RequestMapping("/v1/lightmanager/web")
class LightmanagerWebController(
var configHolder: ConfigHolder,
var client: LightmanagerService
var lightmanagerService: LightmanagerService
) {


@GetMapping("/scenes", produces = ["application/xhtml+xml"])
fun scenes(model: Model): String {
model.addAttribute("theme", configHolder.preferences?.theme)
model.addAttribute("title", "Scenes")
val scenes = LMHtmlScenes(configHolder.scenes())
model.addAttribute("content", scenes.toHtml(configHolder))
val scenes = lightmanagerService.scenes()?.let { LMHtmlScenes(it) }
model.addAttribute("content", scenes?.toHtml(configHolder))
return "pagetemplate"
}

@GetMapping("/zones", produces = ["application/xhtml+xml"])
fun zones(model: Model): String {
model.addAttribute("theme", configHolder.preferences?.theme)
model.addAttribute("title", "Zones")
val zones = client.zones()?.let { LMHtmlZones(it) }
val zones = lightmanagerService.zones()?.let { LMHtmlZones(it) }
model.addAttribute("content", zones?.toHtml(configHolder))
return "pagetemplate"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package de.visualdigits.kotlin.klanglicht.rest.lightmanager.service
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.client.LightmanagerClient
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMMarkers
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMParams
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScenes
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMZones
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import jakarta.annotation.PostConstruct
Expand Down Expand Up @@ -35,6 +36,10 @@ class LightmanagerService(
return client?.knownActors()
}

fun scenes(): LMScenes? {
return client?.scenes()
}

fun markers(): LMMarkers? {
return client?.markers()
}
Expand Down

0 comments on commit 9370426

Please sign in to comment.