Skip to content

Commit

Permalink
Introduce conditions for scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Sep 25, 2024
1 parent a7cacd3 commit 18cd315
Show file tree
Hide file tree
Showing 11 changed files with 2,434 additions and 2,302 deletions.
5 changes: 5 additions & 0 deletions klanglicht-module-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<artifactId>twinkly-api</artifactId>
<version>${version.twinkly}</version>
</dependency>
<dependency>
<groupId>de.visualdiggits</groupId>
<artifactId>solar-time</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>

<!-- serial port -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package de.visualdigits.klanglicht.hardware.lightmanager.model.lm

import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonSubTypes.Type
import com.fasterxml.jackson.annotation.JsonTypeInfo
import de.visualdigits.klanglicht.model.preferences.Preferences

@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.WRAPPER_OBJECT
)
@JsonSubTypes(
Type(name = "night", value = LMConditionNight::class),
)
abstract class LMCondition {

abstract fun evaluate(prefs: Preferences): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.visualdigits.klanglicht.hardware.lightmanager.model.lm

import de.visualdigits.klanglicht.model.preferences.Preferences
import de.visualdigits.solartime.SolarTime
import java.time.ZonedDateTime

class LMConditionNight(
val value: Boolean = true
) : LMCondition() {

override fun evaluate(prefs: Preferences): Boolean {
return value == SolarTime.switchLightsOn(ZonedDateTime.now(), prefs.installationLat, prefs.installationLon)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ class LMScene(
var name: String,
var color: List<String> = listOf(),

var condition: LMCondition? = null,
var actions: List<LMAction> = listOf()
) {

override fun toString(): String {
return "$name: ${color.joinToString(",")}${if (actions.isNotEmpty()) "\n - ${actions.joinToString("\n - ")}" else ""}"
return "$name: ${color.joinToString(",")}${if (condition != null) ", Condition: ${condition?.javaClass?.simpleName}" else ""}${if (actions.isNotEmpty()) "\n - ${actions.joinToString("\n - ")}" else ""}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class LMScenes(
private val mapper = ObjectMapper(YAMLFactory())
.registerModule(kotlinModule())

fun unmarshall(file: File): LMScenes {
fun readValue(file: File): LMScenes {
val lmScenes = mapper.readValue(file, LMScenes::class.java)
lmScenes.scenes.values.forEach { g -> g.scenes.forEach { s -> lmScenes.scenesMap[s.name] = s } }
return lmScenes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import java.nio.file.Paths
@JsonIgnoreProperties("klanglichtDir", "dmxInterface", "fixtures", "serviceMap", "shellyMap", "twinklyMap", "stageMap", "colorWheelMap", "log")
class Preferences(
var name: String = "",
var installationLat: Double = 0.0,
var installationLon: Double = 0.0,
var theme: String = "",
var fadeDurationDefault: Long = 0,
var baseUrl: String? = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.visualdigits.klanglicht.hardware.lightmanager.model.lm

import org.junit.jupiter.api.Test
import java.io.File

class LMScenesTest {

@Test
fun testReadScenes() {
val scenes = LMScenes.readValue(File(ClassLoader.getSystemResource(".klanglicht/resources/scenes.yml").toURI()))
println(scenes)
}
}
Loading

0 comments on commit 18cd315

Please sign in to comment.