Skip to content

Commit

Permalink
Start moving configuration into yaml instead of proprietary json files
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Apr 21, 2024
1 parent ffbf527 commit 2a73d27
Show file tree
Hide file tree
Showing 21 changed files with 133 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
lsp/
*.log
*.iml
**/application-local.yml

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ import java.nio.file.Paths

@JsonIgnoreProperties("klanglichtDir", "dmxInterface", "fixtures", "serviceMap", "shellyMap", "twinklyMap", "stageMap", "log")
data class Preferences(
val name: String = "",
val theme: String = "",
val fadeDurationDefault: Long = 1000,
val ownUrl: String = "http://localhost:8888",
private val services: List<Service> = listOf(),
private val shelly: List<ShellyDevice>? = listOf(),
private val twinkly: List<TwinklyConfiguration>? = listOf(),
Expand Down
58 changes: 58 additions & 0 deletions klanglicht-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
</properties>

<dependencies>
<!-- kotlin -->
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-allopen</artifactId>
<version>${version.kotlin.maven}</version>
</dependency>

<!-- project -->
<dependency>
<groupId>de.visualdigits</groupId>
Expand Down Expand Up @@ -125,6 +132,57 @@
<build>
<finalName>klanglicht-rest-${revision}</finalName>
<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${version.kotlin.maven}</version>
<configuration>
<languageVersion>${version.kotlin.language}</languageVersion>
<jvmTarget>${project.build.target}</jvmTarget>
<compilerPlugins>
<plugin>spring</plugin>
<plugin>no-arg</plugin>
<plugin>all-open</plugin>
</compilerPlugins>
<pluginOptions>
<option>all-open:annotation=javax.persistence.Entity</option>
<option>all-open:annotation=javax.persistence.Embeddable</option>
<option>all-open:annotation=javax.persistence.MappedSuperclass</option>
</pluginOptions>
<args>
<arg>-Xjsr305=strict</arg>
</args>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-noarg</artifactId>
<version>${version.kotlin.maven}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${version.kotlin.maven}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ class ResourceController(

@GetMapping("/resources/**")
fun resource(request: HttpServletRequest, response: HttpServletResponse) {
val src = getRequestUri(request)?.substring("/resources".length)?:""
val src = URLDecoder
.decode(request.requestURI, request.characterEncoding)
?.substring("/resources".length)?:""
val file = configHolder.getAbsoluteResource(src)
try {
FileInputStream(file).use { ins ->
Expand Down Expand Up @@ -61,13 +63,4 @@ class ResourceController(
}
return mimeType
}

private fun getRequestUri(request: HttpServletRequest): String? {
return try {
URLDecoder.decode(request.requestURI, request.characterEncoding)
} catch (e: UnsupportedEncodingException) {
log.debug("Could not decode url '{}'", request)
null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.visualdigits.kotlin.klanglicht.rest.configuration

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.ConfigurationPropertiesScan
import org.springframework.context.annotation.Configuration

@Configuration
@ConfigurationProperties(prefix = "application")
@ConfigurationPropertiesScan
class ApplicationPreferences {
var name: String = ""
var theme: String = ""
var fadeDurationDefault: Long = 0
var ownUrl: String = ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import java.nio.file.Paths

@Configuration
open class WebConfig(
private val prefs: ApplicationPreferences,
private val configHolder: ConfigHolder
) : WebMvcConfigurer {

Expand All @@ -31,7 +32,7 @@ open class WebConfig(
configHolder.klanglichtDirectory.absolutePath,
"resources",
"themes",
configHolder.preferences?.theme,
prefs.theme,
"templates"
).toFile().absolutePath.replace("\\", "/") + "/"
templateResolver.prefix = templatesPath
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.visualdigits.kotlin.klanglicht.rest.hybrid.controller

import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html.LMHtmlScenes
import org.springframework.stereotype.Controller
Expand All @@ -10,15 +11,16 @@ import org.springframework.web.bind.annotation.RequestMapping
@Controller
@RequestMapping("/v1/hybrid/web")
class HybridStageWebController(
private val prefs: ApplicationPreferences,
private val configHolder: ConfigHolder,
) {

@GetMapping("/scenes", produces = ["application/xhtml+xml"])
fun scenes(model: Model): String {
model.addAttribute("theme", configHolder.preferences?.theme)
model.addAttribute("theme", prefs.theme)
model.addAttribute("title", "Scenes")
val scenes = LMHtmlScenes(configHolder.scenes())
model.addAttribute("content", scenes.toHtml(configHolder))
model.addAttribute("content", scenes.toHtml(prefs, configHolder))
return "pagetemplate"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package de.visualdigits.kotlin.klanglicht.rest.hybrid.service

import de.visualdigits.kotlin.klanglicht.hardware.shelly.client.ShellyClient
import de.visualdigits.kotlin.klanglicht.model.hybrid.HybridScene
import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

@Service
class HybridStageService(
private val prefs: ApplicationPreferences,
private val configHolder: ConfigHolder
) {

Expand Down Expand Up @@ -49,7 +51,7 @@ class HybridStageService(
}
log.info("nextScene: $nextScene")

currentScene?.fade(nextScene!!, transition?: configHolder.preferences?.fadeDurationDefault?:1000, configHolder.preferences!!)
currentScene?.fade(nextScene!!, transition?: prefs.fadeDurationDefault, configHolder.preferences!!)
}

fun putColor(
Expand All @@ -65,7 +67,7 @@ class HybridStageService(
transitionDuration: Long?
) {
ids.forEach { id ->
configHolder.getFadeable(id)?.write(configHolder.preferences!!, transitionDuration = transitionDuration?:configHolder.preferences?.fadeDurationDefault?:1000)
configHolder.getFadeable(id)?.write(configHolder.preferences!!, transitionDuration = transitionDuration?:prefs.fadeDurationDefault)
}
}

Expand All @@ -82,7 +84,7 @@ class HybridStageService(
val lastColor = configHolder.getFadeable(sid)
lastColor?.setGain(gain.toDouble())
try {
ShellyClient.setGain(ipAddress = ipAddress, gain = gain, transitionDuration = transitionDuration?: configHolder.preferences?.fadeDurationDefault?:1000)
ShellyClient.setGain(ipAddress = ipAddress, gain = gain, transitionDuration = transitionDuration?: prefs.fadeDurationDefault)
} catch (e: Exception) {
log.warn("Could not get gain for shelly at '$ipAddress'")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.visualdigits.kotlin.klanglicht.rest.lightmanager.controller

import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html.LMHtmlScenes
import de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html.LMHtmlZones
Expand All @@ -12,25 +13,26 @@ import org.springframework.web.bind.annotation.RequestMapping
@Controller
@RequestMapping("/v1/lightmanager/web")
class LightmanagerWebController(
private val prefs: ApplicationPreferences,
private val configHolder: ConfigHolder,
private val lightmanagerService: LightmanagerService
) {

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

@GetMapping("/zones", produces = ["application/xhtml+xml"])
fun zones(model: Model): String {
model.addAttribute("theme", configHolder.preferences?.theme)
model.addAttribute("theme", prefs.theme)
model.addAttribute("title", "Zones")
val zones = lightmanagerService.zones()?.let { LMHtmlZones(it) }
model.addAttribute("content", zones?.toHtml(configHolder))
model.addAttribute("content", zones?.toHtml(prefs, configHolder))
return "pagetemplate"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html

import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -10,11 +11,11 @@ class ColorWheel(

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

override fun toHtml(configHolder: ConfigHolder?): String {
return toHtml(configHolder, false)
override fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?): String {
return toHtml(prefs, configHolder, false)
}

fun toHtml(configHolder: ConfigHolder?, oddEven: Boolean): String {
fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?, oddEven: Boolean): String {
val wheelId = id!!.replace(" ", "")

val sb = StringBuilder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html

import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder


class ColorWheelOddEven(
val id: String? = null
) : HtmlRenderable {

override fun toHtml(configHolder: ConfigHolder?): String {
override fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?): String {
return " <div class=\"colorwheel-wrapper\">\n" +
"\t\t<div class=\"colorwheel-title\"><span class=\"label\">COLORPICKER</span></div>\n" +
"\t\t<div class=\"colorwheel-panel\">\n" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html

import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder

interface HtmlRenderable {

fun toHtml(configHolder: ConfigHolder? = null): String?
fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder? = null): String?

fun renderLabel(sb: StringBuilder, label: String?) {
sb.append("<span class=\"label\">").append(label).append("</span>\n")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActor
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMDefaultRequest
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMMarker
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMRequest
import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder

class LMHtmlActor(
val actor: LMActor
) : HtmlRenderable {

override fun toHtml(configHolder: ConfigHolder?): String {
override fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?): String {
val sb = StringBuilder()
sb.append(" <div class=\"panel\">\n")
renderLabel(sb, actor.name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html

import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScene
import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder


class LMHtmlScene(
val scene: LMScene
) : HtmlRenderable {

override fun toHtml(configHolder: ConfigHolder?): String {
return toHtml(configHolder, "")
override fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?): String {
return toHtml(prefs, configHolder, "")
}

fun toHtml(configHolder: ConfigHolder?, group: String): String {
fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?, group: String): String {
// val url = configHolder.preferences?.getService("lmair")?.url
val url = configHolder?.preferences?.ownUrl
val url = prefs.ownUrl
val sb = StringBuilder()
sb.append(" <div class=\"button\"")
if (scene.color.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package de.visualdigits.kotlin.klanglicht.rest.lightmanager.model.html

import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMSceneGroup
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScenes
import de.visualdigits.kotlin.klanglicht.rest.configuration.ApplicationPreferences
import de.visualdigits.kotlin.klanglicht.rest.configuration.ConfigHolder

class LMHtmlScenes(
val scene: LMScenes
) : HtmlRenderable {

override fun toHtml(configHolder: ConfigHolder?): String {
override fun toHtml(prefs: ApplicationPreferences, configHolder: ConfigHolder?): String {
val sb = StringBuilder()
sb.append("<div class=\"title\" onclick=\"toggleFullScreen();\" title=\"Toggle Fullscreen\">")
.append(scene.name)
Expand All @@ -28,6 +29,7 @@ class LMHtmlScenes(
renderLabel(sb, "S C E N E S")
scene.scenes.values.forEach { sceneGroup ->
val html = renderScenesGroup(
prefs,
configHolder,
sceneGroup
)
Expand All @@ -49,6 +51,7 @@ class LMHtmlScenes(
}

private fun renderScenesGroup(
prefs: ApplicationPreferences,
configHolder: ConfigHolder?,
sceneGroup: LMSceneGroup
): String {
Expand All @@ -73,12 +76,12 @@ class LMHtmlScenes(
}
sb.append("\">\n")
sceneGroup.scenes.forEach { scene ->
sb.append(LMHtmlScene(scene).toHtml(configHolder, sceneGroup.name))
sb.append(LMHtmlScene(scene).toHtml(prefs, configHolder, sceneGroup.name))
}
sb.append(" </div><!-- sub-group -->\n")
sb.append(" </div><!-- group -->\n")
if (sceneGroup.hasColorWheel) {
val html: String = ColorWheel(sceneGroup.name).toHtml(configHolder, sceneGroup.colorWheelOddEven)
val html: String = ColorWheel(sceneGroup.name).toHtml(prefs, configHolder, sceneGroup.colorWheelOddEven)
sb.append(html)
}
return sb.toString()
Expand Down
Loading

0 comments on commit 2a73d27

Please sign in to comment.