]> git.basschouten.com Git - openhab-addons.git/blob
e8d88b551b8d7b477c8c1b43250a8d52b7c86353
[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.powermax.internal.message;
14
15 import java.util.Arrays;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.powermax.internal.state.PowermaxState;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * A class for SETTINGS and SETTINGS_ITEM messages handling
25  *
26  * @author Laurent Garnier - Initial contribution
27  */
28 @NonNullByDefault
29 public class PowermaxSettingsMessage extends PowermaxBaseMessage {
30
31     private final Logger logger = LoggerFactory.getLogger(PowermaxSettingsMessage.class);
32
33     /**
34      * Constructor
35      *
36      * @param message
37      *            the received message as a buffer of bytes
38      */
39     public PowermaxSettingsMessage(byte[] message) {
40         super(message);
41     }
42
43     @Override
44     protected @Nullable PowermaxState handleMessageInternal(@Nullable PowermaxCommManager commManager) {
45         if (commManager == null) {
46             return null;
47         }
48
49         PowermaxState updatedState = commManager.createNewState();
50
51         byte[] message = getRawData();
52         int index = message[2] & 0x000000FF;
53         int page = message[3] & 0x000000FF;
54         int length = 0;
55
56         debug("Page", page, Integer.toString(page));
57         debug("Index", index, Integer.toString(index));
58
59         if (getReceiveType() == PowermaxReceiveType.SETTINGS) {
60             length = message.length - 6;
61             updatedState.setUpdateSettings(Arrays.copyOfRange(message, 2, 2 + 2 + length));
62         } else if (getReceiveType() == PowermaxReceiveType.SETTINGS_ITEM) {
63             length = message[4] & 0x000000FF;
64             byte[] data = new byte[length + 2];
65             int i = 0;
66             for (int j = 2; j <= 3; j++) {
67                 data[i++] = message[j];
68             }
69             for (int j = 0; j < length; j++) {
70                 data[i++] = message[j + 5];
71             }
72             updatedState.setUpdateSettings(data);
73         }
74         if (logger.isDebugEnabled()) {
75             logger.debug("Received Powermax setting page {} index {} length {}", String.format("%02X (%d)", page, page),
76                     String.format("%02X (%d)", index, index), String.format("%02X (%d)", length, length));
77         }
78
79         return updatedState;
80     }
81 }