]> git.basschouten.com Git - openhab-addons.git/blob
a8eb2b25d22f7883ba641f5a9b489cf80f7391be
[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_08;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.function.Function;
18
19 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
20 import org.openhab.binding.enocean.internal.messages.ERP1Message;
21 import org.openhab.core.config.core.Configuration;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.QuantityType;
24 import org.openhab.core.library.unit.SIUnits;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28
29 /**
30  *
31  * @author Daniel Weber - Initial contribution
32  */
33 public abstract class A5_08 extends _4BSMessage {
34
35     public A5_08(ERP1Message packet) {
36         super(packet);
37     }
38
39     protected double getUnscaledTemperatureMin() {
40         return 0;
41     }
42
43     protected double getUnscaledTemperatureMax() {
44         return 255;
45     }
46
47     protected double getUnscaledIlluminationMin() {
48         return 0;
49     }
50
51     protected double getUnscaledIlluminationMax() {
52         return 255;
53     }
54
55     protected abstract double getScaledTemperatureMin();
56
57     protected abstract double getScaledTemperatureMax();
58
59     protected abstract double getScaledIlluminationMin();
60
61     protected abstract double getScaledIlluminationMax();
62
63     protected int getUnscaledTemperatureValue() {
64         return getDB_1Value();
65     }
66
67     protected int getUnscaledIlluminationValue() {
68         return getDB_2Value();
69     }
70
71     @Override
72     protected State convertToStateImpl(String channelId, String channelTypeId,
73             Function<String, State> getCurrentStateFunc, Configuration config) {
74
75         if (channelId.equals(CHANNEL_TEMPERATURE)) {
76             double scaledTemp = getScaledTemperatureMin()
77                     + ((getUnscaledTemperatureValue() * (getScaledTemperatureMax() - getScaledTemperatureMin()))
78                             / (getUnscaledTemperatureMax() - getUnscaledTemperatureMin()));
79             return new QuantityType<>(scaledTemp, SIUnits.CELSIUS);
80         } else if (channelId.equals(CHANNEL_ILLUMINATION)) {
81             double scaledIllumination = getScaledIlluminationMin()
82                     + ((getUnscaledIlluminationValue() * (getScaledIlluminationMax() - getScaledIlluminationMin()))
83                             / (getUnscaledIlluminationMax() - getUnscaledIlluminationMin()));
84             return new QuantityType<>(scaledIllumination, Units.LUX);
85         } else if (channelId.equals(CHANNEL_MOTIONDETECTION)) {
86             return getBit(getDB_0(), 1) ? OnOffType.OFF : OnOffType.ON;
87         } else if (channelId.equals(CHANNEL_OCCUPANCY)) {
88             return getBit(getDB_0(), 0) ? OnOffType.OFF : OnOffType.ON;
89         }
90
91         return UnDefType.UNDEF;
92     }
93 }