]> git.basschouten.com Git - openhab-addons.git/blob
47298a816f907704281533b7d68f40140c6ee155
[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.hue.internal.api.dto.clip2;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.hue.internal.api.dto.clip2.enums.ResourceType;
18
19 /**
20  * DTO that contains an API reference element.
21  *
22  * The V2 API is set up in such a way that all resources of the same type are grouped together under the
23  * {@code /resource/<resourcetype>} endpoint, but all those resources commonly reference each other, which is done in a
24  * standardized way by indicating the resource type (rtype) and resource id (rid).
25  *
26  * A typical usage is in a single physical device that hosts multiple services. An existing example is the Philips Hue
27  * Motion sensor which has a motion, light_level, and temperature service, but theoretically any combination can be
28  * supported such as an integrated device with two independently controllable light points and a motion sensor.
29  *
30  * This means that the information of the device itself can be found under the /device resource endpoint, but it then
31  * contains a services array which references for example the light and motion resources, for which the details can be
32  * found under the /light and /motion resource endpoints respectively. Other services the device might have, such as a
33  * Zigbee radio (zigbee_connectivy) or battery (device_power) are modeled in the same way.
34  *
35  * @author Andrew Fiddian-Green - Initial contribution
36  */
37 @NonNullByDefault
38 public class ResourceReference {
39     private @Nullable String rid;
40     private @NonNullByDefault({}) String rtype;
41
42     @Override
43     public boolean equals(@Nullable Object obj) {
44         String rid = this.rid;
45         return (obj instanceof ResourceReference) && (rid != null) && rid.equals(((ResourceReference) obj).rid);
46     }
47
48     public @Nullable String getId() {
49         return rid;
50     }
51
52     public ResourceType getType() {
53         return ResourceType.of(rtype);
54     }
55
56     public ResourceReference setId(String id) {
57         rid = id;
58         return this;
59     }
60
61     public ResourceReference setType(ResourceType resourceType) {
62         rtype = resourceType.name().toLowerCase();
63         return this;
64     }
65
66     @Override
67     public String toString() {
68         String id = rid;
69         return String.format("id:%s, type:%s", id != null ? id : "*" + " ".repeat(35), getType().name().toLowerCase());
70     }
71 }