]> git.basschouten.com Git - openhab-addons.git/blob
959937cf465facf78cc8aec0259336e5e2e51c29
[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         String key = serialNumber + "-" + cubeCommand.getClass().getSimpleName();
72         return key;
73     }
74
75     /**
76      * @return the key based on the serial and channel
77      *         This is can be used to find duplicated commands in the queue
78      */
79     public String getKey() {
80         return key;
81     }
82
83     public int getId() {
84         return id;
85     }
86
87     public @Nullable ChannelUID getChannelUID() {
88         return channelUID;
89     }
90
91     public void setChannelUID(ChannelUID channelUID) {
92         this.channelUID = channelUID;
93         key = getKey(serialNumber, channelUID);
94     }
95
96     public @Nullable Command getCommand() {
97         return command;
98     }
99
100     public void setCommand(Command command) {
101         this.command = command;
102     }
103
104     public @Nullable CubeCommand getCubeCommand() {
105         return cubeCommand;
106     }
107
108     public String getDeviceSerial() {
109         return serialNumber;
110     }
111
112     public void setDeviceSerial(String device) {
113         this.serialNumber = device;
114         final ChannelUID channelUID = this.channelUID;
115         if (channelUID != null) {
116             key = getKey(serialNumber, channelUID);
117         }
118     }
119
120     public String getCommandText() {
121         return commandText;
122     }
123
124     public void setCommandText(String commandText) {
125         this.commandText = commandText;
126     }
127
128     @Override
129     public String toString() {
130         StringBuilder sb = new StringBuilder();
131         return sb.append("id: ").append(id).append(", channelUID: ").append(channelUID).append(", command: ")
132                 .append(command).append(", cubeCommand: ").append(cubeCommand).append(", serialNumber: ")
133                 .append(serialNumber).append(", key: ").append(key).append(", commandText: ").append(commandText)
134                 .toString();
135     }
136 }