2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.enocean.internal.eep.D2_14;
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
17 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
21 import org.openhab.binding.enocean.internal.messages.ERP1Message;
22 import org.openhab.core.config.core.Configuration;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.library.unit.SIUnits;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
34 * @author Daniel Weber - Initial contribution
37 public class D2_14_30 extends _VLDMessage {
39 private final String[] hygroComfortIndexValues = { "GOOD", "MEDIUM", "BAD", "ERROR" };
40 private final String[] indoorAirAnalysisValues = { "OPTIMAL", "DRY", "HIGH_HUMIDITY", "HIGH_TEMPHUMI",
41 "OUT_OF_RANGE", "RESERVED1", "RESERVED2", "ERROR" };
47 public D2_14_30(ERP1Message packet) {
51 protected State getBatteryLevel() {
52 switch ((bytes[1] & 0b110) >>> 1) {
54 return new QuantityType<>(100, Units.PERCENT); // High
56 return new QuantityType<>(50, Units.PERCENT); // Medium
58 return new QuantityType<>(25, Units.PERCENT); // Low
60 return new QuantityType<>(5, Units.PERCENT); // Critical
63 return UnDefType.UNDEF;
67 public State convertToStateImpl(String channelId, String channelTypeId, Function<String, State> getCurrentStateFunc,
68 Configuration config) {
70 case CHANNEL_SMOKEDETECTION:
71 return OnOffType.from(getBit(bytes[0], 7));
72 case CHANNEL_SENSORFAULT:
73 return OnOffType.from(getBit(bytes[0], 6));
74 case CHANNEL_MAINTENANCESTATUS:
75 return OnOffType.from(getBit(bytes[0], 5));
76 case CHANNEL_SENSORANALYSISHUMIDITYRANGE:
77 return OnOffType.from(getBit(bytes[0], 4));
78 case CHANNEL_SENSORANALYSISTEMPERATURRANGE:
79 return OnOffType.from(getBit(bytes[0], 3));
80 case CHANNEL_TIMESINCELASTMAINTENANCE:
81 return new QuantityType<>(((bytes[0] << 5) + (bytes[0] >>> 3)) & 0xFF, Units.WEEK);
82 case CHANNEL_BATTERY_LEVEL:
83 return getBatteryLevel();
84 case CHANNEL_REMAININGPLT: {
85 int months = ((bytes[1] << 7) + (bytes[2] >>> 1)) & 0xFF;
86 return months < 121 ? new QuantityType<>(months * 4, Units.WEEK) : UnDefType.NULL;
88 case CHANNEL_TEMPERATURE: {
89 int unscaledValue = ((bytes[2] << 7) + (bytes[3] >>> 1)) & 0xFF;
90 return unscaledValue < 251 ? new QuantityType<>(((double) unscaledValue) / 5.0, SIUnits.CELSIUS)
93 case CHANNEL_HUMIDITY: {
94 int unscaledValue = ((bytes[3] << 7) + (bytes[4] >>> 1)) & 0xFF;
95 return unscaledValue < 251 ? new DecimalType(((double) unscaledValue) / 2.0) : UnDefType.NULL;
97 case CHANNEL_HYGROCOMFORTINDEX:
98 return new StringType(hygroComfortIndexValues[(((bytes[4] & 0b1) << 1) + (bytes[5] >>> 7))]);
99 case CHANNEL_INDOORAIRANALYSIS:
100 return new StringType(indoorAirAnalysisValues[(bytes[5] >>> 4) & 0b111]);
103 return UnDefType.UNDEF;
107 protected boolean validateData(byte[] bytes) {
108 return bytes.length == 6;