2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.danfossairunit.internal;
15 import static org.openhab.binding.danfossairunit.internal.Commands.*;
17 import java.io.IOException;
18 import java.math.BigDecimal;
19 import java.math.RoundingMode;
20 import java.net.InetAddress;
21 import java.nio.charset.StandardCharsets;
22 import java.time.DateTimeException;
23 import java.time.ZoneId;
24 import java.time.ZonedDateTime;
26 import javax.measure.quantity.Dimensionless;
27 import javax.measure.quantity.Temperature;
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.openhab.core.library.types.DateTimeType;
31 import org.openhab.core.library.types.DecimalType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.PercentType;
34 import org.openhab.core.library.types.QuantityType;
35 import org.openhab.core.library.types.StringType;
36 import org.openhab.core.library.unit.SIUnits;
37 import org.openhab.core.library.unit.Units;
38 import org.openhab.core.types.Command;
41 * The {@link DanfossAirUnit} class represents the air unit device and build the commands to be sent by
42 * {@link DanfossAirUnitCommunicationController}
44 * @author Ralf Duckstein - Initial contribution
45 * @author Robert Bach - heavy refactorings
48 @SuppressWarnings("SameParameterValue")
50 public class DanfossAirUnit {
52 private final DanfossAirUnitCommunicationController communicationController;
54 public DanfossAirUnit(InetAddress inetAddr, int port) {
55 this.communicationController = new DanfossAirUnitCommunicationController(inetAddr, port);
58 public void cleanUp() {
59 this.communicationController.disconnect();
62 private boolean getBoolean(byte[] operation, byte[] register) throws IOException {
63 return communicationController.sendRobustRequest(operation, register)[0] != 0;
66 private void setSetting(byte[] register, boolean value) throws IOException {
67 setSetting(register, value ? (byte) 1 : (byte) 0);
70 private short getWord(byte[] operation, byte[] register) throws IOException {
71 byte[] resultBytes = communicationController.sendRobustRequest(operation, register);
72 return (short) ((resultBytes[0] << 8) | (resultBytes[1] & 0xFF));
75 private byte getByte(byte[] operation, byte[] register) throws IOException {
76 return communicationController.sendRobustRequest(operation, register)[0];
79 private String getString(byte[] operation, byte[] register) throws IOException {
80 // length of the string is stored in the first byte
81 byte[] result = communicationController.sendRobustRequest(operation, register);
82 return new String(result, 1, result[0], StandardCharsets.US_ASCII);
85 private void set(byte[] operation, byte[] register, byte value) throws IOException {
86 byte[] valueArray = { value };
87 communicationController.sendRobustRequest(operation, register, valueArray);
90 private void set(byte[] operation, byte[] register, short value) throws IOException {
91 communicationController.sendRobustRequest(operation, register, shortToBytes(value));
94 private byte[] shortToBytes(short s) {
95 return new byte[] { (byte) ((s & 0xFF00) >> 8), (byte) (s & 0x00FF) };
98 private short getShort(byte[] operation, byte[] register) throws IOException {
99 byte[] result = communicationController.sendRobustRequest(operation, register);
100 return (short) ((result[0] << 8) + (result[1] & 0xff));
103 private float getTemperature(byte[] operation, byte[] register)
104 throws IOException, UnexpectedResponseValueException {
105 short shortTemp = getShort(operation, register);
106 float temp = ((float) shortTemp) / 100;
107 if (temp <= -274 || temp > 100) {
108 throw new UnexpectedResponseValueException(String.format("Invalid temperature: %s", temp));
113 private ZonedDateTime getTimestamp(byte[] operation, byte[] register)
114 throws IOException, UnexpectedResponseValueException {
115 byte[] result = communicationController.sendRobustRequest(operation, register);
116 return asZonedDateTime(result);
119 private ZonedDateTime asZonedDateTime(byte[] data) throws UnexpectedResponseValueException {
120 int second = data[0];
121 int minute = data[1];
122 int hour = data[2] & 0x1f;
123 int day = data[3] & 0x1f;
125 int year = data[5] + 2000;
127 return ZonedDateTime.of(year, month, day, hour, minute, second, 0, ZoneId.systemDefault());
128 } catch (DateTimeException e) {
129 String msg = String.format("Ignoring invalid timestamp %s.%s.%s %s:%s:%s", day, month, year, hour, minute,
131 throw new UnexpectedResponseValueException(msg, e);
135 private static int asUnsignedByte(byte b) {
139 private static float asPercentByte(byte b) {
140 float f = asUnsignedByte(b);
141 return f * 100 / 255;
144 private void setSetting(byte[] register, short value) throws IOException {
145 byte[] valueArray = new byte[2];
146 valueArray[0] = (byte) (value >> 8);
147 valueArray[1] = (byte) value;
149 communicationController.sendRobustRequest(REGISTER_1_WRITE, register, valueArray);
152 public String getUnitName() throws IOException {
153 return getString(REGISTER_1_READ, UNIT_NAME);
156 public String getUnitSerialNumber() throws IOException {
157 return String.valueOf(getShort(REGISTER_4_READ, UNIT_SERIAL));
160 public StringType getMode() throws IOException {
161 return new StringType(Mode.values()[getByte(REGISTER_1_READ, MODE)].name());
164 public PercentType getManualFanStep() throws IOException {
165 return new PercentType(BigDecimal.valueOf(getByte(REGISTER_1_READ, MANUAL_FAN_SPEED_STEP) * 10));
168 public DecimalType getSupplyFanSpeed() throws IOException {
169 return new DecimalType(BigDecimal.valueOf(getWord(REGISTER_4_READ, SUPPLY_FAN_SPEED)));
172 public DecimalType getExtractFanSpeed() throws IOException {
173 return new DecimalType(BigDecimal.valueOf(getWord(REGISTER_4_READ, EXTRACT_FAN_SPEED)));
176 public PercentType getSupplyFanStep() throws IOException {
177 return new PercentType(BigDecimal.valueOf(getByte(REGISTER_4_READ, SUPPLY_FAN_STEP)));
180 public PercentType getExtractFanStep() throws IOException {
181 return new PercentType(BigDecimal.valueOf(getByte(REGISTER_4_READ, EXTRACT_FAN_STEP)));
184 public OnOffType getBoost() throws IOException {
185 return getBoolean(REGISTER_1_READ, BOOST) ? OnOffType.ON : OnOffType.OFF;
188 public OnOffType getNightCooling() throws IOException {
189 return getBoolean(REGISTER_1_READ, NIGHT_COOLING) ? OnOffType.ON : OnOffType.OFF;
192 public OnOffType getBypass() throws IOException {
193 return getBoolean(REGISTER_1_READ, BYPASS) ? OnOffType.ON : OnOffType.OFF;
196 public QuantityType<Dimensionless> getHumidity() throws IOException {
197 BigDecimal value = BigDecimal.valueOf(asPercentByte(getByte(REGISTER_1_READ, HUMIDITY)));
198 return new QuantityType<>(value.setScale(1, RoundingMode.HALF_UP), Units.PERCENT);
201 public QuantityType<Temperature> getRoomTemperature() throws IOException, UnexpectedResponseValueException {
202 return getTemperatureAsDecimalType(REGISTER_1_READ, ROOM_TEMPERATURE);
205 public QuantityType<Temperature> getRoomTemperatureCalculated()
206 throws IOException, UnexpectedResponseValueException {
207 return getTemperatureAsDecimalType(REGISTER_0_READ, ROOM_TEMPERATURE_CALCULATED);
210 public QuantityType<Temperature> getOutdoorTemperature() throws IOException, UnexpectedResponseValueException {
211 return getTemperatureAsDecimalType(REGISTER_1_READ, OUTDOOR_TEMPERATURE);
214 public QuantityType<Temperature> getSupplyTemperature() throws IOException, UnexpectedResponseValueException {
215 return getTemperatureAsDecimalType(REGISTER_4_READ, SUPPLY_TEMPERATURE);
218 public QuantityType<Temperature> getExtractTemperature() throws IOException, UnexpectedResponseValueException {
219 return getTemperatureAsDecimalType(REGISTER_4_READ, EXTRACT_TEMPERATURE);
222 public QuantityType<Temperature> getExhaustTemperature() throws IOException, UnexpectedResponseValueException {
223 return getTemperatureAsDecimalType(REGISTER_4_READ, EXHAUST_TEMPERATURE);
226 private QuantityType<Temperature> getTemperatureAsDecimalType(byte[] operation, byte[] register)
227 throws IOException, UnexpectedResponseValueException {
228 BigDecimal value = BigDecimal.valueOf(getTemperature(operation, register));
229 return new QuantityType<>(value.setScale(1, RoundingMode.HALF_UP), SIUnits.CELSIUS);
232 public DecimalType getBatteryLife() throws IOException {
233 return new DecimalType(BigDecimal.valueOf(asUnsignedByte(getByte(REGISTER_1_READ, BATTERY_LIFE))));
236 public DecimalType getFilterLife() throws IOException {
237 return new DecimalType(BigDecimal.valueOf(asPercentByte(getByte(REGISTER_1_READ, FILTER_LIFE))));
240 public DateTimeType getCurrentTime() throws IOException, UnexpectedResponseValueException {
241 ZonedDateTime timestamp = getTimestamp(REGISTER_1_READ, CURRENT_TIME);
242 return new DateTimeType(timestamp);
245 public PercentType setManualFanStep(Command cmd) throws IOException {
246 return setPercentTypeRegister(cmd, MANUAL_FAN_SPEED_STEP);
249 private PercentType setPercentTypeRegister(Command cmd, byte[] register) throws IOException {
250 if (cmd instanceof PercentType) {
251 byte value = (byte) ((((PercentType) cmd).intValue() + 5) / 10);
252 set(REGISTER_1_WRITE, register, value);
254 return new PercentType(BigDecimal.valueOf(getByte(REGISTER_1_READ, register) * 10));
257 private OnOffType setOnOffTypeRegister(Command cmd, byte[] register) throws IOException {
258 if (cmd instanceof OnOffType) {
259 set(REGISTER_1_WRITE, register, OnOffType.ON.equals(cmd) ? (byte) 1 : (byte) 0);
261 return getBoolean(REGISTER_1_READ, register) ? OnOffType.ON : OnOffType.OFF;
264 private StringType setStringTypeRegister(Command cmd, byte[] register) throws IOException {
265 if (cmd instanceof StringType) {
266 byte value = (byte) (Mode.valueOf(cmd.toString()).ordinal());
267 set(REGISTER_1_WRITE, register, value);
270 return new StringType(Mode.values()[getByte(REGISTER_1_READ, register)].name());
273 public StringType setMode(Command cmd) throws IOException {
274 return setStringTypeRegister(cmd, MODE);
277 public OnOffType setBoost(Command cmd) throws IOException {
278 return setOnOffTypeRegister(cmd, BOOST);
281 public OnOffType setNightCooling(Command cmd) throws IOException {
282 return setOnOffTypeRegister(cmd, NIGHT_COOLING);
285 public OnOffType setBypass(Command cmd) throws IOException {
286 return setOnOffTypeRegister(cmd, BYPASS);