]> git.basschouten.com Git - openhab-addons.git/blob
f7ad538e2566a8b19a4554e886dcc00eba8b36ab
[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.knx.internal.dpt;
14
15 import static org.openhab.binding.knx.internal.dpt.DPTUtil.NORMALIZED_DPT;
16
17 import java.math.BigDecimal;
18 import java.math.RoundingMode;
19 import java.util.Locale;
20 import java.util.regex.Matcher;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.library.types.DateTimeType;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.HSBType;
27 import org.openhab.core.library.types.IncreaseDecreaseType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.OpenClosedType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.library.types.QuantityType;
32 import org.openhab.core.library.types.StopMoveType;
33 import org.openhab.core.library.types.StringType;
34 import org.openhab.core.library.types.UpDownType;
35 import org.openhab.core.library.unit.SIUnits;
36 import org.openhab.core.types.Type;
37 import org.openhab.core.util.ColorUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import tuwien.auto.calimero.KNXException;
42 import tuwien.auto.calimero.dptxlator.DPT;
43 import tuwien.auto.calimero.dptxlator.DPTXlator;
44 import tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled;
45 import tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat;
46 import tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled;
47 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat;
48 import tuwien.auto.calimero.dptxlator.DPTXlatorDate;
49 import tuwien.auto.calimero.dptxlator.DPTXlatorDateTime;
50 import tuwien.auto.calimero.dptxlator.DPTXlatorTime;
51 import tuwien.auto.calimero.dptxlator.TranslatorTypes;
52
53 /**
54  * This class encodes openHAB data types to strings for sending via Calimero
55  *
56  * Parts of this code are based on the openHAB KNXCoreTypeMapper by Kai Kreuzer et al.
57  *
58  * @author Jan N. Klug - Initial contribution
59  */
60 @NonNullByDefault
61 public class ValueEncoder {
62     private static final Logger LOGGER = LoggerFactory.getLogger(ValueEncoder.class);
63
64     private ValueEncoder() {
65         // prevent instantiation
66     }
67
68     /**
69      * Formats the given value as String for outputting via Calimero.
70      *
71      * @param value the value
72      * @param dptId the DPT id to use for formatting the string (e.g. 9.001)
73      * @return the value formatted as String
74      */
75     public static @Nullable String encode(Type value, String dptId) {
76         Matcher m = DPTUtil.DPT_PATTERN.matcher(dptId);
77         if (!m.matches() || m.groupCount() != 2) {
78             LOGGER.warn("Couldn't identify main/sub number in dptId '{}'", dptId);
79             return null;
80         }
81
82         String mainNumber = m.group("main");
83
84         try {
85             DPTXlator translator = TranslatorTypes.createTranslator(Integer.parseInt(mainNumber),
86                     NORMALIZED_DPT.getOrDefault(dptId, dptId));
87             DPT dpt = translator.getType();
88
89             // check for HSBType first, because it extends PercentType as well
90             if (value instanceof HSBType type) {
91                 return handleHSBType(dptId, type);
92             } else if (value instanceof OnOffType) {
93                 return OnOffType.OFF == value ? dpt.getLowerValue() : dpt.getUpperValue();
94             } else if (value instanceof UpDownType) {
95                 return UpDownType.UP == value ? dpt.getLowerValue() : dpt.getUpperValue();
96             } else if (value instanceof IncreaseDecreaseType) {
97                 DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
98                 return IncreaseDecreaseType.DECREASE == value ? valueDPT.getLowerValue() + " 5"
99                         : valueDPT.getUpperValue() + " 5";
100             } else if (value instanceof OpenClosedType) {
101                 return OpenClosedType.CLOSED == value ? dpt.getLowerValue() : dpt.getUpperValue();
102             } else if (value instanceof StopMoveType) {
103                 return StopMoveType.STOP == value ? dpt.getLowerValue() : dpt.getUpperValue();
104             } else if (value instanceof PercentType type) {
105                 int intValue = type.intValue();
106                 return "251.600".equals(dptId) ? String.format("- - - %d %%", intValue) : String.valueOf(intValue);
107             } else if (value instanceof DecimalType || value instanceof QuantityType<?>) {
108                 return handleNumericTypes(dptId, mainNumber, dpt, value);
109             } else if (value instanceof StringType) {
110                 return value.toString();
111             } else if (value instanceof DateTimeType type) {
112                 return handleDateTimeType(dptId, type);
113             }
114         } catch (KNXException e) {
115             return null;
116         } catch (Exception e) {
117             LOGGER.warn("An exception occurred converting value {} to dpt id {}: error message={}", value, dptId,
118                     e.getMessage());
119             return null;
120         }
121
122         LOGGER.debug("formatAsDPTString: Couldn't convert value {} to dpt id {} (no mapping).", value, dptId);
123         return null;
124     }
125
126     /**
127      * Formats the given internal <code>dateType</code> to a knx readable String
128      * according to the target datapoint type <code>dpt</code>.
129      *
130      * @param value the input value
131      * @param dptId the target datapoint type
132      *
133      * @return a String which contains either an ISO8601 formatted date (yyyy-mm-dd),
134      *         a formatted 24-hour clock with the day of week prepended (Mon, 12:00:00) or
135      *         a formatted 24-hour clock (12:00:00)
136      */
137     private static @Nullable String handleDateTimeType(String dptId, DateTimeType value) {
138         if (DPTXlatorDate.DPT_DATE.getID().equals(dptId)) {
139             return value.format("%tF");
140         } else if (DPTXlatorTime.DPT_TIMEOFDAY.getID().equals(dptId)) {
141             return value.format(Locale.US, "%1$ta, %1$tT");
142         } else if (DPTXlatorDateTime.DPT_DATE_TIME.getID().equals(dptId)) {
143             return value.format(Locale.US, "%tF %1$tT");
144         }
145         LOGGER.warn("Could not format DateTimeType for datapoint type '{}'", dptId);
146         return null;
147     }
148
149     private static String handleHSBType(String dptId, HSBType hsb) {
150         switch (dptId) {
151             case "232.600":
152                 int[] rgb = ColorUtil.hsbToRgb(hsb);
153                 return String.format("r:%d g:%d b:%d", rgb[0], rgb[1], rgb[2]);
154             case "232.60000":
155                 // MDT specific: mis-use 232.600 for hsv instead of rgb
156                 int hue = hsb.getHue().toBigDecimal().multiply(BigDecimal.valueOf(255))
157                         .divide(BigDecimal.valueOf(360), 0, RoundingMode.HALF_UP).intValue();
158                 return "r:" + hue + " g:" + convertPercentToByte(hsb.getSaturation()) + " b:"
159                         + convertPercentToByte(hsb.getBrightness());
160             case "242.600":
161                 double[] xyY = ColorUtil.hsbToXY(hsb);
162                 return String.format("(%,.4f %,.4f) %,.1f %%", xyY[0], xyY[1], xyY[2] * 100.0);
163             case "251.600":
164                 rgb = ColorUtil.hsbToRgb(hsb);
165                 return String.format("%d %d %d - %%", rgb[0], rgb[1], rgb[2]);
166             case "5.003":
167                 return hsb.getHue().toString();
168             default:
169                 return hsb.getBrightness().toString();
170         }
171     }
172
173     private static String handleNumericTypes(String dptId, String mainNumber, DPT dpt, Type value) {
174         BigDecimal bigDecimal;
175         if (value instanceof DecimalType decimalType) {
176             bigDecimal = decimalType.toBigDecimal();
177         } else {
178             String unit = DPTUnits.getUnitForDpt(dptId);
179
180             // exception for DPT using temperature differences
181             // - conversion °C or °F to K is wrong for differences,
182             // - stick to the unit given, fix the scaling for °F
183             // 9.002 DPT_Value_Tempd
184             // 9.003 DPT_Value_Tempa
185             // 9.023 DPT_KelvinPerPercent
186             if (DPTXlator2ByteFloat.DPT_TEMPERATURE_DIFFERENCE.getID().equals(dptId)
187                     || DPTXlator2ByteFloat.DPT_TEMPERATURE_GRADIENT.getID().equals(dptId)
188                     || DPTXlator2ByteFloat.DPT_KELVIN_PER_PERCENT.getID().equals(dptId)) {
189                 // match unicode character or °C
190                 if (value.toString().contains(SIUnits.CELSIUS.getSymbol()) || value.toString().contains("°C")) {
191                     if (unit != null) {
192                         unit = unit.replace("K", "°C");
193                     }
194                 } else if (value.toString().contains("°F")) {
195                     if (unit != null) {
196                         unit = unit.replace("K", "°F");
197                     }
198                     value = ((QuantityType<?>) value).multiply(BigDecimal.valueOf(5.0 / 9.0));
199                 }
200             } else if (DPTXlator4ByteFloat.DPT_LIGHT_QUANTITY.getID().equals(dptId)) {
201                 if (!value.toString().contains("J")) {
202                     if (unit != null) {
203                         unit = unit.replace("J", "lm*s");
204                     }
205                 }
206             } else if (DPTXlator4ByteFloat.DPT_ELECTRIC_FLUX.getID().equals(dptId)) {
207                 // use alternate definition of flux
208                 if (value.toString().contains("C")) {
209                     unit = "C";
210                 }
211             }
212
213             if (unit != null) {
214                 QuantityType<?> converted = ((QuantityType<?>) value).toUnit(unit);
215                 if (converted == null) {
216                     LOGGER.warn("Could not convert {} to unit {}, stripping unit only. Check your configuration.",
217                             value, unit);
218                     bigDecimal = ((QuantityType<?>) value).toBigDecimal();
219                 } else {
220                     bigDecimal = converted.toBigDecimal();
221                 }
222             } else {
223                 bigDecimal = ((QuantityType<?>) value).toBigDecimal();
224             }
225         }
226         switch (mainNumber) {
227             case "2":
228                 DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
229                 switch (bigDecimal.intValue()) {
230                     case 0:
231                         return "0 " + valueDPT.getLowerValue();
232                     case 1:
233                         return "0 " + valueDPT.getUpperValue();
234                     case 2:
235                         return "1 " + valueDPT.getLowerValue();
236                     default:
237                         return "1 " + valueDPT.getUpperValue();
238                 }
239             case "18":
240                 int intVal = bigDecimal.intValue();
241                 if (intVal > 63) {
242                     return "learn " + (intVal - 0x80);
243                 } else {
244                     return "activate " + intVal;
245                 }
246             default:
247                 return bigDecimal.stripTrailingZeros().toPlainString();
248         }
249     }
250
251     /**
252      * convert 0...100% to 1 byte 0..255
253      *
254      * @param percent
255      * @return int 0..255
256      */
257     private static int convertPercentToByte(PercentType percent) {
258         return percent.toBigDecimal().multiply(BigDecimal.valueOf(255))
259                 .divide(BigDecimal.valueOf(100), 0, RoundingMode.HALF_UP).intValue();
260     }
261 }