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