]> git.basschouten.com Git - openhab-addons.git/blob
45a12bff54449b94692baa1597b4e245a0ce2ae6
[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.luxom.internal.protocol;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17
18 /**
19  * luxom command
20  * 
21  * @author Kris Jespers - Initial contribution
22  */
23 @NonNullByDefault
24 public class LuxomCommand {
25     private final LuxomAction action;
26     private final @Nullable String address; // must for data byte commands be set after construction
27
28     private @Nullable String data;
29
30     public LuxomCommand(String command) {
31         if (command.length() == 0) {
32             action = LuxomAction.INVALID_ACTION;
33             data = command;
34             address = null;
35             return;
36         }
37         String[] parts = command.split(",");
38
39         if (parts.length == 1) {
40             if (command.startsWith(LuxomAction.MODULE_INFORMATION.getCommand())) {
41                 action = LuxomAction.MODULE_INFORMATION;
42                 data = command.substring(2);
43             } else if (command.equals(LuxomAction.PASSWORD_REQUEST.getCommand())) {
44                 action = LuxomAction.PASSWORD_REQUEST;
45                 data = null;
46             } else if (command.equals(LuxomAction.ACKNOWLEDGE.getCommand())) {
47                 action = LuxomAction.ACKNOWLEDGE;
48                 data = null;
49             } else {
50                 action = LuxomAction.INVALID_ACTION;
51                 data = command;
52             }
53             address = null;
54         } else {
55             action = LuxomAction.of(parts[0]);
56             StringBuilder stringBuilder = new StringBuilder();
57             if (action.isHasAddress()) {
58                 // first 0 not needed ?
59                 for (int i = 2; i < parts.length; i++) {
60                     stringBuilder.append(parts[i]);
61                     if (i != (parts.length - 1)) {
62                         stringBuilder.append(",");
63                     }
64                 }
65                 address = stringBuilder.toString();
66                 data = null;
67             } else {
68                 for (int i = 1; i < parts.length; i++) {
69                     stringBuilder.append(parts[i]);
70                     if (i != (parts.length - 1)) {
71                         stringBuilder.append(",");
72                     }
73                 }
74                 address = null;
75                 data = stringBuilder.toString();
76             }
77         }
78     }
79
80     @Override
81     public String toString() {
82         return "LuxomCommand{" + "action=" + action + ", address='" + address + '\'' + ", data='" + data + '\'' + '}';
83     }
84
85     public LuxomAction getAction() {
86         return action;
87     }
88
89     @Nullable
90     public String getData() {
91         return data;
92     }
93
94     @Nullable
95     public String getAddress() {
96         return address;
97     }
98
99     public void setData(@Nullable String data) {
100         this.data = data;
101     }
102 }