]> git.basschouten.com Git - openhab-addons.git/blob
2c8f82db72b182aa38b45deab2818d20e99b11c2
[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.lgtvserial.internal.protocol.serial.commands;
14
15 import org.openhab.binding.lgtvserial.internal.protocol.serial.LGSerialResponse;
16 import org.openhab.binding.lgtvserial.internal.protocol.serial.responses.DecimalResponse;
17 import org.openhab.core.library.types.DecimalType;
18
19 /**
20  * This command is the base command to handle decimal type commands in hex format on the wire.
21  *
22  * @author Richard Lavoie - Initial contribution
23  *
24  */
25 public abstract class BaseDecimalCommand extends BaseLGSerialCommand {
26
27     /**
28      * Create a command.
29      *
30      * @param command1 Command category
31      * @param command2 Command key
32      * @param setId TV Set id this command is tied to
33      * @param updatable Define if this command is one that can update the TV or can only ever be a read status command.
34      */
35     protected BaseDecimalCommand(char command1, char command2, int setId, boolean updatable) {
36         super(command1, command2, setId, updatable);
37     }
38
39     /**
40      * Create a command that can update the TV.
41      *
42      * @param command1 Command category
43      * @param command2 Command key
44      * @param setId TV Set id this command is tied to
45      */
46
47     protected BaseDecimalCommand(char command1, char command2, int setId) {
48         super(command1, command2, setId, true);
49     }
50
51     @Override
52     protected String computeSerialDataFrom(Object data) {
53         return String.format("%02x", ((DecimalType) data).intValue());
54     }
55
56     @Override
57     protected LGSerialResponse createResponse(int set, boolean success, String data) {
58         String decimalValue = Integer.toString(Integer.parseInt(data, 16));
59         return new DecimalResponse(set, success, DecimalType.valueOf(decimalValue));
60     }
61 }