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