]> git.basschouten.com Git - openhab-addons.git/blob
efea0d69f77d539d4fdb6100ebb8542987b847a3
[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.freeathomesystem.internal.datamodel;
14
15 import static org.openhab.binding.freeathomesystem.internal.datamodel.FreeAtHomeDatapoint.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.freeathomesystem.internal.util.FreeAtHomeGeneralException;
20 import org.openhab.binding.freeathomesystem.internal.util.PidTranslationUtils;
21 import org.openhab.binding.freeathomesystem.internal.valuestateconverter.BooleanValueStateConverter;
22 import org.openhab.binding.freeathomesystem.internal.valuestateconverter.DecimalValueStateConverter;
23 import org.openhab.binding.freeathomesystem.internal.valuestateconverter.ShuttercontrolValueStateConverter;
24 import org.openhab.binding.freeathomesystem.internal.valuestateconverter.ValueStateConverter;
25 import org.openhab.core.library.CoreItemFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 import com.google.gson.JsonObject;
30
31 /**
32  *
33  * @author Andras Uhrin - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class FreeAtHomeDatapointGroup {
38
39     private final Logger logger = LoggerFactory.getLogger(FreeAtHomeDatapointGroup.class);
40
41     public enum DatapointGroupDirection {
42         UNDEFINED,
43         INPUT,
44         OUTPUT,
45         INPUTOUTPUT
46     }
47
48     private DatapointGroupDirection datapointGroupDirection;
49     private int pairingId;
50
51     private String functionString = "";
52     private @Nullable FreeAtHomeDatapoint inputDatapoint;
53     private @Nullable FreeAtHomeDatapoint outputDatapoint;
54
55     FreeAtHomeDatapointGroup() {
56         datapointGroupDirection = DatapointGroupDirection.UNDEFINED;
57         inputDatapoint = null;
58         outputDatapoint = null;
59     }
60
61     boolean addDatapointToGroup(DatapointDirection direction, int neededPairingId, String channelId,
62             JsonObject jsonObjectOfChannel) {
63         FreeAtHomeDatapoint newDatapoint = new FreeAtHomeDatapoint();
64
65         DatapointDirection resultingDirection = newDatapoint.searchForDatapoint(direction, neededPairingId, channelId,
66                 jsonObjectOfChannel);
67
68         if (resultingDirection != DatapointDirection.UNKNOWN) {
69             switch (resultingDirection) {
70                 case INPUT: {
71                     inputDatapoint = newDatapoint;
72
73                     pairingId = neededPairingId;
74
75                     break;
76                 }
77                 case OUTPUT: {
78                     outputDatapoint = newDatapoint;
79
80                     if (inputDatapoint == null) {
81                         pairingId = neededPairingId;
82                     }
83
84                     break;
85                 }
86                 case INPUTOUTPUT:
87                 case INPUT_AS_OUTPUT:
88                 case UNKNOWN:
89                     break;
90             }
91         }
92
93         if (inputDatapoint != null && outputDatapoint != null) {
94             datapointGroupDirection = DatapointGroupDirection.INPUTOUTPUT;
95         } else {
96             if (inputDatapoint == null && outputDatapoint != null) {
97                 datapointGroupDirection = DatapointGroupDirection.OUTPUT;
98             } else {
99                 if (inputDatapoint != null && outputDatapoint == null) {
100                     datapointGroupDirection = DatapointGroupDirection.INPUT;
101                 } else {
102                     datapointGroupDirection = DatapointGroupDirection.UNDEFINED;
103                 }
104             }
105         }
106
107         return resultingDirection != DatapointDirection.UNKNOWN;
108     }
109
110     public void applyChangesForVirtualDevice() {
111         // The input and output datapoints are meant from the device point of view. Because the virtual devices are
112         // outside of the free@home system the input and output datapoint must be switched
113         @Nullable
114         FreeAtHomeDatapoint localDatapoint = inputDatapoint;
115
116         inputDatapoint = outputDatapoint;
117         outputDatapoint = localDatapoint;
118
119         if (inputDatapoint != null && outputDatapoint != null) {
120             datapointGroupDirection = DatapointGroupDirection.INPUTOUTPUT;
121         } else {
122             if (inputDatapoint == null && outputDatapoint != null) {
123                 datapointGroupDirection = DatapointGroupDirection.OUTPUT;
124             } else {
125                 if (inputDatapoint != null && outputDatapoint == null) {
126                     datapointGroupDirection = DatapointGroupDirection.INPUT;
127                 } else {
128                     datapointGroupDirection = DatapointGroupDirection.UNDEFINED;
129                 }
130             }
131         }
132
133         return;
134     }
135
136     public @Nullable FreeAtHomeDatapoint getInputDatapoint() {
137         return inputDatapoint;
138     }
139
140     public @Nullable FreeAtHomeDatapoint getOutputDatapoint() {
141         return outputDatapoint;
142     }
143
144     public DatapointGroupDirection getDirection() {
145         return datapointGroupDirection;
146     }
147
148     public boolean isDecimal() throws FreeAtHomeGeneralException {
149         return PidTranslationUtils.PID_VALUETYPE_DECIMAL
150                 .equals(PidTranslationUtils.getValueTypeForPairingId(String.format("0x%04X", pairingId)));
151     }
152
153     public boolean isInteger() throws FreeAtHomeGeneralException {
154         return PidTranslationUtils.PID_VALUETYPE_INTEGER
155                 .equals(PidTranslationUtils.getValueTypeForPairingId(String.format("0x%04X", pairingId)));
156     }
157
158     public boolean isReadOnly() {
159         return (DatapointGroupDirection.INPUTOUTPUT == datapointGroupDirection) ? false
160                 : ((DatapointGroupDirection.INPUT == datapointGroupDirection) ? false : true);
161     }
162
163     public int getMax() throws FreeAtHomeGeneralException {
164         return PidTranslationUtils.getMax(String.format("0x%04X", pairingId));
165     }
166
167     public int getMin() throws FreeAtHomeGeneralException {
168         return PidTranslationUtils.getMin(String.format("0x%04X", pairingId));
169     }
170
171     public String getLabel() throws FreeAtHomeGeneralException {
172         return PidTranslationUtils.getShortTextForPairingId(String.format("0x%04X", pairingId));
173     }
174
175     public String getDescription() throws FreeAtHomeGeneralException {
176         return PidTranslationUtils.getDescriptionTextForPairingId(String.format("0x%04X", pairingId));
177     }
178
179     public String getValueType() throws FreeAtHomeGeneralException {
180         return PidTranslationUtils.getValueTypeForPairingId(String.format("0x%04X", pairingId));
181     }
182
183     public String getTypePattern() throws FreeAtHomeGeneralException {
184         String pattern = "";
185
186         functionString = PidTranslationUtils.getValueTypeForPairingId(String.format("0x%04X", pairingId));
187
188         switch (functionString) {
189             case PidTranslationUtils.PID_VALUETYPE_DECIMAL:
190                 pattern = "%.1f";
191                 break;
192             case PidTranslationUtils.PID_VALUETYPE_INTEGER:
193                 pattern = "%d";
194                 break;
195             default:
196                 pattern = "";
197                 logger.debug("Type pattern not found for PairingID {} - using default",
198                         String.format("0x%04X", pairingId));
199                 break;
200         }
201
202         return pattern;
203     }
204
205     public ValueStateConverter getValueStateConverter() throws FreeAtHomeGeneralException {
206         ValueStateConverter valueStateConverter = new BooleanValueStateConverter();
207
208         functionString = PidTranslationUtils.getValueTypeForPairingId(String.format("0x%04X", pairingId));
209
210         switch (functionString) {
211             case PidTranslationUtils.PID_VALUETYPE_BOOLEAN:
212                 valueStateConverter = new BooleanValueStateConverter();
213                 break;
214             case PidTranslationUtils.PID_VALUETYPE_DECIMAL:
215             case PidTranslationUtils.PID_VALUETYPE_INTEGER:
216                 valueStateConverter = new DecimalValueStateConverter();
217                 break;
218             case PidTranslationUtils.PID_VALUETYPE_STRING:
219                 break;
220             case PidTranslationUtils.PID_VALUETYPE_SHUTTERMOVEMENT:
221                 valueStateConverter = new ShuttercontrolValueStateConverter();
222                 break;
223             default:
224                 valueStateConverter = new DecimalValueStateConverter();
225                 logger.debug("Value converter not found for PairingID {} - using default",
226                         String.format("0x%04X", pairingId));
227                 break;
228         }
229
230         return valueStateConverter;
231     }
232
233     public String getOpenHabItemType() throws FreeAtHomeGeneralException {
234         String itemTypeString = "";
235
236         functionString = PidTranslationUtils.getValueTypeForPairingId(String.format("0x%04X", pairingId));
237
238         switch (functionString) {
239             case PidTranslationUtils.PID_VALUETYPE_BOOLEAN:
240                 itemTypeString = CoreItemFactory.SWITCH;
241                 break;
242             case PidTranslationUtils.PID_VALUETYPE_DECIMAL:
243             case PidTranslationUtils.PID_VALUETYPE_INTEGER:
244                 itemTypeString = CoreItemFactory.NUMBER;
245                 break;
246             case PidTranslationUtils.PID_VALUETYPE_SHUTTERMOVEMENT:
247                 itemTypeString = CoreItemFactory.ROLLERSHUTTER;
248                 break;
249             default:
250                 itemTypeString = CoreItemFactory.NUMBER;
251                 logger.debug("Item type constant not found for PairingID {} - using default",
252                         String.format("0x%04X", pairingId));
253                 break;
254         }
255
256         return itemTypeString;
257     }
258
259     public String getOpenHabCategory() throws FreeAtHomeGeneralException {
260         return PidTranslationUtils.getCategoryForPairingId(String.format("0x%04X", pairingId));
261     }
262 }