]> git.basschouten.com Git - openhab-addons.git/blob
a93858b97d8d0ad0f96dfa68908cef0da54ac0a5
[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.velux.internal.things;
14
15 import java.util.Map;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import java.util.stream.Stream;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.velux.internal.VeluxBindingConstants;
22
23 /**
24  * <B>Velux</B> product characteristics: Velocity.
25  * <P>
26  * See <a href=
27  * "https://velcdn.azureedge.net/~/media/com/api/klf200/technical%20specification%20for%20klf%20200%20api.pdf#page=45">KLF200
28  * Velocity parameter</a>
29  * <P>
30  * Methods in handle this type of information:
31  * <UL>
32  * <LI>{@link #getVelocity()} to retrieve the value of the characteristic.</LI>
33  * <LI>{@link #get(short)} to convert a value into the characteristic.</LI>
34  * <LI>{@link #getByName(String)} to convert a name into the characteristic.</LI>
35  * <LI>{@link #dump} to retrieve a human-readable description of all values.</LI>
36  * </UL>
37  *
38  * @see VeluxKLFAPI
39  *
40  * @author Guenther Schreiner - initial contribution.
41  */
42 @NonNullByDefault
43 public enum VeluxProductVelocity {
44     DEFAULT((short) 0, "default"),
45     SILENT((short) 1, "silent"),
46     FAST((short) 2, "fast"),
47     VELOCITY_NOT_AVAILABLE((short) 255, ""),
48     UNDEFTYPE((short) 0xffff, VeluxBindingConstants.UNKNOWN);
49
50     // Class internal
51
52     private short velocity;
53     private String velocityName;
54
55     // Reverse-lookup map for getting a VeluxProductVelocity from a value.
56     private static final Map<Short, VeluxProductVelocity> LOOKUPTYPEID2ENUM = Stream.of(VeluxProductVelocity.values())
57             .collect(Collectors.toMap(VeluxProductVelocity::getVelocity, Function.identity()));
58
59     // Constructor
60
61     private VeluxProductVelocity(short velocity, String velocityName) {
62         this.velocity = velocity;
63         this.velocityName = velocityName;
64     }
65
66     // Class access methods
67
68     public short getVelocity() {
69         return velocity;
70     }
71
72     public static VeluxProductVelocity get(short velocity) {
73         return LOOKUPTYPEID2ENUM.getOrDefault(velocity, VeluxProductVelocity.UNDEFTYPE);
74     }
75
76     public static VeluxProductVelocity getByName(String velocityName) {
77         for (VeluxProductVelocity enumItem : VeluxProductVelocity.values()) {
78             if (enumItem.velocityName.equals(velocityName)) {
79                 return enumItem;
80             }
81         }
82         return VeluxProductVelocity.UNDEFTYPE;
83     }
84
85     public static String dump() {
86         StringBuilder sb = new StringBuilder();
87         for (VeluxProductVelocity typeId : VeluxProductVelocity.values()) {
88             sb.append(typeId).append(VeluxBindingConstants.OUTPUT_VALUE_SEPARATOR);
89         }
90         if (sb.lastIndexOf(VeluxBindingConstants.OUTPUT_VALUE_SEPARATOR) > 0) {
91             sb.deleteCharAt(sb.lastIndexOf(VeluxBindingConstants.OUTPUT_VALUE_SEPARATOR));
92         }
93         return sb.toString();
94     }
95 }