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