]> git.basschouten.com Git - openhab-addons.git/blob
0b41b6a24ea238e5eee47a6129f76e037be6ed86
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.List;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.core.library.CoreItemFactory;
20 import org.openhab.core.library.types.PercentType;
21 import org.openhab.core.library.types.StopMoveType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.types.Command;
25
26 /**
27  * Implements a rollershutter value.
28  * <p>
29  * The stop, up and down strings have multiple purposes.
30  * For one if those strings are received via MQTT they are recognised as corresponding commands
31  * and also posted as Commands to the framework.
32  * And if a user commands an Item->Channel to perform Stop the corresponding string is send. For Up,Down
33  * the percentage 0 and 100 is send.
34  *
35  * @author David Graeff - Initial contribution
36  */
37 @NonNullByDefault
38 public class RollershutterValue extends Value {
39     private final @Nullable String upString;
40     private final @Nullable String downString;
41     private final String stopString;
42     private boolean nextIsStop = false; // If set: getMQTTpublishValue will return the stop string
43
44     /**
45      * Creates a new rollershutter value.
46      *
47      * @param upString The UP value string. This will be compared to MQTT messages.
48      * @param downString The DOWN value string. This will be compared to MQTT messages.
49      * @param stopString The STOP value string. This will be compared to MQTT messages.
50      */
51     public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
52         super(CoreItemFactory.ROLLERSHUTTER,
53                 List.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class));
54         this.upString = upString;
55         this.downString = downString;
56         this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
57     }
58
59     @Override
60     public void update(Command command) throws IllegalArgumentException {
61         nextIsStop = false;
62         if (command instanceof StopMoveType) {
63             nextIsStop = (((StopMoveType) command) == StopMoveType.STOP);
64             return;
65         } else if (command instanceof UpDownType) {
66             state = ((UpDownType) command) == UpDownType.UP ? PercentType.ZERO : PercentType.HUNDRED;
67             return;
68         } else if (command instanceof PercentType) {
69             state = (PercentType) command;
70             return;
71         } else if (command instanceof StringType) {
72             final String updatedValue = command.toString();
73             if (updatedValue.equals(upString)) {
74                 state = PercentType.ZERO;
75                 return;
76             } else if (updatedValue.equals(downString)) {
77                 state = PercentType.HUNDRED;
78                 return;
79             } else if (updatedValue.equals(stopString)) {
80                 nextIsStop = true;
81                 return;
82             }
83         }
84         throw new IllegalStateException("Cannot call update() with " + command.toString());
85     }
86
87     /**
88      * The stop command will not update the internal state and is posted to the framework.
89      * <p>
90      * The Up/Down commands (100%/0%) are not updating the state directly and are also
91      * posted as percent value to the framework. It is up to the user if the posted values
92      * are applied to the item state immediately (autoupdate=true) or not.
93      */
94     @Override
95     public @Nullable Command isPostOnly(Command command) {
96         if (command instanceof UpDownType) {
97             return command;
98         } else if (command instanceof StopMoveType) {
99             return command;
100         } else if (command instanceof StringType) {
101             final String updatedValue = command.toString();
102             if (updatedValue.equals(upString)) {
103                 return UpDownType.UP.as(PercentType.class);
104             } else if (updatedValue.equals(downString)) {
105                 return UpDownType.DOWN.as(PercentType.class);
106             } else if (updatedValue.equals(stopString)) {
107                 return StopMoveType.STOP;
108             }
109         }
110         return null;
111     }
112
113     @Override
114     public String getMQTTpublishValue(@Nullable String pattern) {
115         final String upString = this.upString;
116         final String downString = this.downString;
117         if (this.nextIsStop) {
118             this.nextIsStop = false;
119             return stopString;
120         } else if (state instanceof PercentType) {
121             if (state.equals(PercentType.HUNDRED) && downString != null) {
122                 return downString;
123             } else if (state.equals(PercentType.ZERO) && upString != null) {
124                 return upString;
125             } else {
126                 return String.valueOf(((PercentType) state).intValue());
127             }
128         } else {
129             return "UNDEF";
130         }
131     }
132 }