]> git.basschouten.com Git - openhab-addons.git/blob
62f455fe8e2856289307c8de55efdcc146375695
[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.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     public String stateContent = "";
49     public boolean escapedUrl = false;
50
51     public HttpChannelMode mode = HttpChannelMode.READWRITE;
52
53     // number
54     public @Nullable String unit;
55
56     // switch, dimmer, color
57     public @Nullable String onValue;
58     public @Nullable String offValue;
59
60     // dimmer, color
61     public BigDecimal step = BigDecimal.ONE;
62     public @Nullable String increaseValue;
63     public @Nullable String decreaseValue;
64
65     // color
66     public ColorItemConverter.ColorMode colorMode = ColorItemConverter.ColorMode.RGB;
67
68     // contact
69     public @Nullable String openValue;
70     public @Nullable String closedValue;
71
72     // rollershutter
73     public @Nullable String upValue;
74     public @Nullable String downValue;
75     public @Nullable String stopValue;
76     public @Nullable String moveValue;
77
78     // player
79     public @Nullable String playValue;
80     public @Nullable String pauseValue;
81     public @Nullable String nextValue;
82     public @Nullable String previousValue;
83     public @Nullable String rewindValue;
84     public @Nullable String fastforwardValue;
85
86     /**
87      * maps a command to a user-defined string
88      *
89      * @param command the command to map
90      * @return a string or null if no mapping found
91      */
92     public @Nullable String commandToFixedValue(Command command) {
93         if (!initialized) {
94             createMaps();
95         }
96
97         return commandStringMap.get(command);
98     }
99
100     /**
101      * maps a user-defined string to a state
102      *
103      * @param string the string to map
104      * @return the state or null if no mapping found
105      */
106     public @Nullable State fixedValueToState(String string) {
107         if (!initialized) {
108             createMaps();
109         }
110
111         return stringStateMap.get(string);
112     }
113
114     private void createMaps() {
115         addToMaps(this.onValue, OnOffType.ON);
116         addToMaps(this.offValue, OnOffType.OFF);
117         addToMaps(this.openValue, OpenClosedType.OPEN);
118         addToMaps(this.closedValue, OpenClosedType.CLOSED);
119         addToMaps(this.upValue, UpDownType.UP);
120         addToMaps(this.downValue, UpDownType.DOWN);
121
122         commandStringMap.put(IncreaseDecreaseType.INCREASE, increaseValue);
123         commandStringMap.put(IncreaseDecreaseType.DECREASE, decreaseValue);
124         commandStringMap.put(StopMoveType.STOP, stopValue);
125         commandStringMap.put(StopMoveType.MOVE, moveValue);
126         commandStringMap.put(PlayPauseType.PLAY, playValue);
127         commandStringMap.put(PlayPauseType.PAUSE, pauseValue);
128         commandStringMap.put(NextPreviousType.NEXT, nextValue);
129         commandStringMap.put(NextPreviousType.PREVIOUS, previousValue);
130         commandStringMap.put(RewindFastforwardType.REWIND, rewindValue);
131         commandStringMap.put(RewindFastforwardType.FASTFORWARD, fastforwardValue);
132
133         initialized = true;
134     }
135
136     private void addToMaps(@Nullable String value, State state) {
137         if (value != null) {
138             commandStringMap.put((Command) state, value);
139             stringStateMap.put(value, state);
140         }
141     }
142 }