2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.gce.internal.model;
15 import java.util.stream.Stream;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * The {@link PortDefinition} enum defines and handle port
21 * definition constants
23 * @author Gaƫl L'hopital - Initial Contribution
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);
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
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;
44 public String getNodeName() {
48 public String getPortName() {
53 public String toString() {
54 return name().toLowerCase();
57 public boolean isAdvanced(int id) {
58 return id >= quantity;
61 public String getM2mCommand() {
65 public static Stream<PortDefinition> asStream() {
66 return Stream.of(PortDefinition.values());
69 public static PortDefinition fromM2MCommand(String m2mCommand) {
70 return asStream().filter(v -> m2mCommand.startsWith(v.m2mCommand)).findFirst().get();
73 public static PortDefinition fromPortName(String portName) {
74 return asStream().filter(v -> portName.startsWith(v.portName)).findFirst().get();
77 public static PortDefinition fromGroupId(String groupId) {
78 return valueOf(groupId.toUpperCase());
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);