Skip to content

Commit

Permalink
improved logic for dimmer control to avoid duplicate state updates
Browse files Browse the repository at this point in the history
Signed-off-by: Massimo Valla <[email protected]>
  • Loading branch information
mvalla committed Aug 7, 2020
1 parent 68b156c commit c64112f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ public class OpenWebNetLightingHandler extends OpenWebNetThingHandler {
private final Logger logger = LoggerFactory.getLogger(OpenWebNetLightingHandler.class);

public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.LIGHTING_SUPPORTED_THING_TYPES;

private static final int BRIGHTNESS_CHANGE_DELAY_MSEC = 1500; // delay to wait before sending another brightness
// status request

private long lastBrightnessChangeSentTS = 0; // timestamp when last brightness change was sent to the device

private boolean brightnessLevelRequested = false; // was the brightness level requested ?
private int latestBrightnessWhat = -1; // latest brightness WHAT value (-1 = unknown)
private int latestBrightnessWhatBeforeOff = -1; // latest brightness WHAT value before device was set to off
Expand Down Expand Up @@ -143,16 +143,14 @@ private void handleBrightnessCommand(Command command) {
* Helper method to dim light to a valid OWN value
*/
private void dimLightTo(int whatInt, Command command) {
final String channel = CHANNEL_BRIGHTNESS;
int newWhatInt = whatInt;
logger.debug("-DIM- dimLightTo() latestBriWhat={} latestBriBeforeOff={} briLevelRequested={}",
latestBrightnessWhat, latestBrightnessWhatBeforeOff, brightnessLevelRequested);
What newWhat;
if (OnOffType.ON.equals(command) && latestBrightnessWhat <= 0) {
// ON after OFF/Unknown -> we reset channel to last value before OFF (if exists)
if (latestBrightnessWhatBeforeOff > 0) {
if (latestBrightnessWhatBeforeOff > 0) { // we know last brightness -> set dimmer to it
newWhatInt = latestBrightnessWhatBeforeOff;
updateState(channel, new PercentType(Lighting.levelToPercent(newWhatInt)));
} else { // we do not know last brightness -> set dimmer to 100%
newWhatInt = 10;
}
Expand All @@ -169,19 +167,15 @@ private void dimLightTo(int whatInt, Command command) {
if (newWhatInt == 0) {
latestBrightnessWhatBeforeOff = latestBrightnessWhat;
}
lastBrightnessChangeSentTS = System.currentTimeMillis();
Where w = deviceWhere;
if (w != null) {
try {
lastBrightnessChangeSentTS = System.currentTimeMillis();
send(Lighting.requestDimTo(w.value(), newWhat));
} catch (OWNException e) {
logger.warn("Exception while sending dimLightTo for command {}: {}", command, e.getMessage());
}
}
if (!(command instanceof PercentType)) {
updateState(channel, new PercentType(Lighting.levelToPercent(newWhatInt)));
}
latestBrightnessWhat = newWhatInt;
} else {
logger.debug("-DIM- do nothing");
}
Expand All @@ -204,7 +198,7 @@ protected void handleMessage(BaseOpenMessage msg) {
}

/**
* Updates light state based on a OWN Lighting message received
* Updates light state based on a OWN Lighting event message received
*
* @param msg the Lighting message received
*/
Expand All @@ -219,7 +213,7 @@ private void updateLightState(Lighting msg) {
}

/**
* Updates on/off state based on a OWN Lighting message received
* Updates on/off state based on a OWN Lighting event message received
*
* @param msg the Lighting message received
*/
Expand Down Expand Up @@ -249,56 +243,49 @@ private void updateLightOnOffState(Lighting msg) {
}

/**
* Updates brightness level based on a OWN Lighting message received
* Updates brightness level based on a OWN Lighting event message received
*
* @param msg the Lighting message received
*/
private synchronized void updateLightBrightnessState(Lighting msg) {
final String channel = CHANNEL_BRIGHTNESS;
logger.debug("$BRI updateLightBrightnessState() msg={}", msg);
logger.debug("$BRI updateLightBr() latestBriWhat={} latestBriBeforeOff={} brightnessLevelRequested={}",
logger.debug(" $BRI updateLightBrightnessState() msg={}", msg);
logger.debug(" $BRI updateLightBr() latestBriWhat={} latestBriBeforeOff={} brightnessLevelRequested={}",
latestBrightnessWhat, latestBrightnessWhatBeforeOff, brightnessLevelRequested);
long now = System.currentTimeMillis();
long delta = now - lastBrightnessChangeSentTS;
logger.debug("$BRI now={} delta={}", now, delta);
logger.debug(" $BRI now={} -> delta={}", now, delta);
if (msg.isOn() && !brightnessLevelRequested) {
if (delta >= BRIGHTNESS_CHANGE_DELAY_MSEC) {
// we send a light brightness status request ONLY if last brightness change
// was sent >BRIGHTNESS_CHANGE_DELAY_MSEC ago
logger.debug("$BRI change sent >={}ms ago, sending requestStatus...", BRIGHTNESS_CHANGE_DELAY_MSEC);
brightnessLevelRequested = true;
// was not just sent (>=BRIGHTNESS_CHANGE_DELAY_MSEC ago)
logger.debug(" $BRI change sent >={}ms ago, sending requestStatus...", BRIGHTNESS_CHANGE_DELAY_MSEC);
Where w = deviceWhere;
if (w != null) {
try {
send(Lighting.requestStatus(w.value()));
brightnessLevelRequested = true;
} catch (OWNException e) {
logger.warn("$BRI exception while requesting light state: {}", e.getMessage());
logger.warn(" $BRI exception while requesting light state: {}", e.getMessage());
}
}
} else {
logger.debug("$BRI change sent {}<{}ms, NO requestStatus needed", delta, BRIGHTNESS_CHANGE_DELAY_MSEC);
logger.debug(" $BRI change sent {}<{}ms, NO requestStatus needed", delta,
BRIGHTNESS_CHANGE_DELAY_MSEC);
}
} else {
logger.debug("$BRI update from network -> level should be present in WHAT part of the message");
logger.debug(" $BRI update from network -> level should be present in WHAT part of the message");
if (msg.getWhat() != null) {
int newLevel = msg.getWhat().value();
logger.debug("$BRI current level={} ----> new level={}", latestBrightnessWhat, newLevel);
logger.debug(" $BRI current level={} ----> new level={}", latestBrightnessWhat, newLevel);
if (latestBrightnessWhat != newLevel) {
if (delta >= BRIGHTNESS_CHANGE_DELAY_MSEC) {
logger.debug("$BRI change sent >={}ms ago, updating state...", BRIGHTNESS_CHANGE_DELAY_MSEC);
updateState(channel, new PercentType(Lighting.levelToPercent(newLevel)));
} else if (msg.isOff()) {
logger.debug("$BRI change just sent, but OFF from network received, updating state...");
updateState(channel, new PercentType(Lighting.levelToPercent(newLevel)));
} else {
logger.debug("$BRI change just sent, NO update needed.");
}
updateState(channel, new PercentType(Lighting.levelToPercent(newLevel)));
if (msg.isOff()) {
latestBrightnessWhatBeforeOff = latestBrightnessWhat;
}
latestBrightnessWhat = newLevel;
} else {
logger.debug("$BRI no change");
logger.debug(" $BRI no change");
}
brightnessLevelRequested = false;
} else { // dimension notification
Expand All @@ -311,7 +298,7 @@ private synchronized void updateLightBrightnessState(Lighting msg) {
return;
}
int newLevel = Lighting.percentToWhat(newPercent).value();
logger.debug("$BRI latest level={} ----> new percent={} ----> new level={}", latestBrightnessWhat,
logger.debug(" $BRI latest level={} ----> new percent={} ----> new level={}", latestBrightnessWhat,
newPercent, newLevel);
updateState(channel, new PercentType(newPercent));
if (newPercent == 0) {
Expand All @@ -326,7 +313,7 @@ private synchronized void updateLightBrightnessState(Lighting msg) {
}
}
}
logger.debug("$BRI latestBriWhat={} latestBriBeforeOff={} brightnessLevelRequested={}", latestBrightnessWhat,
logger.debug(" $BRI latestBriWhat={} latestBriBeforeOff={} brightnessLevelRequested={}", latestBrightnessWhat,
latestBrightnessWhatBeforeOff, brightnessLevelRequested);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ public String getPasswd() {
public Boolean getDiscoveryByActivation() {
return discoveryByActivation;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
logger.warn("ThingType {} is not supported by this binding", thing.getThingTypeUID());
return null;
}

}

0 comments on commit c64112f

Please sign in to comment.