]> git.basschouten.com Git - openhab-addons.git/blob
8626f8df36c5aa7213e21d36c04acce700022c21
[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
43     /**
44      * Creates a new rollershutter value.
45      *
46      * @param upString The UP value string. This will be compared to MQTT messages.
47      * @param downString The DOWN value string. This will be compared to MQTT messages.
48      * @param stopString The STOP value string. This will be compared to MQTT messages.
49      */
50     public RollershutterValue(@Nullable String upString, @Nullable String downString, @Nullable String stopString) {
51         super(CoreItemFactory.ROLLERSHUTTER,
52                 List.of(UpDownType.class, StopMoveType.class, PercentType.class, StringType.class));
53         this.upString = upString;
54         this.downString = downString;
55         this.stopString = stopString == null ? StopMoveType.STOP.name() : stopString;
56     }
57
58     @Override
59     public Command parseCommand(Command command) throws IllegalArgumentException {
60         if (command instanceof StopMoveType) {
61             if (command == StopMoveType.STOP) {
62                 return command;
63             } else {
64                 throw new IllegalArgumentException(command.toString() + " is not a valid command for MQTT.");
65             }
66         } else if (command instanceof UpDownType) {
67             if (command == UpDownType.UP) {
68                 if (upString != null) {
69                     return command;
70                 } else {
71                     return PercentType.ZERO;
72                 }
73             } else {
74                 if (downString != null) {
75                     return command;
76                 } else {
77                     return PercentType.HUNDRED;
78                 }
79             }
80         } else if (command instanceof PercentType) {
81             return (PercentType) command;
82         } else if (command instanceof StringType) {
83             final String updatedValue = command.toString();
84             if (updatedValue.equals(upString)) {
85                 return UpDownType.UP;
86             } else if (updatedValue.equals(downString)) {
87                 return UpDownType.DOWN;
88             } else if (updatedValue.equals(stopString)) {
89                 return StopMoveType.STOP;
90             }
91         }
92         throw new IllegalStateException("Cannot call parseCommand() with " + command.toString());
93     }
94
95     @Override
96     public String getMQTTpublishValue(Command command, @Nullable String pattern) {
97         final String upString = this.upString;
98         final String downString = this.downString;
99         final String stopString = this.stopString;
100         if (command == UpDownType.UP) {
101             if (upString != null) {
102                 return upString;
103             } else {
104                 return ((UpDownType) command).name();
105             }
106         } else if (command == UpDownType.DOWN) {
107             if (downString != null) {
108                 return downString;
109             } else {
110                 return ((UpDownType) command).name();
111             }
112         } else if (command == StopMoveType.STOP) {
113             if (stopString != null) {
114                 return stopString;
115             } else {
116                 return ((StopMoveType) command).name();
117             }
118         } else if (command instanceof PercentType) {
119             if (command.equals(PercentType.HUNDRED) && downString != null) {
120                 return downString;
121             } else if (command.equals(PercentType.ZERO) && upString != null) {
122                 return upString;
123             } else {
124                 return String.valueOf(((PercentType) command).intValue());
125             }
126         } else {
127             throw new IllegalArgumentException("Invalid command type for Rollershutter item");
128         }
129     }
130 }