]> git.basschouten.com Git - openhab-addons.git/blob
35fa62945d4e962bf44c91f87bd932f8f35c6d2b
[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.io.neeo.internal.servletservices.models;
14
15 import java.util.Objects;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.io.neeo.internal.models.ItemSubType;
20 import org.openhab.io.neeo.internal.models.NeeoThingUID;
21
22 /**
23  * The route path information class. This class will evaluate the route path into it's logical components.
24  *
25  * @author Tim Roberts - Initial Contribution
26  */
27 @NonNullByDefault
28 public class PathInfo {
29
30     /** The thing uid */
31     private final NeeoThingUID thingUid;
32
33     /** The item name */
34     private final String itemName;
35
36     /** The channel number */
37     private final ItemSubType subType;
38
39     /** The channel number */
40     private final int channelNbr;
41
42     /** The component type */
43     private final String componentType;
44
45     /** The component sub type */
46     private final String componentSubType;
47
48     /** The action value */
49     private final @Nullable String actionValue;
50
51     /**
52      * Creates the path info object from the route path
53      *
54      * @param paths the non-null, non-empty route paths
55      */
56     public PathInfo(String[] paths) {
57         Objects.requireNonNull(paths, "paths cannot be null");
58
59         // Note - the following check ensures that the path contains exactly what we check
60         // and will fail if not (in the case of when a firmware update changes what we expect)
61         if (paths.length < 7 || paths.length > 9) {
62             throw new IllegalArgumentException(
63                     "Path length invalid (must be between 7 and 9): " + String.join(",", paths));
64         }
65
66         // The path can be one of the following formats:
67         // 1. /device/{thingUID}/{itemName}/{channelNbr}/button/on/default
68         // 2. /device/{thingUID}/{itemName}/{channelNbr}/{component}/{componentsubtype}/default/[value]
69         // 3. /device/{thingUID}/{itemName}/{itemSubType}/{channelNbr}/button/on/default
70         // 4. /device/{thingUID}/{itemName}/{itemSubType}/{channelNbr}/{component}/{componentsubtype}/default/[value]
71
72         // new ThingUID can throw illegal argument exception as well
73         int idx = 1;
74         thingUid = new NeeoThingUID(paths[idx++]);
75
76         itemName = paths[idx++];
77
78         subType = ItemSubType.isValid(paths[idx]) ? ItemSubType.parse(paths[idx++]) : ItemSubType.NONE;
79
80         try {
81             channelNbr = Integer.parseInt(paths[idx++]);
82         } catch (NumberFormatException e) {
83             throw new IllegalArgumentException("channelNbr is not a valid integer: " + paths[idx - 1]);
84         }
85
86         // button/textlabel/slider/etc
87         componentType = paths[idx++];
88
89         // actor/sensor/buttoncmd
90         if ("button".equalsIgnoreCase(componentType)) {
91             componentSubType = "actor";
92             actionValue = paths[idx++];
93         } else {
94             componentSubType = paths[idx++];
95             actionValue = paths.length == 9 ? paths[idx + 1] : null;
96         }
97
98         // we don't care about the next one currently (always 'default')
99         // componentId = paths[6];
100     }
101
102     /**
103      * Gets the {@link NeeoThingUID}
104      *
105      * @return the {@link NeeoThingUID}
106      */
107     public NeeoThingUID getThingUid() {
108         return thingUid;
109     }
110
111     /**
112      * Gets the item name
113      *
114      * @return the item name
115      */
116     public String getItemName() {
117         return itemName;
118     }
119
120     /**
121      * Gets the item subtype
122      *
123      * @return the item subtype
124      */
125     public ItemSubType getSubType() {
126         return subType;
127     }
128
129     /**
130      * Gets the channel number
131      *
132      * @return the channel number
133      */
134     public int getChannelNbr() {
135         return channelNbr;
136     }
137
138     /**
139      * Gets the component type
140      *
141      * @return the component type
142      */
143     public String getComponentType() {
144         return componentType;
145     }
146
147     /**
148      * Gets the component sub type
149      *
150      * @return the component sub type
151      */
152     public String getComponentSubType() {
153         return componentSubType;
154     }
155
156     /**
157      * Gets the action value
158      *
159      * @return the action value
160      */
161     @Nullable
162     public String getActionValue() {
163         return actionValue;
164     }
165
166     @Override
167     public String toString() {
168         return "PathInfo [thingUid=" + thingUid + ", itemName=" + itemName + ", subType=" + subType + ", channelNbr="
169                 + channelNbr + ", componentType=" + componentType + ", componentSubType=" + componentSubType
170                 + ", actionValue=" + actionValue + "]";
171     }
172 }