]> git.basschouten.com Git - openhab-addons.git/blob
2ed6cc4ea7837d26ae48bae5cf470ce63446e975
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 static java.util.function.Predicate.not;
16
17 import java.util.List;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.CoreItemFactory;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.CommandDescriptionBuilder;
28 import org.openhab.core.types.CommandOption;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.StateDescriptionFragmentBuilder;
31 import org.openhab.core.types.StateOption;
32
33 /**
34  * Implements a text/string value. Allows to restrict the incoming value to a set of states.
35  *
36  * @author David Graeff - Initial contribution
37  */
38 @NonNullByDefault
39 public class TextValue extends Value {
40     private final @Nullable Set<String> states;
41     private final @Nullable Set<String> commands;
42
43     /**
44      * Create a string value with a limited number of allowed states and commands.
45      *
46      * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
47      *            will be allowed.
48      * @param commands Allowed commands. Empty commands are filtered out. If the resulting set is empty, all string
49      *            values will be allowed.
50      */
51     public TextValue(String[] states, String[] commands) {
52         super(CoreItemFactory.STRING, List.of(StringType.class));
53         Set<String> s = Stream.of(states).filter(not(String::isBlank)).collect(Collectors.toSet());
54         if (!s.isEmpty()) {
55             this.states = s;
56         } else {
57             this.states = null;
58         }
59         Set<String> c = Stream.of(commands).filter(not(String::isBlank)).collect(Collectors.toSet());
60         if (!c.isEmpty()) {
61             this.commands = c;
62         } else {
63             this.commands = null;
64         }
65     }
66
67     /**
68      * Create a string value with a limited number of allowed states.
69      *
70      * @param states Allowed states. Empty states are filtered out. If the resulting set is empty, all string values
71      *            will be allowed. This same array is also used for allowed commands.
72      */
73     public TextValue(String[] states) {
74         this(states, states);
75     }
76
77     public TextValue() {
78         super(CoreItemFactory.STRING, List.of(StringType.class));
79         this.states = null;
80         this.commands = null;
81     }
82
83     @Override
84     public StringType parseCommand(Command command) throws IllegalArgumentException {
85         final Set<String> commands = this.commands;
86         String valueStr = command.toString();
87         if (commands != null && !commands.contains(valueStr)) {
88             throw new IllegalArgumentException("Value " + valueStr + " not within range");
89         }
90         return new StringType(valueStr);
91     }
92
93     @Override
94     public State parseMessage(Command command) throws IllegalArgumentException {
95         final Set<String> states = this.states;
96         String valueStr = command.toString();
97         if (states != null && !states.contains(valueStr)) {
98             throw new IllegalArgumentException("Value " + valueStr + " not within range");
99         }
100         return new StringType(valueStr);
101     }
102
103     /**
104      * @return valid states. Can be null.
105      */
106     public @Nullable Set<String> getStates() {
107         return states;
108     }
109
110     @Override
111     public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly) {
112         StateDescriptionFragmentBuilder builder = super.createStateDescription(readOnly);
113         final Set<String> states = this.states;
114         if (states != null) {
115             for (String state : states) {
116                 builder = builder.withOption(new StateOption(state, state));
117             }
118         }
119         return builder;
120     }
121
122     @Override
123     public CommandDescriptionBuilder createCommandDescription() {
124         CommandDescriptionBuilder builder = super.createCommandDescription();
125         final Set<String> commands = this.commands;
126         if (commands != null) {
127             for (String command : commands) {
128                 builder = builder.withCommandOption(new CommandOption(command, command));
129             }
130         }
131         return builder;
132     }
133 }