]> git.basschouten.com Git - openhab-addons.git/blob
3fe71b023641acd21678da2e530e6ad146e841e1
[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.gce.internal.model;
14
15 import java.util.stream.Stream;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
20  * The {@link PortDefinition} enum defines and handle port
21  * definition constants
22  *
23  * @author GaĆ«l L'hopital - Initial Contribution
24  */
25 @NonNullByDefault
26 public enum PortDefinition {
27     COUNTER("count", "C", "GetCount", 8),
28     ANALOG("analog", "A", "GetAn", 4),
29     RELAY("led", "O", "GetOut", 8),
30     CONTACT("btn", "I", "GetIn", 8);
31
32     private final String nodeName; // Name used in the status xml file
33     private final String portName; // Name used by the M2M protocol
34     private final String m2mCommand; // associated M2M command
35     private final int quantity; // base number of ports
36
37     PortDefinition(String nodeName, String portName, String m2mCommand, int quantity) {
38         this.nodeName = nodeName;
39         this.portName = portName;
40         this.m2mCommand = m2mCommand;
41         this.quantity = quantity;
42     }
43
44     public String getNodeName() {
45         return nodeName;
46     }
47
48     public String getPortName() {
49         return portName;
50     }
51
52     @Override
53     public String toString() {
54         return name().toLowerCase();
55     }
56
57     public boolean isAdvanced(int id) {
58         return id >= quantity;
59     }
60
61     public String getM2mCommand() {
62         return m2mCommand;
63     }
64
65     public static Stream<PortDefinition> asStream() {
66         return Stream.of(PortDefinition.values());
67     }
68
69     public static PortDefinition fromM2MCommand(String m2mCommand) {
70         return asStream().filter(v -> m2mCommand.startsWith(v.m2mCommand)).findFirst().get();
71     }
72
73     public static PortDefinition fromPortName(String portName) {
74         return asStream().filter(v -> portName.startsWith(v.portName)).findFirst().get();
75     }
76
77     public static PortDefinition fromGroupId(String groupId) {
78         return valueOf(groupId.toUpperCase());
79     }
80
81     public static String asChannelId(String portDefinition) {
82         String portKind = portDefinition.substring(0, 1);
83         PortDefinition result = asStream().filter(v -> v.portName.startsWith(portKind)).findFirst().get();
84         return result.toString() + "#" + portDefinition.substring(1);
85     }
86 }