]> git.basschouten.com Git - openhab-addons.git/blob
56efa13111057cbf3b30f84f06a37357ed0caf7f
[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.oceanic.internal;
14
15 import java.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.core.library.types.DateTimeType;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.types.Type;
26
27 /**
28  * The {@link OceanicBindingConstants} class defines common constants, which are used
29  * across the whole binding.
30  *
31  * @author Karel Goderis - Initial contribution
32  */
33 @NonNullByDefault
34 public class OceanicBindingConstants {
35
36     public static final String BINDING_ID = "oceanic";
37
38     // List of all Thing Type UIDs
39     public static final ThingTypeUID THING_TYPE_SERIAL = new ThingTypeUID(BINDING_ID, "serial");
40     public static final ThingTypeUID THING_TYPE_NETWORK = new ThingTypeUID(BINDING_ID, "network");
41
42     // List of all Channel ids
43     public enum OceanicChannelSelector {
44
45         getSRN("serial", StringType.class, ValueSelectorType.GET, true),
46         getMAC("mac", StringType.class, ValueSelectorType.GET, true),
47         getDNA("name", StringType.class, ValueSelectorType.GET, true),
48         getSCR("type", StringType.class, ValueSelectorType.GET, true) {
49             @Override
50             public String convertValue(String value) {
51                 int index = Integer.valueOf(value);
52                 String convertedValue = value;
53                 switch (index) {
54                     case 0:
55                         convertedValue = "Single";
56                         break;
57                     case 1:
58                         convertedValue = "Double Alternative";
59                         break;
60                     case 2:
61                         convertedValue = "Triple Alternative";
62                         break;
63                     case 3:
64                         convertedValue = "Double Parallel";
65                         break;
66                     case 4:
67                         convertedValue = "Triple Parallel";
68                         break;
69                     case 5:
70                         convertedValue = "Single Filter";
71                         break;
72                     case 6:
73                         convertedValue = "Double Filter";
74                         break;
75                     case 7:
76                         convertedValue = "Triple Filter";
77                         break;
78                     default:
79                         break;
80                 }
81                 return convertedValue;
82             }
83         },
84         getALM("alarm", StringType.class, ValueSelectorType.GET, false) {
85             @Override
86             public String convertValue(String value) {
87                 int index = Integer.valueOf(value);
88                 String convertedValue = value;
89                 switch (index) {
90                     case 0:
91                         convertedValue = "No Alarm";
92                         break;
93                     case 1:
94                         convertedValue = "Lack of salt during regeneration";
95                         break;
96                     case 2:
97                         convertedValue = "Water pressure too low";
98                         break;
99                     case 3:
100                         convertedValue = "Water pressure too high";
101                         break;
102                     case 4:
103                         convertedValue = "Pressure sensor failure";
104                         break;
105                     case 5:
106                         convertedValue = "Camshaft failure";
107                         break;
108                     default:
109                         break;
110                 }
111                 return convertedValue;
112             }
113         },
114         getNOT("alert", StringType.class, ValueSelectorType.GET, false) {
115             @Override
116             public String convertValue(String value) {
117                 int index = Integer.valueOf(value);
118                 String convertedValue = value;
119                 switch (index) {
120                     case 0:
121                         convertedValue = "No Alert";
122                         break;
123                     case 1:
124                         convertedValue = "Imminent lack of salt";
125                         break;
126                     default:
127                         break;
128                 }
129                 return convertedValue;
130             }
131         },
132         getFLO("totalflow", DecimalType.class, ValueSelectorType.GET, false),
133         getRES("reserve", DecimalType.class, ValueSelectorType.GET, false),
134         getCYN("cycle", StringType.class, ValueSelectorType.GET, false),
135         getCYT("endofcycle", StringType.class, ValueSelectorType.GET, false),
136         getRTI("endofregeneration", StringType.class, ValueSelectorType.GET, false),
137         getWHU("hardnessunit", StringType.class, ValueSelectorType.GET, false) {
138             @Override
139             public String convertValue(String value) {
140                 int index = Integer.valueOf(value);
141                 String convertedValue = value;
142                 switch (index) {
143                     case 0:
144                         convertedValue = "dH";
145                         break;
146                     case 1:
147                         convertedValue = "fH";
148                         break;
149                     case 2:
150                         convertedValue = "e";
151                         break;
152                     case 3:
153                         convertedValue = "mg CaCO3/l";
154                         break;
155                     case 4:
156                         convertedValue = "ppm";
157                         break;
158                     case 5:
159                         convertedValue = "mmol/l";
160                         break;
161                     case 6:
162                         convertedValue = "mval/l";
163                         break;
164                     default:
165                         break;
166                 }
167                 return convertedValue;
168             }
169         },
170         getIWH("inlethardness", DecimalType.class, ValueSelectorType.GET, false),
171         getOWH("outlethardness", DecimalType.class, ValueSelectorType.GET, false),
172         getRG1("cylinderstate", StringType.class, ValueSelectorType.GET, false) {
173             @Override
174             public String convertValue(String value) {
175                 int index = Integer.valueOf(value);
176                 String convertedValue = value;
177                 switch (index) {
178                     case 0:
179                         convertedValue = "No regeneration";
180                         break;
181                     case 1:
182                         convertedValue = "Paused";
183                         break;
184                     case 2:
185                         convertedValue = "Regeneration";
186                         break;
187                     default:
188                         break;
189                 }
190                 return convertedValue;
191             }
192         },
193         setSV1("salt", DecimalType.class, ValueSelectorType.SET, false),
194         getSV1("salt", DecimalType.class, ValueSelectorType.GET, false),
195         setSIR("regeneratenow", OnOffType.class, ValueSelectorType.SET, false),
196         setSDR("regeneratelater", OnOffType.class, ValueSelectorType.SET, false),
197         setSMR("multiregenerate", OnOffType.class, ValueSelectorType.SET, false),
198         getMOF("consumptionmonday", DecimalType.class, ValueSelectorType.GET, false),
199         getTUF("consumptiontuesday", DecimalType.class, ValueSelectorType.GET, false),
200         getWEF("consumptionwednesday", DecimalType.class, ValueSelectorType.GET, false),
201         getTHF("consumptionthursday", DecimalType.class, ValueSelectorType.GET, false),
202         getFRF("consumptionfriday", DecimalType.class, ValueSelectorType.GET, false),
203         getSAF("consumptionsaturday", DecimalType.class, ValueSelectorType.GET, false),
204         getSUF("consumptionsunday", DecimalType.class, ValueSelectorType.GET, false),
205         getTOF("consumptiontoday", DecimalType.class, ValueSelectorType.GET, false),
206         getYEF("consumptionyesterday", DecimalType.class, ValueSelectorType.GET, false),
207         getCWF("consumptioncurrentweek", DecimalType.class, ValueSelectorType.GET, false),
208         getLWF("consumptionlastweek", DecimalType.class, ValueSelectorType.GET, false),
209         getCMF("consumptioncurrentmonth", DecimalType.class, ValueSelectorType.GET, false),
210         getLMF("consumptionlastmonth", DecimalType.class, ValueSelectorType.GET, false),
211         getCOF("consumptioncomplete", DecimalType.class, ValueSelectorType.GET, false),
212         getUWF("consumptionuntreated", DecimalType.class, ValueSelectorType.GET, false),
213         getTFO("consumptionpeaklevel", DecimalType.class, ValueSelectorType.GET, false),
214         getPRS("pressure", DecimalType.class, ValueSelectorType.GET, false),
215         getMXP("maxpressure", DecimalType.class, ValueSelectorType.GET, false),
216         getMNP("minpressure", DecimalType.class, ValueSelectorType.GET, false),
217         getMXF("maxflow", DecimalType.class, ValueSelectorType.GET, false),
218         getLAR("lastgeneration", DateTimeType.class, ValueSelectorType.GET, false) {
219             @Override
220             public String convertValue(String value) {
221                 final SimpleDateFormat inDateFormatter = new SimpleDateFormat("dd.MM.yy HH:mm:ss");
222                 final SimpleDateFormat outDateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
223
224                 try {
225                     Date date = inDateFormatter.parse(value);
226                     return outDateFormatter.format(date);
227                 } catch (ParseException fpe) {
228                     throw new IllegalArgumentException(value + " is not in a valid format.", fpe);
229                 }
230             }
231         },
232         getNOR("normalregenerations", DecimalType.class, ValueSelectorType.GET, false),
233         getSRE("serviceregenerations", DecimalType.class, ValueSelectorType.GET, false),
234         getINR("incompleteregenerations", DecimalType.class, ValueSelectorType.GET, false),
235         getTOR("allregenerations", DecimalType.class, ValueSelectorType.GET, false);
236
237         private final String text;
238         private Class<? extends Type> typeClass;
239         private ValueSelectorType typeValue;
240         private boolean isProperty;
241
242         private OceanicChannelSelector(final String text, Class<? extends Type> typeClass, ValueSelectorType typeValue,
243                 boolean isProperty) {
244             this.text = text;
245             this.typeClass = typeClass;
246             this.typeValue = typeValue;
247             this.isProperty = isProperty;
248         }
249
250         @Override
251         public String toString() {
252             return text;
253         }
254
255         public Class<? extends Type> getTypeClass() {
256             return typeClass;
257         }
258
259         public ValueSelectorType getTypeValue() {
260             return typeValue;
261         }
262
263         public boolean isProperty() {
264             return isProperty;
265         }
266
267         /**
268          * Procedure to convert selector string to value selector class.
269          *
270          * @param valueSelectorText selector string e.g. RawData, Command, Temperature
271          * @return corresponding selector value.
272          * @throws IllegalArgumentException Not valid class for value selector.
273          */
274         public static OceanicChannelSelector getValueSelector(String valueSelectorText,
275                 ValueSelectorType valueSelectorType) throws IllegalArgumentException {
276             for (OceanicChannelSelector c : OceanicChannelSelector.values()) {
277                 if (c.text.equals(valueSelectorText) && c.typeValue == valueSelectorType) {
278                     return c;
279                 }
280             }
281
282             throw new IllegalArgumentException("Not valid value selector");
283         }
284
285         public static ValueSelectorType getValueSelectorType(String valueSelectorText) throws IllegalArgumentException {
286             for (OceanicChannelSelector c : OceanicChannelSelector.values()) {
287                 if (c.text.equals(valueSelectorText)) {
288                     return c.typeValue;
289                 }
290             }
291
292             throw new IllegalArgumentException("Not valid value selector");
293         }
294
295         public String convertValue(String value) {
296             return value;
297         }
298
299         public enum ValueSelectorType {
300             GET,
301             SET
302         }
303     }
304 }