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.text.ParseException;
16 import java.text.SimpleDateFormat;
17 import java.util.Date;
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;
28 * The {@link OceanicBindingConstants} class defines common constants, which are used
29 * across the whole binding.
31 * @author Karel Goderis - Initial contribution
34 public class OceanicBindingConstants {
36 public static final String BINDING_ID = "oceanic";
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");
42 // List of all Channel ids
43 public enum OceanicChannelSelector {
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) {
50 public String convertValue(String value) {
51 int index = Integer.valueOf(value);
52 String convertedValue = value;
55 convertedValue = "Single";
58 convertedValue = "Double Alternative";
61 convertedValue = "Triple Alternative";
64 convertedValue = "Double Parallel";
67 convertedValue = "Triple Parallel";
70 convertedValue = "Single Filter";
73 convertedValue = "Double Filter";
76 convertedValue = "Triple Filter";
81 return convertedValue;
84 getALM("alarm", StringType.class, ValueSelectorType.GET, false) {
86 public String convertValue(String value) {
87 int index = Integer.valueOf(value);
88 String convertedValue = value;
91 convertedValue = "No Alarm";
94 convertedValue = "Lack of salt during regeneration";
97 convertedValue = "Water pressure too low";
100 convertedValue = "Water pressure too high";
103 convertedValue = "Pressure sensor failure";
106 convertedValue = "Camshaft failure";
111 return convertedValue;
114 getNOT("alert", StringType.class, ValueSelectorType.GET, false) {
116 public String convertValue(String value) {
117 int index = Integer.valueOf(value);
118 String convertedValue = value;
121 convertedValue = "No Alert";
124 convertedValue = "Imminent lack of salt";
129 return convertedValue;
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) {
139 public String convertValue(String value) {
140 int index = Integer.valueOf(value);
141 String convertedValue = value;
144 convertedValue = "dH";
147 convertedValue = "fH";
150 convertedValue = "e";
153 convertedValue = "mg CaCO3/l";
156 convertedValue = "ppm";
159 convertedValue = "mmol/l";
162 convertedValue = "mval/l";
167 return convertedValue;
170 getIWH("inlethardness", DecimalType.class, ValueSelectorType.GET, false),
171 getOWH("outlethardness", DecimalType.class, ValueSelectorType.GET, false),
172 getRG1("cylinderstate", StringType.class, ValueSelectorType.GET, false) {
174 public String convertValue(String value) {
175 int index = Integer.valueOf(value);
176 String convertedValue = value;
179 convertedValue = "No regeneration";
182 convertedValue = "Paused";
185 convertedValue = "Regeneration";
190 return convertedValue;
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) {
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");
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);
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);
237 private final String text;
238 private Class<? extends Type> typeClass;
239 private ValueSelectorType typeValue;
240 private boolean isProperty;
242 private OceanicChannelSelector(final String text, Class<? extends Type> typeClass, ValueSelectorType typeValue,
243 boolean isProperty) {
245 this.typeClass = typeClass;
246 this.typeValue = typeValue;
247 this.isProperty = isProperty;
251 public String toString() {
255 public Class<? extends Type> getTypeClass() {
259 public ValueSelectorType getTypeValue() {
263 public boolean isProperty() {
268 * Procedure to convert selector string to value selector class.
270 * @param valueSelectorText selector string e.g. RawData, Command, Temperature
271 * @return corresponding selector value.
272 * @throws IllegalArgumentException Not valid class for value selector.
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) {
282 throw new IllegalArgumentException("Not valid value selector");
285 public static ValueSelectorType getValueSelectorType(String valueSelectorText) throws IllegalArgumentException {
286 for (OceanicChannelSelector c : OceanicChannelSelector.values()) {
287 if (c.text.equals(valueSelectorText)) {
292 throw new IllegalArgumentException("Not valid value selector");
295 public String convertValue(String value) {
299 public enum ValueSelectorType {