2 * Copyright (c) 2010-2022 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.rotel.internal.protocol.ascii;
15 import java.nio.charset.StandardCharsets;
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;
27 * Class for handling the Rotel ASCII V1 protocol (build of command messages, decoding of incoming data)
29 * @author Laurent Garnier - Initial contribution
32 public class RotelAsciiV1ProtocolHandler extends RotelAbstractAsciiProtocolHandler {
34 private static final char CHAR_END_RESPONSE = '!';
36 private final Logger logger = LoggerFactory.getLogger(RotelAsciiV1ProtocolHandler.class);
41 * @param model the Rotel model in use
43 public RotelAsciiV1ProtocolHandler(RotelModel model) {
44 super(model, CHAR_END_RESPONSE);
48 public RotelProtocol getProtocol() {
49 return RotelProtocol.ASCII_V1;
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.getLabel() + "\" ignored: not available for ASCII V1 protocol");
61 messageStr += String.format("%d", value);
67 } else if (value > 0) {
68 messageStr += String.format("+%02d", value);
70 messageStr += String.format("-%02d", -value);
76 } else if (value > 0) {
77 messageStr += String.format("R%02d", value);
79 messageStr += String.format("L%02d", -value);
82 case DIMMER_LEVEL_SET:
83 if (value > 0 && model.getDimmerLevelMin() < 0) {
84 messageStr += String.format("+%d", value);
86 messageStr += String.format("%d", value);
93 if (!messageStr.endsWith("?")) {
96 byte[] message = messageStr.getBytes(StandardCharsets.US_ASCII);
97 logger.debug("Command \"{}\" => {}", cmd, messageStr);