]> git.basschouten.com Git - openhab-addons.git/blob
5e47063ccb6a997981a087d8e0aeaf96dc3316be
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.rotel.internal.protocol.ascii;
14
15 import java.nio.charset.StandardCharsets;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.rotel.internal.RotelException;
20 import org.openhab.binding.rotel.internal.RotelModel;
21 import org.openhab.binding.rotel.internal.communication.RotelCommand;
22 import org.openhab.binding.rotel.internal.protocol.RotelProtocol;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Class for handling the Rotel ASCII V1 protocol (build of command messages, decoding of incoming data)
28  *
29  * @author Laurent Garnier - Initial contribution
30  */
31 @NonNullByDefault
32 public class RotelAsciiV1ProtocolHandler extends RotelAbstractAsciiProtocolHandler {
33
34     private static final char CHAR_END_RESPONSE = '!';
35
36     private final Logger logger = LoggerFactory.getLogger(RotelAsciiV1ProtocolHandler.class);
37
38     /**
39      * Constructor
40      *
41      * @param model the Rotel model in use
42      */
43     public RotelAsciiV1ProtocolHandler(RotelModel model) {
44         super(model, CHAR_END_RESPONSE);
45     }
46
47     @Override
48     public RotelProtocol getProtocol() {
49         return RotelProtocol.ASCII_V1;
50     }
51
52     @Override
53     public byte[] buildCommandMessage(RotelCommand cmd, @Nullable Integer value) throws RotelException {
54         String messageStr = cmd.getAsciiCommandV1();
55         if (messageStr == null) {
56             throw new RotelException("Command \"" + cmd.getName() + "\" ignored: not available for ASCII V1 protocol");
57         }
58         if (value != null) {
59             switch (cmd) {
60                 case VOLUME_SET:
61                     messageStr += String.format("%d", value);
62                     break;
63                 case BASS_SET:
64                 case TREBLE_SET:
65                     if (value == 0) {
66                         messageStr += "000";
67                     } else if (value > 0) {
68                         messageStr += String.format("+%02d", value);
69                     } else {
70                         messageStr += String.format("-%02d", -value);
71                     }
72                     break;
73                 case BALANCE_SET:
74                     if (value == 0) {
75                         messageStr += "000";
76                     } else if (value > 0) {
77                         messageStr += String.format("R%02d", value);
78                     } else {
79                         messageStr += String.format("L%02d", -value);
80                     }
81                     break;
82                 case DIMMER_LEVEL_SET:
83                     if (value > 0 && model.getDimmerLevelMin() < 0) {
84                         messageStr += String.format("+%d", value);
85                     } else {
86                         messageStr += String.format("%d", value);
87                     }
88                     break;
89                 default:
90                     break;
91             }
92         }
93         if (!messageStr.endsWith("?")) {
94             messageStr += "!";
95         }
96         byte[] message = messageStr.getBytes(StandardCharsets.US_ASCII);
97         logger.debug("Command \"{}\" => {}", cmd.getName(), messageStr);
98         return message;
99     }
100 }