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