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