One function for "ON/OFF", "long press" and "double press"

This one here should work correctly:

import java.util.Map

val Map<String, Timer> timers = newHashMap()
val Map<String, Long> timerStartTimes = newHashMap()
val lightButtonDimmerTimer = [ SwitchItem button, DimmerItem light, Map<String, Timer> timers, Map<String, Long> timerStartTimes |
        val longPressDuration = 300     // min time button to press to be recognized as "long press"
        val doublePressDuration = 300   // time within button needs to be pressed twice to be "double press"
        val fadingStepTime = 200        // time between to fading changes
        var fadingStep = 10             // fading steps
        val String timerId = button.getUID() + "|||" + light.getUID()

        logInfo("lightButtonDimmerTimer logging", "start rule")
        logInfo("lightButtonDimmerTimer logging", "button id " + button.getUID())
        logInfo("lightButtonDimmerTimer logging", "light id " + light.getUID())

        var percent = (light.state as DecimalType).intValue
        logInfo("lightButtonDimmerTimer logging", "current light percent: "+percent)

        // check if we need to fade IN our OUT
        var fadeIn = light.state < 100
        logInfo("loggerttonDimmerTimer loggingName", "fadeIn: "+fadeIn)

        // inverse fadeing if needed (fadeOut)
        if (!fadeIn) fadingStep *= -1 
        logInfo("loggerttonDimmerTimer loggingName", "fadingStep: "+fadingStep)

        logInfo("loggerttonDimmerTimer loggingName", "button.state: "+button.state)
        if (button.state == ON) {
            logInfo("loggerttonDimmerTimer loggingName", "button pressed: "+button.state)

            val Timer fade_Timer = null
            fade_Timer = createTimer(now.plusMillis(longPressDuration)) [|
                logInfo("loggerttonDimmerTimer loggingName", "inside timer, percent: "+percent)
                if (percent >= 0 && percent <= 100) {
                    light.sendCommand(percent)
                    percent += fadingStep
                    logInfo("loggerttonDimmerTimer loggingName", "change percent to: "+percent)
                    fade_Timer.reschedule(now.plusMillis(fadingStepTime))
                    logInfo("loggerttonDimmerTimer loggingName", "rescheduled timer: "+fadingStepTime)
                }
            ]
            timers.put(timerId, fade_Timer)
            timerStartTimes.put(timerId, now.getMillis())
        } else {
            logInfo("loggerttonDimmerTimer loggingName", "button released: "+button.state)
            val fade_Timer = timers.remove(timerId)
            fade_Timer?.cancel() // needs to be canceled in any case

            val clickDuration = now.getMillis() - timerStartTimes.remove(timerId)
            logInfo("loggerttonDimmerTimer loggingName", "click duration = " + clickDuration + "ms")
            if (clickDuration < longPressDuration) {
                logInfo("loggerttonDimmerTimer loggingName", "short pressed. switch power")
                if (light.state > 0) {
                    light.sendCommand(OFF)
                } else {
                    light.sendCommand(ON)
                }
            }
        }
    ]
1 Like