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