]> git.basschouten.com Git - openhab-addons.git/blob
2effb1a0b16bbcd119ca2a18bf951175dc8c40f1
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.http.internal.config;
14
15 import java.math.BigDecimal;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.http.internal.converter.ColorItemConverter;
22 import org.openhab.core.library.types.IncreaseDecreaseType;
23 import org.openhab.core.library.types.NextPreviousType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
26 import org.openhab.core.library.types.PlayPauseType;
27 import org.openhab.core.library.types.RewindFastforwardType;
28 import org.openhab.core.library.types.StopMoveType;
29 import org.openhab.core.library.types.UpDownType;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.State;
32
33 /**
34  * The {@link HttpChannelConfig} class contains fields mapping channel configuration parameters.
35  *
36  * @author Jan N. Klug - Initial contribution
37  */
38 @NonNullByDefault
39 public class HttpChannelConfig {
40     private final Map<String, State> stringStateMap = new HashMap<>();
41     private final Map<Command, @Nullable String> commandStringMap = new HashMap<>();
42     private boolean initialized = false;
43
44     public @Nullable String stateExtension;
45     public @Nullable String commandExtension;
46     public @Nullable String stateTransformation;
47     public @Nullable String commandTransformation;
48
49     public HttpChannelMode mode = HttpChannelMode.READWRITE;
50
51     // number
52     public @Nullable String unit;
53
54     // switch, dimmer, color
55     public @Nullable String onValue;
56     public @Nullable String offValue;
57
58     // dimmer, color
59     public BigDecimal step = BigDecimal.ONE;
60     public @Nullable String increaseValue;
61     public @Nullable String decreaseValue;
62
63     // color
64     public ColorItemConverter.ColorMode colorMode = ColorItemConverter.ColorMode.RGB;
65
66     // contact
67     public @Nullable String openValue;
68     public @Nullable String closedValue;
69
70     // rollershutter
71     public @Nullable String upValue;
72     public @Nullable String downValue;
73     public @Nullable String stopValue;
74     public @Nullable String moveValue;
75
76     // player
77     public @Nullable String playValue;
78     public @Nullable String pauseValue;
79     public @Nullable String nextValue;
80     public @Nullable String previousValue;
81     public @Nullable String rewindValue;
82     public @Nullable String fastforwardValue;
83
84     /**
85      * maps a command to a user-defined string
86      *
87      * @param command the command to map
88      * @return a string or null if no mapping found
89      */
90     public @Nullable String commandToFixedValue(Command command) {
91         if (!initialized) {
92             createMaps();
93         }
94
95         return commandStringMap.get(command);
96     }
97
98     /**
99      * maps a user-defined string to a state
100      *
101      * @param string the string to map
102      * @return the state or null if no mapping found
103      */
104     public @Nullable State fixedValueToState(String string) {
105         if (!initialized) {
106             createMaps();
107         }
108
109         return stringStateMap.get(string);
110     }
111
112     private void createMaps() {
113         addToMaps(this.onValue, OnOffType.ON);
114         addToMaps(this.offValue, OnOffType.OFF);
115         addToMaps(this.openValue, OpenClosedType.OPEN);
116         addToMaps(this.closedValue, OpenClosedType.CLOSED);
117         addToMaps(this.upValue, UpDownType.UP);
118         addToMaps(this.downValue, UpDownType.DOWN);
119
120         commandStringMap.put(IncreaseDecreaseType.INCREASE, increaseValue);
121         commandStringMap.put(IncreaseDecreaseType.DECREASE, decreaseValue);
122         commandStringMap.put(StopMoveType.STOP, stopValue);
123         commandStringMap.put(StopMoveType.MOVE, moveValue);
124         commandStringMap.put(PlayPauseType.PLAY, playValue);
125         commandStringMap.put(PlayPauseType.PAUSE, pauseValue);
126         commandStringMap.put(NextPreviousType.NEXT, nextValue);
127         commandStringMap.put(NextPreviousType.PREVIOUS, previousValue);
128         commandStringMap.put(RewindFastforwardType.REWIND, rewindValue);
129         commandStringMap.put(RewindFastforwardType.FASTFORWARD, fastforwardValue);
130
131         initialized = true;
132     }
133
134     private void addToMaps(@Nullable String value, State state) {
135         if (value != null) {
136             commandStringMap.put((Command) state, value);
137             stringStateMap.put(value, state);
138         }
139     }
140 }