-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement hybrid scene equivalent to dmx scene
- Loading branch information
Showing
8 changed files
with
240 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...t-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/hybrid/model/HybridScene.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package de.visualdigits.kotlin.klanglicht.rest.hybrid.model | ||
|
||
import de.visualdigits.kotlin.klanglicht.model.color.RGBColor | ||
import de.visualdigits.kotlin.klanglicht.model.hybrid.HybridDevice | ||
import de.visualdigits.kotlin.klanglicht.model.hybrid.HybridDeviceType | ||
import de.visualdigits.kotlin.klanglicht.model.parameter.Fadeable | ||
import de.visualdigits.kotlin.klanglicht.model.parameter.IntParameter | ||
import de.visualdigits.kotlin.klanglicht.model.parameter.ParameterSet | ||
import de.visualdigits.kotlin.klanglicht.model.parameter.Scene | ||
import de.visualdigits.kotlin.klanglicht.model.preferences.Preferences | ||
import de.visualdigits.kotlin.klanglicht.rest.shelly.client.ShellyClient | ||
import kotlin.math.min | ||
import kotlin.math.roundToInt | ||
|
||
class HybridScene( | ||
val ids: String, | ||
val hexColors: String, | ||
val gains: String, | ||
val preferences: Preferences | ||
) : Fadeable<HybridScene> { | ||
|
||
private val fadeables: List<Fadeable<*>> | ||
|
||
init { | ||
val lIds = ids | ||
.split(",") | ||
.filter { it.isNotEmpty() } | ||
.map { it.trim() } | ||
val nd = lIds.size - 1 | ||
var d = 0 | ||
|
||
val lHexColors = hexColors | ||
.split(",") | ||
.filter { it.isNotEmpty() } | ||
val nh = lHexColors.size - 1 | ||
var h = 0 | ||
|
||
val lGains = gains | ||
.split(",") | ||
.filter { it.isNotEmpty() } | ||
.map { it.toFloat() } | ||
val ng = lGains.size - 1 | ||
var g = 0 | ||
|
||
fadeables = if (lIds.isNotEmpty()) { | ||
lIds.mapNotNull { id -> | ||
val device = preferences.stageMap[id] | ||
val hexColor = lHexColors[min(nh, h++)] | ||
val gain = lGains.getOrNull(min(ng, g++)) | ||
device?.let { d -> processDevice(d, preferences, id, gain, hexColor) } | ||
} | ||
} else { | ||
preferences.stage.mapNotNull { device -> | ||
val hexColor = lHexColors[min(nh, h++)] | ||
val gain = lGains.getOrNull(min(ng, g++)) | ||
processDevice(device, preferences, device.id, gain, hexColor) } | ||
} | ||
} | ||
|
||
override fun toString(): String { | ||
return hexColors | ||
.split(",") | ||
.filter { it.isNotEmpty() } | ||
.map { RGBColor(it).ansiColor() } | ||
.joinToString("") | ||
} | ||
|
||
private fun processDevice( | ||
device: HybridDevice, | ||
preferences: Preferences, | ||
id: String, | ||
gain: Float?, | ||
hexColor: String | ||
): Fadeable<*>? { | ||
return when (device.type) { | ||
HybridDeviceType.dmx -> { | ||
val dmxDevice = preferences.dmx.dmxDevices[id] | ||
if (dmxDevice != null) { | ||
ParameterSet( | ||
baseChannel = dmxDevice.baseChannel, | ||
parameters = mutableListOf( | ||
IntParameter("MasterDimmer", (255 * (gain ?: dmxDevice.gain)).roundToInt()), | ||
RGBColor(hexColor) | ||
) | ||
) | ||
} else null | ||
} | ||
|
||
HybridDeviceType.shelly -> { | ||
val shellyDevice = preferences.shellyMap[id] | ||
if (shellyDevice != null) { | ||
ShellyColor(shellyDevice.ipAddress, RGBColor(hexColor), gain ?: shellyDevice.gain) | ||
} else null | ||
} | ||
|
||
else -> null | ||
} | ||
} | ||
|
||
fun write(preferences: Preferences, write: Boolean = true) { | ||
Scene( | ||
name = "HybridScene", | ||
parameterSet = fadeables.filterIsInstance<ParameterSet>() | ||
).write(preferences, write) | ||
fadeables | ||
.filterIsInstance<ShellyColor>() | ||
.forEach { shellyColor -> ShellyClient.setColor(shellyColor.ipAddress, shellyColor.color, shellyColor.gain) } | ||
} | ||
|
||
fun fade( | ||
other: HybridScene, | ||
fadeDuration: Long, | ||
preferences: Preferences | ||
) { | ||
val dmxFrameTime = preferences.dmx.frameTime // millis | ||
val step = 1.0 / fadeDuration.toDouble() * dmxFrameTime.toDouble() | ||
var factor = 0.0 | ||
|
||
while (factor < 1.0) { | ||
fade(other, factor).write(preferences) | ||
factor += step | ||
Thread.sleep(dmxFrameTime) | ||
} | ||
other.write(preferences) | ||
} | ||
|
||
override fun fade(other: Any, factor: Double): HybridScene { | ||
return if (other is HybridScene) { | ||
val fadedHexColors = fadeables | ||
.zip(other.fadeables) | ||
.mapNotNull { | ||
val faded = it.first.fade(it.second, factor) | ||
when (faded) { | ||
is ShellyColor -> faded.color.hex() | ||
is ParameterSet -> faded.parameters | ||
.filterIsInstance<RGBColor>() | ||
.firstOrNull() | ||
?.let { c -> c.hex() } | ||
else -> null | ||
} | ||
}.joinToString(",") | ||
HybridScene(ids, fadedHexColors, gains, preferences) | ||
} else { | ||
throw IllegalArgumentException("Cannot not fade another type") | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...t-rest/src/main/kotlin/de/visualdigits/kotlin/klanglicht/rest/hybrid/model/ShellyColor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package de.visualdigits.kotlin.klanglicht.rest.hybrid.model | ||
|
||
import de.visualdigits.kotlin.klanglicht.model.color.RGBColor | ||
import de.visualdigits.kotlin.klanglicht.model.parameter.Fadeable | ||
|
||
class ShellyColor( | ||
val ipAddress: String, | ||
val color: RGBColor, | ||
val gain: Float | ||
): Fadeable<ShellyColor> { | ||
|
||
override fun fade(other: Any, factor: Double): ShellyColor { | ||
return if (other is ShellyColor) { | ||
ShellyColor(ipAddress, color.fade(other.color, factor), gain) | ||
} else { | ||
throw IllegalArgumentException("Cannot not fade another type") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.