2 * Copyright (c) 2010-2021 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.StopMoveType;
38 import org.openhab.core.library.types.StringType;
39 import org.openhab.core.library.types.UpDownType;
40 import org.openhab.core.types.Type;
41 import org.openhab.core.types.UnDefType;
42 import org.osgi.service.component.annotations.Component;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 import tuwien.auto.calimero.KNXException;
47 import tuwien.auto.calimero.KNXFormatException;
48 import tuwien.auto.calimero.KNXIllegalArgumentException;
49 import tuwien.auto.calimero.datapoint.Datapoint;
50 import tuwien.auto.calimero.dptxlator.DPT;
51 import tuwien.auto.calimero.dptxlator.DPTXlator;
52 import tuwien.auto.calimero.dptxlator.DPTXlator1BitControlled;
53 import tuwien.auto.calimero.dptxlator.DPTXlator2ByteFloat;
54 import tuwien.auto.calimero.dptxlator.DPTXlator2ByteUnsigned;
55 import tuwien.auto.calimero.dptxlator.DPTXlator3BitControlled;
56 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteFloat;
57 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteSigned;
58 import tuwien.auto.calimero.dptxlator.DPTXlator4ByteUnsigned;
59 import tuwien.auto.calimero.dptxlator.DPTXlator64BitSigned;
60 import tuwien.auto.calimero.dptxlator.DPTXlator8BitSigned;
61 import tuwien.auto.calimero.dptxlator.DPTXlator8BitUnsigned;
62 import tuwien.auto.calimero.dptxlator.DPTXlatorBoolean;
63 import tuwien.auto.calimero.dptxlator.DPTXlatorDate;
64 import tuwien.auto.calimero.dptxlator.DPTXlatorDateTime;
65 import tuwien.auto.calimero.dptxlator.DPTXlatorRGB;
66 import tuwien.auto.calimero.dptxlator.DPTXlatorSceneControl;
67 import tuwien.auto.calimero.dptxlator.DPTXlatorSceneNumber;
68 import tuwien.auto.calimero.dptxlator.DPTXlatorString;
69 import tuwien.auto.calimero.dptxlator.DPTXlatorTime;
70 import tuwien.auto.calimero.dptxlator.DPTXlatorUtf8;
71 import tuwien.auto.calimero.dptxlator.TranslatorTypes;
74 * This class provides type mapping between all openHAB core types and KNX data point types.
76 * Each 'MainType' delivered from calimero, has a default mapping
77 * for all it's children to a openHAB Typeclass.
78 * All these 'MainType' mapping's are put into 'dptMainTypeMap'.
80 * Default 'MainType' mapping's we can override by a specific mapping.
81 * All specific mapping's are put into 'dptTypeMap'.
83 * If for a 'MainType' there is currently no specific mapping registered,
84 * you can find a commented example line, with it's correct 'DPTXlator' class.
87 * @author Volker Daube
89 * @author Helmut Lehmeyer - Java8, generic DPT Mapper
92 public class KNXCoreTypeMapper implements KNXTypeMapper {
94 private final Logger logger = LoggerFactory.getLogger(KNXCoreTypeMapper.class);
96 private static final String TIME_DAY_FORMAT = new String("EEE, HH:mm:ss");
97 private static final String DATE_FORMAT = new String("yyyy-MM-dd");
100 * stores the openHAB type class for (supported) KNX datapoint types in a generic way.
101 * dptTypeMap stores more specific type class and exceptions.
103 private final Map<Integer, Class<? extends Type>> dptMainTypeMap;
105 /** stores the openHAB type class for all (supported) KNX datapoint types */
106 private final Map<String, Class<? extends Type>> dptTypeMap;
108 /** stores the default KNX DPT to use for each openHAB type */
109 private final Map<Class<? extends Type>, String> defaultDptMap;
111 public KNXCoreTypeMapper() {
112 @SuppressWarnings("unused")
113 final List<Class<?>> xlators = Arrays.<Class<?>> asList(DPTXlator1BitControlled.class,
114 DPTXlator2ByteFloat.class, DPTXlator2ByteUnsigned.class, DPTXlator3BitControlled.class,
115 DPTXlator4ByteFloat.class, DPTXlator4ByteSigned.class, DPTXlator4ByteUnsigned.class,
116 DPTXlator64BitSigned.class, DPTXlator8BitSigned.class, DPTXlator8BitUnsigned.class,
117 DPTXlatorBoolean.class, DPTXlatorDate.class, DPTXlatorDateTime.class, DPTXlatorRGB.class,
118 DPTXlatorSceneControl.class, DPTXlatorSceneNumber.class, DPTXlatorString.class, DPTXlatorTime.class,
119 DPTXlatorUtf8.class);
121 dptTypeMap = new HashMap<>();
122 dptMainTypeMap = new HashMap<>();
126 * 1.000: General bool
127 * 1.001: DPT_Switch values: 0 = off 1 = on
128 * 1.002: DPT_Bool values: 0 = false 1 = true
129 * 1.003: DPT_Enable values: 0 = disable 1 = enable
130 * 1.004: DPT_Ramp values: 0 = no ramp 1 = ramp
131 * 1.005: DPT_Alarm values: 0 = no alarm 1 = alarm
132 * 1.006: DPT_BinaryValue values: 0 = low 1 = high
133 * 1.007: DPT_Step values: 0 = decrease 1 = increase
134 * 1.008: DPT_UpDown values: 0 = up 1 = down
135 * 1.009: DPT_OpenClose values: 0 = open 1 = close
136 * 1.010: DPT_Start values: 0 = stop 1 = start
137 * 1.011: DPT_State values: 0 = inactive 1 = active
138 * 1.012: DPT_Invert values: 0 = not inverted 1 = inverted
139 * 1.013: DPT_DimSendStyle values: 0 = start/stop 1 = cyclic
140 * 1.014: DPT_InputSource values: 0 = fixed 1 = calculated
141 * 1.015: DPT_Reset values: 0 = no action 1 = reset
142 * 1.016: DPT_Ack values: 0 = no action 1 = acknowledge
143 * 1.017: DPT_Trigger values: 0 = trigger 1 = trigger
144 * 1.018: DPT_Occupancy values: 0 = not occupied 1 = occupied
145 * 1.019: DPT_Window_Door values: 0 = closed 1 = open
146 * 1.021: DPT_LogicalFunction values: 0 = OR 1 = AND
147 * 1.022: DPT_Scene_AB values: 0 = scene A 1 = scene B
148 * 1.023: DPT_ShutterBlinds_Mode values: 0 = only move up/down 1 = move up/down + step-stop
149 * 1.100: DPT_Heat/Cool values: 0 = cooling 1 = heating
151 dptMainTypeMap.put(1, OnOffType.class);
152 /** Exceptions Datapoint Types "B1", Main number 1 */
153 dptTypeMap.put(DPTXlatorBoolean.DPT_UPDOWN.getID(), UpDownType.class);
154 dptTypeMap.put(DPTXlatorBoolean.DPT_OPENCLOSE.getID(), OpenClosedType.class);
155 dptTypeMap.put(DPTXlatorBoolean.DPT_START.getID(), StopMoveType.class);
156 dptTypeMap.put(DPTXlatorBoolean.DPT_WINDOW_DOOR.getID(), OpenClosedType.class);
157 dptTypeMap.put(DPTXlatorBoolean.DPT_SCENE_AB.getID(), DecimalType.class);
161 * 2.001: DPT_Switch_Control values: 0 = off 1 = on
162 * 2.002: DPT_Bool_Control values: 0 = false 1 = true
163 * 2.003: DPT_Enable_Control values: 0 = disable 1 = enable
164 * 2.004: DPT_Ramp_Control values: 0 = no ramp 1 = ramp
165 * 2.005: DPT_Alarm_Control values: 0 = no alarm 1 = alarm
166 * 2.006: DPT_BinaryValue_Control values: 0 = low 1 = high
167 * 2.007: DPT_Step_Control values: 0 = decrease 1 = increase
168 * 2.008: DPT_Direction1_Control values: 0 = up 1 = down
169 * 2.009: DPT_Direction2_Control values: 0 = open 1 = close
170 * 2.010: DPT_Start_Control values: 0 = stop 1 = start
171 * 2.011: DPT_State_Control values: 0 = inactive 1 = active
172 * 2.012: DPT_Invert_Control values: 0 = not inverted 1 = inverted
174 dptMainTypeMap.put(2, DecimalType.class);
175 /** Exceptions Datapoint Types "B2", Main number 2 */
176 // Example: dptTypeMap.put(DPTXlator1BitControlled.DPT_SWITCH_CONTROL.getID(), DecimalType.class);
180 * 3.007: DPT_Control_Dimming values: 0 = decrease 1 = increase
181 * 3.008: DPT_Control_Blinds values: 0 = up 1 = down
183 dptMainTypeMap.put(3, IncreaseDecreaseType.class);
184 /** Exceptions Datapoint Types "B1U3", Main number 3 */
185 dptTypeMap.put(DPTXlator3BitControlled.DPT_CONTROL_BLINDS.getID(), UpDownType.class);
189 * 4.001: DPT_Char_ASCII
190 * 4.002: DPT_Char_8859_1
192 dptMainTypeMap.put(4, StringType.class);
196 * 5.000: General byte
197 * 5.001: DPT_Scaling values: 0...100 %
198 * 5.003: DPT_Angle values: 0...360 °
199 * 5.004: DPT_Percent_U8 (8 Bit) values: 0...255 %
200 * 5.005: DPT_DecimalFactor values: 0...255 ratio
201 * 5.006: DPT_Tariff values: 0...254
202 * 5.010: DPT_Value_1_Ucount Unsigned count values: 0...255 counter pulses
204 dptMainTypeMap.put(5, DecimalType.class);
205 /** Exceptions Types "8-Bit Unsigned Value", Main number 5 */
206 dptTypeMap.put(DPTXlator8BitUnsigned.DPT_SCALING.getID(), PercentType.class);
207 dptTypeMap.put(DPTXlator8BitUnsigned.DPT_PERCENT_U8.getID(), PercentType.class);
211 * 6.001: DPT_Percent_V8 (8 Bit) values: -128...127 %
212 * 6.010: DPT_Value_1_Count values: signed -128...127 counter pulses
213 * 6.020: DPT_Status_Mode3 with mode values: 0/0/0/0/0 0...1/1/1/1/1 2
215 dptMainTypeMap.put(6, DecimalType.class);
216 /** Exceptions Datapoint Types "8-Bit Signed Value", Main number 6 */
217 dptTypeMap.put(DPTXlator8BitSigned.DPT_PERCENT_V8.getID(), PercentType.class);
218 dptTypeMap.put(DPTXlator8BitSigned.DPT_STATUS_MODE3.getID(), StringType.class);
222 * 7.000: General unsigned integer
223 * 7.001: DPT_Value_2_Ucount values: 0...65535 pulses
224 * 7.002: DPT_TimePeriodMsec values: 0...65535 res 1 ms
225 * 7.003: DPT_TimePeriod10MSec values: 0...655350 res 10 ms
226 * 7.004: DPT_TimePeriod100MSec values: 0...6553500 res 100 ms
227 * 7.005: DPT_TimePeriodSec values: 0...65535 s
228 * 7.006: DPT_TimePeriodMin values: 0...65535 min
229 * 7.007: DPT_TimePeriodHrs values: 0...65535 h
230 * 7.010: DPT_PropDataType values: 0...65535
231 * 7.011: DPT_Length_mm values: 0...65535 mm
232 * 7.012: DPT_UElCurrentmA values: 0...65535 mA
233 * 7.013: DPT_Brightness values: 0...65535 lx
234 * Calimero does not map: (map/use to 7.000 until then)
235 * 7.600: DPT_Colour_Temperature values: 0...65535 K, 2000K 3000K 5000K 8000K
237 dptMainTypeMap.put(7, DecimalType.class);
238 /** Exceptions Datapoint Types "2-Octet Unsigned Value", Main number 7 */
239 dptTypeMap.put(DPTXlator2ByteFloat.DPT_HUMIDITY.getID(), PercentType.class);
243 * 8.000: General integer
244 * 8.001: DPT_Value_2_Count
245 * 8.002: DPT_DeltaTimeMsec
246 * 8.003: DPT_DeltaTime10MSec
247 * 8.004: DPT_DeltaTime100MSec
248 * 8.005: DPT_DeltaTimeSec
249 * 8.006: DPT_DeltaTimeMin
250 * 8.007: DPT_DeltaTimeHrs
251 * 8.010: DPT_Percent_V16
252 * 8.011: DPT_Rotation_Angle
254 dptMainTypeMap.put(8, DecimalType.class);
258 * 9.000: General float
259 * 9.001: DPT_Value_Temp values: -273...+670760 °C
260 * 9.002: DPT_Value_Tempd values: -670760...+670760 K
261 * 9.003: DPT_Value_Tempa values: -670760...+670760 K/h
262 * 9.004: DPT_Value_Lux values: 0...+670760 lx
263 * 9.005: DPT_Value_Wsp values: 0...+670760 m/s
264 * 9.006: DPT_Value_Pres values: 0...+670760 Pa
265 * 9.007: DPT_Value_Humidity values: 0...+670760 %
266 * 9.008: DPT_Value_AirQuality values: 0...+670760 ppm
267 * 9.010: DPT_Value_Time1 values: -670760...+670760 s
268 * 9.011: DPT_Value_Time2 values: -670760...+670760 ms
269 * 9.020: DPT_Value_Volt values: -670760...+670760 mV
270 * 9.021: DPT_Value_Curr values: -670760...+670760 mA
271 * 9.022: DPT_PowerDensity values: -670760...+670760 W/m²
272 * 9.023: DPT_KelvinPerPercent values: -670760...+670760 K/%
273 * 9.024: DPT_Power values: -670760...+670760 kW
274 * 9.025: DPT_Value_Volume_Flow values: -670760...+670760 l/h
275 * 9.026: DPT_Rain_Amount values: -671088.64...670760.96 l/m²
276 * 9.027: DPT_Value_Temp_F values: -459.6...670760.96 °F
277 * 9.028: DPT_Value_Wsp_kmh values: 0...670760.96 km/h
279 dptMainTypeMap.put(9, DecimalType.class);
280 /** Exceptions Datapoint Types "2-Octet Float Value", Main number 9 */
281 dptTypeMap.put(DPTXlator2ByteFloat.DPT_HUMIDITY.getID(), PercentType.class);
285 * 10.001: DPT_TimeOfDay values: 1 = Monday...7 = Sunday, 0 = no-day, 00:00:00 Sun, 23:59:59 dow, hh:mm:ss
287 dptMainTypeMap.put(10, DateTimeType.class);
288 /** Exceptions Datapoint Types "Time", Main number 10 */
289 // Example: dptTypeMap.put(DPTXlatorTime.DPT_TIMEOFDAY.getID(), DateTimeType.class);
293 * 11.001: DPT_Date values: 1990-01-01...2089-12-31, yyyy-mm-dd
295 dptMainTypeMap.put(11, DateTimeType.class);
296 /** Exceptions Datapoint Types “Date”", Main number 11 */
297 // Example: dptTypeMap.put(DPTXlatorDate.DPT_DATE.getID(), DateTimeType.class);
301 * 12.000: General unsigned long
302 * 12.001: DPT_Value_4_Ucount values: 0...4294967295 counter pulses
304 dptMainTypeMap.put(12, DecimalType.class);
305 /** Exceptions Datapoint Types "4-Octet Unsigned Value", Main number 12 */
306 // Example: dptTypeMap.put(DPTXlator4ByteUnsigned.DPT_VALUE_4_UCOUNT.getID(), DecimalType.class);
310 * 13.000: General long
311 * 13.001: DPT_Value_4_Count values: -2147483648...2147483647 counter pulses
312 * 13.002: DPT_FlowRate_m3h values: -2147483648...2147483647 m3/h
313 * 13.010: DPT_ActiveEnergy values: -2147483648...2147483647 Wh
314 * 13.011: DPT_ApparantEnergy values: -2147483648...2147483647 VAh
315 * 13.012: DPT_ReactiveEnergy values: -2147483648...2147483647 VARh
316 * 13.013: DPT_ActiveEnergy_kWh values: -2147483648...2147483647 kWh
317 * 13.014: DPT_ApparantEnergy_kVAh values: -2147483648...2147483647 kVAh
318 * 13.015: DPT_ReactiveEnergy_kVARh values: -2147483648...2147483647 kVAR
319 * 13.100: DPT_LongDeltaTimeSec values: -2147483648...2147483647 s
321 dptMainTypeMap.put(13, DecimalType.class);
322 /** Exceptions Datapoint Types "4-Octet Signed Value", Main number 13 */
323 // Example: dptTypeMap.put(DPTXlator4ByteSigned.DPT_COUNT.getID(), DecimalType.class);
326 * MainType: 14, Range: [-3.40282347e+38f...3.40282347e+38f]
327 * 14.000: Acceleration, values: ms⁻²
328 * 14.001: Acceleration, angular, values: rad s⁻²
329 * 14.002: Activation energy, values: J/mol
330 * 14.003: Activity, values: s⁻¹
331 * 14.004: Mol, values: mol
332 * 14.005: Amplitude, values:
333 * 14.006: Angle, values: rad
334 * 14.007: Angle, values: °
335 * 14.008: Momentum, values: Js
336 * 14.009: Angular velocity, values: rad/s
337 * 14.010: Area, values: m²
338 * 14.011: Capacitance, values: F
339 * 14.012: Charge density (surface), values: C m⁻²
340 * 14.013: Charge density (volume), values: C m⁻³
341 * 14.014: Compressibility, values: m²/N
342 * 14.015: Conductance, values: Ω⁻¹
343 * 14.016: Conductivity, electrical, values: Ω⁻¹m⁻¹
344 * 14.017: Density, values: kg m⁻³
345 * 14.018: Electric charge, values: C
346 * 14.019: Electric current, values: A
347 * 14.020: Electric current density, values: A m⁻²
348 * 14.021: Electric dipole moment, values: Cm
349 * 14.022: Electric displacement, values: C m⁻²
350 * 14.023: Electric field strength, values: V/m
351 * 14.024: Electric flux, values: Vm
352 * 14.025: Electric flux density, values: C m⁻²
353 * 14.026: Electric polarization, values: C m⁻²
354 * 14.027: Electric potential, values: V
355 * 14.028: Electric potential difference, values: V
356 * 14.029: Electromagnetic moment, values: A m²
357 * 14.030: Electromotive force, values: V
358 * 14.031: Energy, values: J
359 * 14.032: Force, values: N
360 * 14.033: Frequency, values: Hz
361 * 14.034: Frequency, angular, values: rad/s
362 * 14.035: Heat capacity, values: J/K
363 * 14.036: Heat flow rate, values: W
364 * 14.037: Heat quantity, values: J
365 * 14.038: Impedance, values: Ω
366 * 14.039: Length, values: m
367 * 14.040: Quantity of Light, values: J
368 * 14.041: Luminance, values: cd m⁻²
369 * 14.042: Luminous flux, values: lm
370 * 14.043: Luminous intensity, values: cd
371 * 14.044: Magnetic field strength, values: A/m
372 * 14.045: Magnetic flux, values: Wb
373 * 14.046: Magnetic flux density, values: T
374 * 14.047: Magnetic moment, values: A m²
375 * 14.048: Magnetic polarization, values: T
376 * 14.049: Magnetization, values: A/m
377 * 14.050: Magneto motive force, values: A
378 * 14.051: Mass, values: kg
379 * 14.052: Mass flux, values: kg/s
380 * 14.053: Momentum, values: N/s
381 * 14.054: Phase angle, radiant, values: rad
382 * 14.055: Phase angle, degree, values: °
383 * 14.056: Power, values: W
384 * 14.057: Power factor, values:
385 * 14.058: Pressure, values: Pa
386 * 14.059: Reactance, values: Ω
387 * 14.060: Resistance, values: Ω
388 * 14.061: Resistivity, values: Ωm
389 * 14.062: Self inductance, values: H
390 * 14.063: Solid angle, values: sr
391 * 14.064: Sound intensity, values: W m⁻²
392 * 14.065: Speed, values: m/s
393 * 14.066: Stress, values: Pa
394 * 14.067: Surface tension, values: N/m
395 * 14.068: Temperature in Celsius Degree, values: °C
396 * 14.069: Temperature, absolute, values: K
397 * 14.070: Temperature difference, values: K
398 * 14.071: Thermal capacity, values: J/K
399 * 14.072: Thermal conductivity, values: W/m K⁻¹
400 * 14.073: Thermoelectric power, values: V/K
401 * 14.074: Time, values: s
402 * 14.075: Torque, values: Nm
403 * 14.076: Volume, values: m³
404 * 14.077: Volume flux, values: m³/s
405 * 14.078: Weight, values: N
406 * 14.079: Work, values: J
408 dptMainTypeMap.put(14, DecimalType.class);
409 /** Exceptions Datapoint Types "4-Octet Float Value", Main number 14 */
410 // Example: dptTypeMap.put(DPTXlator4ByteFloat.DPT_ACCELERATION_ANGULAR.getID(), DecimalType.class);
414 * 16.000: ASCII string
415 * 16.001: ISO-8859-1 string (Latin 1)
417 dptMainTypeMap.put(16, StringType.class);
418 /** Exceptions Datapoint Types "String", Main number 16 */
419 dptTypeMap.put(DPTXlatorString.DPT_STRING_8859_1.getID(), StringType.class);
420 dptTypeMap.put(DPTXlatorString.DPT_STRING_ASCII.getID(), StringType.class);
424 * 17.001: Scene Number, values: 0...63
426 dptMainTypeMap.put(17, DecimalType.class);
427 /** Exceptions Datapoint Types "Scene Number", Main number 17 */
428 // Example: dptTypeMap.put(DPTXlatorSceneNumber.DPT_SCENE_NUMBER.getID(), DecimalType.class);
432 * 18.001: Scene Control, values: 0...63, 0 = activate, 1 = learn
434 dptMainTypeMap.put(18, DecimalType.class);
435 /** Exceptions Datapoint Types "Scene Control", Main number 18 */
436 // Example: dptTypeMap.put(DPTXlatorSceneControl.DPT_SCENE_CONTROL.getID(), DecimalType.class);
440 * 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
442 dptMainTypeMap.put(19, DateTimeType.class);
443 /** Exceptions Datapoint Types "DateTime", Main number 19 */
444 // Example: dptTypeMap.put(DPTXlatorDateTime.DPT_DATE_TIME.getID(), DateTimeType.class);
448 * 20.001: System Clock Mode, enumeration [0..2]
449 * 20.002: Building Mode, enumeration [0..2]
450 * 20.003: Occupancy Mode, enumeration [0..2]
451 * 20.004: Priority, enumeration [0..3]
452 * 20.005: Light Application Mode, enumeration [0..2]
453 * 20.006: Application Area, enumeration [0..14]
454 * 20.007: Alarm Class Type, enumeration [0..3]
455 * 20.008: PSU Mode, enumeration [0..2]
456 * 20.011: Error Class System, enumeration [0..18]
457 * 20.012: Error Class HVAC, enumeration [0..4]
458 * 20.013: Time Delay, enumeration [0..25]
459 * 20.014: Beaufort Wind Force Scale, enumeration [0..12]
460 * 20.017: Sensor Select, enumeration [0..4]
461 * 20.020: Actuator Connect Type, enumeration [1..2]
462 * 20.100: Fuel Type, enumeration [0..3]
463 * 20.101: Burner Type, enumeration [0..3]
464 * 20.102: HVAC Mode, enumeration [0..4]
465 * 20.103: DHW Mode, enumeration [0..4]
466 * 20.104: Load Priority, enumeration [0..2]
467 * 20.105: HVAC Control Mode, enumeration [0..20]
468 * 20.106: HVAC Emergency Mode, enumeration [0..5]
469 * 20.107: Changeover Mode, enumeration [0..2]
470 * 20.108: Valve Mode, enumeration [1..5]
471 * 20.109: Damper Mode, enumeration [1..4]
472 * 20.110: Heater Mode, enumeration [1..3]
473 * 20.111: Fan Mode, enumeration [0..2]
474 * 20.112: Master/Slave Mode, enumeration [0..2]
475 * 20.113: Status Room Setpoint, enumeration [0..2]
476 * 20.114: Metering Device Type, enumeration [0..41/255]
477 * 20.120: Air Damper Actuator Type, enumeration [1..2]
478 * 20.121: Backup Mode, enumeration [0..1]
479 * 20.122: Start Synchronization, enumeration [0..2]
480 * 20.600: Behavior Lock/Unlock, enumeration [0..6]
481 * 20.601: Behavior Bus Power Up/Down, enumeration [0..4]
482 * 20.602: DALI Fade Time, enumeration [0..15]
483 * 20.603: Blinking Mode, enumeration [0..2]
484 * 20.604: Light Control Mode, enumeration [0..1]
485 * 20.605: Switch PB Model, enumeration [1..2]
486 * 20.606: PB Action, enumeration [0..3]
487 * 20.607: Dimm PB Model, enumeration [1..4]
488 * 20.608: Switch On Mode, enumeration [0..2]
489 * 20.609: Load Type Set, enumeration [0..2]
490 * 20.610: Load Type Detected, enumeration [0..3]
491 * 20.801: SAB Except Behavior, enumeration [0..4]
492 * 20.802: SAB Behavior Lock/Unlock, enumeration [0..6]
493 * 20.803: SSSB Mode, enumeration [1..4]
494 * 20.804: Blinds Control Mode, enumeration [0..1]
495 * 20.1000: Comm Mode, enumeration [0..255]
496 * 20.1001: Additional Info Type, enumeration [0..7]
497 * 20.1002: RF Mode Select, enumeration [0..2]
498 * 20.1003: RF Filter Select, enumeration [0..3]
499 * 20.1200: M-Bus Breaker/Valve State, enumeration [0..255]
500 * 20.1202: Gas Measurement Condition, enumeration [0..3]
503 dptMainTypeMap.put(20, StringType.class);
504 /** Exceptions Datapoint Types, Main number 20 */
505 // Example since calimero 2.4: dptTypeMap.put(DPTXlator8BitEnum.DptSystemClockMode.getID(), StringType.class);
509 * 21.001: General Status, values: 0...31
510 * 21.002: Device Control, values: 0...7
511 * 21.100: Forcing Signal, values: 0...255
512 * 21.101: Forcing Signal Cool, values: 0...1
513 * 21.102: Room Heating Controller Status, values: 0...255
514 * 21.103: Solar Dhw Controller Status, values: 0...7
515 * 21.104: Fuel Type Set, values: 0...7
516 * 21.105: Room Cooling Controller Status, values: 0...1
517 * 21.106: Ventilation Controller Status, values: 0...15
518 * 21.601: Light Actuator Error Info, values: 0...127
519 * 21.1000: R F Comm Mode Info, values: 0...7
520 * 21.1001: R F Filter Modes, values: 0...7
521 * 21.1010: Channel Activation State, values: 0...255
523 dptMainTypeMap.put(21, StringType.class);
524 /** Exceptions Datapoint Types, Main number 21 */
525 // Example since calimero 2.4: dptTypeMap.put(DptXlator8BitSet.DptGeneralStatus.getID(), StringType.class);
531 dptMainTypeMap.put(28, StringType.class);
532 /** Exceptions Datapoint Types "String" UTF-8, Main number 28 */
533 // Example: dptTypeMap.put(DPTXlatorUtf8.DPT_UTF8.getID(), StringType.class);
537 * 29.010: Active Energy, values: -9223372036854775808...9223372036854775807 Wh
538 * 29.011: Apparent energy, values: -9223372036854775808...9223372036854775807 VAh
539 * 29.012: Reactive energy, values: -9223372036854775808...9223372036854775807 VARh
541 dptMainTypeMap.put(29, DecimalType.class);
542 /** Exceptions Datapoint Types "64-Bit Signed Value", Main number 29 */
543 // Example: dptTypeMap.put(DPTXlator64BitSigned.DPT_ACTIVE_ENERGY.getID(), DecimalType.class);
547 * 229.001: Metering Value, values: -2147483648...2147483647
549 dptMainTypeMap.put(229, DecimalType.class);
550 /** Exceptions Datapoint Types "4-Octet Signed Value", Main number 229 */
551 // Example: dptTypeMap.put(DptXlatorMeteringValue.DptMeteringValue.getID(), DecimalType.class);
554 * MainType: 232, 3 bytes
555 * 232.600: DPT_Colour_RGB, values: 0 0 0...255 255 255, r g b
557 dptMainTypeMap.put(232, HSBType.class);
558 /** Exceptions Datapoint Types "RGB Color", Main number 232 */
559 // Example: dptTypeMap.put(DPTXlatorRGB.DPT_RGB.getID(), HSBType.class);
561 defaultDptMap = new HashMap<>();
562 defaultDptMap.put(OnOffType.class, DPTXlatorBoolean.DPT_SWITCH.getID());
563 defaultDptMap.put(UpDownType.class, DPTXlatorBoolean.DPT_UPDOWN.getID());
564 defaultDptMap.put(StopMoveType.class, DPTXlatorBoolean.DPT_START.getID());
565 defaultDptMap.put(OpenClosedType.class, DPTXlatorBoolean.DPT_WINDOW_DOOR.getID());
566 defaultDptMap.put(IncreaseDecreaseType.class, DPTXlator3BitControlled.DPT_CONTROL_DIMMING.getID());
567 defaultDptMap.put(PercentType.class, DPTXlator8BitUnsigned.DPT_SCALING.getID());
568 defaultDptMap.put(DecimalType.class, DPTXlator2ByteFloat.DPT_TEMPERATURE.getID());
569 defaultDptMap.put(DateTimeType.class, DPTXlatorTime.DPT_TIMEOFDAY.getID());
570 defaultDptMap.put(StringType.class, DPTXlatorString.DPT_STRING_8859_1.getID());
571 defaultDptMap.put(HSBType.class, DPTXlatorRGB.DPT_RGB.getID());
575 public String toDPTValue(Type type, String dptID) {
577 int mainNumber = getMainNumber(dptID);
578 if (mainNumber == -1) {
579 logger.error("toDPTValue couldn't identify mainnumber in dptID: {}", dptID);
582 int subNumber = getSubNumber(dptID);
583 if (subNumber == -1) {
584 logger.debug("toType: couldn't identify sub number in dptID: {}.", dptID);
589 DPTXlator translator = TranslatorTypes.createTranslator(mainNumber, dptID);
590 dpt = translator.getType();
591 } catch (KNXException e) {
596 // check for HSBType first, because it extends PercentType as well
597 if (type instanceof HSBType) {
598 switch (mainNumber) {
601 case 3: // * 5.003: Angle, values: 0...360 °
602 return ((HSBType) type).getHue().toString();
603 case 1: // * 5.001: Scaling, values: 0...100 %
605 return ((HSBType) type).getBrightness().toString();
610 HSBType hc = ((HSBType) type);
611 return "r:" + convertPercentToByte(hc.getRed()) + " g:"
612 + convertPercentToByte(hc.getGreen()) + " b:"
613 + convertPercentToByte(hc.getBlue());
616 HSBType hc = ((HSBType) type);
617 return "r:" + hc.getRed().intValue() + " g:" + hc.getGreen().intValue() + " b:"
618 + hc.getBlue().intValue();
620 } else if (type instanceof OnOffType) {
621 return type.equals(OnOffType.OFF) ? dpt.getLowerValue() : dpt.getUpperValue();
622 } else if (type instanceof UpDownType) {
623 return type.equals(UpDownType.UP) ? dpt.getLowerValue() : dpt.getUpperValue();
624 } else if (type instanceof IncreaseDecreaseType) {
625 DPT valueDPT = ((DPTXlator3BitControlled.DPT3BitControlled) dpt).getControlDPT();
626 return type.equals(IncreaseDecreaseType.DECREASE) ? valueDPT.getLowerValue() + " 5"
627 : valueDPT.getUpperValue() + " 5";
628 } else if (type instanceof OpenClosedType) {
629 return type.equals(OpenClosedType.CLOSED) ? dpt.getLowerValue() : dpt.getUpperValue();
630 } else if (type instanceof StopMoveType) {
631 return type.equals(StopMoveType.STOP) ? dpt.getLowerValue() : dpt.getUpperValue();
632 } else if (type instanceof PercentType) {
633 return String.valueOf(((DecimalType) type).intValue());
634 } else if (type instanceof DecimalType) {
635 switch (mainNumber) {
637 DPT valueDPT = ((DPTXlator1BitControlled.DPT1BitControlled) dpt).getValueDPT();
638 switch (((DecimalType) type).intValue()) {
640 return "0 " + valueDPT.getLowerValue();
642 return "0 " + valueDPT.getUpperValue();
644 return "1 " + valueDPT.getLowerValue();
646 return "1 " + valueDPT.getUpperValue();
649 int intVal = ((DecimalType) type).intValue();
651 return "learn " + (intVal - 0x80);
653 return "activate " + intVal;
656 return ((DecimalType) type).toBigDecimal().stripTrailingZeros().toPlainString();
658 } else if (type instanceof StringType) {
659 return type.toString();
660 } else if (type instanceof DateTimeType) {
661 return formatDateTime((DateTimeType) type, dptID);
663 } catch (Exception e) {
664 logger.warn("An exception occurred converting type {} to dpt id {}: error message={}", type, dptID,
669 logger.debug("toDPTValue: Couldn't convert type {} to dpt id {} (no mapping).", type, dptID);
675 public Type toType(Datapoint datapoint, byte[] data) {
677 DPTXlator translator = TranslatorTypes.createTranslator(datapoint.getMainNumber(), datapoint.getDPT());
678 translator.setData(data);
679 String value = translator.getValue();
681 String id = translator.getType().getID();
682 logger.trace("toType datapoint DPT = {}", datapoint.getDPT());
684 int mainNumber = getMainNumber(id);
685 if (mainNumber == -1) {
686 logger.debug("toType: couldn't identify mainnumber in dptID: {}.", id);
689 int subNumber = getSubNumber(id);
690 if (subNumber == -1) {
691 logger.debug("toType: couldn't identify sub number in dptID: {}.", id);
695 * Following code section deals with specific mapping of values from KNX to openHAB types were the String
696 * received from the DPTXlator is not sufficient to set the openHAB type or has bugs
698 switch (mainNumber) {
700 DPTXlatorBoolean translatorBoolean = (DPTXlatorBoolean) translator;
703 return translatorBoolean.getValueBoolean() ? UpDownType.DOWN : UpDownType.UP;
705 return translatorBoolean.getValueBoolean() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
707 return translatorBoolean.getValueBoolean() ? StopMoveType.MOVE : StopMoveType.STOP;
709 return translatorBoolean.getValueBoolean() ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
711 return DecimalType.valueOf(translatorBoolean.getValueBoolean() ? "1" : "0");
713 return translatorBoolean.getValueBoolean() ? OnOffType.ON : OnOffType.OFF;
716 DPTXlator1BitControlled translator1BitControlled = (DPTXlator1BitControlled) translator;
717 int decValue = (translator1BitControlled.getControlBit() ? 2 : 0)
718 + (translator1BitControlled.getValueBit() ? 1 : 0);
719 return new DecimalType(decValue);
721 DPTXlator3BitControlled translator3BitControlled = (DPTXlator3BitControlled) translator;
722 if (translator3BitControlled.getStepCode() == 0) {
723 logger.debug("toType: KNX DPT_Control_Dimming: break received.");
724 return UnDefType.UNDEF;
728 return translator3BitControlled.getControlBit() ? IncreaseDecreaseType.INCREASE
729 : IncreaseDecreaseType.DECREASE;
731 return translator3BitControlled.getControlBit() ? UpDownType.DOWN : UpDownType.UP;
735 * FIXME: Workaround for a bug in Calimero / Openhab DPTXlator4ByteFloat.makeString(): is using a
737 * translating a Float to String. It could happen the a ',' is used as separator, such as
739 * Openhab's DecimalType expects this to be in US format and expects '.': 3.14159E20.
740 * There is no issue with DPTXlator2ByteFloat since calimero is using a non-localized translation
743 DPTXlator4ByteFloat translator4ByteFloat = (DPTXlator4ByteFloat) translator;
744 Float f = translator4ByteFloat.getValueFloat();
745 if (Math.abs(f) < 100000) {
746 value = String.valueOf(f);
748 NumberFormat dcf = NumberFormat.getInstance(Locale.US);
749 if (dcf instanceof DecimalFormat) {
750 ((DecimalFormat) dcf).applyPattern("0.#####E0");
752 value = dcf.format(f);
756 DPTXlatorSceneControl translatorSceneControl = (DPTXlatorSceneControl) translator;
757 int decimalValue = translatorSceneControl.getSceneNumber();
758 if (value.startsWith("learn")) {
759 decimalValue += 0x80;
761 value = String.valueOf(decimalValue);
765 DPTXlatorDateTime translatorDateTime = (DPTXlatorDateTime) translator;
766 if (translatorDateTime.isFaultyClock()) {
767 // Not supported: faulty clock
768 logger.debug("toType: KNX clock msg ignored: clock faulty bit set, which is not supported");
770 } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
771 && translatorDateTime.isValidField(DPTXlatorDateTime.DATE)) {
772 // Not supported: "/1/1" (month and day without year)
774 "toType: KNX clock msg ignored: no year, but day and month, which is not supported");
776 } else if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
777 && !translatorDateTime.isValidField(DPTXlatorDateTime.DATE)) {
778 // Not supported: "1900" (year without month and day)
780 "toType: KNX clock msg ignored: no day and month, but year, which is not supported");
782 } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
783 && !translatorDateTime.isValidField(DPTXlatorDateTime.DATE)
784 && !translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
785 // Not supported: No year, no date and no time
786 logger.debug("toType: KNX clock msg ignored: no day and month or year, which is not supported");
790 Calendar cal = Calendar.getInstance();
791 if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
792 && !translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
793 // Pure date format, no time information
794 cal.setTimeInMillis(translatorDateTime.getValueMilliseconds());
795 value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
796 return DateTimeType.valueOf(value);
797 } else if (!translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
798 && translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
799 // Pure time format, no date information
801 cal.set(Calendar.HOUR_OF_DAY, translatorDateTime.getHour());
802 cal.set(Calendar.MINUTE, translatorDateTime.getMinute());
803 cal.set(Calendar.SECOND, translatorDateTime.getSecond());
804 value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
805 return DateTimeType.valueOf(value);
806 } else if (translatorDateTime.isValidField(DPTXlatorDateTime.YEAR)
807 && translatorDateTime.isValidField(DPTXlatorDateTime.TIME)) {
808 // Date format and time information
809 cal.setTimeInMillis(translatorDateTime.getValueMilliseconds());
810 value = new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(cal.getTime());
811 return DateTimeType.valueOf(value);
816 Class<? extends Type> typeClass = toTypeClass(id);
817 if (typeClass == null) {
821 if (typeClass.equals(PercentType.class)) {
822 return new PercentType(BigDecimal.valueOf(Math.round(translator.getNumericValue())));
824 if (typeClass.equals(DecimalType.class)) {
825 return new DecimalType(translator.getNumericValue());
827 if (typeClass.equals(StringType.class)) {
828 return StringType.valueOf(value);
831 if (typeClass.equals(DateTimeType.class)) {
832 String date = formatDateTime(value, datapoint.getDPT());
833 if ((date == null) || (date.isEmpty())) {
834 logger.debug("toType: KNX clock msg ignored: date object null or empty {}.", date);
837 return DateTimeType.valueOf(date);
841 if (typeClass.equals(HSBType.class)) {
842 // value has format of "r:<red value> g:<green value> b:<blue value>"
843 int r = Integer.parseInt(value.split(" ")[0].split(":")[1]);
844 int g = Integer.parseInt(value.split(" ")[1].split(":")[1]);
845 int b = Integer.parseInt(value.split(" ")[2].split(":")[1]);
847 return HSBType.fromRGB(r, g, b);
850 } catch (KNXFormatException kfe) {
851 logger.info("Translator couldn't parse data for datapoint type '{}' (KNXFormatException).",
853 } catch (KNXIllegalArgumentException kiae) {
854 logger.info("Translator couldn't parse data for datapoint type '{}' (KNXIllegalArgumentException).",
856 } catch (KNXException e) {
857 logger.warn("Failed creating a translator for datapoint type '{}'.", datapoint.getDPT(), e);
864 * Converts a datapoint type id into an openHAB type class
866 * @param dptId the datapoint type id
867 * @return the openHAB type (command or state) class or {@code null} if the datapoint type id is not supported.
870 public Class<? extends Type> toTypeClass(String dptId) {
871 Class<? extends Type> ohClass = dptTypeMap.get(dptId);
872 if (ohClass == null) {
873 int mainNumber = getMainNumber(dptId);
874 if (mainNumber == -1) {
875 logger.debug("Couldn't convert KNX datapoint type id into openHAB type class for dptId: {}.", dptId);
878 ohClass = dptMainTypeMap.get(mainNumber);
884 * Converts an openHAB type class into a datapoint type id.
886 * @param typeClass the openHAB type class
887 * @return the datapoint type id
889 public String toDPTid(Class<? extends Type> typeClass) {
890 return defaultDptMap.get(typeClass);
894 * Formats the given <code>value</code> according to the datapoint type
895 * <code>dpt</code> to a String which can be processed by {@link DateTimeType}.
900 * @return a formatted String like </code>yyyy-MM-dd'T'HH:mm:ss</code> which
901 * is target format of the {@link DateTimeType}
903 private String formatDateTime(String value, String dpt) {
907 if (DPTXlatorDate.DPT_DATE.getID().equals(dpt)) {
908 date = new SimpleDateFormat(DATE_FORMAT).parse(value);
909 } else if (DPTXlatorTime.DPT_TIMEOFDAY.getID().equals(dpt)) {
910 if (value.contains("no-day")) {
912 * KNX "no-day" needs special treatment since openHAB's DateTimeType doesn't support "no-day".
913 * Workaround: remove the "no-day" String, parse the remaining time string, which will result in a
914 * date of "1970-01-01".
915 * Replace "no-day" with the current day name
917 StringBuffer stb = new StringBuffer(value);
918 int start = stb.indexOf("no-day");
919 int end = start + "no-day".length();
920 stb.replace(start, end, String.format(Locale.US, "%1$ta", Calendar.getInstance()));
921 value = stb.toString();
923 date = new SimpleDateFormat(TIME_DAY_FORMAT, Locale.US).parse(value);
925 } catch (ParseException pe) {
926 // do nothing but logging
927 logger.warn("Could not parse '{}' to a valid date", value);
930 return date != null ? new SimpleDateFormat(DateTimeType.DATE_PATTERN).format(date) : "";
934 * Formats the given internal <code>dateType</code> to a knx readable String
935 * according to the target datapoint type <code>dpt</code>.
938 * @param dpt the target datapoint type
940 * @return a String which contains either an ISO8601 formatted date (yyyy-mm-dd),
941 * a formatted 24-hour clock with the day of week prepended (Mon, 12:00:00) or
942 * a formatted 24-hour clock (12:00:00)
944 * @throws IllegalArgumentException if none of the datapoint types DPT_DATE or
945 * DPT_TIMEOFDAY has been used.
947 private static String formatDateTime(DateTimeType dateType, String dpt) {
948 if (DPTXlatorDate.DPT_DATE.getID().equals(dpt)) {
949 return dateType.format("%tF");
950 } else if (DPTXlatorTime.DPT_TIMEOFDAY.getID().equals(dpt)) {
951 return dateType.format(Locale.US, "%1$ta, %1$tT");
952 } else if (DPTXlatorDateTime.DPT_DATE_TIME.getID().equals(dpt)) {
953 return dateType.format(Locale.US, "%tF %1$tT");
955 throw new IllegalArgumentException("Could not format date to datapoint type '" + dpt + "'");
960 * Retrieves sub number from a DTP ID such as "14.001"
962 * @param dptID String with DPT ID
963 * @return sub number or -1
965 private int getSubNumber(String dptID) {
968 throw new IllegalArgumentException("Parameter dptID cannot be null");
971 int dptSepratorPosition = dptID.indexOf('.');
972 if (dptSepratorPosition > 0) {
974 result = Integer.parseInt(dptID.substring(dptSepratorPosition + 1, dptID.length()));
975 } catch (NumberFormatException nfe) {
976 logger.error("toType couldn't identify main and/or sub number in dptID (NumberFormatException): {}",
978 } catch (IndexOutOfBoundsException ioobe) {
979 logger.error("toType couldn't identify main and/or sub number in dptID (IndexOutOfBoundsException): {}",
987 * Retrieves main number from a DTP ID such as "14.001"
989 * @param dptID String with DPT ID
990 * @return main number or -1
992 private int getMainNumber(String dptID) {
995 throw new IllegalArgumentException("Parameter dptID cannot be null");
998 int dptSepratorPosition = dptID.indexOf('.');
999 if (dptSepratorPosition > 0) {
1001 result = Integer.parseInt(dptID.substring(0, dptSepratorPosition));
1002 } catch (NumberFormatException nfe) {
1003 logger.error("toType couldn't identify main and/or sub number in dptID (NumberFormatException): {}",
1005 } catch (IndexOutOfBoundsException ioobe) {
1006 logger.error("toType couldn't identify main and/or sub number in dptID (IndexOutOfBoundsException): {}",
1014 * convert 0...100% to 1 byte 0..255
1017 * @return int 0..255
1019 private int convertPercentToByte(PercentType percent) {
1020 return percent.toBigDecimal().multiply(BigDecimal.valueOf(255))
1021 .divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP).intValue();