]> git.basschouten.com Git - openhab-addons.git/blob
f6083d94add0191f7a18abe173d731c444b80961
[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.intesis.internal.api;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * @author Cody Cutrer - Initial contribution
25  */
26 @NonNullByDefault
27 public class IntesisBoxMessage {
28     public static final String ID = "ID";
29     public static final String INFO = "INFO";
30     public static final String SET = "SET";
31     public static final String CHN = "CHN";
32     public static final String GET = "GET";
33     public static final String LOGIN = "LOGIN";
34     public static final String LOGOUT = "LOGOUT";
35     public static final String CFG = "CFG";
36     public static final String LIMITS = "LIMITS";
37     public static final String DISCOVER = "DISCOVER";
38
39     private static final Pattern REGEX = Pattern.compile("^([^,]+)(?:,(\\d+))?:([^,]+),([A-Z0-9.,\\[\\]]+)$");
40
41     @SuppressWarnings("unused")
42     private final String acNum;
43     private final String command;
44     private final String function;
45     private final String value;
46
47     private IntesisBoxMessage(String command, String acNum, String function, String value) {
48         this.command = command;
49         this.acNum = acNum;
50         this.function = function;
51         this.value = value;
52     }
53
54     public String getCommand() {
55         return command;
56     }
57
58     public String getFunction() {
59         return function;
60     }
61
62     public String getValue() {
63         return value;
64     }
65
66     public List<String> getLimitsValue() {
67         return Arrays.asList(value.substring(1, value.length() - 1).split(","));
68     }
69
70     public static @Nullable IntesisBoxMessage parse(String message) {
71         Matcher m = REGEX.matcher(message);
72         if (!m.find()) {
73             return null;
74         }
75
76         return new IntesisBoxMessage(m.group(1), m.group(2), m.group(3), m.group(4));
77     }
78 }