2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.neeo.internal.servletservices.models;
15 import java.util.Objects;
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;
24 * The route path information class. This class will evaluate the route path into it's logical components.
26 * @author Tim Roberts - Initial Contribution
29 public class PathInfo {
32 private final NeeoThingUID thingUid;
35 private final String itemName;
37 /** The channel number */
38 private final ItemSubType subType;
40 /** The channel number */
41 private final int channelNbr;
43 /** The component type */
44 private final String componentType;
46 /** The component sub type */
47 private final String componentSubType;
49 /** The action value */
51 private final String actionValue;
54 * Creates the path info object from the route path
56 * @param paths the non-null, non-empty route paths
58 public PathInfo(String[] paths) {
59 Objects.requireNonNull(paths, "paths cannot be null");
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));
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]
74 // new ThingUID can throw illegal argument exception as well
76 thingUid = new NeeoThingUID(paths[idx++]);
78 itemName = paths[idx++];
80 subType = ItemSubType.isValid(paths[idx]) ? ItemSubType.parse(paths[idx++]) : ItemSubType.NONE;
83 channelNbr = Integer.parseInt(paths[idx++]);
84 } catch (NumberFormatException e) {
85 throw new IllegalArgumentException("channelNbr is not a valid integer: " + paths[idx - 1]);
88 // button/textlabel/slider/etc
89 componentType = paths[idx++];
91 // actor/sensor/buttoncmd
92 if (StringUtils.equalsIgnoreCase("button", componentType)) {
93 componentSubType = "actor";
94 actionValue = paths[idx++];
96 componentSubType = paths[idx++];
97 actionValue = paths.length == 9 ? paths[idx + 1] : null;
100 // we don't care about the next one currently (always 'default')
101 // componentId = paths[6];
105 * Gets the {@link NeeoThingUID}
107 * @return the {@link NeeoThingUID}
109 public NeeoThingUID getThingUid() {
116 * @return the item name
118 public String getItemName() {
123 * Gets the item subtype
125 * @return the item subtype
127 public ItemSubType getSubType() {
132 * Gets the channel number
134 * @return the channel number
136 public int getChannelNbr() {
141 * Gets the component type
143 * @return the component type
145 public String getComponentType() {
146 return componentType;
150 * Gets the component sub type
152 * @return the component sub type
154 public String getComponentSubType() {
155 return componentSubType;
159 * Gets the action value
161 * @return the action value
164 public String getActionValue() {
169 public String toString() {
170 return "PathInfo [thingUid=" + thingUid + ", itemName=" + itemName + ", subType=" + subType + ", channelNbr="
171 + channelNbr + ", componentType=" + componentType + ", componentSubType=" + componentSubType
172 + ", actionValue=" + actionValue + "]";