]> git.basschouten.com Git - openhab-addons.git/blob
600253be496274307d4bfa4471f47cfa33e5b359
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.bluetooth.daikinmadoka.internal.model.commands;
14
15 import java.nio.ByteOrder;
16 import java.util.concurrent.Executor;
17
18 import javax.measure.quantity.Time;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaMessage;
23 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaParsingException;
24 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.MadokaValue;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.unit.Units;
27 import org.openhab.core.util.HexUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * This command returns the operating hours of internal unit
33  *
34  * @author Benjamin Lafois - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 public class GetOperationHoursCommand extends BRC1HCommand {
39
40     private final Logger logger = LoggerFactory.getLogger(GetOperationHoursCommand.class);
41
42     private @Nullable QuantityType<Time> indoorOperationHours;
43     private @Nullable QuantityType<Time> indoorFanHours;
44     private @Nullable QuantityType<Time> indoorPowerHours;
45
46     @Override
47     public byte[][] getRequest() {
48         MadokaValue specificUnitNumber = new MadokaValue(0x02, 1, new byte[] { (byte) 0x00 });
49         MadokaValue p40 = new MadokaValue(0x40, 0, new byte[] {});
50         MadokaValue p41 = new MadokaValue(0x41, 0, new byte[] {});
51         MadokaValue p42 = new MadokaValue(0x42, 0, new byte[] {});
52         MadokaValue p43 = new MadokaValue(0x43, 0, new byte[] {});
53         MadokaValue p44 = new MadokaValue(0x44, 0, new byte[] {});
54         MadokaValue p45 = new MadokaValue(0x45, 0, new byte[] {});
55         MadokaValue p46 = new MadokaValue(0x46, 0, new byte[] {});
56         MadokaValue p47 = new MadokaValue(0x47, 0, new byte[] {});
57         MadokaValue p48 = new MadokaValue(0x48, 0, new byte[] {});
58
59         return MadokaMessage.createRequest(this, specificUnitNumber, p40, p41, p42, p43, p44, p45, p46, p47, p48);
60     }
61
62     @Override
63     public void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
64             throws MadokaParsingException {
65         try {
66
67             byte[] msg = mm.getRawMessage();
68             if (logger.isDebugEnabled() && msg != null) {
69                 logger.debug("Got response for {} : {}", this.getClass().getSimpleName(), HexUtils.bytesToHex(msg));
70             }
71
72             // The specific GetOperationHours requires 2 consecutive runs for some reason.
73             // If value size is 0, then it will be for the next query!
74             if (mm.getValues().get(0x40).getSize() == 0) {
75                 setState(State.SUCCEEDED);
76                 return;
77             }
78
79             Integer iIndoorOperationHours = (int) (mm.getValues().get(0x40).getComputedValue(ByteOrder.LITTLE_ENDIAN));
80             Integer iIndoorFanHours = (int) (mm.getValues().get(0x41).getComputedValue(ByteOrder.LITTLE_ENDIAN));
81             Integer iIndoorPowerHours = (int) (mm.getValues().get(0x42).getComputedValue(ByteOrder.LITTLE_ENDIAN));
82
83             this.indoorOperationHours = new QuantityType<Time>(iIndoorOperationHours, Units.HOUR);
84             this.indoorFanHours = new QuantityType<Time>(iIndoorFanHours, Units.HOUR);
85             this.indoorPowerHours = new QuantityType<Time>(iIndoorPowerHours, Units.HOUR);
86
87             logger.debug("indoorOperationHours: {}", indoorOperationHours);
88             logger.debug("indoorFanHours: {}", indoorFanHours);
89             logger.debug("indoorPowerHours: {}", indoorPowerHours);
90
91             setState(State.SUCCEEDED);
92             executor.execute(() -> listener.receivedResponse(this));
93         } catch (Exception e) {
94             setState(State.FAILED);
95             throw new MadokaParsingException(e);
96         }
97     }
98
99     @Override
100     public int getCommandId() {
101         return 274;
102     }
103
104     public @Nullable QuantityType<Time> getIndoorOperationHours() {
105         return indoorOperationHours;
106     }
107
108     public @Nullable QuantityType<Time> getIndoorFanHours() {
109         return indoorFanHours;
110     }
111
112     public @Nullable QuantityType<Time> getIndoorPowerHours() {
113         return indoorPowerHours;
114     }
115 }