]> git.basschouten.com Git - openhab-addons.git/blob
6c159a7e74b18a4a70e379e476cf2ca67565fd85
[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     UNKNOWN("", "", null);
39
40     private static final Map<String, ResourceType> MAPPING = Arrays.stream(ResourceType.values())
41             .collect(Collectors.toMap(v -> v.identifier, v -> v));
42     private static final Logger LOGGER = LoggerFactory.getLogger(ResourceType.class);
43
44     private String identifier;
45     private String commandUrl;
46     private @Nullable Class<? extends DeconzBaseMessage> expectedMessageType;
47
48     ResourceType(String identifier, String commandUrl,
49             @Nullable Class<? extends DeconzBaseMessage> expectedMessageType) {
50         this.identifier = identifier;
51         this.commandUrl = commandUrl;
52         this.expectedMessageType = expectedMessageType;
53     }
54
55     /**
56      * get the identifier string of this resource type
57      *
58      * @return
59      */
60     public String getIdentifier() {
61         return identifier;
62     }
63
64     /**
65      * get the commandUrl part for this resource type
66      *
67      * @return
68      */
69     public String getCommandUrl() {
70         return commandUrl;
71     }
72
73     /**
74      * get the expected message type for this resource type
75      *
76      * @return
77      */
78     public @Nullable Class<? extends DeconzBaseMessage> getExpectedMessageType() {
79         return expectedMessageType;
80     }
81
82     /**
83      * get the resource type from a string
84      *
85      * @param s the string
86      * @return the corresponding resource type (or UNKNOWN)
87      */
88     public static ResourceType fromString(String s) {
89         ResourceType lightType = MAPPING.getOrDefault(s, UNKNOWN);
90         if (lightType == UNKNOWN) {
91             LOGGER.debug("Unknown resource type '{}' found. This should be reported.", s);
92         }
93         return lightType;
94     }
95 }