]> git.basschouten.com Git - openhab-addons.git/blob
f97c9e7041c27507155a879e77effe202ceacdf4
[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
115         switch (channelId) {
116
117             case CHANNEL_BATTERY_VOLTAGE:
118                 return getSupplyVoltage();
119
120             case CHANNEL_ILLUMINATION:
121                 return getIllumination();
122
123             case CHANNEL_FANSPEEDSTAGE:
124                 return getFanSpeedStage();
125
126             case CHANNEL_SETPOINT:
127                 return new DecimalType(getSetPointValue());
128
129             case CHANNEL_HUMIDITY:
130                 return new DecimalType(getHumidityValue() / 2.5);
131
132             case CHANNEL_TEMPERATURE:
133                 return getTemperature();
134
135             case CHANNEL_BATTERYLOW:
136                 return getBit(getDB0(), 4) ? OnOffType.ON : OnOffType.OFF;
137
138             case CHANNEL_OCCUPANCY:
139                 return getBit(getDB0(), 0) ? OnOffType.OFF : OnOffType.ON;
140
141             case CHANNEL_DAYNIGHTMODESTATE:
142                 return new DecimalType(getDB0Value() & 0x01);
143
144             case CHANNEL_CONTACT:
145                 return getBit(getDB0(), 0) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
146
147         }
148
149         return UnDefType.UNDEF;
150     }
151 }