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.onewire.internal;
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.CHANNEL_DIGITAL;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.onewire.internal.owserver.OwserverDeviceParameter;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.types.State;
26 * The {@link DigitalIoConfig} class provides the configuration of a digital IO channel
28 * @author Jan N. Klug - Initial contribution
31 public class DigitalIoConfig {
32 private final String channelID;
33 private final ChannelUID channelUID;
34 private final OwserverDeviceParameter inParam;
35 private final OwserverDeviceParameter outParam;
36 private DigitalIoMode ioMode = DigitalIoMode.INPUT;
37 private DigitalIoLogic ioLogic = DigitalIoLogic.NORMAL;
39 public DigitalIoConfig(Thing thing, Integer channelIndex, OwserverDeviceParameter inParam,
40 OwserverDeviceParameter outParam) {
41 this.channelUID = new ChannelUID(thing.getUID(), String.format("%s%d", CHANNEL_DIGITAL, channelIndex));
42 this.channelID = String.format("%s%d", CHANNEL_DIGITAL, channelIndex);
43 this.inParam = inParam;
44 this.outParam = outParam;
47 public void setIoMode(String ioMode) {
48 this.ioMode = DigitalIoMode.valueOf(ioMode.toUpperCase());
51 public void setIoLogic(String ioLogic) {
52 this.ioLogic = DigitalIoLogic.valueOf(ioLogic.toUpperCase());
55 public Boolean isInverted() {
56 return (ioLogic == DigitalIoLogic.INVERTED);
59 public ChannelUID getChannelUID() {
63 public String getChannelId() {
67 public OwserverDeviceParameter getParameter() {
68 return (ioMode == DigitalIoMode.INPUT) ? inParam : outParam;
71 public Boolean isInput() {
72 return (ioMode == DigitalIoMode.INPUT);
75 public Boolean isOutput() {
76 return (ioMode == DigitalIoMode.OUTPUT);
79 public DigitalIoMode getIoDirection() {
83 public State convertState(Boolean rawValue) {
84 if (ioLogic == DigitalIoLogic.NORMAL) {
85 return rawValue ? OnOffType.ON : OnOffType.OFF;
87 return rawValue ? OnOffType.OFF : OnOffType.ON;
91 public DecimalType convertState(OnOffType command) {
92 if (ioLogic == DigitalIoLogic.NORMAL) {
93 return command.equals(OnOffType.ON) ? new DecimalType(1) : DecimalType.ZERO;
95 return command.equals(OnOffType.ON) ? DecimalType.ZERO : new DecimalType(1);
100 public String toString() {
101 return String.format("path=%s, mode=%s, logic=%s", getParameter(), ioMode, ioLogic);