2 * Copyright (c) 2010-2022 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.knx.internal.dpt;
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.text.DecimalFormat;
18 import java.text.NumberFormat;
19 import java.text.ParseException;
20 import java.text.SimpleDateFormat;
21 import java.util.Arrays;
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Locale;
29 import org.openhab.binding.knx.internal.KNXTypeMapper;
30 import org.openhab.core.library.types.DateTimeType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.HSBType;
33 import org.openhab.core.library.types.IncreaseDecreaseType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.OpenClosedType;
36 import org.openhab.core.library.types.PercentType;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.library.types.StopMoveType;
39 import org.openhab.core.library.types.StringType;
40 import org.openhab.core.library.types.UpDownType;
41 import org.openhab.core.types.Type;
42 import org.openhab.core.types.UnDefType;
43 import org.osgi.service.component.annotations.Component;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import tuwien.auto.calimero.KNXException;
48 import tuwien.auto.calimero.KNXFormatException;
49 import tuwien.auto.calimero.KNXIllegalArgumentException;
50 import tuwien.auto.calimero.datapoint.Datapoint;
51 import tuwien.auto.calimero.dptxlator.DPT;
52 import tuwien.auto.calimero.dptxlator.DPTXlator;
53 import tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled;
54 import tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat;
55 import tuwien.auto.calimero.dptxlator.DPTXlator2ByteUnsigned;
56 import tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled;
57 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat;
58 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteSigned;
59 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteUnsigned;
60 import tuwien.auto.calimero.dptxlator.DPTXlator64BitSigned;
61 import tuwien.auto.calimero.dptxlator.DPTXlator8BitSigned;
62 import tuwien.auto.calimero.dptxlator.DPTXlator8BitUnsigned;
63 import tuwien.auto.calimero.dptxlator.DPTXlatorBoolean;
64 import tuwien.auto.calimero.dptxlator.DPTXlatorDate;
65 import tuwien.auto.calimero.dptxlator.DPTXlatorDateTime;
66 import tuwien.auto.calimero.dptxlator.DPTXlatorRGB;
67 import tuwien.auto.calimero.dptxlator.DPTXlatorSceneControl;
68 import tuwien.auto.calimero.dptxlator.DPTXlatorSceneNumber;
69 import tuwien.auto.calimero.dptxlator.DPTXlatorString;
70 import tuwien.auto.calimero.dptxlator.DPTXlatorTime;
71 import tuwien.auto.calimero.dptxlator.DPTXlatorUtf8;
72 import tuwien.auto.calimero.dptxlator.TranslatorTypes;
75 * This class provides type mapping between all openHAB core types and KNX data point types.
77 * Each 'MainType' delivered from calimero, has a default mapping
78 * for all it's children to an openHAB Typeclass.
79 * All these 'MainType' mapping's are put into 'dptMainTypeMap'.
81 * Default 'MainType' mapping's we can override by a specific mapping.
82 * All specific mapping's are put into 'dptTypeMap'.
84 * If for a 'MainType' there is currently no specific mapping registered,
85 * you can find a commented example line, with it's correct 'DPTXlator' class.
88 * @author Volker Daube
90 * @author Helmut Lehmeyer - Java8, generic DPT Mapper
93 public class KNXCoreTypeMapper implements KNXTypeMapper {
95 private final Logger logger = LoggerFactory.getLogger(KNXCoreTypeMapper.class);
97 private static final String TIME_DAY_FORMAT = new String("EEE, HH:mm:ss");
98 private static final String TIME_FORMAT = new String("HH:mm:ss");
99 private static final String DATE_FORMAT = new String("yyyy-MM-dd");
102 * stores the openHAB type class for (supported) KNX datapoint types in a generic way.
103 * dptTypeMap stores more specific type class and exceptions.
105 private final Map<Integer, Class<? extends Type>> dptMainTypeMap;
107 /** stores the openHAB type class for all (supported) KNX datapoint types */
108 private final Map<String, Class<? extends Type>> dptTypeMap;
110 /** stores the default KNX DPT to use for each openHAB type */
111 private final Map<Class<? extends Type>, String> defaultDptMap;
113 public KNXCoreTypeMapper() {
114 @SuppressWarnings("unused")
115 final List<Class<?>> xlators = Arrays.<Class<?>> asList(DPTXlator1BitControlled.class,
116 DPTXlator2ByteFloat.class, DPTXlator2ByteUnsigned.class, DPTXlator3BitControlled.class,
117 DPTXlator4ByteFloat.class, DPTXlator4ByteSigned.class, DPTXlator4ByteUnsigned.class,
118 DPTXlator64BitSigned.class, DPTXlator8BitSigned.class, DPTXlator8BitUnsigned.class,
119 DPTXlatorBoolean.class, DPTXlatorDate.class, DPTXlatorDateTime.class, DPTXlatorRGB.class,
120 DPTXlatorSceneControl.class, DPTXlatorSceneNumber.class, DPTXlatorString.class, DPTXlatorTime.class,
121 DPTXlatorUtf8.class);
123 dptTypeMap = new HashMap<>();
124 dptMainTypeMap = new HashMap<>();
128 * 1.000: General bool
129 * 1.001: DPT_Switch values: 0 = off 1 = on
130 * 1.002: DPT_Bool values: 0 = false 1 = true
131 * 1.003: DPT_Enable values: 0 = disable 1 = enable
132 * 1.004: DPT_Ramp values: 0 = no ramp 1 = ramp
133 * 1.005: DPT_Alarm values: 0 = no alarm 1 = alarm
134 * 1.006: DPT_BinaryValue values: 0 = low 1 = high
135 * 1.007: DPT_Step values: 0 = decrease 1 = increase
136 * 1.008: DPT_UpDown values: 0 = up 1 = down
137 * 1.009: DPT_OpenClose values: 0 = open 1 = close
138 * 1.010: DPT_Start values: 0 = stop 1 = start
139 * 1.011: DPT_State values: 0 = inactive 1 = active
140 * 1.012: DPT_Invert values: 0 = not inverted 1 = inverted
141 * 1.013: DPT_DimSendStyle values: 0 = start/stop 1 = cyclic
142 * 1.014: DPT_InputSource values: 0 = fixed 1 = calculated
143 * 1.015: DPT_Reset values: 0 = no action 1 = reset
144 * 1.016: DPT_Ack values: 0 = no action 1 = acknowledge
145 * 1.017: DPT_Trigger values: 0 = trigger 1 = trigger
146 * 1.018: DPT_Occupancy values: 0 = not occupied 1 = occupied
147 * 1.019: DPT_Window_Door values: 0 = closed 1 = open
148 * 1.021: DPT_LogicalFunction values: 0 = OR 1 = AND
149 * 1.022: DPT_Scene_AB values: 0 = scene A 1 = scene B
150 * 1.023: DPT_ShutterBlinds_Mode values: 0 = only move up/down 1 = move up/down + step-stop
151 * 1.100: DPT_Heat/Cool values: 0 = cooling 1 = heating
153 dptMainTypeMap.put(1, OnOffType.class);
154 /** Exceptions Datapoint Types "B1", Main number 1 */
155 dptTypeMap.put(DPTXlatorBoolean.DPT_UPDOWN.getID(), UpDownType.class);
156 dptTypeMap.put(DPTXlatorBoolean.DPT_OPENCLOSE.getID(), OpenClosedType.class);
157 dptTypeMap.put(DPTXlatorBoolean.DPT_START.getID(), StopMoveType.class);
158 dptTypeMap.put(DPTXlatorBoolean.DPT_WINDOW_DOOR.getID(), OpenClosedType.class);
159 dptTypeMap.put(DPTXlatorBoolean.DPT_SCENE_AB.getID(), DecimalType.class);
163 * 2.001: DPT_Switch_Control values: 0 = off 1 = on
164 * 2.002: DPT_Bool_Control values: 0 = false 1 = true
165 * 2.003: DPT_Enable_Control values: 0 = disable 1 = enable
166 * 2.004: DPT_Ramp_Control values: 0 = no ramp 1 = ramp
167 * 2.005: DPT_Alarm_Control values: 0 = no alarm 1 = alarm
168 * 2.006: DPT_BinaryValue_Control values: 0 = low 1 = high
169 * 2.007: DPT_Step_Control values: 0 = decrease 1 = increase
170 * 2.008: DPT_Direction1_Control values: 0 = up 1 = down
171 * 2.009: DPT_Direction2_Control values: 0 = open 1 = close
172 * 2.010: DPT_Start_Control values: 0 = stop 1 = start
173 * 2.011: DPT_State_Control values: 0 = inactive 1 = active
174 * 2.012: DPT_Invert_Control values: 0 = not inverted 1 = inverted
176 dptMainTypeMap.put(2, DecimalType.class);
177 /** Exceptions Datapoint Types "B2", Main number 2 */
178 // Example: dptTypeMap.put(DPTXlator1BitControlled.DPT_SWITCH_CONTROL.getID(), DecimalType.class);
182 * 3.007: DPT_Control_Dimming values: 0 = decrease 1 = increase
183 * 3.008: DPT_Control_Blinds values: 0 = up 1 = down
185 dptMainTypeMap.put(3, IncreaseDecreaseType.class);
186 /** Exceptions Datapoint Types "B1U3", Main number 3 */
187 dptTypeMap.put(DPTXlator3BitControlled.DPT_CONTROL_BLINDS.getID(), UpDownType.class);
191 * 4.001: DPT_Char_ASCII
192 * 4.002: DPT_Char_8859_1
194 dptMainTypeMap.put(4, StringType.class);
198 * 5.000: General byte
199 * 5.001: DPT_Scaling values: 0...100 %
200 * 5.003: DPT_Angle values: 0...360 °
201 * 5.004: DPT_Percent_U8 (8 Bit) values: 0...255 %
202 * 5.005: DPT_DecimalFactor values: 0...255 ratio
203 * 5.006: DPT_Tariff values: 0...254
204 * 5.010: DPT_Value_1_Ucount Unsigned count values: 0...255 counter pulses
206 dptMainTypeMap.put(5, DecimalType.class);
207 /** Exceptions Types "8-Bit Unsigned Value", Main number 5 */
208 dptTypeMap.put(DPTXlator8BitUnsigned.DPT_SCALING.getID(), PercentType.class);
209 dptTypeMap.put(DPTXlator8BitUnsigned.DPT_PERCENT_U8.getID(), PercentType.class);
213 * 6.001: DPT_Percent_V8 (8 Bit) values: -128...127 %
214 * 6.010: DPT_Value_1_Count values: signed -128...127 counter pulses
215 * 6.020: DPT_Status_Mode3 with mode values: 0/0/0/0/0 0...1/1/1/1/1 2
217 dptMainTypeMap.put(6, DecimalType.class);
218 /** Exceptions Datapoint Types "8-Bit Signed Value", Main number 6 */
219 dptTypeMap.put(DPTXlator8BitSigned.DPT_PERCENT_V8.getID(), PercentType.class);
220 dptTypeMap.put(DPTXlator8BitSigned.DPT_STATUS_MODE3.getID(), StringType.class);
224 * 7.000: General unsigned integer
225 * 7.001: DPT_Value_2_Ucount values: 0...65535 pulses
226 * 7.002: DPT_TimePeriodMsec values: 0...65535 res 1 ms
227 * 7.003: DPT_TimePeriod10MSec values: 0...655350 res 10 ms
228 * 7.004: DPT_TimePeriod100MSec values: 0...6553500 res 100 ms
229 * 7.005: DPT_TimePeriodSec values: 0...65535 s
230 * 7.006: DPT_TimePeriodMin values: 0...65535 min
231 * 7.007: DPT_TimePeriodHrs values: 0...65535 h
232 * 7.010: DPT_PropDataType values: 0...65535
233 * 7.011: DPT_Length_mm values: 0...65535 mm
234 * 7.012: DPT_UElCurrentmA values: 0...65535 mA
235 * 7.013: DPT_Brightness values: 0...65535 lx
236 * 7.600: DPT_Colour_Temperature values: 0...65535 K, 2000K 3000K 5000K 8000K
238 dptMainTypeMap.put(7, DecimalType.class);
239 /** Exceptions Datapoint Types "2-Octet Unsigned Value", Main number 7 */
240 dptTypeMap.put(DPTXlator2ByteFloat.DPT_HUMIDITY.getID(), PercentType.class);
244 * 8.000: General integer
245 * 8.001: DPT_Value_2_Count
246 * 8.002: DPT_DeltaTimeMsec
247 * 8.003: DPT_DeltaTime10MSec
248 * 8.004: DPT_DeltaTime100MSec
249 * 8.005: DPT_DeltaTimeSec
250 * 8.006: DPT_DeltaTimeMin
251 * 8.007: DPT_DeltaTimeHrs
252 * 8.010: DPT_Percent_V16
253 * 8.011: DPT_Rotation_Angle
254 * 8.012: DPT_Length_m
256 dptMainTypeMap.put(8, DecimalType.class);
260 * 9.000: General float
261 * 9.001: DPT_Value_Temp values: -273...+670760 °C
262 * 9.002: DPT_Value_Tempd values: -670760...+670760 K
263 * 9.003: DPT_Value_Tempa values: -670760...+670760 K/h
264 * 9.004: DPT_Value_Lux values: 0...+670760 lx
265 * 9.005: DPT_Value_Wsp values: 0...+670760 m/s
266 * 9.006: DPT_Value_Pres values: 0...+670760 Pa
267 * 9.007: DPT_Value_Humidity values: 0...+670760 %
268 * 9.008: DPT_Value_AirQuality values: 0...+670760 ppm
269 * 9.010: DPT_Value_Time1 values: -670760...+670760 s
270 * 9.011: DPT_Value_Time2 values: -670760...+670760 ms
271 * 9.020: DPT_Value_Volt values: -670760...+670760 mV
272 * 9.021: DPT_Value_Curr values: -670760...+670760 mA
273 * 9.022: DPT_PowerDensity values: -670760...+670760 W/m²
274 * 9.023: DPT_KelvinPerPercent values: -670760...+670760 K/%
275 * 9.024: DPT_Power values: -670760...+670760 kW
276 * 9.025: DPT_Value_Volume_Flow values: -670760...+670760 l/h
277 * 9.026: DPT_Rain_Amount values: -671088.64...670760.96 l/m²
278 * 9.027: DPT_Value_Temp_F values: -459.6...670760.96 °F
279 * 9.028: DPT_Value_Wsp_kmh values: 0...670760.96 km/h
280 * 9.029: DPT_Value_Absolute_Humidity: 0...670760 g/m³
281 * 9.030: DPT_Concentration_μgm3: 0...670760 µg/m³
283 dptMainTypeMap.put(9, DecimalType.class);
284 /** Exceptions Datapoint Types "2-Octet Float Value", Main number 9 */
285 dptTypeMap.put(DPTXlator2ByteFloat.DPT_HUMIDITY.getID(), PercentType.class);
289 * 10.001: DPT_TimeOfDay values: 1 = Monday...7 = Sunday, 0 = no-day, 00:00:00 Sun, 23:59:59 dow, hh:mm:ss
291 dptMainTypeMap.put(10, DateTimeType.class);
292 /** Exceptions Datapoint Types "Time", Main number 10 */
293 // Example: dptTypeMap.put(DPTXlatorTime.DPT_TIMEOFDAY.getID(), DateTimeType.class);
297 * 11.001: DPT_Date values: 1990-01-01...2089-12-31, yyyy-mm-dd
299 dptMainTypeMap.put(11, DateTimeType.class);
300 /** Exceptions Datapoint Types “Date”", Main number 11 */
301 // Example: dptTypeMap.put(DPTXlatorDate.DPT_DATE.getID(), DateTimeType.class);
305 * 12.000: General unsigned long
306 * 12.001: DPT_Value_4_Ucount values: 0...4294967295 counter pulses
307 * 12.100: DPT_LongTimePeriod_Sec values: 0...4294967295 s
308 * 12.101: DPT_LongTimePeriod_Min values: 0...4294967295 min
309 * 12.102: DPT_LongTimePeriod_Hrs values: 0...4294967295 h
310 * 12.1200: DPT_VolumeLiquid_Litre values: 0..4294967295 l
311 * 12.1201: DPT_Volume_m3 values: 0..4294967295 m3
313 dptMainTypeMap.put(12, DecimalType.class);
314 /** Exceptions Datapoint Types "4-Octet Unsigned Value", Main number 12 */
315 // Example: dptTypeMap.put(DPTXlator4ByteUnsigned.DPT_VALUE_4_UCOUNT.getID(), DecimalType.class);
319 * 13.000: General long
320 * 13.001: DPT_Value_4_Count values: -2147483648...2147483647 counter pulses
321 * 13.002: DPT_FlowRate_m3h values: -2147483648...2147483647 m3/h
322 * 13.010: DPT_ActiveEnergy values: -2147483648...2147483647 Wh
323 * 13.011: DPT_ApparantEnergy values: -2147483648...2147483647 VAh
324 * 13.012: DPT_ReactiveEnergy values: -2147483648...2147483647 VARh
325 * 13.013: DPT_ActiveEnergy_kWh values: -2147483648...2147483647 kWh
326 * 13.014: DPT_ApparantEnergy_kVAh values: -2147483648...2147483647 kVAh
327 * 13.015: DPT_ReactiveEnergy_kVARh values: -2147483648...2147483647 kVAR
328 * 13.016: DPT_ActiveEnergy_MWh4 values: -2147483648...2147483647 MWh
329 * 13.100: DPT_LongDeltaTimeSec values: -2147483648...2147483647 s
330 * 13.1200: DPT_DeltaVolumeLiquid_Litre values: -2147483648...2147483647 l
331 * 13.1201: DPT_DeltaVolume_m3 values: -2147483648...2147483647 m³
333 dptMainTypeMap.put(13, DecimalType.class);
334 /** Exceptions Datapoint Types "4-Octet Signed Value", Main number 13 */
335 // Example: dptTypeMap.put(DPTXlator4ByteSigned.DPT_COUNT.getID(), DecimalType.class);
338 * MainType: 14, Range: [-3.40282347e+38f...3.40282347e+38f]
339 * 14.000: Acceleration, values: ms⁻²
340 * 14.001: Acceleration, angular, values: rad s⁻²
341 * 14.002: Activation energy, values: J/mol
342 * 14.003: Activity, values: s⁻¹
343 * 14.004: Mol, values: mol
344 * 14.005: Amplitude, values:
345 * 14.006: Angle, values: rad
346 * 14.007: Angle, values: °
347 * 14.008: Momentum, values: Js
348 * 14.009: Angular velocity, values: rad/s
349 * 14.010: Area, values: m²
350 * 14.011: Capacitance, values: F
351 * 14.012: Charge density (surface), values: C m⁻²
352 * 14.013: Charge density (volume), values: C m⁻³
353 * 14.014: Compressibility, values: m²/N
354 * 14.015: Conductance, values: Ω⁻¹
355 * 14.016: Conductivity, electrical, values: Ω⁻¹m⁻¹
356 * 14.017: Density, values: kg m⁻³
357 * 14.018: Electric charge, values: C
358 * 14.019: Electric current, values: A
359 * 14.020: Electric current density, values: A m⁻²
360 * 14.021: Electric dipole moment, values: Cm
361 * 14.022: Electric displacement, values: C m⁻²
362 * 14.023: Electric field strength, values: V/m
363 * 14.024: Electric flux, values: Vm
364 * 14.025: Electric flux density, values: C m⁻²
365 * 14.026: Electric polarization, values: C m⁻²
366 * 14.027: Electric potential, values: V
367 * 14.028: Electric potential difference, values: V
368 * 14.029: Electromagnetic moment, values: A m²
369 * 14.030: Electromotive force, values: V
370 * 14.031: Energy, values: J
371 * 14.032: Force, values: N
372 * 14.033: Frequency, values: Hz
373 * 14.034: Frequency, angular, values: rad/s
374 * 14.035: Heat capacity, values: J/K
375 * 14.036: Heat flow rate, values: W
376 * 14.037: Heat quantity, values: J
377 * 14.038: Impedance, values: Ω
378 * 14.039: Length, values: m
379 * 14.040: Quantity of Light, values: J
380 * 14.041: Luminance, values: cd m⁻²
381 * 14.042: Luminous flux, values: lm
382 * 14.043: Luminous intensity, values: cd
383 * 14.044: Magnetic field strength, values: A/m
384 * 14.045: Magnetic flux, values: Wb
385 * 14.046: Magnetic flux density, values: T
386 * 14.047: Magnetic moment, values: A m²
387 * 14.048: Magnetic polarization, values: T
388 * 14.049: Magnetization, values: A/m
389 * 14.050: Magneto motive force, values: A
390 * 14.051: Mass, values: kg
391 * 14.052: Mass flux, values: kg/s
392 * 14.053: Momentum, values: N/s
393 * 14.054: Phase angle, radiant, values: rad
394 * 14.055: Phase angle, degree, values: °
395 * 14.056: Power, values: W
396 * 14.057: Power factor, values:
397 * 14.058: Pressure, values: Pa
398 * 14.059: Reactance, values: Ω
399 * 14.060: Resistance, values: Ω
400 * 14.061: Resistivity, values: Ωm
401 * 14.062: Self inductance, values: H
402 * 14.063: Solid angle, values: sr
403 * 14.064: Sound intensity, values: W m⁻²
404 * 14.065: Speed, values: m/s
405 * 14.066: Stress, values: Pa
406 * 14.067: Surface tension, values: N/m
407 * 14.068: Temperature in Celsius Degree, values: °C
408 * 14.069: Temperature, absolute, values: K
409 * 14.070: Temperature difference, values: K
410 * 14.071: Thermal capacity, values: J/K
411 * 14.072: Thermal conductivity, values: W/m K⁻¹
412 * 14.073: Thermoelectric power, values: V/K
413 * 14.074: Time, values: s
414 * 14.075: Torque, values: Nm
415 * 14.076: Volume, values: m³
416 * 14.077: Volume flux, values: m³/s
417 * 14.078: Weight, values: N
418 * 14.079: Work, values: J
419 * 14.080: apparent power: VA
421 dptMainTypeMap.put(14, DecimalType.class);
422 /** Exceptions Datapoint Types "4-Octet Float Value", Main number 14 */
423 // Example: dptTypeMap.put(DPTXlator4ByteFloat.DPT_ACCELERATION_ANGULAR.getID(), DecimalType.class);
427 * 16.000: ASCII string
428 * 16.001: ISO-8859-1 string (Latin 1)
430 dptMainTypeMap.put(16, StringType.class);
431 /** Exceptions Datapoint Types "String", Main number 16 */
432 dptTypeMap.put(DPTXlatorString.DPT_STRING_8859_1.getID(), StringType.class);
433 dptTypeMap.put(DPTXlatorString.DPT_STRING_ASCII.getID(), StringType.class);
437 * 17.001: Scene Number, values: 0...63
439 dptMainTypeMap.put(17, DecimalType.class);
440 /** Exceptions Datapoint Types "Scene Number", Main number 17 */
441 // Example: dptTypeMap.put(DPTXlatorSceneNumber.DPT_SCENE_NUMBER.getID(), DecimalType.class);
445 * 18.001: Scene Control, values: 0...63, 0 = activate, 1 = learn
447 dptMainTypeMap.put(18, DecimalType.class);
448 /** Exceptions Datapoint Types "Scene Control", Main number 18 */
449 // Example: dptTypeMap.put(DPTXlatorSceneControl.DPT_SCENE_CONTROL.getID(), DecimalType.class);
453 * 19.001: Date with time, values: 0 = 1900, 255 = 2155, 01/01 00:00:00, 12/31 24:00:00 yr/mth/day hr:min:sec
455 dptMainTypeMap.put(19, DateTimeType.class);
456 /** Exceptions Datapoint Types "DateTime", Main number 19 */
457 // Example: dptTypeMap.put(DPTXlatorDateTime.DPT_DATE_TIME.getID(), DateTimeType.class);
461 * 20.001: System Clock Mode, enumeration [0..2]
462 * 20.002: Building Mode, enumeration [0..2]
463 * 20.003: Occupancy Mode, enumeration [0..2]
464 * 20.004: Priority, enumeration [0..3]
465 * 20.005: Light Application Mode, enumeration [0..2]
466 * 20.006: Application Area, enumeration [0..14]
467 * 20.007: Alarm Class Type, enumeration [0..3]
468 * 20.008: PSU Mode, enumeration [0..2]
469 * 20.011: Error Class System, enumeration [0..18]
470 * 20.012: Error Class HVAC, enumeration [0..4]
471 * 20.013: Time Delay, enumeration [0..25]
472 * 20.014: Beaufort Wind Force Scale, enumeration [0..12]
473 * 20.017: Sensor Select, enumeration [0..4]
474 * 20.020: Actuator Connect Type, enumeration [1..2]
475 * 20.100: Fuel Type, enumeration [0..3]
476 * 20.101: Burner Type, enumeration [0..3]
477 * 20.102: HVAC Mode, enumeration [0..4]
478 * 20.103: DHW Mode, enumeration [0..4]
479 * 20.104: Load Priority, enumeration [0..2]
480 * 20.105: HVAC Control Mode, enumeration [0..20]
481 * 20.106: HVAC Emergency Mode, enumeration [0..5]
482 * 20.107: Changeover Mode, enumeration [0..2]
483 * 20.108: Valve Mode, enumeration [1..5]
484 * 20.109: Damper Mode, enumeration [1..4]
485 * 20.110: Heater Mode, enumeration [1..3]
486 * 20.111: Fan Mode, enumeration [0..2]
487 * 20.112: Master/Slave Mode, enumeration [0..2]
488 * 20.113: Status Room Setpoint, enumeration [0..2]
489 * 20.114: Metering Device Type, enumeration [0..41/255]
490 * 20.120: Air Damper Actuator Type, enumeration [1..2]
491 * 20.121: Backup Mode, enumeration [0..1]
492 * 20.122: Start Synchronization, enumeration [0..2]
493 * 20.600: Behavior Lock/Unlock, enumeration [0..6]
494 * 20.601: Behavior Bus Power Up/Down, enumeration [0..4]
495 * 20.602: DALI Fade Time, enumeration [0..15]
496 * 20.603: Blinking Mode, enumeration [0..2]
497 * 20.604: Light Control Mode, enumeration [0..1]
498 * 20.605: Switch PB Model, enumeration [1..2]
499 * 20.606: PB Action, enumeration [0..3]
500 * 20.607: Dimm PB Model, enumeration [1..4]
501 * 20.608: Switch On Mode, enumeration [0..2]
502 * 20.609: Load Type Set, enumeration [0..2]
503 * 20.610: Load Type Detected, enumeration [0..3]
504 * 20.801: SAB Except Behavior, enumeration [0..4]
505 * 20.802: SAB Behavior Lock/Unlock, enumeration [0..6]
506 * 20.803: SSSB Mode, enumeration [1..4]
507 * 20.804: Blinds Control Mode, enumeration [0..1]
508 * 20.1000: Comm Mode, enumeration [0..255]
509 * 20.1001: Additional Info Type, enumeration [0..7]
510 * 20.1002: RF Mode Select, enumeration [0..2]
511 * 20.1003: RF Filter Select, enumeration [0..3]
512 * 20.1200: M-Bus Breaker/Valve State, enumeration [0..255]
513 * 20.1202: Gas Measurement Condition, enumeration [0..3]
516 dptMainTypeMap.put(20, StringType.class);
517 /** Exceptions Datapoint Types, Main number 20 */
518 // Example since calimero 2.4: dptTypeMap.put(DPTXlator8BitEnum.DptSystemClockMode.getID(), StringType.class);
522 * 21.001: General Status, values: 0...31
523 * 21.002: Device Control, values: 0...7
524 * 21.100: Forcing Signal, values: 0...255
525 * 21.101: Forcing Signal Cool, values: 0...1
526 * 21.102: Room Heating Controller Status, values: 0...255
527 * 21.103: Solar Dhw Controller Status, values: 0...7
528 * 21.104: Fuel Type Set, values: 0...7
529 * 21.105: Room Cooling Controller Status, values: 0...1
530 * 21.106: Ventilation Controller Status, values: 0...15
531 * 21.601: Light Actuator Error Info, values: 0...127
532 * 21.1000: R F Comm Mode Info, values: 0...7
533 * 21.1001: R F Filter Modes, values: 0...7
534 * 21.1010: Channel Activation State, values: 0...255
536 dptMainTypeMap.put(21, StringType.class);
537 /** Exceptions Datapoint Types, Main number 21 */
538 // Example since calimero 2.4: dptTypeMap.put(DptXlator8BitSet.DptGeneralStatus.getID(), StringType.class);
544 dptMainTypeMap.put(28, StringType.class);
545 /** Exceptions Datapoint Types "String" UTF-8, Main number 28 */
546 // Example: dptTypeMap.put(DPTXlatorUtf8.DPT_UTF8.getID(), StringType.class);
550 * 29.010: Active Energy, values: -9223372036854775808...9223372036854775807 Wh
551 * 29.011: Apparent energy, values: -9223372036854775808...9223372036854775807 VAh
552 * 29.012: Reactive energy, values: -9223372036854775808...9223372036854775807 VARh
554 dptMainTypeMap.put(29, DecimalType.class);
555 /** Exceptions Datapoint Types "64-Bit Signed Value", Main number 29 */
556 // Example: dptTypeMap.put(DPTXlator64BitSigned.DPT_ACTIVE_ENERGY.getID(), DecimalType.class);
560 * 229.001: Metering Value, values: -2147483648...2147483647
562 dptMainTypeMap.put(229, DecimalType.class);
563 /** Exceptions Datapoint Types "4-Octet Signed Value", Main number 229 */
564 // Example: dptTypeMap.put(DptXlatorMeteringValue.DptMeteringValue.getID(), DecimalType.class);
567 * MainType: 232, 3 bytes
568 * 232.600: DPT_Colour_RGB, values: 0 0 0...255 255 255, r g b
570 dptMainTypeMap.put(232, HSBType.class);
571 /** Exceptions Datapoint Types "RGB Color", Main number 232 */
572 // Example: dptTypeMap.put(DPTXlatorRGB.DPT_RGB.getID(), HSBType.class);
574 defaultDptMap = new HashMap<>();
575 defaultDptMap.put(OnOffType.class, DPTXlatorBoolean.DPT_SWITCH.getID());
576 defaultDptMap.put(UpDownType.class, DPTXlatorBoolean.DPT_UPDOWN.getID());
577 defaultDptMap.put(StopMoveType.class, DPTXlatorBoolean.DPT_START.getID());
578 defaultDptMap.put(OpenClosedType.class, DPTXlatorBoolean.DPT_WINDOW_DOOR.getID());
579 defaultDptMap.put(IncreaseDecreaseType.class, DPTXlator3BitControlled.DPT_CONTROL_DIMMING.getID());
580 defaultDptMap.put(PercentType.class, DPTXlator8BitUnsigned.DPT_SCALING.getID());
581 defaultDptMap.put(DecimalType.class, DPTXlator2ByteFloat.DPT_TEMPERATURE.getID());
582 defaultDptMap.put(DateTimeType.class, DPTXlatorTime.DPT_TIMEOFDAY.getID());
583 defaultDptMap.put(StringType.class, DPTXlatorString.DPT_STRING_8859_1.getID());
584 defaultDptMap.put(HSBType.class, DPTXlatorRGB.DPT_RGB.getID());
588 * This function computes the target unit for type conversion from OH quantity type to DPT types.
589 * Calimero library provides units which can be used for most of the DPTs. There are some deviations
590 * from the OH unit scheme which are handled.
592 private String quantityTypeToDPTValue(QuantityType<?> qt, int mainNumber, int subNumber, String dpUnit)
593 throws KNXException {
594 String targetOhUnit = dpUnit;
595 double scaleFactor = 1.0;
596 switch (mainNumber) {
607 // special case: temperature deltas specified in different units
608 // ignore the offset, but run a conversion to handle prefixes like mK
609 // scaleFactor is needed to properly handle °F
611 final String unit = qt.getUnit().toString();
612 // find out if the unit is based on °C or K, getSystemUnit() does not help here as it always
614 if (unit.contains("°C")) {
616 } else if (unit.contains("°F")) {
618 scaleFactor = 5.0 / 9.0;
619 } else if (unit.contains("K")) {
627 final String unit = qt.getUnit().toString();
628 if (unit.contains("°C")) {
629 targetOhUnit = "°C/h";
630 } else if (unit.contains("°F")) {
631 targetOhUnit = "°F/h";
632 scaleFactor = 5.0 / 9.0;
633 } else if (unit.contains("K")) {
634 targetOhUnit = "K/h";
641 final String unit = qt.getUnit().toString();
642 if (unit.contains("°C")) {
643 targetOhUnit = "°C/%";
644 } else if (unit.contains("°F")) {
645 targetOhUnit = "°F/%";
646 scaleFactor = 5.0 / 9.0;
647 } else if (unit.contains("K")) {
648 targetOhUnit = "K/%";
659 // Calimero uses "litre"
668 // Calimero uses VARh, OH uses varh
669 targetOhUnit = targetOhUnit.replace("VARh", "varh");
672 // OH does not accept kVAh, only VAh
673 targetOhUnit = targetOhUnit.replace("kVAh", "VAh");
674 scaleFactor = 1.0 / 1000.0;
680 targetOhUnit = targetOhUnit.replace("Ω\u207B¹", "S");
681 // Calimero uses a special unicode character to specify units like m*s^-2
682 // this needs to be rewritten to m/s²
683 final int posMinus = targetOhUnit.indexOf("\u207B");
685 targetOhUnit = targetOhUnit.substring(0, posMinus - 1) + "/" + targetOhUnit.charAt(posMinus - 1)
686 + targetOhUnit.substring(posMinus + 1);
690 // OH does not support unut Js, need to expand
691 targetOhUnit = "J*s";
694 targetOhUnit = "C*m";
701 targetOhUnit = "A*m²";
704 if (qt.getUnit().toString().contains("J")) {
707 targetOhUnit = "lm*s";
711 targetOhUnit = "Ohm*m";
714 targetOhUnit = "N*m";
721 // Calimero uses VARh, OH uses varh
722 targetOhUnit = targetOhUnit.replace("VARh", "varh");
727 // replace e.g. m3 by m³
728 targetOhUnit = targetOhUnit.replace("3", "³").replace("2", "²");
730 final QuantityType<?> result = qt.toUnit(targetOhUnit);
731 if (result == null) {
732 throw new KNXException("incompatible types: " + qt.getUnit().toString() + ", " + targetOhUnit);
734 return String.valueOf(result.doubleValue() * scaleFactor);
738 public String toDPTValue(Type type, String dptID) {
740 int mainNumber = getMainNumber(dptID);
741 if (mainNumber == -1) {
742 logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
745 int subNumber = getSubNumber(dptID);
746 if (subNumber == -1) {
747 logger.debug("toType: couldn't identify sub number in dptID: {}.", dptID);
752 DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
753 dpt = translator.getType();
754 } catch (KNXException e) {
759 // check for HSBType first, because it extends PercentType as well
760 if (type instanceof HSBType) {
761 switch (mainNumber) {
764 case 3: // * 5.003: Angle, values: 0...360 °
765 return ((HSBType) type).getHue().toString();
766 case 1: // * 5.001: Scaling, values: 0...100 %
768 return ((HSBType) type).getBrightness().toString();
773 HSBType hc = ((HSBType) type);
774 return "r:" + convertPercentToByte(hc.getRed()) + " g:"
775 + convertPercentToByte(hc.getGreen()) + " b:"
776 + convertPercentToByte(hc.getBlue());
779 HSBType hc = ((HSBType) type);
780 return "r:" + hc.getRed().intValue() + " g:" + hc.getGreen().intValue() + " b:"
781 + hc.getBlue().intValue();
783 } else if (type instanceof OnOffType) {
784 return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
785 } else if (type instanceof UpDownType) {
786 return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
787 } else if (type instanceof IncreaseDecreaseType) {
788 DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
789 return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5"
790 : valueDPT.getUpperValue() + " 5";
791 } else if (type instanceof OpenClosedType) {
792 return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
793 } else if (type instanceof StopMoveType) {
794 return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
795 } else if (type instanceof PercentType) {
796 return String.valueOf(((DecimalType) type).intValue());
797 } else if (type instanceof DecimalType) {
798 switch (mainNumber) {
800 DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
801 switch (((DecimalType) type).intValue()) {
803 return "0 " + valueDPT.getLowerValue();
805 return "0 " + valueDPT.getUpperValue();
807 return "1 " + valueDPT.getLowerValue();
809 return "1 " + valueDPT.getUpperValue();
812 int intVal = ((DecimalType) type).intValue();
814 return "learn " + (intVal - 0x80);
816 return "activate " + intVal;
819 return ((DecimalType) type).toBigDecimal().stripTrailingZeros().toPlainString();
821 } else if (type instanceof StringType) {
822 return type.toString();
823 } else if (type instanceof DateTimeType) {
824 return formatDateTime((DateTimeType) type, dptID);
825 } else if (type instanceof QuantityType) {
826 final QuantityType<?> qt = (QuantityType<?>) type;
827 return quantityTypeToDPTValue(qt, mainNumber, subNumber, dpt.getUnit());
829 } catch (Exception e) {
830 logger.warn("An exception occurred converting type {} to dpt id {}: error message={}", type, dptID,
835 logger.debug("toDPTValue: Couldn't convert type {} to dpt id {} (no mapping).", type, dptID);
841 public Type toType(Datapoint datapoint, byte[] data) {
843 DPTXlator translator = TranslatorTypes.createTranslator(datapoint.getMainNumber(), datapoint.getDPT());
844 translator.setData(data);
845 String value = translator.getValue();
847 String id = translator.getType().getID();
848 logger.trace("toType datapoint DPT = {}", datapoint.getDPT());
850 int mainNumber = getMainNumber(id);
851 if (mainNumber == -1) {
852 logger.debug("toType: couldn't identify mainnumber in dptID: {}.", id);
855 int subNumber = getSubNumber(id);
856 if (subNumber == -1) {
857 logger.debug("toType: couldn't identify sub number in dptID: {}.", id);
861 * Following code section deals with specific mapping of values from KNX to openHAB types were the String
862 * received from the DPTXlator is not sufficient to set the openHAB type or has bugs
864 switch (mainNumber) {
866 DPTXlatorBoolean translatorBoolean = (DPTXlatorBoolean) translator;
869 return translatorBoolean.getValueBoolean() ? UpDownType.DOWN : UpDownType.UP;
871 return translatorBoolean.getValueBoolean() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
873 return translatorBoolean.getValueBoolean() ? StopMoveType.MOVE : StopMoveType.STOP;
875 return translatorBoolean.getValueBoolean() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
877 return DecimalType.valueOf(translatorBoolean.getValueBoolean() ? "1" : "0");
879 return translatorBoolean.getValueBoolean() ? OnOffType.ON : OnOffType.OFF;
882 DPTXlator1BitControlled translator1BitControlled = (DPTXlator1BitControlled) translator;
883 int decValue = (translator1BitControlled.getControlBit() ? 2 : 0)
884 + (translator1BitControlled.getValueBit() ? 1 : 0);
885 return new DecimalType(decValue);
887 DPTXlator3BitControlled translator3BitControlled = (DPTXlator3BitControlled) translator;
888 if (translator3BitControlled.getStepCode() == 0) {
889 logger.debug("toType: KNX DPT_Control_Dimming: break received.");
890 return UnDefType.UNDEF;
894 return translator3BitControlled.getControlBit() ? IncreaseDecreaseType.INCREASE
895 : IncreaseDecreaseType.DECREASE;
897 return translator3BitControlled.getControlBit() ? UpDownType.DOWN : UpDownType.UP;
901 * FIXME: Workaround for a bug in Calimero / Openhab DPTXlator4ByteFloat.makeString(): is using a
903 * translating a Float to String. It could happen the a ',' is used as separator, such as
905 * Openhab's DecimalType expects this to be in US format and expects '.': 3.14159E20.
906 * There is no issue with DPTXlator2ByteFloat since calimero is using a non-localized translation
909 DPTXlator4ByteFloat translator4ByteFloat = (DPTXlator4ByteFloat) translator;
910 Float f = translator4ByteFloat.getValueFloat();
911 if (Math.abs(f) < 100000) {
912 value = String.valueOf(f);
914 NumberFormat dcf = NumberFormat.getInstance(Locale.US);
915 if (dcf instanceof DecimalFormat) {
916 ((DecimalFormat) dcf).applyPattern("0.#####E0");
918 value = dcf.format(f);
922 DPTXlatorSceneControl translatorSceneControl = (DPTXlatorSceneControl) translator;
923 int decimalValue = translatorSceneControl.getSceneNumber();
924 if (value.startsWith("learn")) {
925 decimalValue += 0x80;
927 value = String.valueOf(decimalValue);
931 DPTXlatorDateTime translatorDateTime = (DPTXlatorDateTime) translator;
932 if (translatorDateTime.isFaultyClock()) {
933 // Not supported: faulty clock
934 logger.debug("toType: KNX clock msg ignored: clock faulty bit set, which is not supported");
936 } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
937 && translatorDateTime.isValidField(DPTXlatorDateTime.DATE)) {
938 // Not supported: "/1/1" (month and day without year)
940 "toType: KNX clock msg ignored: no year, but day and month, which is not supported");
942 } else if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
943 && !translatorDateTime.isValidField(DPTXlatorDateTime.DATE)) {
944 // Not supported: "1900" (year without month and day)
946 "toType: KNX clock msg ignored: no day and month, but year, which is not supported");
948 } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
949 && !translatorDateTime.isValidField(DPTXlatorDateTime.DATE)
950 && !translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
951 // Not supported: No year, no date and no time
952 logger.debug("toType: KNX clock msg ignored: no day and month or year, which is not supported");
956 Calendar cal = Calendar.getInstance();
957 if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
958 && !translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
959 // Pure date format, no time information
960 cal.setTimeInMillis(translatorDateTime.getValueMilliseconds());
961 value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
962 return DateTimeType.valueOf(value);
963 } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
964 && translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
965 // Pure time format, no date information
967 cal.set(Calendar.HOUR_OF_DAY, translatorDateTime.getHour());
968 cal.set(Calendar.MINUTE, translatorDateTime.getMinute());
969 cal.set(Calendar.SECOND, translatorDateTime.getSecond());
970 value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
971 return DateTimeType.valueOf(value);
972 } else if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
973 && translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
974 // Date format and time information
975 cal.setTimeInMillis(translatorDateTime.getValueMilliseconds());
976 value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
977 return DateTimeType.valueOf(value);
982 Class<? extends Type> typeClass = toTypeClass(id);
983 if (typeClass == null) {
987 if (typeClass.equals(PercentType.class)) {
988 return new PercentType(BigDecimal.valueOf(Math.round(translator.getNumericValue())));
990 if (typeClass.equals(DecimalType.class)) {
991 return new DecimalType(translator.getNumericValue());
993 if (typeClass.equals(StringType.class)) {
994 return StringType.valueOf(value);
997 if (typeClass.equals(DateTimeType.class)) {
998 String date = formatDateTime(value, datapoint.getDPT());
999 if ((date == null) || (date.isEmpty())) {
1000 logger.debug("toType: KNX clock msg ignored: date object null or empty {}.", date);
1003 return DateTimeType.valueOf(date);
1007 if (typeClass.equals(HSBType.class)) {
1008 // value has format of "r:<red value> g:<green value> b:<blue value>"
1009 int r = Integer.parseInt(value.split(" ")[0].split(":")[1]);
1010 int g = Integer.parseInt(value.split(" ")[1].split(":")[1]);
1011 int b = Integer.parseInt(value.split(" ")[2].split(":")[1]);
1013 return HSBType.fromRGB(r, g, b);
1016 } catch (KNXFormatException kfe) {
1017 logger.info("Translator couldn't parse data for datapoint type '{}' (KNXFormatException).",
1018 datapoint.getDPT());
1019 } catch (KNXIllegalArgumentException kiae) {
1020 logger.info("Translator couldn't parse data for datapoint type '{}' (KNXIllegalArgumentException).",
1021 datapoint.getDPT());
1022 } catch (KNXException e) {
1023 logger.warn("Failed creating a translator for datapoint type '{}'.", datapoint.getDPT(), e);
1030 * Converts a datapoint type id into an openHAB type class
1032 * @param dptId the datapoint type id
1033 * @return the openHAB type (command or state) class or {@code null} if the datapoint type id is not supported.
1036 public Class<? extends Type> toTypeClass(String dptId) {
1037 Class<? extends Type> ohClass = dptTypeMap.get(dptId);
1038 if (ohClass == null) {
1039 int mainNumber = getMainNumber(dptId);
1040 if (mainNumber == -1) {
1041 logger.debug("Couldn't convert KNX datapoint type id into openHAB type class for dptId: {}.", dptId);
1044 ohClass = dptMainTypeMap.get(mainNumber);
1050 * Converts an openHAB type class into a datapoint type id.
1052 * @param typeClass the openHAB type class
1053 * @return the datapoint type id
1055 public String toDPTid(Class<? extends Type> typeClass) {
1056 return defaultDptMap.get(typeClass);
1060 * Formats the given <code>value</code> according to the datapoint type
1061 * <code>dpt</code> to a String which can be processed by {@link DateTimeType}.
1066 * @return a formatted String like </code>yyyy-MM-dd'T'HH:mm:ss</code> which
1067 * is target format of the {@link DateTimeType}
1069 private String formatDateTime(String value, String dpt) {
1073 if (DPTXlatorDate.DPT_DATE.getID().equals(dpt)) {
1074 date = new SimpleDateFormat(DATE_FORMAT).parse(value);
1075 } else if (DPTXlatorTime.DPT_TIMEOFDAY.getID().equals(dpt)) {
1076 if (value.contains("no-day")) {
1078 * KNX "no-day" needs special treatment since openHAB's DateTimeType doesn't support "no-day".
1079 * Workaround: remove the "no-day" String, parse the remaining time string, which will result in a
1080 * date of "1970-01-01".
1081 * Replace "no-day" with the current day name
1083 StringBuffer stb = new StringBuffer(value);
1084 int start = stb.indexOf("no-day");
1085 int end = start + "no-day".length();
1086 stb.replace(start, end, String.format(Locale.US, "%1$ta", Calendar.getInstance()));
1087 value = stb.toString();
1090 date = new SimpleDateFormat(TIME_DAY_FORMAT, Locale.US).parse(value);
1091 } catch (ParseException pe) {
1092 date = new SimpleDateFormat(TIME_FORMAT, Locale.US).parse(value);
1095 } catch (ParseException pe) {
1096 // do nothing but logging
1097 logger.warn("Could not parse '{}' to a valid date", value);
1100 return date != null ? new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(date) : "";
1104 * Formats the given internal <code>dateType</code> to a knx readable String
1105 * according to the target datapoint type <code>dpt</code>.
1108 * @param dpt the target datapoint type
1110 * @return a String which contains either an ISO8601 formatted date (yyyy-mm-dd),
1111 * a formatted 24-hour clock with the day of week prepended (Mon, 12:00:00) or
1112 * a formatted 24-hour clock (12:00:00)
1114 * @throws IllegalArgumentException if none of the datapoint types DPT_DATE or
1115 * DPT_TIMEOFDAY has been used.
1117 private static String formatDateTime(DateTimeType dateType, String dpt) {
1118 if (DPTXlatorDate.DPT_DATE.getID().equals(dpt)) {
1119 return dateType.format("%tF");
1120 } else if (DPTXlatorTime.DPT_TIMEOFDAY.getID().equals(dpt)) {
1121 return dateType.format(Locale.US, "%1$ta, %1$tT");
1122 } else if (DPTXlatorDateTime.DPT_DATE_TIME.getID().equals(dpt)) {
1123 return dateType.format(Locale.US, "%tF %1$tT");
1125 throw new IllegalArgumentException("Could not format date to datapoint type '" + dpt + "'");
1130 * Retrieves sub number from a DTP ID such as "14.001"
1132 * @param dptID String with DPT ID
1133 * @return sub number or -1
1135 private int getSubNumber(String dptID) {
1137 if (dptID == null) {
1138 throw new IllegalArgumentException("Parameter dptID cannot be null");
1141 int dptSepratorPosition = dptID.indexOf('.');
1142 if (dptSepratorPosition > 0) {
1144 result = Integer.parseInt(dptID.substring(dptSepratorPosition + 1, dptID.length()));
1145 } catch (NumberFormatException nfe) {
1146 logger.error("toType couldn't identify main and/or sub number in dptID (NumberFormatException): {}",
1148 } catch (IndexOutOfBoundsException ioobe) {
1149 logger.error("toType couldn't identify main and/or sub number in dptID (IndexOutOfBoundsException): {}",
1157 * Retrieves main number from a DTP ID such as "14.001"
1159 * @param dptID String with DPT ID
1160 * @return main number or -1
1162 private int getMainNumber(String dptID) {
1164 if (dptID == null) {
1165 throw new IllegalArgumentException("Parameter dptID cannot be null");
1168 int dptSepratorPosition = dptID.indexOf('.');
1169 if (dptSepratorPosition > 0) {
1171 result = Integer.parseInt(dptID.substring(0, dptSepratorPosition));
1172 } catch (NumberFormatException nfe) {
1173 logger.error("toType couldn't identify main and/or sub number in dptID (NumberFormatException): {}",
1175 } catch (IndexOutOfBoundsException ioobe) {
1176 logger.error("toType couldn't identify main and/or sub number in dptID (IndexOutOfBoundsException): {}",
1184 * convert 0...100% to 1 byte 0..255
1187 * @return int 0..255
1189 private int convertPercentToByte(PercentType percent) {
1190 return percent.toBigDecimal().multiply(BigDecimal.valueOf(255))
1191 .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).intValue();