]> git.basschouten.com Git - openhab-addons.git/blob
1f54bfd192ab3ef287c9408dc32aeef1aba488d5
[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.loxone.internal.controls;
14
15 import static org.openhab.binding.loxone.internal.LxBindingConstants.*;
16
17 import java.io.IOException;
18
19 import org.openhab.binding.loxone.internal.types.LxUuid;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.type.ChannelTypeUID;
23 import org.openhab.core.types.Command;
24
25 /**
26  * An UpDownDigital type of control on Loxone Miniserver.
27  * <p>
28  * According to Loxone API documentation, UpDownDigital control is a virtual input that is digital and has an input type
29  * up-down buttons. Buttons act like on an integrated up/down arrows switch - only one direction can be active at a
30  * time. Pushing button in one direction will automatically set the other direction to off.
31  * This control has no states and can only accept commands. Only up/down on/off commands are generated. Pulse
32  * commands are not supported, because of lack of corresponding feature in openHAB. Pulse can be emulated by quickly
33  * alternating between ON and OFF commands. Because this control has no states, there will be no openHAB state changes
34  * triggered by the Miniserver and we need to take care of updating the states inside this class.
35  *
36  * @author Pawel Pieczul - initial contribution
37  *
38  */
39 class LxControlUpDownDigital extends LxControl {
40
41     static class Factory extends LxControlInstance {
42         @Override
43         LxControl create(LxUuid uuid) {
44             return new LxControlUpDownDigital(uuid);
45         }
46
47         @Override
48         String getType() {
49             return "updowndigital";
50         }
51     }
52
53     private static final String CMD_UP_ON = "UpOn";
54     private static final String CMD_UP_OFF = "UpOff";
55     private static final String CMD_DOWN_ON = "DownOn";
56     private static final String CMD_DOWN_OFF = "DownOff";
57
58     private OnOffType upState = OnOffType.OFF;
59     private OnOffType downState = OnOffType.OFF;
60     private ChannelUID upChannelId;
61     private ChannelUID downChannelId;
62
63     LxControlUpDownDigital(LxUuid uuid) {
64         super(uuid);
65     }
66
67     @Override
68     public void initialize(LxControlConfig config) {
69         initialize(config, " / Up", "Up/Down Digital: Up", " / Down", "Up/Down Digital: Down");
70     }
71
72     void initialize(LxControlConfig config, String upChannelLabel, String upChannelDescription, String downChannelLabel,
73             String downChannelDescription) {
74         super.initialize(config);
75         upChannelId = addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH),
76                 defaultChannelLabel + upChannelLabel, upChannelDescription, tags, this::handleUpCommands,
77                 () -> upState);
78         downChannelId = addChannel("Switch", new ChannelTypeUID(BINDING_ID, MINISERVER_CHANNEL_TYPE_SWITCH),
79                 defaultChannelLabel + downChannelLabel, downChannelDescription, tags, this::handleDownCommands,
80                 () -> downState);
81     }
82
83     private void handleUpCommands(Command command) throws IOException {
84         if (command instanceof OnOffType) {
85             if ((OnOffType) command == OnOffType.ON && upState == OnOffType.OFF) {
86                 setStates(OnOffType.ON, OnOffType.OFF);
87                 sendAction(CMD_UP_ON);
88             } else if (upState == OnOffType.ON) {
89                 setStates(OnOffType.OFF, OnOffType.OFF);
90                 sendAction(CMD_UP_OFF);
91             }
92         }
93     }
94
95     private void handleDownCommands(Command command) throws IOException {
96         if (command instanceof OnOffType) {
97             if ((OnOffType) command == OnOffType.ON && downState == OnOffType.OFF) {
98                 setStates(OnOffType.OFF, OnOffType.ON);
99                 sendAction(CMD_DOWN_ON);
100             } else if (downState == OnOffType.ON) {
101                 setStates(OnOffType.OFF, OnOffType.OFF);
102                 sendAction(CMD_DOWN_OFF);
103             }
104         }
105     }
106
107     private void setStates(OnOffType upState, OnOffType downState) {
108         this.upState = upState;
109         this.downState = downState;
110         setChannelState(upChannelId, upState);
111         setChannelState(downChannelId, downState);
112     }
113 }