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.ihc.internal.config;
15 import static org.openhab.binding.ihc.internal.IhcBindingConstants.*;
17 import java.math.BigDecimal;
19 import java.util.function.Function;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.openhab.binding.ihc.internal.ws.exeptions.ConversionException;
23 import org.openhab.core.thing.Channel;
24 import org.openhab.core.thing.type.ChannelTypeUID;
27 * Class for holding channel parameterization.
29 * @author Pauli Anttila - Initial contribution
31 public class ChannelParams {
33 private String channelTypeId;
34 private Integer resourceId;
35 private String direction;
36 private String commandToReact;
37 private Integer pulseWidth;
38 private Boolean inverted;
39 private Long serialNumber;
40 private Integer longPressTime;
41 private Integer onLevel;
43 public ChannelParams(@NonNull Channel channel) throws ConversionException {
44 ChannelTypeUID channelTypeUID = channel.getChannelTypeUID();
45 if (channelTypeUID != null) {
46 channelTypeId = channelTypeUID.getId();
49 Map<String, Object> map = channel.getConfiguration().getProperties();
50 for (Map.Entry<String, Object> entry : map.entrySet()) {
51 switch (entry.getKey()) {
52 case PARAM_RESOURCE_ID:
53 resourceId = getChannelParameterAsInteger(entry.getValue());
56 direction = getChannelParameterAsString(entry.getValue());
58 case PARAM_CMD_TO_REACT:
59 commandToReact = getChannelParameterAsString(entry.getValue());
61 case PARAM_SERIAL_NUMBER:
62 serialNumber = getChannelParameterAsLong(entry.getValue());
64 case PARAM_PULSE_WIDTH:
65 pulseWidth = getChannelParameterAsInteger(entry.getValue());
67 case PARAM_LONG_PRESS_TIME:
68 longPressTime = getChannelParameterAsInteger(entry.getValue());
71 inverted = getChannelParameterAsBoolean(entry.getValue());
74 onLevel = getChannelParameterAsInteger(entry.getValue());
80 public String getChannelTypeId() {
84 public Integer getResourceId() {
88 public String getDirection() {
92 public boolean isDirectionBoth() {
93 return direction != null && DIRECTION_READ_WRITE.equals(direction);
96 public boolean isDirectionReadOnly() {
97 return direction != null && DIRECTION_READ_ONLY.equals(direction);
100 public boolean isDirectionWriteOnly() {
101 return direction != null && DIRECTION_WRITE_ONLY.equals(direction);
104 public String getCommandToReact() {
105 return commandToReact;
108 public Integer getPulseWidth() {
112 public Boolean getInverted() {
116 public boolean isInverted() {
117 if (inverted != null) {
123 public Long getSerialNumber() {
127 public Integer getLongPressTime() {
128 return longPressTime;
131 public Integer getOnLevel() {
135 private static Boolean getChannelParameterAsBoolean(Object value) throws ConversionException {
136 return convert(value, (v) -> (Boolean) v);
139 private Integer getChannelParameterAsInteger(Object value) throws ConversionException {
140 return convert(value, (v) -> ((BigDecimal) v).intValue());
143 private Long getChannelParameterAsLong(Object value) throws ConversionException {
144 return convert(value, (v) -> ((BigDecimal) v).longValue());
147 private String getChannelParameterAsString(Object value) throws ConversionException {
148 return convert(value, (v) -> (String) v);
151 private static <T> T convert(Object value, Function<Object, T> f) throws ConversionException {
157 return f.apply(value);
158 } catch (ClassCastException e) {
159 throw new ConversionException(String.format("Failed to convert, reason %s.", e.getMessage()), e);
164 public String toString() {
165 return String.format(
166 "[ channelTypeId=%s, resourceId=%d, direction=%s, commandToReact=%s, pulseWidth=%d, inverted=%b, serialNumber=%d, longPressTime=%d, onLevel=%d ]",
167 channelTypeId, resourceId, direction, commandToReact, pulseWidth, inverted, serialNumber, longPressTime,