]> git.basschouten.com Git - openhab-addons.git/blob
cbedac149375762d3f890e08bc889a912c4f1d7a
[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.onewire.internal;
14
15 import static org.openhab.binding.onewire.internal.OwBindingConstants.CHANNEL_DIGITAL;
16
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;
24
25 /**
26  * The {@link DigitalIoConfig} class provides the configuration of a digital IO channel
27  *
28  * @author Jan N. Klug - Initial contribution
29  */
30 @NonNullByDefault
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;
38
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;
45     }
46
47     public void setIoMode(String ioMode) {
48         this.ioMode = DigitalIoMode.valueOf(ioMode.toUpperCase());
49     }
50
51     public void setIoLogic(String ioLogic) {
52         this.ioLogic = DigitalIoLogic.valueOf(ioLogic.toUpperCase());
53     }
54
55     public Boolean isInverted() {
56         return (ioLogic == DigitalIoLogic.INVERTED);
57     }
58
59     public ChannelUID getChannelUID() {
60         return channelUID;
61     }
62
63     public String getChannelId() {
64         return channelID;
65     }
66
67     public OwserverDeviceParameter getParameter() {
68         return (ioMode == DigitalIoMode.INPUT) ? inParam : outParam;
69     }
70
71     public Boolean isInput() {
72         return (ioMode == DigitalIoMode.INPUT);
73     }
74
75     public Boolean isOutput() {
76         return (ioMode == DigitalIoMode.OUTPUT);
77     }
78
79     public DigitalIoMode getIoDirection() {
80         return ioMode;
81     }
82
83     public State convertState(Boolean rawValue) {
84         if (ioLogic == DigitalIoLogic.NORMAL) {
85             return rawValue ? OnOffType.ON : OnOffType.OFF;
86         } else {
87             return rawValue ? OnOffType.OFF : OnOffType.ON;
88         }
89     }
90
91     public DecimalType convertState(OnOffType command) {
92         if (ioLogic == DigitalIoLogic.NORMAL) {
93             return command.equals(OnOffType.ON) ? new DecimalType(1) : DecimalType.ZERO;
94         } else {
95             return command.equals(OnOffType.ON) ? DecimalType.ZERO : new DecimalType(1);
96         }
97     }
98
99     @Override
100     public String toString() {
101         return String.format("path=%s, mode=%s, logic=%s", getParameter(), ioMode, ioLogic);
102     }
103 }