]> git.basschouten.com Git - openhab-addons.git/blob
8fb3dc61ee0b2399437ac5ee6f5ec8afc4dd0d95
[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.A5_10;
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.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.enocean.internal.Helper;
22 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
23 import org.openhab.binding.enocean.internal.messages.ERP1Message;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.types.State;
32 import org.openhab.core.types.UnDefType;
33
34 /**
35  * From A5_10_01 up to A5_10_0D temperature is given as a 8Bit value (range: 255..0).
36  * Therefore higher values mean lower temperatures.
37  * Temperature range 0..40.
38  * 
39  * @author Daniel Weber - Initial contribution
40  */
41 @NonNullByDefault
42 public abstract class A5_10 extends _4BSMessage {
43
44     public A5_10(ERP1Message packet) {
45         super(packet);
46     }
47
48     protected int getSetPointValue() {
49         // this is the default one
50         return getDB2Value();
51     }
52
53     protected double getMinTemperatureValue() {
54         return 0.0;
55     }
56
57     protected double getMinUnscaledTemperatureValue() {
58         return 255.0;
59     }
60
61     protected double getMaxTemperatureValue() {
62         return 40.0;
63     }
64
65     protected double getMaxUnscaledTemperatureValue() {
66         return 0.0;
67     }
68
69     protected double getTemperatureValue() {
70         return getDB1Value();
71     }
72
73     protected State getTemperature() {
74         return new QuantityType<>(
75                 Helper.scaleValue(getTemperatureValue(), getMinUnscaledTemperatureValue(),
76                         getMaxUnscaledTemperatureValue(), getMinTemperatureValue(), getMaxTemperatureValue()),
77                 SIUnits.CELSIUS);
78     }
79
80     protected State getFanSpeedStage() {
81         if (getDB3Value() > 209) {
82             return new DecimalType(-1);
83         } else if (getDB3Value() > 189) {
84             return new DecimalType(0);
85         } else if (getDB3Value() > 164) {
86             return new DecimalType(1);
87         } else if (getDB3Value() > 144) {
88             return new DecimalType(2);
89         } else {
90             return new DecimalType(3);
91         }
92     }
93
94     protected int getIlluminationValue() {
95         return getDB3Value();
96     }
97
98     protected State getIllumination() {
99         return new QuantityType<>(getIlluminationValue() * 4, Units.LUX);
100     }
101
102     protected double getHumidityValue() {
103         return getDB2Value();
104     }
105
106     protected State getSupplyVoltage() {
107         double voltage = ((double) getDB3Value()) / 50.0;
108         return new QuantityType<>(voltage, Units.VOLT);
109     }
110
111     @Override
112     protected State convertToStateImpl(String channelId, String channelTypeId,
113             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
114         switch (channelId) {
115             case CHANNEL_BATTERY_VOLTAGE:
116                 return getSupplyVoltage();
117
118             case CHANNEL_ILLUMINATION:
119                 return getIllumination();
120
121             case CHANNEL_FANSPEEDSTAGE:
122                 return getFanSpeedStage();
123
124             case CHANNEL_SETPOINT:
125                 return new DecimalType(getSetPointValue());
126
127             case CHANNEL_HUMIDITY:
128                 return new DecimalType(getHumidityValue() / 2.5);
129
130             case CHANNEL_TEMPERATURE:
131                 return getTemperature();
132
133             case CHANNEL_BATTERYLOW:
134                 return OnOffType.from(getBit(getDB0(), 4));
135
136             case CHANNEL_OCCUPANCY:
137                 return OnOffType.from(!getBit(getDB0(), 0));
138
139             case CHANNEL_DAYNIGHTMODESTATE:
140                 return new DecimalType(getDB0Value() & 0x01);
141
142             case CHANNEL_CONTACT:
143                 return getBit(getDB0(), 0) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
144
145         }
146
147         return UnDefType.UNDEF;
148     }
149 }