]> git.basschouten.com Git - openhab-addons.git/blob
7059c0b26bba1d4dc666397b766db78d27b99da4
[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.deconz.internal.types;
14
15 import java.util.Arrays;
16 import java.util.Map;
17 import java.util.stream.Collectors;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.deconz.internal.dto.DeconzBaseMessage;
22 import org.openhab.binding.deconz.internal.dto.GroupMessage;
23 import org.openhab.binding.deconz.internal.dto.LightMessage;
24 import org.openhab.binding.deconz.internal.dto.SensorMessage;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * The {@link ResourceType} defines an enum for websocket messages
30  *
31  * @author Jan N. Klug - Initial contribution
32  */
33 @NonNullByDefault
34 public enum ResourceType {
35     GROUPS("groups", "action", GroupMessage.class),
36     LIGHTS("lights", "state", LightMessage.class),
37     SENSORS("sensors", "config", SensorMessage.class),
38     SCENES("scenes", "", DeconzBaseMessage.class),
39     UNKNOWN("", "", null);
40
41     private static final Map<String, ResourceType> MAPPING = Arrays.stream(ResourceType.values())
42             .collect(Collectors.toMap(v -> v.identifier, v -> v));
43     private static final Logger LOGGER = LoggerFactory.getLogger(ResourceType.class);
44
45     private final String identifier;
46     private final String commandUrl;
47     private @Nullable Class<? extends DeconzBaseMessage> expectedMessageType;
48
49     ResourceType(String identifier, String commandUrl,
50             @Nullable Class<? extends DeconzBaseMessage> expectedMessageType) {
51         this.identifier = identifier;
52         this.commandUrl = commandUrl;
53         this.expectedMessageType = expectedMessageType;
54     }
55
56     /**
57      * get the identifier string of this resource type
58      *
59      * @return
60      */
61     public String getIdentifier() {
62         return identifier;
63     }
64
65     /**
66      * get the commandUrl part for this resource type
67      *
68      * @return
69      */
70     public String getCommandUrl() {
71         return commandUrl;
72     }
73
74     /**
75      * get the expected message type for this resource type
76      *
77      * @return
78      */
79     public @Nullable Class<? extends DeconzBaseMessage> getExpectedMessageType() {
80         return expectedMessageType;
81     }
82
83     /**
84      * get the resource type from a string
85      *
86      * @param s the string
87      * @return the corresponding resource type (or UNKNOWN)
88      */
89     public static ResourceType fromString(String s) {
90         ResourceType lightType = MAPPING.getOrDefault(s, UNKNOWN);
91         if (lightType == UNKNOWN) {
92             LOGGER.debug("Unknown resource type '{}' found. This should be reported.", s);
93         }
94         return lightType;
95     }
96 }