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