2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.oceanic.internal;
15 import java.io.InvalidClassException;
16 import java.text.ParseException;
17 import java.text.SimpleDateFormat;
18 import java.util.Date;
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;
29 * The {@link OceanicBinding} class defines common constants, which are used
30 * across the whole binding.
32 * @author Karel Goderis - Initial contribution
35 public class OceanicBindingConstants {
37 public static final String BINDING_ID = "oceanic";
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");
43 // List of all Channel ids
44 public enum OceanicChannelSelector {
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) {
51 public String convertValue(String value) {
52 int index = Integer.valueOf(value);
53 String convertedValue = value;
56 convertedValue = "Single";
59 convertedValue = "Double Alternative";
62 convertedValue = "Triple Alternative";
65 convertedValue = "Double Parallel";
68 convertedValue = "Triple Parallel";
71 convertedValue = "Single Filter";
74 convertedValue = "Double Filter";
77 convertedValue = "Triple Filter";
82 return convertedValue;
85 getALM("alarm", StringType.class, ValueSelectorType.GET, false) {
87 public String convertValue(String value) {
88 int index = Integer.valueOf(value);
89 String convertedValue = value;
92 convertedValue = "No Alarm";
95 convertedValue = "Lack of salt during regeneration";
98 convertedValue = "Water pressure too low";
101 convertedValue = "Water pressure too high";
104 convertedValue = "Pressure sensor failure";
107 convertedValue = "Camshaft failure";
112 return convertedValue;
115 getNOT("alert", StringType.class, ValueSelectorType.GET, false) {
117 public String convertValue(String value) {
118 int index = Integer.valueOf(value);
119 String convertedValue = value;
122 convertedValue = "No Alert";
125 convertedValue = "Imminent lack of salt";
130 return convertedValue;
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) {
140 public String convertValue(String value) {
141 int index = Integer.valueOf(value);
142 String convertedValue = value;
145 convertedValue = "dH";
148 convertedValue = "fH";
151 convertedValue = "e";
154 convertedValue = "mg CaCO3/l";
157 convertedValue = "ppm";
160 convertedValue = "mmol/l";
163 convertedValue = "mval/l";
168 return convertedValue;
171 getIWH("inlethardness", DecimalType.class, ValueSelectorType.GET, false),
172 getOWH("outlethardness", DecimalType.class, ValueSelectorType.GET, false),
173 getRG1("cylinderstate", StringType.class, ValueSelectorType.GET, false) {
175 public String convertValue(String value) {
176 int index = Integer.valueOf(value);
177 String convertedValue = value;
180 convertedValue = "No regeneration";
183 convertedValue = "Paused";
186 convertedValue = "Regeneration";
191 return convertedValue;
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) {
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");
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);
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);
238 private final String text;
239 private Class<? extends Type> typeClass;
240 private ValueSelectorType typeValue;
241 private boolean isProperty;
243 private OceanicChannelSelector(final String text, Class<? extends Type> typeClass, ValueSelectorType typeValue,
244 boolean isProperty) {
246 this.typeClass = typeClass;
247 this.typeValue = typeValue;
248 this.isProperty = isProperty;
252 public String toString() {
256 public Class<? extends Type> getTypeClass() {
260 public ValueSelectorType getTypeValue() {
264 public boolean isProperty() {
269 * Procedure to convert selector string to value selector class.
271 * @param valueSelectorText selector string e.g. RawData, Command, Temperature
272 * @return corresponding selector value.
273 * @throws InvalidClassException Not valid class for value selector.
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) {
283 throw new IllegalArgumentException("Not valid value selector");
286 public static ValueSelectorType getValueSelectorType(String valueSelectorText) throws IllegalArgumentException {
287 for (OceanicChannelSelector c : OceanicChannelSelector.values()) {
288 if (c.text.equals(valueSelectorText)) {
293 throw new IllegalArgumentException("Not valid value selector");
296 public String convertValue(String value) {
300 public enum ValueSelectorType {