]> git.basschouten.com Git - openhab-addons.git/blob
05b5297180ebb55bbc622bbf71b56f0b1f8c842f
[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_11;
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.eep.EEPHelper;
23 import org.openhab.binding.enocean.internal.messages.ERP1Message;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.unit.Units;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
32 import org.openhab.core.util.HexUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  *
38  * @author Vincent Bakker - Initial contribution
39  */
40 @NonNullByDefault
41 public class A5_11_04 extends _4BSMessage {
42
43     private enum Error {
44         NO_ERROR_PRESENT,
45         LAMP_FAILURE,
46         INTERNAL_FAILURE,
47         FAILURE_ON_THE_EXTERNAL_PERIPHERY
48     }
49
50     private enum ParameterMode {
51         EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS,
52         RGB_VALUE,
53         ENERGY_METERING_VALUE,
54         NOT_USED
55     }
56
57     public enum EnergyUnit {
58         MILLIWATT,
59         WATT,
60         KILOWATT,
61         MEGAWATT,
62         WATTHOUR,
63         KILOWATTHOUR,
64         MEGAWATTHOUR,
65         GIGAWATTHOUR,
66         NOT_SUPPORTED
67     }
68
69     private Logger logger = LoggerFactory.getLogger(A5_11_04.class);
70
71     public A5_11_04(ERP1Message packet) {
72         super(packet);
73     }
74
75     protected boolean isErrorState() {
76         byte db0 = getDB0();
77
78         int state = (db0 >> 4) & 0x03;
79
80         if (state != 0) {
81             // TODO: display error state on thing
82             logger.warn("Received error {}: {}", state, Error.values()[state]);
83             return true;
84         } else {
85             return false;
86         }
87     }
88
89     protected ParameterMode getParameterMode() {
90         int pm = (getDB0() >> 1) & 0x03;
91         return ParameterMode.values()[pm];
92     }
93
94     protected EnergyUnit getEnergyUnit() {
95         int unit = getDB1();
96         if (unit < 8) {
97             return EnergyUnit.values()[unit];
98         }
99
100         return EnergyUnit.NOT_SUPPORTED;
101     }
102
103     protected State getLightingStatus() {
104         byte db0 = getDB0();
105         boolean lightOn = getBit(db0, 0);
106
107         return lightOn ? OnOffType.ON : OnOffType.OFF;
108     }
109
110     protected State getDimmerStatus() {
111         if (getParameterMode() == ParameterMode.EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS) {
112             return new PercentType(getDB3Value() * 100 / 255);
113         }
114         return UnDefType.UNDEF;
115     }
116
117     protected State getEnergyMeasurementData() {
118         if (getParameterMode() == ParameterMode.ENERGY_METERING_VALUE) {
119             EnergyUnit unit = getEnergyUnit();
120
121             float factor = 1;
122             switch (unit) {
123                 case WATTHOUR:
124                     factor /= 1000;
125                     break;
126                 case KILOWATTHOUR:
127                     factor = 1;
128                     break;
129                 case GIGAWATTHOUR:
130                     factor *= 1000;
131                 case MEGAWATTHOUR:
132                     factor *= 1000;
133                     break;
134                 default:
135                     return UnDefType.UNDEF;
136             }
137
138             return new QuantityType<>(
139                     Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB3(), getDB2() }), 16) * factor,
140                     Units.KILOWATT_HOUR);
141         }
142
143         return UnDefType.UNDEF;
144     }
145
146     protected State getPowerMeasurementData() {
147         if (getParameterMode() == ParameterMode.ENERGY_METERING_VALUE) {
148             EnergyUnit unit = getEnergyUnit();
149
150             float factor = 1;
151             switch (unit) {
152                 case MILLIWATT:
153                     factor /= 1000;
154                     break;
155                 case WATT:
156                     factor = 1;
157                     break;
158                 case MEGAWATT:
159                     factor *= 1000;
160                 case KILOWATT:
161                     factor *= 1000;
162                     break;
163                 default:
164                     return UnDefType.UNDEF;
165             }
166
167             return new QuantityType<>(
168                     Long.parseLong(HexUtils.bytesToHex(new byte[] { getDB3(), getDB2() }), 16) * factor, Units.WATT);
169         }
170
171         return UnDefType.UNDEF;
172     }
173
174     protected State getOperatingHours() {
175         if (getParameterMode() == ParameterMode.EIGHT_BIT_DIMMER_VALUE_AND_LAMP_OPERATING_HOURS) {
176             return new DecimalType(getDB2Value() << 8 + getDB1Value());
177         }
178
179         return UnDefType.UNDEF;
180     }
181
182     @Override
183     protected State convertToStateImpl(String channelId, String channelTypeId,
184             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
185         if (isErrorState()) {
186             return UnDefType.UNDEF;
187         }
188
189         switch (channelId) {
190             case CHANNEL_GENERAL_SWITCHING:
191                 return getLightingStatus();
192             case CHANNEL_DIMMER:
193                 return getDimmerStatus();
194             case CHANNEL_INSTANTPOWER:
195                 return getPowerMeasurementData();
196             case CHANNEL_TOTALUSAGE:
197                 State value = getEnergyMeasurementData();
198                 State currentState = getCurrentStateFunc.apply(channelId);
199                 return EEPHelper.validateTotalUsage(value, currentState, config);
200             case CHANNEL_COUNTER:
201                 return getOperatingHours();
202         }
203
204         return UnDefType.UNDEF;
205     }
206 }