]> git.basschouten.com Git - openhab-addons.git/blob
1ca1d4a40b1a6d3e789cab33ffc7d2009628cb3c
[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.max.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.max.internal.command.CubeCommand;
18 import org.openhab.core.thing.ChannelUID;
19 import org.openhab.core.types.Command;
20
21 /**
22  * Class for sending a command.
23  *
24  * @author Marcel Verpaalen - Initial contribution
25  */
26 @NonNullByDefault
27 public final class SendCommand {
28
29     private int id;
30     private static int commandId = -1;
31
32     private @Nullable ChannelUID channelUID;
33     private @Nullable Command command;
34     private @Nullable CubeCommand cubeCommand;
35     private String serialNumber;
36     private String key;
37     private String commandText;
38
39     public SendCommand(String serialNumber, ChannelUID channelUID, Command command) {
40         commandId++;
41         id = commandId;
42         this.serialNumber = serialNumber;
43         this.channelUID = channelUID;
44         this.command = command;
45         key = getKey(serialNumber, channelUID);
46         this.commandText = command.toString();
47     }
48
49     public SendCommand(String serialNumber, CubeCommand cubeCommand, String commandText) {
50         commandId++;
51         id = commandId;
52         this.serialNumber = serialNumber;
53         this.cubeCommand = cubeCommand;
54         key = getKey(serialNumber, cubeCommand);
55         this.commandText = commandText;
56     }
57
58     /**
59      * Get the key based on the serial and channel
60      * This is can be used to find duplicated commands in the queue
61      */
62     private static String getKey(String serialNumber, ChannelUID channelUID) {
63         return serialNumber + "-" + channelUID.getId();
64     }
65
66     /**
67      * Get the key based on the serial and channel
68      * This is can be used to find duplicated commands in the queue
69      */
70     private static String getKey(String serialNumber, CubeCommand cubeCommand) {
71         return serialNumber + "-" + cubeCommand.getClass().getSimpleName();
72     }
73
74     /**
75      * @return the key based on the serial and channel
76      *         This is can be used to find duplicated commands in the queue
77      */
78     public String getKey() {
79         return key;
80     }
81
82     public int getId() {
83         return id;
84     }
85
86     public @Nullable ChannelUID getChannelUID() {
87         return channelUID;
88     }
89
90     public void setChannelUID(ChannelUID channelUID) {
91         this.channelUID = channelUID;
92         key = getKey(serialNumber, channelUID);
93     }
94
95     public @Nullable Command getCommand() {
96         return command;
97     }
98
99     public void setCommand(Command command) {
100         this.command = command;
101     }
102
103     public @Nullable CubeCommand getCubeCommand() {
104         return cubeCommand;
105     }
106
107     public String getDeviceSerial() {
108         return serialNumber;
109     }
110
111     public void setDeviceSerial(String device) {
112         this.serialNumber = device;
113         final ChannelUID channelUID = this.channelUID;
114         if (channelUID != null) {
115             key = getKey(serialNumber, channelUID);
116         }
117     }
118
119     public String getCommandText() {
120         return commandText;
121     }
122
123     public void setCommandText(String commandText) {
124         this.commandText = commandText;
125     }
126
127     @Override
128     public String toString() {
129         StringBuilder sb = new StringBuilder();
130         return sb.append("id: ").append(id).append(", channelUID: ").append(channelUID).append(", command: ")
131                 .append(command).append(", cubeCommand: ").append(cubeCommand).append(", serialNumber: ")
132                 .append(serialNumber).append(", key: ").append(key).append(", commandText: ").append(commandText)
133                 .toString();
134     }
135 }