2 * Copyright (c) 2010-2020 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.bluetooth.daikinmadoka.internal.model;
15 import java.io.ByteArrayOutputStream;
16 import java.io.DataOutputStream;
17 import java.io.IOException;
18 import java.nio.ByteBuffer;
19 import java.util.Arrays;
20 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.bluetooth.daikinmadoka.internal.model.commands.BRC1HCommand;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * This class represents a message transmitted or received from the BRC1H controller as a serial protocol
32 * @author Benjamin Lafois - Initial contribution
35 public class MadokaMessage {
37 private static final Logger logger = LoggerFactory.getLogger(MadokaMessage.class);
39 private int messageId;
40 private final Map<Integer, MadokaValue> values;
42 private byte @Nullable [] rawMessage;
44 private MadokaMessage() {
45 values = new HashMap<>();
48 public static byte[] createRequest(BRC1HCommand command, MadokaValue... parameters) {
50 ByteArrayOutputStream output = new ByteArrayOutputStream();
51 DataOutputStream request = new DataOutputStream(output);
53 // Message Length - Computed in the end
57 // Command ID, coded on 3 bytes
59 request.writeShort(command.getCommandId());
61 if (parameters.length == 0) {
65 for (MadokaValue mv : parameters) {
66 request.writeByte(mv.getId());
67 request.writeByte(mv.getSize());
68 request.write(mv.getRawValue());
72 // Finally, compute array size
73 byte[] ret = output.toByteArray();
74 ret[1] = (byte) (ret.length - 1);
77 } catch (IOException e) {
78 logger.info("Error while building request", e);
79 throw new RuntimeException(e);
83 public static MadokaMessage parse(byte[] msg) throws MadokaParsingException {
84 // Msg format (bytes):
85 // <Msg Length> <msg id> <msg id> <msg id> ...
86 // So MINIMAL length is 4, to cover the message length + message ID
88 throw new MadokaParsingException("Message received is too short to be parsed.");
90 if (msg[0] != msg.length) {
91 throw new MadokaParsingException("Message size is not valid (different from byte[0]).");
94 MadokaMessage m = new MadokaMessage();
96 m.messageId = ByteBuffer.wrap(msg, 2, 2).getShort();
98 MadokaValue mv = null;
100 // Starting here, we are not on the safe side with previous msg.length check
101 for (int i = 4; i < msg.length;) {
102 if ((i + 1) >= msg.length) {
103 throw new MadokaParsingException("Truncated message detected while parsing response value header");
106 mv = new MadokaValue();
108 mv.setSize(Byte.toUnsignedInt(msg[i + 1]));
110 if ((i + 1 + mv.getSize()) >= msg.length) {
111 throw new MadokaParsingException("Truncated message detected while parsing response value content");
114 mv.setRawValue(Arrays.copyOfRange(msg, i + 2, i + 2 + mv.getSize()));
116 i += 2 + mv.getSize();
118 m.values.put(mv.getId(), mv);
124 private void setRawMessage(byte[] rawMessage) {
125 this.rawMessage = rawMessage;
128 public byte @Nullable [] getRawMessage() {
129 return this.rawMessage;
132 public int getMessageId() {
136 public Map<Integer, MadokaValue> getValues() {
141 public String toString() {
142 StringBuilder sb = new StringBuilder();
144 sb.append(String.format("{ messageId: %d, values: [", this.messageId));
146 for (Map.Entry<Integer, MadokaValue> entry : values.entrySet()) {
147 sb.append(String.format(" { valueId: %d, valueSize: %d },", entry.getKey(), entry.getValue().getSize()));
151 return sb.toString();