Skip to content

Commit

Permalink
Remove scene scraping from lightmanager as it is no longer used that way
Browse files Browse the repository at this point in the history
  • Loading branch information
sknull committed Apr 20, 2024
1 parent 4a35497 commit fb86c9e
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 2,427 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
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.LMActionHybrid
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionLmAir
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.LMActionShelly
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActionUrl
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMActor
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMMarker
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMMarkers
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMParams
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScene
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScenes
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMZones
import de.visualdigits.kotlin.util.get
import de.visualdigits.kotlin.util.post
Expand Down Expand Up @@ -64,20 +53,6 @@ class LightmanagerClient(
return zones
}

fun getActorById(actorId: Int): LMActor? {
var a: LMActor? = null
val zones: LMZones = zones()
for (zone in zones.zones) {
for (actor in zone.actors) {
if (actor.id == actorId) {
a = actor
break
}
}
}
return a
}

fun knownActors(): Map<Int, String> {
val actors: MutableMap<Int, String> = mutableMapOf()
val zones: LMZones = zones()
Expand All @@ -89,91 +64,6 @@ class LightmanagerClient(
return actors
}

fun scenes(lmAirProject: LmAirProject? = null): LMScenes {
val document = Jsoup.parse(html()!!)
val setupName = document.select("div[id=mytitle]").first()?.text()
val scenes = LMScenes(setupName)
document
.select("div[id=scenes]")
.first()
?.select("div[class=sbElement]")
?.forEach { elem -> scenes.add(LMScene(name = elem.child(0).text())) }
determineActions(lmAirProject, scenes)
return scenes
}

private fun determineActions(
lmAirProject: LmAirProject?,
scenes: LMScenes
) {
if (lmAirProject != null) {
scenes.scenes.values.forEach { g ->
g.scenes.forEach { scene ->
lmAirProject.scenesMap[scene.name]?.let { s ->
val actuatorProperties = s.getActuatorProperties()
scene.actions = actuatorProperties.mapNotNull { ap ->
when (ap.ntype) {
NType.irlan -> {
lmAirProject.actuatorsMap[ap.actorIndex]?.let { actuator ->
val url = if (ap.command == 1) {
actuator.properties?.url2
} else {
actuator.properties?.url
}
if (url != null) {
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 }.associate { p -> Pair(p[0], p[1]) }
when (u.path) {
"/v1/yamaha/avantage/json/surroundProgram" ->
LMActionLmYamahaAvantage(
command = "surroundProgram",
program = params["program"]
)

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

"/v1/hybrid/json/hexColor" ->
LMActionHybrid(
ids = params["ids"],
hexColors = params["hexColors"],
gains = params["gains"],
)
else ->
LMActionUrl(
url = url.substringAfter("v1/")
)
}
} else {
val param = if (ap.command == 1) {
1499
} else {
42
}
LMActionLmAir(sceneIndex = param)
}
}
}

NType.pause -> LMActionPause(duration = ap.duration?.let { d -> 1000 * d } ?: 0)
else ->
null
}
}
}
}
}
}
}

fun markers(): LMMarkers {
val markerState: BooleanArray = params().markerState
val document = Jsoup.parse(html()!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ import java.util.Locale

@JsonIgnoreProperties("scenesMap")
class LMScenes(
val name: String? = null
) {

val name: String? = null,
val scenes: LinkedHashMap<String, LMSceneGroup> = LinkedHashMap()
) {

val scenesMap: LinkedHashMap<String, LMScene> = LinkedHashMap()

Expand All @@ -32,33 +31,4 @@ class LMScenes(
override fun toString(): String {
return "$name\n" + scenes.toMap().map { e -> " ${e.key}\n ${e.value.scenes.joinToString("\n ")}" }.joinToString("\n")
}

fun add(scene: LMScene) {
scenesMap[scene.name!!] = scene
var group: String? = "common"
val attributes = LMNamedAttributes(scene.name, "group", "color")
if (attributes.matched()) {
val name = attributes.name
if (name?.isNotEmpty() == true) {
scene.name = name
}
val g = attributes["group"]
if (g.isNotEmpty()) {
group = g
}
scene.color = attributes["color"]
}
if ("hidden" != group) {
group
?.replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
?.let { name ->
var g = scenes[name]
if (g == null) {
g = LMSceneGroup(name)
scenes[name] = g
}
g.scenes.add(scene)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.client
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.json.LmAirProject
import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.lm.LMScenes
import org.junit.jupiter.api.Test
import java.io.File
Expand Down Expand Up @@ -54,27 +53,9 @@ class LightmanagerClientTest {
println(html)
}

@Test
fun testScrapeScenes() {
val lmAirProject = LmAirProject.unmarshall(File(ClassLoader.getSystemResource("lmair/lmair-config.json").toURI()))
val scenes = client.scenes(lmAirProject)
val json = mapper.writeValueAsString(scenes)

println(json)
}

@Test
fun testScrapeSceneConfig() {
val lmAirProject = LmAirProject.unmarshall(File(ClassLoader.getSystemResource("lmair/lmair-config.json").toURI()))
val scenes = client.scenes(lmAirProject)
val json = mapper.writeValueAsString(scenes)

println(json)
}

@Test
fun testLoadScenes() {
val file = File(ClassLoader.getSystemResource("lmair/scenes.json").toURI())
val file = File(ClassLoader.getSystemResource(".klanglicht/preferences/scenes.json").toURI())
val scenes = LMScenes.unmarshall(file)
println(scenes)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.visualdigits.kotlin.klanglicht.hardware.lightmanager.json

import de.visualdigits.kotlin.klanglicht.hardware.lightmanager.model.json.LmAirProject
import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import java.io.File

Expand All @@ -9,6 +10,6 @@ class LmAirProjectTest {
@Test
fun testLoadModel() {
val config = LmAirProject.unmarshall(File(ClassLoader.getSystemResource("lmair/lmair-config.json").toURI()))
println(config)
assertNotNull(config)
}
}
Loading

0 comments on commit fb86c9e

Please sign in to comment.