2 * Copyright (c) 2010-2023 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.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;
23 * The route path information class. This class will evaluate the route path into it's logical components.
25 * @author Tim Roberts - Initial Contribution
28 public class PathInfo {
31 private final NeeoThingUID thingUid;
34 private final String itemName;
36 /** The channel number */
37 private final ItemSubType subType;
39 /** The channel number */
40 private final int channelNbr;
42 /** The component type */
43 private final String componentType;
45 /** The component sub type */
46 private final String componentSubType;
48 /** The action value */
49 private final @Nullable String actionValue;
52 * Creates the path info object from the route path
54 * @param paths the non-null, non-empty route paths
56 public PathInfo(String[] paths) {
57 Objects.requireNonNull(paths, "paths cannot be null");
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));
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]
72 // new ThingUID can throw illegal argument exception as well
74 thingUid = new NeeoThingUID(paths[idx++]);
76 itemName = paths[idx++];
78 subType = ItemSubType.isValid(paths[idx]) ? ItemSubType.parse(paths[idx++]) : ItemSubType.NONE;
81 channelNbr = Integer.parseInt(paths[idx++]);
82 } catch (NumberFormatException e) {
83 throw new IllegalArgumentException("channelNbr is not a valid integer: " + paths[idx - 1]);
86 // button/textlabel/slider/etc
87 componentType = paths[idx++];
89 // actor/sensor/buttoncmd
90 if ("button".equalsIgnoreCase(componentType)) {
91 componentSubType = "actor";
92 actionValue = paths[idx++];
94 componentSubType = paths[idx++];
95 actionValue = paths.length == 9 ? paths[idx + 1] : null;
98 // we don't care about the next one currently (always 'default')
99 // componentId = paths[6];
103 * Gets the {@link NeeoThingUID}
105 * @return the {@link NeeoThingUID}
107 public NeeoThingUID getThingUid() {
114 * @return the item name
116 public String getItemName() {
121 * Gets the item subtype
123 * @return the item subtype
125 public ItemSubType getSubType() {
130 * Gets the channel number
132 * @return the channel number
134 public int getChannelNbr() {
139 * Gets the component type
141 * @return the component type
143 public String getComponentType() {
144 return componentType;
148 * Gets the component sub type
150 * @return the component sub type
152 public String getComponentSubType() {
153 return componentSubType;
157 * Gets the action value
159 * @return the action value
162 public String getActionValue() {
167 public String toString() {
168 return "PathInfo [thingUid=" + thingUid + ", itemName=" + itemName + ", subType=" + subType + ", channelNbr="
169 + channelNbr + ", componentType=" + componentType + ", componentSubType=" + componentSubType
170 + ", actionValue=" + actionValue + "]";