]> git.basschouten.com Git - openhab-addons.git/blob
275c410606afebd07843e63d41576b9209a91659
[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.satel.internal.command;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.satel.internal.event.EventDispatcher;
17 import org.openhab.binding.satel.internal.event.ZoneTemperatureEvent;
18 import org.openhab.binding.satel.internal.protocol.SatelMessage;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Command class for command that reads temperature in a zone.
24  *
25  * @author Krzysztof Goworek - Initial contribution
26  */
27 @NonNullByDefault
28 public class ReadZoneTemperature extends SatelCommandBase {
29
30     private final Logger logger = LoggerFactory.getLogger(this.getClass());
31
32     public static final byte COMMAND_CODE = (byte) 0x7d;
33
34     /**
35      * Creates new command class instance to read temperature in given zone.
36      *
37      * @param zoneNbr zone number, 1 ... 256
38      */
39     public ReadZoneTemperature(int zoneNbr) {
40         super(COMMAND_CODE, new byte[] { (byte) (zoneNbr == 256 ? 0 : zoneNbr) });
41     }
42
43     /**
44      * Returns zone temperature (Celsius degrees).
45      *
46      * @return zone temperature
47      */
48     public float getTemperature() {
49         final byte[] payload = getResponse().getPayload();
50         int temp = ((payload[1] & 0xff) << 8) + (payload[2] & 0xff);
51         return (temp - 110) / 2.0f;
52     }
53
54     @Override
55     protected boolean isResponseValid(SatelMessage response) {
56         // validate response
57         if (response.getPayload().length != 3) {
58             logger.debug("Invalid payload length: {}", response.getPayload().length);
59             return false;
60         }
61         return true;
62     }
63
64     @Override
65     protected void handleResponseInternal(final EventDispatcher eventDispatcher) {
66         // dispatch temperature event
67         int zoneNbr = getResponse().getPayload()[0];
68         eventDispatcher.dispatchEvent(new ZoneTemperatureEvent(zoneNbr == 0 ? 256 : zoneNbr, getTemperature()));
69     }
70 }