Skip to content

Commit

Permalink
Use local config instead of lightmanagers config - part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Mar 31, 2024
1 parent 13d8319 commit 6b0d01b
Show file tree
Hide file tree
Showing 35 changed files with 3,631 additions and 1,613 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ 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.LMActionIR
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionLmAir
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionHybrid
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionShelly
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.LMActionUrl
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActor
Expand All @@ -15,9 +18,7 @@ import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMZones
import de.visualdigits.kotlin.util.get
import de.visualdigits.kotlin.util.post
import org.jsoup.Jsoup
import java.net.HttpURLConnection
import java.net.URL
import java.util.zip.GZIPInputStream

class LightmanagerClient(
val lightmanagerUrl: String
Expand Down Expand Up @@ -59,7 +60,7 @@ class LightmanagerClient(
val zones = LMZones(setUpName)
document
.select("div[class=bigBlock]")
.forEach { zoneElem -> zones.add(lightmanagerUrl!!, markers, zoneElem) }
.forEach { zoneElem -> zones.add(lightmanagerUrl, markers, zoneElem) }
return zones
}

Expand Down Expand Up @@ -128,18 +129,51 @@ class LightmanagerClient(
actuator.properties?.url
}
if (url != null) {
LMActionUrl(
comment = actuator.properties?.comment,
url = url.substringAfter("v1/")
)
val uu = if (url.startsWith("http")) url else "http://$url"
val u = URL(uu)
val params = u.query
.split("&")
.filter { p -> p.isNotEmpty() }
.map { p -> p.split("=") }
.filter { p -> p.size == 2 }
.map { p -> Pair(p[0], p[1]) }
.toMap()
when (u.path) {
"/v1/yamaha/avantage/json/surroundProgram" ->
LMActionLmYamahaAvantage(
comment = actuator.properties?.comment,
command = "surroundProgram",
program = params["program"]
)

"/v1/shelly/power" ->
LMActionShelly(
comment = actuator.properties?.comment,
ids = params["ids"],
turnOn = params["turnOn"]?.let { p -> p.toBoolean()}?:false
)

"/v1/hybrid/json/hexColor" ->
LMActionHybrid(
comment = actuator.properties?.comment,
ids = params["ids"],
hexColors = params["hexColors"],
gains = params["gains"],
)
else ->
LMActionUrl(
comment = actuator.properties?.comment,
url = url.substringAfter("v1/")
)
}
} else {
val param = if (ap.command == 1) {
actuator.properties?.paramOff
1499
} else {
actuator.properties?.paramOn
42
}
if (param != null) {
LMActionIR(comment = actuator.properties?.comment, param = param)
LMActionLmAir(comment = actuator.properties?.comment, sceneIndex = param)
} else {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo
@JsonSubTypes(
Type(name = "pause", value = LMActionPause::class),
Type(name = "url", value = LMActionUrl::class),
Type(name = "shelly", value = LMActionShelly::class),
Type(name = "ir", value = LMActionIR::class),
Type(name = "hybrid", value = LMActionHybrid::class),
Type(name = "yamahaAvantage", value = LMActionLmYamahaAvantage::class),
Type(name = "lmair", value = LMActionLmAir::class),
)
abstract class LMAction(
val comment: String? = null
)
) {

open fun url(): String = ""
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMActionHybrid(
comment: String? = null,
val ids: String? = null,
val hexColors: String? = null,
val gains: String? = null,
) : LMAction(comment) {

override fun toString(): String {
return "[LMAir] $comment: ids=$ids hexColors=$hexColors"
}

override fun url(): String = "/v1/scenes/json/hybrid?ids=$ids&hexColors=$hexColors&gains=$gains&"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMActionLmAir(
comment: String? = null,
val sceneIndex: Int? = null,
) : LMAction(comment) {

override fun toString(): String {
return "[LMAir] $comment: $sceneIndex"
}

override fun url(): String = "/v1/scenes/json/lmair?sceneIndex=$sceneIndex&"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMActionLmYamahaAvantage(
comment: String? = null,
val command: String? = null,
val program: String? = null,
) : LMAction(comment) {

override fun toString(): String {
return "[YamahaAvantage] $comment: command=$command program=$program"
}

override fun url(): String = "/v1/scenes/json/yamahaAvantage?command=$command&program=$program&"
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

class LMActionShelly(
comment: String? = null,
val ids: String? = null,
val turnOn: Boolean?= null
) : LMAction(comment) {

override fun toString(): String {
return "[LMAir] $comment: ids=$ids turnOn=$turnOn"
}

override fun url(): String = "/v1/scenes/json/shelly?ids=$ids&turnOn=$turnOn&"
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
import java.io.File
import java.util.Locale

class LMScenes(
Expand All @@ -8,11 +12,27 @@ class LMScenes(

val scenes: MutableMap<String, MutableList<LMScene>> = mutableMapOf()

val scenesMap: MutableMap<Int, LMScene> = mutableMapOf()

companion object {
private val mapper = jacksonMapperBuilder()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.enable(SerializationFeature.INDENT_OUTPUT)
.build()

fun unmarshall(file: File): LMScenes {
val lmScenes = mapper.readValue(file, LMScenes::class.java)
lmScenes.scenes.values.forEach { l -> l.forEach { s -> lmScenes.scenesMap[s.id!!] = s } }
return lmScenes
}
}

override fun toString(): String {
return "$name\n" + scenes.toMap().map { e -> " ${e.key}\n ${e.value.joinToString("\n ")}" }.joinToString("\n")
}

fun add(scene: LMScene) {
scenesMap[scene.id!!] = scene
var group: String? = "common"
val attributes = LMNamedAttributes(scene.name, "group", "color")
if (attributes.matched()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ object ShellyClient {

fun setPower(
ipAddress: String,
turnOn: Boolean,
turnOn: Boolean? = false,
command: String = "",
transitionDuration: Long = 1
): String? {
log.debug("setPower: $ipAddress = $turnOn")
return URL("http://$ipAddress/$command?turn=${if (turnOn) "on" else "off"}&transition=$transitionDuration&").get<String>()
return URL("http://$ipAddress/$command?turn=${if (turnOn == true) "on" else "off"}&transition=$transitionDuration&").get<String>()
}

fun setGain(
Expand All @@ -43,11 +43,11 @@ object ShellyClient {
rgbColor: RGBColor,
gain: Float,
transitionDuration: Long = 1, // zero is interpreted as empty which leads to the default of 2000 millis
turnOn: Boolean = true,
turnOn: Boolean? = true,
): Light? {
log.debug("setColor: $ipAddress = ${rgbColor.ansiColor()} [$gain]")
return URL(
"http://$ipAddress/color/0?turn=${if (turnOn) "on" else "off"}&red=${rgbColor.red}&green=${rgbColor.green}&blue=${rgbColor.blue}&white=0&gain=${(100 * gain).toInt()}&transition=$transitionDuration&"
"http://$ipAddress/color/0?turn=${if (turnOn == true) "on" else "off"}&red=${rgbColor.red}&green=${rgbColor.green}&blue=${rgbColor.blue}&white=0&gain=${(100 * gain).toInt()}&transition=$transitionDuration&"
).get<Light>()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ShellyColor(
val ipAddress: String,
private var color: RGBColor,
private var deviceGain: Float,
private var deviceTurnOn: Boolean
private var deviceTurnOn: Boolean?
) : Fadeable<ShellyColor> {

private val log: Logger = LoggerFactory.getLogger(javaClass)
Expand All @@ -22,9 +22,9 @@ class ShellyColor(
return ShellyColor(deviceId, ipAddress, color.clone(), deviceGain, deviceTurnOn)
}

override fun getTurnOn(): Boolean = deviceTurnOn
override fun getTurnOn(): Boolean = deviceTurnOn?:false

override fun setTurnOn(turnOn: Boolean) {
override fun setTurnOn(turnOn: Boolean?) {
this.deviceTurnOn = turnOn
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class YamahaReceiverClient(
?.getMenu<Menu>("Main Zone/Volume/Level")
?.createCommand(volume.toString())
?.let { control(it) }

}

fun controlSurroundProgram(program: String?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface Fadeable<T : Fadeable<T>> {

fun getTurnOn(): Boolean? = false

fun setTurnOn(turnOn: Boolean) {
fun setTurnOn(turnOn: Boolean?) {
}

fun getGain(): Float = 1.0f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ class HybridScene() : Fadeable<HybridScene> {

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

private var ids: String = ""
private var hexColors: String = ""
private var gains: String = ""
private var turnOns: String = ""
private var ids: String? = null
private var hexColors: String? = null
private var gains: String? = null
private var turnOns: String? = null
private var preferences: Preferences? = null

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

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

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

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

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

val lTurnOns = turnOns
.split(",")
.filter { it.isNotEmpty() }
.map { it.toBoolean() }
?.split(",")
?.filter { it.isNotEmpty() }
?.map { it.toBoolean() }
?: listOf()
val nt = lTurnOns.size - 1
var t = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import java.nio.file.Paths
data class Preferences(
val name: String = "",
val theme: String = "",
val fadeDurationDefault: Long,
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
Loading

0 comments on commit 6b0d01b

Please sign in to comment.