]> git.basschouten.com Git - openhab-addons.git/blob
5b75806bfc2422a8092187e020cdb6edb5e6b68e
[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_12;
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.config.EnOceanChannelTariffInfoConfig;
20 import org.openhab.binding.enocean.internal.eep.Base._4BSMessage;
21 import org.openhab.binding.enocean.internal.eep.EEPHelper;
22 import org.openhab.binding.enocean.internal.messages.ERP1Message;
23 import org.openhab.core.config.core.Configuration;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.openhab.core.util.HexUtils;
29
30 /**
31  *
32  * @author Dominik Krickl-Vorreiter - Initial contribution
33  */
34 public abstract class A5_12 extends _4BSMessage {
35     public A5_12(ERP1Message packet) {
36         super(packet);
37     }
38
39     protected State calcCumulativeValue(float value) {
40         return new QuantityType<>(value, Units.ONE);
41     }
42
43     protected State calcCurrentValue(float value) {
44         return new QuantityType<>(value, Units.ONE);
45     }
46
47     protected State getCumulativeValue() {
48         byte db0 = getDB_0();
49         boolean dt = getBit(db0, 2);
50
51         if (!dt) {
52             byte div = (byte) (db0 & 0x03);
53
54             float factor = 1;
55
56             switch (div) {
57                 case 0:
58                     factor = 1;
59                     break;
60                 case 1:
61                     factor /= 10;
62                     break;
63                 case 2:
64                     factor /= 100;
65                     break;
66                 case 3:
67                     factor /= 1000;
68                     break;
69                 default:
70                     return UnDefType.UNDEF;
71             }
72
73             float cumulativeValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2(), getDB_1() }),
74                     16) * factor;
75             return calcCumulativeValue(cumulativeValue);
76         }
77
78         return UnDefType.UNDEF;
79     }
80
81     protected State getCurrentValue() {
82         byte db0 = getDB_0();
83         boolean dt = getBit(db0, 2);
84
85         if (dt) {
86             byte div = (byte) (db0 & 0x03);
87
88             float factor = 1;
89
90             switch (div) {
91                 case 0:
92                     factor = 1;
93                     break;
94                 case 1:
95                     factor /= 10;
96                     break;
97                 case 2:
98                     factor /= 100;
99                     break;
100                 case 3:
101                     factor /= 1000;
102                     break;
103                 default:
104                     return UnDefType.UNDEF;
105             }
106
107             float currentValue = Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB_3(), getDB_2(), getDB_1() }), 16)
108                     * factor;
109
110             return calcCurrentValue(currentValue);
111         }
112
113         return UnDefType.UNDEF;
114     }
115
116     protected int getTariffInfo() {
117         return ((getDB_0() >>> 4) & 0xff);
118     }
119
120     @Override
121     protected State convertToStateImpl(String channelId, String channelTypeId,
122             Function<String, State> getCurrentStateFunc, Configuration config) {
123
124         EnOceanChannelTariffInfoConfig c = config.as(EnOceanChannelTariffInfoConfig.class);
125         if (c.tariff != getTariffInfo()) {
126             return UnDefType.UNDEF;
127         }
128
129         switch (channelTypeId) {
130             case CHANNEL_INSTANTPOWER:
131             case CHANNEL_CURRENTFLOW:
132             case CHANNEL_CURRENTNUMBER:
133                 return getCurrentValue();
134             case CHANNEL_TOTALUSAGE:
135                 State value = getCumulativeValue();
136                 State currentState = getCurrentStateFunc.apply(channelId);
137                 return EEPHelper.validateTotalUsage(value, currentState, config);
138             case CHANNEL_CUMULATIVEVALUE:
139             case CHANNEL_COUNTER:
140                 return getCumulativeValue();
141         }
142
143         return UnDefType.UNDEF;
144     }
145 }