]> git.basschouten.com Git - openhab-addons.git/blob
2674a27a3727e150e352403ffd4346b6240c4ad2
[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.enocean.internal.eep.D2_14;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
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;
31
32 /**
33  *
34  * @author Daniel Weber - Initial contribution
35  */
36 @NonNullByDefault
37 public class D2_14_30 extends _VLDMessage {
38
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" };
42
43     public D2_14_30() {
44         super();
45     }
46
47     public D2_14_30(ERP1Message packet) {
48         super(packet);
49     }
50
51     protected State getBatteryLevel() {
52         switch ((bytes[1] & 0b110) >>> 1) {
53             case 0:
54                 return new QuantityType<>(100, Units.PERCENT); // High
55             case 1:
56                 return new QuantityType<>(50, Units.PERCENT); // Medium
57             case 2:
58                 return new QuantityType<>(25, Units.PERCENT); // Low
59             case 3:
60                 return new QuantityType<>(5, Units.PERCENT); // Critical
61         }
62
63         return UnDefType.UNDEF;
64     }
65
66     @Override
67     public State convertToStateImpl(String channelId, String channelTypeId, Function<String, State> getCurrentStateFunc,
68             Configuration config) {
69         switch (channelId) {
70             case CHANNEL_SMOKEDETECTION:
71                 return getBit(bytes[0], 7) ? OnOffType.ON : OnOffType.OFF;
72             case CHANNEL_SENSORFAULT:
73                 return getBit(bytes[0], 6) ? OnOffType.ON : OnOffType.OFF;
74             case CHANNEL_MAINTENANCESTATUS:
75                 return getBit(bytes[0], 5) ? OnOffType.ON : OnOffType.OFF;
76             case CHANNEL_SENSORANALYSISHUMIDITYRANGE:
77                 return getBit(bytes[0], 4) ? OnOffType.ON : OnOffType.OFF;
78             case CHANNEL_SENSORANALYSISTEMPERATURRANGE:
79                 return getBit(bytes[0], 3) ? OnOffType.ON : OnOffType.OFF;
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;
87             }
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)
91                         : UnDefType.NULL;
92             }
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;
96             }
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]);
101         }
102
103         return UnDefType.UNDEF;
104     }
105
106     @Override
107     protected boolean validateData(byte[] bytes) {
108         return bytes.length == 6;
109     }
110 }