2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.mqtt.generic.values;
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.CoreItemFactory;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.StopMoveType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.library.types.UpDownType;
25 import org.openhab.core.types.Command;
28 * Implements an rollershutter value.
30 * The stop, up and down strings have multiple purposes.
31 * For one if those strings are received via MQTT they are recognised as corresponding commands
32 * and also posted as Commands to the framework.
33 * And if a user commands an Item->Channel to perform Stop the corresponding string is send. For Up,Down
34 * the percentage 0 and 100 is send.
36 * @author David Graeff - Initial contribution
39 public class RollershutterValue extends Value {
40 private final @Nullable String upString;
41 private final @Nullable String downString;
42 private final String stopString;
43 private boolean nextIsStop = false; // If set: getMQTTpublishValue will return the stop string
46 * Creates a new rollershutter value.
48 * @param upString The UP value string. This will be compared to MQTT messages.
49 * @param downString The DOWN value string. This will be compared to MQTT messages.
50 * @param stopString The STOP value string. This will be compared to MQTT messages.
52 public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
53 super(CoreItemFactory.ROLLERSHUTTER,
54 Stream.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class)
55 .collect(Collectors.toList()));
56 this.upString = upString;
57 this.downString = downString;
58 this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
62 public void update(Command command) throws IllegalArgumentException {
64 if (command instanceof StopMoveType) {
65 nextIsStop = (((StopMoveType) command) == StopMoveType.STOP);
67 } else if (command instanceof UpDownType) {
68 state = ((UpDownType) command) == UpDownType.UP ? PercentType.ZERO : PercentType.HUNDRED;
70 } else if (command instanceof PercentType) {
71 state = (PercentType) command;
73 } else if (command instanceof StringType) {
74 final String updatedValue = command.toString();
75 if (updatedValue.equals(upString)) {
76 state = PercentType.ZERO;
78 } else if (updatedValue.equals(downString)) {
79 state = PercentType.HUNDRED;
81 } else if (updatedValue.equals(stopString)) {
86 throw new IllegalStateException("Cannot call update() with " + command.toString());
90 * The stop command will not update the internal state and is posted to the framework.
92 * The Up/Down commands (100%/0%) are not updating the state directly and are also
93 * posted as percent value to the framework. It is up to the user if the posted values
94 * are applied to the item state immediately (autoupdate=true) or not.
97 public @Nullable Command isPostOnly(Command command) {
98 if (command instanceof UpDownType) {
100 } else if (command instanceof StopMoveType) {
102 } else if (command instanceof StringType) {
103 final String updatedValue = command.toString();
104 if (updatedValue.equals(upString)) {
105 return UpDownType.UP.as(PercentType.class);
106 } else if (updatedValue.equals(downString)) {
107 return UpDownType.DOWN.as(PercentType.class);
108 } else if (updatedValue.equals(stopString)) {
109 return StopMoveType.STOP;
116 public String getMQTTpublishValue(@Nullable String pattern) {
117 final String upString = this.upString;
118 final String downString = this.downString;
119 if (this.nextIsStop) {
120 this.nextIsStop = false;
122 } else if (state instanceof PercentType) {
123 if (state.equals(PercentType.HUNDRED) && downString != null) {
125 } else if (state.equals(PercentType.ZERO) && upString != null) {
128 return String.valueOf(((PercentType) state).intValue());