]> git.basschouten.com Git - openhab-addons.git/blob
3775eff16d60ed6cd00f7f905e36c9029517b121
[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.D2_01;
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.EnOceanChannelDimmerConfig;
22 import org.openhab.binding.enocean.internal.eep.Base._VLDMessage;
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.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.library.types.QuantityType;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
36 import org.openhab.core.util.HexUtils;
37
38 /**
39  *
40  * @author Daniel Weber - Initial contribution
41  */
42 @NonNullByDefault
43 public abstract class D2_01 extends _VLDMessage {
44
45     protected static final byte CMD_MASK = 0x0f;
46     protected static final byte OUTPUT_VALUE_MASK = 0x7f;
47     protected static final byte OUTPUT_CHANNEL_MASK = 0x1f;
48
49     protected static final byte CMD_ACTUATOR_SET_STATUS = 0x01;
50     protected static final byte CMD_ACTUATOR_STATUS_QUERY = 0x03;
51     protected static final byte CMD_ACTUATOR_STATUS_RESPONE = 0x04;
52     protected static final byte CMD_ACTUATOR_MEASUREMENT_QUERY = 0x06;
53     protected static final byte CMD_ACTUATOR_MEASUREMENT_RESPONE = 0x07;
54
55     protected static final byte ALL_CHANNELS_MASK = 0x1e;
56     protected static final byte CHANNEL_A_MASK = 0x00;
57     protected static final byte CHANNEL_B_MASK = 0x01;
58
59     protected static final byte STATUS_SWITCHING_ON = 0x01;
60     protected static final byte STATUS_SWITCHING_OFF = 0x00;
61     protected static final byte STATUS_DIMMING_100 = 0x64;
62
63     public D2_01() {
64         super();
65     }
66
67     public D2_01(ERP1Message packet) {
68         super(packet);
69     }
70
71     protected byte getCMD() {
72         return (byte) (bytes[0] & CMD_MASK);
73     }
74
75     protected void setSwitchingData(OnOffType command, byte outputChannel) {
76         if (command == OnOffType.ON) {
77             setData(CMD_ACTUATOR_SET_STATUS, outputChannel, STATUS_SWITCHING_ON);
78         } else {
79             setData(CMD_ACTUATOR_SET_STATUS, outputChannel, STATUS_SWITCHING_OFF);
80         }
81     }
82
83     protected void setSwitchingQueryData(byte outputChannel) {
84         setData(CMD_ACTUATOR_STATUS_QUERY, outputChannel);
85     }
86
87     protected State getSwitchingData() {
88         if (getCMD() == CMD_ACTUATOR_STATUS_RESPONE) {
89             return (bytes[bytes.length - 1] & OUTPUT_VALUE_MASK) == STATUS_SWITCHING_OFF ? OnOffType.OFF : OnOffType.ON;
90         }
91
92         return UnDefType.UNDEF;
93     }
94
95     protected byte getChannel() {
96         return (byte) (bytes[1] & OUTPUT_CHANNEL_MASK);
97     }
98
99     protected State getSwitchingData(byte channel) {
100         if (getCMD() == CMD_ACTUATOR_STATUS_RESPONE && (getChannel() == channel || getChannel() == ALL_CHANNELS_MASK)) {
101             return (bytes[bytes.length - 1] & OUTPUT_VALUE_MASK) == STATUS_SWITCHING_OFF ? OnOffType.OFF : OnOffType.ON;
102         }
103
104         return UnDefType.UNDEF;
105     }
106
107     protected void setDimmingData(Command command, byte outputChannel, Configuration config) {
108         byte outputValue;
109
110         if (command instanceof DecimalType decimalCommand) {
111             if (decimalCommand.equals(DecimalType.ZERO)) {
112                 outputValue = STATUS_SWITCHING_OFF;
113             } else {
114                 outputValue = decimalCommand.byteValue();
115             }
116         } else if ((OnOffType) command == OnOffType.ON) {
117             outputValue = STATUS_DIMMING_100;
118         } else {
119             outputValue = STATUS_SWITCHING_OFF;
120         }
121
122         EnOceanChannelDimmerConfig c = config.as(EnOceanChannelDimmerConfig.class);
123         byte rampingTime = Integer.valueOf(c.rampingTime).byteValue();
124
125         setData(CMD_ACTUATOR_SET_STATUS, (byte) ((rampingTime << 5) | outputChannel), outputValue);
126     }
127
128     protected State getDimmingData() {
129         if (getCMD() == CMD_ACTUATOR_STATUS_RESPONE) {
130             return new PercentType((bytes[bytes.length - 1] & OUTPUT_VALUE_MASK));
131         }
132
133         return UnDefType.UNDEF;
134     }
135
136     protected void setEnergyMeasurementQueryData(byte outputChannel) {
137         setData(CMD_ACTUATOR_MEASUREMENT_QUERY, outputChannel);
138     }
139
140     protected void setPowerMeasurementQueryData(byte outputChannel) {
141         setData(CMD_ACTUATOR_MEASUREMENT_QUERY, (byte) (0x20 | outputChannel));
142     }
143
144     protected State getEnergyMeasurementData() {
145         if (getCMD() == CMD_ACTUATOR_MEASUREMENT_RESPONE) {
146             float factor = 1;
147
148             switch (bytes[1] >>> 5) {
149                 case 0: // value is given as watt seconds, so divide it by 3600 to get watt hours, and 1000 to get
150                         // kilowatt hours
151                     factor /= (3600 * 1000);
152                     break;
153                 case 1: // value is given as watt hours, so divide it by 1000 to get kilowatt hours
154                     factor /= 1000;
155                     break;
156                 case 2: // value is given as kilowatt hours
157                     factor = 1;
158                     break;
159                 default:
160                     return UnDefType.UNDEF;
161             }
162
163             float energy = Long.parseLong(HexUtils.bytesToHex(new byte[] { bytes[2], bytes[3], bytes[4], bytes[5] }),
164                     16) * factor;
165             return new QuantityType<>(energy, Units.KILOWATT_HOUR);
166         }
167
168         return UnDefType.UNDEF;
169     }
170
171     protected State getPowerMeasurementData() {
172         if (getCMD() == CMD_ACTUATOR_MEASUREMENT_RESPONE) {
173             float factor = 1;
174
175             switch (bytes[1] >>> 5) {
176                 case 3: // value is given as watt
177                     factor = 1;
178                     break;
179                 case 4: // value is given as kilowatt
180                     factor /= 1000;
181                     break;
182                 default:
183                     return UnDefType.UNDEF;
184             }
185
186             float power = Long.parseLong(HexUtils.bytesToHex(new byte[] { bytes[2], bytes[3], bytes[4], bytes[5] }), 16)
187                     * factor;
188
189             return new QuantityType<>(power, Units.WATT);
190         }
191
192         return UnDefType.UNDEF;
193     }
194
195     @Override
196     public void addConfigPropertiesTo(DiscoveryResultBuilder discoveredThingResultBuilder) {
197         discoveredThingResultBuilder.withProperty(PARAMETER_SENDINGEEPID, getEEPType().getId())
198                 .withProperty(PARAMETER_RECEIVINGEEPID, getEEPType().getId());
199     }
200
201     @Override
202     protected void convertFromCommandImpl(String channelId, String channelTypeId, Command command,
203             Function<String, State> getCurrentStateFunc, @Nullable Configuration config) {
204         if (channelId.equals(CHANNEL_GENERAL_SWITCHING)) {
205             if (command == RefreshType.REFRESH) {
206                 setSwitchingQueryData(ALL_CHANNELS_MASK);
207             } else {
208                 setSwitchingData((OnOffType) command, ALL_CHANNELS_MASK);
209             }
210         } else if (channelId.equals(CHANNEL_GENERAL_SWITCHINGA)) {
211             if (command == RefreshType.REFRESH) {
212                 setSwitchingQueryData(CHANNEL_A_MASK);
213             } else {
214                 setSwitchingData((OnOffType) command, CHANNEL_A_MASK);
215             }
216         } else if (channelId.equals(CHANNEL_GENERAL_SWITCHINGB)) {
217             if (command == RefreshType.REFRESH) {
218                 setSwitchingQueryData(CHANNEL_B_MASK);
219             } else {
220                 setSwitchingData((OnOffType) command, CHANNEL_B_MASK);
221             }
222         } else if (channelId.equals(CHANNEL_DIMMER)) {
223             if (command == RefreshType.REFRESH) {
224                 setSwitchingQueryData(ALL_CHANNELS_MASK);
225             } else {
226                 if (config != null) {
227                     setDimmingData(command, ALL_CHANNELS_MASK, config);
228                 } else {
229                     logger.error("Cannot set dimming data when config is null");
230                 }
231             }
232         } else if (channelId.equals(CHANNEL_INSTANTPOWER) && command == RefreshType.REFRESH) {
233             setPowerMeasurementQueryData(ALL_CHANNELS_MASK);
234         } else if (channelId.equals(CHANNEL_TOTALUSAGE) && command == RefreshType.REFRESH) {
235             setEnergyMeasurementQueryData(ALL_CHANNELS_MASK);
236         }
237     }
238
239     @Override
240     protected State convertToStateImpl(String channelId, String channelTypeId,
241             Function<String, @Nullable State> getCurrentStateFunc, Configuration config) {
242         switch (channelId) {
243             case CHANNEL_GENERAL_SWITCHING:
244                 return getSwitchingData();
245             case CHANNEL_GENERAL_SWITCHINGA:
246                 return getSwitchingData(CHANNEL_A_MASK);
247             case CHANNEL_GENERAL_SWITCHINGB:
248                 return getSwitchingData(CHANNEL_B_MASK);
249             case CHANNEL_DIMMER:
250                 return getDimmingData();
251             case CHANNEL_INSTANTPOWER:
252                 return getPowerMeasurementData();
253             case CHANNEL_TOTALUSAGE:
254                 State value = getEnergyMeasurementData();
255                 State currentState = getCurrentStateFunc.apply(channelId);
256                 return EEPHelper.validateTotalUsage(value, currentState, config);
257         }
258
259         return UnDefType.UNDEF;
260     }
261 }