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 java.util.Arrays;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.onewire.internal.owserver.OwserverDeviceParameter;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.State;
28 * The {@link DigitalIoConfig} class provides the configuration of a digital IO channel
30 * @author Jan N. Klug - Initial contribution
33 public class DigitalIoConfig {
34 private final String channelID;
35 private final ChannelUID channelUID;
36 private final OwserverDeviceParameter inParam;
37 private final OwserverDeviceParameter outParam;
38 private DigitalIoMode ioMode = DigitalIoMode.INPUT;
39 private DigitalIoLogic ioLogic = DigitalIoLogic.NORMAL;
41 public DigitalIoConfig(Thing thing, Integer channelIndex, OwserverDeviceParameter inParam,
42 OwserverDeviceParameter outParam) {
43 this.channelUID = new ChannelUID(thing.getUID(), String.format("%s%d", CHANNEL_DIGITAL, channelIndex));
44 this.channelID = String.format("%s%d", CHANNEL_DIGITAL, channelIndex);
45 this.inParam = inParam;
46 this.outParam = outParam;
49 public void setIoMode(String ioMode) {
50 this.ioMode = DigitalIoMode.valueOf(ioMode.toUpperCase());
53 public void setIoLogic(String ioLogic) {
54 this.ioLogic = DigitalIoLogic.valueOf(ioLogic.toUpperCase());
57 public Boolean isInverted() {
58 return (ioLogic == DigitalIoLogic.INVERTED);
61 public ChannelUID getChannelUID() {
65 public String getChannelId() {
69 public OwserverDeviceParameter getParameter() {
70 return (ioMode == DigitalIoMode.INPUT) ? inParam : outParam;
73 public Boolean isInput() {
74 return (ioMode == DigitalIoMode.INPUT);
77 public Boolean isOutput() {
78 return (ioMode == DigitalIoMode.OUTPUT);
81 public DigitalIoMode getIoDirection() {
85 public State convertState(Boolean rawValue) {
86 if (ioLogic == DigitalIoLogic.NORMAL) {
87 return rawValue ? OnOffType.ON : OnOffType.OFF;
89 return rawValue ? OnOffType.OFF : OnOffType.ON;
93 public DecimalType convertState(OnOffType command) {
94 if (ioLogic == DigitalIoLogic.NORMAL) {
95 return command.equals(OnOffType.ON) ? new DecimalType(1) : DecimalType.ZERO;
97 return command.equals(OnOffType.ON) ? DecimalType.ZERO : new DecimalType(1);
102 public String toString() {
103 return String.format("path=%s, mode=%s, logic=%s", Arrays.asList(getParameter()), ioMode, ioLogic);