Skip to content

Commit

Permalink
Fix potential division by zero, added current scene display in scenes…
Browse files Browse the repository at this point in the history
… view
  • Loading branch information
sknull committed Jan 4, 2024
1 parent 6378dc8 commit 028c8ca
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package de.visualdigits.kotlin.klanglicht.model.fixture

import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
import java.io.File
import java.nio.file.Paths

class Fixtures(
val fixtures: List<Fixture>
) {
companion object {
private val mapper = jacksonMapperBuilder().build()

private var fixtures: Fixtures? = null

fun load(klanglichtDir: File): Fixtures {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package de.visualdigits.kotlin.klanglicht.model.lightmanager.json

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.io.IOException
import java.io.InputStream

Expand Down Expand Up @@ -42,11 +43,12 @@ class Project(
}

companion object {
val MAPPER: JsonMapper = JsonMapper()
private val mapper = jacksonObjectMapper()

fun load(ins: InputStream?): Project {
val project: Project
try {
project = MAPPER.readValue<Project>(ins, Project::class.java)
project = mapper.readValue<Project>(ins, Project::class.java)
val scenesMap = project.scenesMap
project.determineScenes(project.scenes, scenesMap.toMutableMap())
project.determineActuators(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
import com.fasterxml.jackson.module.kotlin.kotlinModule
import java.io.IOException
import java.io.InputStream

Expand All @@ -14,11 +15,12 @@ class Lightman(
) {

companion object {
val MAPPER: XmlMapper = XmlMapper()
val mapper: XmlMapper = XmlMapper.builder().addModule(kotlinModule()).build()

fun load(ins: InputStream?): Lightman {
val lightman: Lightman
lightman = try {
MAPPER.readValue<Lightman>(ins, Lightman::class.java)
mapper.readValue<Lightman>(ins, Lightman::class.java)
} catch (e: IOException) {
throw IllegalStateException("Could not load xml file", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ interface Fadeable<T : Fadeable<T>> {
fadeDuration: Long,
preferences: Preferences
) {
val dmxFrameTime = preferences.getDmxFrameTime()
val step = 1.0 / fadeDuration.toDouble() * dmxFrameTime.toDouble()
var factor = 0.0

while (factor <= 1.0) {
val faded = fade(other, factor)
faded.write(preferences)
factor += step
Thread.sleep(dmxFrameTime)
if (fadeDuration > 0) {
val dmxFrameTime = preferences.getDmxFrameTime()
val step = 1.0 / fadeDuration.toDouble() * dmxFrameTime.toDouble()
var factor = 0.0

while (factor <= 1.0) {
val faded = fade(other, factor)
faded.write(preferences)
factor += step
Thread.sleep(dmxFrameTime)
}
}
other.write(preferences)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.jacksonMapperBuilder
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.time.LocalTime
import java.time.OffsetDateTime

Expand Down Expand Up @@ -44,16 +46,15 @@ class Status(
) {

companion object {
val MAPPER: JsonMapper = JsonMapper
.builder()
private val mapper = jacksonMapperBuilder()
.disable(SerializationFeature.INDENT_OUTPUT)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.addModule(JavaTimeModule())
.build()

fun load(json: String?): Status {
return try {
MAPPER.readValue<Status>(json, Status::class.java)
mapper.readValue<Status>(json, Status::class.java)
} catch (e: JsonProcessingException) {
throw IllegalStateException("Could not read JSON string", e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement
import com.fasterxml.jackson.module.kotlin.kotlinModule
import java.io.IOException
import java.io.InputStream

Expand Down Expand Up @@ -35,11 +36,10 @@ class UnitDescription : AbstractMenuProvider() {
}

companion object {
val mapper: XmlMapper = XmlMapper()

init {
mapper.enable(SerializationFeature.INDENT_OUTPUT)
}
val mapper = XmlMapper.builder()
.addModule(kotlinModule())
.enable(SerializationFeature.INDENT_OUTPUT)
.build()

fun load(ins: InputStream): UnitDescription {
val unitDescription: UnitDescription
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ class LMScenes(
sb.append("<div class=\"title\" onclick=\"toggleFullScreen();\" alt=\"Toggle Fullscreen\" title=\"Toggle Fullscreen\">")
.append(name)
.append("</div>\n")
sb.append("<div class=\"center-category\">\n")
renderLabel(sb, "C U R R E N T S C E N E")
sb.append("<div class=\"center-group\">\n")
configHolder.currentScene?.fadeables()?.forEach { fadeable ->
val color = fadeable.getRgbColor()?.web()?:"#000000"
renderPanel(sb, "circle", color, "")
}
sb.append("</div><!-- sub-group -->\n\n")
sb.append("</div><!-- current scene -->\n\n")

sb.append("<div class=\"category\">\n")
renderLabel(sb, "S C E N E S")
val scenesMap: Map<String, Collection<LMScene>> = TreeMap<String, Collection<LMScene>>(scenes.asMap())
Expand All @@ -55,6 +65,15 @@ class LMScenes(
return sb.toString()
}

private fun renderPanel(sb: StringBuilder, clazz: String, bgColor: String, value: String?) {
sb.append(" <div class=\"").append(clazz).append("\" style=\"background-color:").append(bgColor)
.append("\" >\n")
if (value != null && !value.isEmpty()) {
renderLabel(sb, value)
}
sb.append(" </div> <!-- ").append(clazz).append(" -->\n")
}

private fun renderScenesGroup(
sb: StringBuilder,
configHolder: ConfigHolder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ShellyHandler {
try {
status = ShellyClient.getStatus(ipAddress)
} catch (e: Exception) {
log.warn("Could not get ststus for shelly at '$ipAddress'")
log.warn("Could not get status for shelly at '$ipAddress'")
status = Status()
status.mode = "offline"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ class ShellyStatus : HtmlRenderable {
renderPanel(sb, "textpanel", bgColor, "Power&nbsp;$power")

// on/off status
var isOn = false
val lightColors: MutableList<String> = ArrayList()
var isOn = false
if (isOnline) {
val relays = status.relays
if (relays != null) {
for (relay in relays) {
if (relay.isOn == true) {
isOn = true
break
}
}
isOn = relays.any { it.isOn == true}
bgColor = if (isOn) "red" else "green"
} else {
val lights = status.lights
Expand Down

0 comments on commit 028c8ca

Please sign in to comment.