diff --git a/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/client/LightmanagerClient.kt b/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/client/LightmanagerClient.kt index fbd3cf6..ae5023f 100644 --- a/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/client/LightmanagerClient.kt +++ b/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/client/LightmanagerClient.kt @@ -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 @@ -18,33 +28,33 @@ class LightmanagerClient( const val COLOR_OFF = "#91FFAA" } - fun html(): String? { - return URL("$lightmanagerUrl/").get() + fun html(): String { + return URL("$lightmanagerUrl/").get()?:"" } - fun configXml(): String? { - return URL("$lightmanagerUrl/config.xml").get() + fun configXml(): String { + return URL("$lightmanagerUrl/config.xml").get()?:"" } - fun paramsJson(): String? { - return URL("$lightmanagerUrl/params.json").get() + fun paramsJson(): String { + return URL("$lightmanagerUrl/params.json").get()?:"" } - fun controlScene(sceneId: Int): String? { - return URL("$lightmanagerUrl/control?key=$sceneId").post() + fun controlScene(sceneId: Int): String { + return URL("$lightmanagerUrl/control?key=$sceneId").post()?:"" } - fun controlIndex(index: Int): String? { - return URL("$lightmanagerUrl/control?scene=$index").post() + fun controlIndex(index: Int): String { + return URL("$lightmanagerUrl/control?scene=$index").post()?:"" } 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 @@ -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 diff --git a/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/model/lm/LMScenes.kt b/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/model/lm/LMScenes.kt index eed30fb..cebc97c 100644 --- a/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/model/lm/LMScenes.kt +++ b/klanglicht-module-base/src/main/kotlin/de/visualdigits/kotlin/klanglicht/hardware/lightmanager/model/lm/LMScenes.kt @@ -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) + } + } + } } diff --git a/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/hybrid/controller/HybridStageWebController.kt b/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/hybrid/controller/HybridStageWebController.kt new file mode 100644 index 0000000..e7822e9 --- /dev/null +++ b/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/hybrid/controller/HybridStageWebController.kt @@ -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" + } +} diff --git a/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/controller/LightmanagerWebController.kt b/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/controller/LightmanagerWebController.kt index 5455a91..323caee 100644 --- a/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/controller/LightmanagerWebController.kt +++ b/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/controller/LightmanagerWebController.kt @@ -13,16 +13,15 @@ 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" } @@ -30,7 +29,7 @@ class LightmanagerWebController( 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" } diff --git a/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/service/LightmanagerService.kt b/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/service/LightmanagerService.kt index 731fbbb..9cdb3ed 100644 --- a/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/service/LightmanagerService.kt +++ b/klanglicht-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/lightmanager/service/LightmanagerService.kt @@ -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 @@ -35,6 +36,10 @@ class LightmanagerService( return client?.knownActors() } + fun scenes(): LMScenes? { + return client?.scenes() + } + fun markers(): LMMarkers? { return client?.markers() }