]> git.basschouten.com Git - openhab-addons.git/blob
995bfbd79dea21ffaf49cb43a4ab0810af2d73a0
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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         if (!isValid()) {
75             return UnDefType.UNDEF;
76         }
77
78         if (channelId.equals(CHANNEL_TEMPERATURE)) {
79             double scaledTemp = getScaledTemperatureMin()
80                     + ((getUnscaledTemperatureValue() * (getScaledTemperatureMax() - getScaledTemperatureMin()))
81                             / (getUnscaledTemperatureMax() - getUnscaledTemperatureMin()));
82             return new QuantityType<>(scaledTemp, SIUnits.CELSIUS);
83         } else if (channelId.equals(CHANNEL_ILLUMINATION)) {
84             double scaledIllumination = getScaledIlluminationMin()
85                     + ((getUnscaledIlluminationValue() * (getScaledIlluminationMax() - getScaledIlluminationMin()))
86                             / (getUnscaledIlluminationMax() - getUnscaledIlluminationMin()));
87             return new QuantityType<>(scaledIllumination, Units.LUX);
88         } else if (channelId.equals(CHANNEL_MOTIONDETECTION)) {
89             return getBit(getDB_0(), 1) ? OnOffType.OFF : OnOffType.ON;
90         } else if (channelId.equals(CHANNEL_OCCUPANCY)) {
91             return getBit(getDB_0(), 0) ? OnOffType.OFF : OnOffType.ON;
92         }
93
94         return UnDefType.UNDEF;
95     }
96 }