Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
MQTT: Rollershutter channel. Proper STOP command support.
Browse files Browse the repository at this point in the history
The Rollershutter channel stores its value as percentage.
UP/DOWN/STOP commands received via MQTT do not change that state.

Instead of trying to handle those commands, they are now
posted to the framework.

Signed-off-by: David Gräff <david.graeff@web.de>
  • Loading branch information
David Gräff committed Dec 18, 2018
1 parent 2d592e9 commit 6739a16
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,10 @@ public void updateChannelState(ChannelUID channelUID, State value) {
* Proxy method to condense all three MQTT subscriptions to one channel
*/
@Override
public void postChannelState(ChannelUID channelUID, Command value) {
public void postChannelCommand(ChannelUID channelUID, Command value) {
ChannelStateUpdateListener listener = channelStateUpdateListener;
if (listener != null) {
listener.postChannelState(colorChannel.channelUID, value);
listener.postChannelCommand(colorChannel.channelUID, value);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ public void processMessage(String topic, byte[] payload) {
return;
}

Command postOnlyCommand = cachedValue.isPostOnly(command);
if (postOnlyCommand != null) {
channelStateUpdateListener.postChannelCommand(channelUID, postOnlyCommand);
receivedOrTimeout();
return;
}

// Map the string to an ESH command, update the cached value and post the command to the framework
try {
cachedValue.update(command);
Expand All @@ -172,7 +179,7 @@ public void processMessage(String topic, byte[] payload) {
}

if (config.postCommand) {
channelStateUpdateListener.postChannelState(channelUID, (Command) cachedValue.getChannelState());
channelStateUpdateListener.postChannelCommand(channelUID, (Command) cachedValue.getChannelState());
} else {
channelStateUpdateListener.updateChannelState(channelUID, cachedValue.getChannelState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public interface ChannelStateUpdateListener {
* @param channelUID The channel uid
* @param value The new value. Doesn't necessarily need to be different than the value before.
*/
void postChannelState(ChannelUID channelUID, Command value);
void postChannelCommand(ChannelUID channelUID, Command value);

/**
* A new value got published on a configured MQTT topic associated with the given channel uid.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void triggerChannel(ChannelUID channelUID, String event) {
}

@Override
public void postChannelState(ChannelUID channelUID, Command command) {
public void postChannelCommand(ChannelUID channelUID, Command command) {
postCommand(channelUID, command);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,29 @@ public void update(Command command) throws IllegalArgumentException {
}
state = newState;
} else if (command instanceof StopMoveType) {
if (command.equals(StopMoveType.STOP)) {
state = UnDefType.UNDEF;
}
throw new IllegalStateException("Cannot call update() with StopMoveType");
} else if (command instanceof PercentType) {
state = (PercentType) command;
} else {
throw new IllegalStateException("Cannot call update() with custom stop/move/up/down");
}
}

@Override
public @Nullable Command isPostOnly(Command command) {
if (command instanceof StopMoveType) {
return command;
} else if (command instanceof StringType) {
final String updatedValue = command.toString();
if (updatedValue.equals(upString)) {
state = PercentType.ZERO;
return UpDownType.UP;
} else if (updatedValue.equals(downString)) {
state = PercentType.HUNDRED;
return UpDownType.DOWN;
} else if (updatedValue.equals(stopString)) {
state = UnDefType.UNDEF;
} else {
state = PercentType.valueOf(updatedValue);
return StopMoveType.STOP;
}
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.binding.mqtt.generic.internal.handler.GenericThingHandler;
import org.eclipse.smarthome.core.library.CoreItemFactory;
import org.eclipse.smarthome.core.library.types.DecimalType;
Expand Down Expand Up @@ -118,6 +119,17 @@ public final void resetState() {
*/
public abstract void update(Command command) throws IllegalArgumentException;

/**
* Returns the given command if it cannot be handled by {@link #update(Command)}
* or {@link #update(byte[])} and need to be posted straight to the framework instead.
* Returns null otherwise.
*
* @param command The command to decide about
*/
public @Nullable Command isPostOnly(Command command) {
return null;
}

/**
* Updates the internal value state with the given binary payload.
*
Expand Down

0 comments on commit 6739a16

Please sign in to comment.