Skip to content

Commit

Permalink
Convert comma separated strings in scenes config by lists
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Apr 20, 2024
1 parent fb86c9e commit 792736d
Show file tree
Hide file tree
Showing 22 changed files with 2,160 additions and 541 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMActionHybrid(
val ids: String? = null,
val hexColors: String? = null,
val gains: String? = null,
val ids: List<String> = listOf(),
val hexColors: List<String> = listOf(),
val gains: List<Double> = listOf(),
) : LMAction() {

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMActionShelly(
val ids: String? = null,
val ids: List<String> = listOf(),
val turnOn: Boolean?= null
) : LMAction() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMScene(
var name: String? = null,
var color: String? = null,
var color: List<String> = listOf(),

var actions: List<LMAction> = listOf()
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object ShellyClient {
fun setColor(
ipAddress: String,
rgbColor: RGBColor,
gain: Float,
gain: Double,
transitionDuration: Long = 1, // zero is interpreted as empty which leads to the default of 2000 millis
turnOn: Boolean? = true,
): Light? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ShellyColor(
private val deviceId: String,
val ipAddress: String,
private var color: RGBColor,
private var deviceGain: Float,
private var deviceGain: Double,
private var deviceTurnOn: Boolean?
) : Fadeable<ShellyColor> {

Expand All @@ -24,7 +24,7 @@ class ShellyColor(

override fun getTurnOn(): Boolean {
var turnOn = deviceTurnOn?:false
if ((color.red == 0 && color.green == 0 && color.blue == 0) || deviceGain == 0.0f) {
if ((color.red == 0 && color.green == 0 && color.blue == 0) || deviceGain == 0.0) {
turnOn = false
}
return turnOn
Expand All @@ -36,9 +36,9 @@ class ShellyColor(

override fun getId(): String = deviceId

override fun getGain(): Float = deviceGain
override fun getGain(): Double = deviceGain

override fun setGain(gain: Float) {
override fun setGain(gain: Double) {
this.deviceGain = gain
}

Expand All @@ -63,7 +63,7 @@ class ShellyColor(

override fun fade(other: Any, factor: Double): ShellyColor {
return if (other is ShellyColor) {
ShellyColor(deviceId, ipAddress, color.fade(other.color, factor), min(1.0, (deviceGain + factor * (other.deviceGain - deviceGain))).toFloat(), other.deviceTurnOn)
ShellyColor(deviceId, ipAddress, color.fade(other.color, factor), min(1.0, (deviceGain + factor * (other.deviceGain - deviceGain))), other.deviceTurnOn)
} else {
throw IllegalArgumentException("Cannot not fade another type")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class ShellyDevice(
val model: String = "",
val command: String = "",
val ipAddress: String = "",
val gain: Float = 0.0f
val gain: Double = 0.0
) {

override fun toString(): String {
Expand Down Expand Up @@ -48,7 +48,7 @@ data class ShellyDevice(

fun setColor(
rgbColor: RGBColor,
gain: Float = 1.0f,
gain: Double = 1.0,
transitionDuration: Long = 0,
turnOn: Boolean = true,
): Light? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import de.visualdigits.kotlin.twinkly.model.device.xled.XledArray
class TwinklyConfiguration(
val name: String,
val deviceOrigin: String,
val gain: Float,
val gain: Double,
val array: Array<Array<XledDeviceConfiguration>>
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import de.visualdigits.kotlin.twinkly.model.color.RGBColor as TwinklyRGBColor
class XledFrameFadeable(
private val deviceId: String,
private var xledFrame: XledFrame,
private var deviceGain: Float
private var deviceGain: Double
) : Fadeable<XledFrameFadeable> {

private val log: Logger = LoggerFactory.getLogger(javaClass)
Expand All @@ -28,9 +28,9 @@ class XledFrameFadeable(

override fun getId(): String = deviceId

override fun getGain(): Float = deviceGain
override fun getGain(): Double = deviceGain

override fun setGain(gain: Float) {
override fun setGain(gain: Double) {
this.deviceGain = gain
}

Expand All @@ -49,7 +49,7 @@ class XledFrameFadeable(
val xledArray = twinklyDevice.xledArray
if (write && xledArray.isLoggedIn()) {
log.debug("Writing xledFrame\n{}", this)
xledArray.setBrightness(deviceGain)
xledArray.setBrightness(deviceGain.toFloat())
xledFrame.play(xledArray)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ data class DmxDevice(
val model: String = "",
val mode: String = "",
val baseChannel: Int = 0,
val gain: Float = 0.0f
val gain: Double = 0.0
) {
var fixture: Fixture? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ interface Fadeable<T : Fadeable<T>> {
fun setTurnOn(turnOn: Boolean?) {
}

fun getGain(): Float = 1.0f
fun getGain(): Double = 1.0

fun setGain(gain: Float) {
fun setGain(gain: Double) {
}

fun getRgbColor(): RGBColor? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ class ParameterSet(

override fun getId(): String = baseChannel.toString()

override fun getGain(): Float = parameters
override fun getGain(): Double = parameters
.filterIsInstance<IntParameter>()
.filter { it.name == "MasterDimmer" }
.firstOrNull()
?.let { it.value / 255.0f }
?:1.0f
?.let { it.value / 255.0 }
?:1.0

override fun setGain(gain: Float) {
override fun setGain(gain: Double) {
parameters
.filterIsInstance<IntParameter>()
.filter { it.name == "MasterDimmer" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ class HybridScene() : Fadeable<HybridScene> {

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

private var ids: String? = null
private var hexColors: String? = null
private var gains: String? = null
private var ids: List<String> = listOf()
private var hexColors: List<String> = listOf()
private var gains: List<Double> = listOf()
private var turnOns: String? = null
private var preferences: Preferences? = null

private val fadeables: MutableMap<String, Fadeable<*>> = mutableMapOf()

constructor(
ids: String? = null,
hexColors: String? = null,
gains: String? = null,
ids: List<String> = listOf(),
hexColors: List<String> = listOf(),
gains: List<Double> = listOf(),
turnOns: String? = "true",
preferences: Preferences? = null
) : this() {
Expand Down Expand Up @@ -95,35 +95,23 @@ class HybridScene() : Fadeable<HybridScene> {
}

private fun initializeFromFadeables() {
this.ids = this.fadeables().map { sc -> sc.getId() }.joinToString(",")
this.hexColors = this.fadeables().mapNotNull { sc -> sc.getRgbColor()?.hex() }.joinToString(",")
this.gains = this.fadeables().map { sc -> sc.getGain() }.joinToString(",")
this.ids = this.fadeables().map { sc -> sc.getId() }
this.hexColors = this.fadeables().mapNotNull { sc -> sc.getRgbColor()?.hex() }
this.gains = this.fadeables().map { sc -> sc.getGain() }
this.turnOns = this.fadeables().mapNotNull { sc -> sc.getTurnOn() }.joinToString(",")
}

private fun initializeFromParameters() {
val lIds = if (ids?.isNotEmpty() == true) {
ids?.split(",")
?.filter { it.isNotEmpty() }
?.map { it.trim() }
?: listOf()
val lIds = if (ids.isNotEmpty() == true) {
ids
} else {
preferences?.getStageIds() ?: listOf()
}

val lHexColors = hexColors
?.split(",")
?.filter { it.isNotEmpty() }
?: listOf()
val nh = lHexColors.size - 1
val nh = hexColors.size - 1
var h = 0

val lGains = gains
?.split(",")
?.filter { it.isNotEmpty() }
?.map { it.toFloat() }
?: listOf()
val ng = lGains.size - 1
val ng = gains.size - 1
var g = 0

val lTurnOns = turnOns
Expand All @@ -141,16 +129,16 @@ class HybridScene() : Fadeable<HybridScene> {
.forEach { twinklyDevice ->
val xa = twinklyDevice.xledArray
if (xa.isLoggedIn()) {
val lc = RGBColor(lHexColors.last())
val lc = RGBColor(hexColors.last())
val frame = XledFrame(
width = xa.width,
height = xa.height,
initialColor = TwinklyRGBColor(lc.red, lc.green, lc.blue)
)
val nc = lHexColors.size
val nc = hexColors.size
val barWidth = xa.width / nc
for (x in 0 until nc - 1) {
val rgbColor = RGBColor(lHexColors[x])
val rgbColor = RGBColor(hexColors[x])
val bar = XledFrame(
width = barWidth,
height = xa.height,
Expand All @@ -171,8 +159,8 @@ class HybridScene() : Fadeable<HybridScene> {
lIds.forEach { id ->
val device = preferences?.getHybridDevice(id)
if (device != null) {
val hexColor = lHexColors[min(nh, h++)]
val gain = lGains.getOrNull(min(ng, g++))
val hexColor = hexColors[min(nh, h++)]
val gain = gains.getOrNull(min(ng, g++))
val turnOn = lTurnOns.getOrNull(min(nt, t++)) ?: false
val rgbColor = RGBColor(hexColor)
when (device.type) {
Expand Down Expand Up @@ -219,12 +207,12 @@ class HybridScene() : Fadeable<HybridScene> {

fun getTurnOn(id: String): Boolean? = fadeables[id]?.getTurnOn()

fun setGain(id: String, gain: Float) {
fun setGain(id: String, gain: Double) {
fadeables[id]?.setGain(gain)
initializeFromFadeables()
}

fun getGain(id: String): Float = fadeables[id]?.getGain() ?: 1.0f
fun getGain(id: String): Double = fadeables[id]?.getGain() ?: 1.0

fun setRgbColor(id: String, rgbColor: RGBColor) {
fadeables[id]?.setRgbColor(rgbColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,17 @@ data class Preferences(
val gains = stage.mapNotNull { sd ->
when (sd.type) {
HybridDeviceType.dmx -> {
dmx?.dmxDevices?.get(sd.id)?.gain?:1.0f
dmx?.dmxDevices?.get(sd.id)?.gain?:1.0
}

HybridDeviceType.shelly -> getShellyGain(sd.id)
HybridDeviceType.twinkly -> getTwinklyConfiguration(sd.id)?.let { if (it.xledArray.isLoggedIn()) it.gain else null }
else -> null
}
}.joinToString(",")
}
return HybridScene(
ids = stage.joinToString(",") { it.id },
hexColors = "000000",
ids = stage.map { it.id },
hexColors = listOf("000000"),
gains = gains,
turnOns = "false",
preferences = this
Expand Down Expand Up @@ -180,7 +180,7 @@ data class Preferences(
fun getDmxFixture(baseChannel: Int) = fixtures[baseChannel]


fun getShellyGain(id: String): Float = shellyMap[id]?.gain?:1.0f
fun getShellyGain(id: String): Double = shellyMap[id]?.gain?:1.0

fun getShellyDevice(id: String): ShellyDevice? = shellyMap[id]
fun getShellyDevices(): List<ShellyDevice> = shelly?:listOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class ShellyColorTest {
@Test
fun testTiming() {
val shellyDevice = preferences.getShellyDevice("Starwars")!!
val red = ShellyColor("Starwars", shellyDevice.ipAddress, RGBColor(255, 0, 0), 1.0f, true)
val green = ShellyColor("Starwars", shellyDevice.ipAddress, RGBColor(0, 255, 0), 1.0f, true)
val red = ShellyColor("Starwars", shellyDevice.ipAddress, RGBColor(255, 0, 0), 1.0, true)
val green = ShellyColor("Starwars", shellyDevice.ipAddress, RGBColor(0, 255, 0), 1.0, true)
val t = System.currentTimeMillis()
red.write(preferences, transitionDuration = 3000)
val d = System.currentTimeMillis() - t
Expand All @@ -30,8 +30,8 @@ class ShellyColorTest {
fun testFade() {
val shellyDevice = preferences.getShellyDevice("Starwars")
if (shellyDevice != null) {
val color1 = ShellyColor("foo", shellyDevice.ipAddress, RGBColor(255, 0, 0), 1.0f, true)
val color2 = ShellyColor("bar", shellyDevice.ipAddress, RGBColor(0, 255, 0), 1.0f, true)
val color1 = ShellyColor("foo", shellyDevice.ipAddress, RGBColor(255, 0, 0), 1.0, true)
val color2 = ShellyColor("bar", shellyDevice.ipAddress, RGBColor(0, 255, 0), 1.0, true)
// color2.write()

for (i in 0 until 5) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ class HybridDmxSceneTest {
@Test
fun testFade() {
val scene1 = HybridScene(
ids = "Starwars,Rgbw,15,29,Bar",
hexColors = "#ff0000,#00ff00,#0000ff,#ffff00,#00ffff",
gains = "",
ids = listOf("Starwars", "Rgbw", "15", "29", "Bar"),
hexColors = listOf("#ff0000", "#00ff00", "#0000ff", "#ffff00", "#00ffff"),
gains = listOf(1.0),
preferences = preferences
)
// println(scene1)
val scene2 = HybridScene(
ids = "Starwars,Rgbw,15,29,Bar",
hexColors = "#00ffff,#ff00ff,#ffff00,#0000ff,#ff0000",
gains = "",
ids = listOf("Starwars", "Rgbw", "15", "29", "Bar"),
hexColors = listOf("#00ffff", "#ff00ff", "#ffff00", "#0000ff", "#ff0000"),
gains = listOf(1.0),
preferences = preferences
)
Thread.sleep(5000)
Expand Down
Loading

0 comments on commit 792736d

Please sign in to comment.