]> git.basschouten.com Git - openhab-addons.git/blob
f3fc46f3a88a275e3c09d0e50ed3894ba58193b1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.mqtt.generic.values;
14
15 import java.util.stream.Collectors;
16 import java.util.stream.Stream;
17
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;
26
27 /**
28  * Implements an rollershutter value.
29  * <p>
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.
35  *
36  * @author David Graeff - Initial contribution
37  */
38 @NonNullByDefault
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
44
45     /**
46      * Creates a new rollershutter value.
47      *
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.
51      */
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;
59     }
60
61     @Override
62     public void update(Command command) throws IllegalArgumentException {
63         nextIsStop = false;
64         if (command instanceof StopMoveType) {
65             nextIsStop = (((StopMoveType) command) == StopMoveType.STOP);
66             return;
67         } else if (command instanceof UpDownType) {
68             state = ((UpDownType) command) == UpDownType.UP ? PercentType.ZERO : PercentType.HUNDRED;
69             return;
70         } else if (command instanceof PercentType) {
71             state = (PercentType) command;
72             return;
73         } else if (command instanceof StringType) {
74             final String updatedValue = command.toString();
75             if (updatedValue.equals(upString)) {
76                 state = PercentType.ZERO;
77                 return;
78             } else if (updatedValue.equals(downString)) {
79                 state = PercentType.HUNDRED;
80                 return;
81             } else if (updatedValue.equals(stopString)) {
82                 nextIsStop = true;
83                 return;
84             }
85         }
86         throw new IllegalStateException("Cannot call update() with " + command.toString());
87     }
88
89     /**
90      * The stop command will not update the internal state and is posted to the framework.
91      * <p>
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.
95      */
96     @Override
97     public @Nullable Command isPostOnly(Command command) {
98         if (command instanceof UpDownType) {
99             return command;
100         } else if (command instanceof StopMoveType) {
101             return command;
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;
110             }
111         }
112         return null;
113     }
114
115     @Override
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;
121             return stopString;
122         } else if (state instanceof PercentType) {
123             if (state.equals(PercentType.HUNDRED) && downString != null) {
124                 return downString;
125             } else if (state.equals(PercentType.ZERO) && upString != null) {
126                 return upString;
127             } else {
128                 return String.valueOf(((PercentType) state).intValue());
129             }
130         } else {
131             return "UNDEF";
132         }
133     }
134 }