]> git.basschouten.com Git - openhab-addons.git/blob
f082b926eaef3d4e751628828bddc8b41e218792
[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.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.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * This command returns the operating hours of internal unit
32  *
33  * @author Benjamin Lafois - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class GetOperationHoursCommand extends BRC1HCommand {
38
39     private final Logger logger = LoggerFactory.getLogger(GetOperationHoursCommand.class);
40
41     private @Nullable QuantityType<Time> indoorOperationHours;
42     private @Nullable QuantityType<Time> indoorFanHours;
43     private @Nullable QuantityType<Time> indoorPowerHours;
44
45     @Override
46     public byte[][] getRequest() {
47         MadokaValue specificUnitNumber = new MadokaValue(0x02, 1, new byte[] { (byte) 0x00 });
48         MadokaValue p40 = new MadokaValue(0x40, 0, new byte[] {});
49         MadokaValue p41 = new MadokaValue(0x41, 0, new byte[] {});
50         MadokaValue p42 = new MadokaValue(0x42, 0, new byte[] {});
51         MadokaValue p43 = new MadokaValue(0x43, 0, new byte[] {});
52         MadokaValue p44 = new MadokaValue(0x44, 0, new byte[] {});
53         MadokaValue p45 = new MadokaValue(0x45, 0, new byte[] {});
54         MadokaValue p46 = new MadokaValue(0x46, 0, new byte[] {});
55         MadokaValue p47 = new MadokaValue(0x47, 0, new byte[] {});
56         MadokaValue p48 = new MadokaValue(0x48, 0, new byte[] {});
57
58         return MadokaMessage.createRequest(this, specificUnitNumber, p40, p41, p42, p43, p44, p45, p46, p47, p48);
59     }
60
61     @Override
62     public void handleResponse(Executor executor, ResponseListener listener, MadokaMessage mm)
63             throws MadokaParsingException {
64         MadokaValue opHours = mm.getValues().get(0x40);
65         MadokaValue fanHours = mm.getValues().get(0x41);
66         MadokaValue powerHours = mm.getValues().get(0x42);
67         if (opHours == null || fanHours == null || powerHours == null) {
68             String message = "indoorOperationHours, indoorFanHours or indoorPowerHours is null when handling the response";
69             setState(State.FAILED);
70             throw new MadokaParsingException(message);
71         }
72
73         if (opHours.getSize() == 0) {
74             setState(State.SUCCEEDED);
75             return;
76         }
77
78         Integer iIndoorOperationHours = (int) (opHours.getComputedValue(ByteOrder.LITTLE_ENDIAN));
79         Integer iIndoorFanHours = (int) (fanHours.getComputedValue(ByteOrder.LITTLE_ENDIAN));
80         Integer iIndoorPowerHours = (int) (powerHours.getComputedValue(ByteOrder.LITTLE_ENDIAN));
81
82         this.indoorOperationHours = new QuantityType<Time>(iIndoorOperationHours, Units.HOUR);
83         this.indoorFanHours = new QuantityType<Time>(iIndoorFanHours, Units.HOUR);
84         this.indoorPowerHours = new QuantityType<Time>(iIndoorPowerHours, Units.HOUR);
85
86         logger.debug("indoorOperationHours: {}", indoorOperationHours);
87         logger.debug("indoorFanHours: {}", indoorFanHours);
88         logger.debug("indoorPowerHours: {}", indoorPowerHours);
89
90         setState(State.SUCCEEDED);
91         try {
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 }