]> git.basschouten.com Git - openhab-addons.git/blob
875b2885c9bf32e7ddef9a8780a7feb0fdd9bbe6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.nikohomecontrol.internal.protocol.nhc2;
14
15 import static org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.nikohomecontrol.internal.protocol.NhcAction;
20 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlCommunication;
21 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlConstants.ActionType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * The {@link NhcAction2} class represents the action Niko Home Control II communication object. It contains all fields
27  * representing a Niko Home Control action and has methods to trigger the action in Niko Home Control and receive action
28  * updates.
29  *
30  * @author Mark Herwege - Initial Contribution
31  */
32 @NonNullByDefault
33 public class NhcAction2 extends NhcAction {
34
35     private final Logger logger = LoggerFactory.getLogger(NhcAction2.class);
36
37     private volatile boolean booleanState;
38     private String model;
39     private String technology;
40
41     NhcAction2(String id, String name, String model, String technology, ActionType type, @Nullable String location,
42             NikoHomeControlCommunication nhcComm) {
43         super(id, name, type, location, nhcComm);
44         this.model = model;
45         this.technology = technology;
46     }
47
48     /**
49      * Get on/off state of action.
50      * <p>
51      * true for on, false for off
52      *
53      * @return action on/off state
54      */
55     boolean booleanState() {
56         return booleanState;
57     }
58
59     @Override
60     public int getState() {
61         return booleanState ? state : 0;
62     }
63
64     /**
65      * Sets on/off state of action.
66      *
67      * @param state - boolean false for on, true for off
68      */
69     public void setBooleanState(boolean state) {
70         booleanState = state;
71         if (getType().equals(ActionType.DIMMER)) {
72             if (booleanState) {
73                 // only send stored brightness value if on
74                 updateState();
75             } else {
76                 updateState(0);
77             }
78         } else {
79             if (booleanState) {
80                 this.state = 100;
81                 updateState(100);
82             } else {
83                 this.state = 0;
84                 updateState(0);
85             }
86         }
87     }
88
89     /**
90      * Sets state of action. This version is used for Niko Home Control II.
91      *
92      * @param state - The allowed values depend on the action type.
93      *            switch action: 0 or 100
94      *            dimmer action: between 0 and 100
95      *            rollershutter action: between 0 and 100
96      */
97     @Override
98     public void setState(int state) {
99         this.state = state;
100         if (getType().equals(ActionType.DIMMER)) { // for dimmers, only send the update to the event
101                                                    // handler if on
102             if (booleanState) {
103                 updateState();
104             }
105         } else {
106             updateState();
107         }
108     }
109
110     /**
111      * Sends action to Niko Home Control. This version is used for Niko Home Control II, that has extra status options.
112      *
113      * @param command - The allowed values depend on the action type.
114      *            switch action: On or Off
115      *            dimmer action: between 0 and 100, On or Off
116      *            rollershutter action: between 0 and 100, Up, Down or Stop
117      */
118     @Override
119     public void execute(String command) {
120         logger.debug("execute action {} of type {} for {}", command, type, id);
121
122         String cmd;
123         if ("flag".equals(model)) {
124             cmd = NHCON.equals(command) ? NHCTRUE : NHCFALSE;
125         } else {
126             cmd = command;
127         }
128
129         nhcComm.executeAction(id, cmd);
130     }
131
132     /**
133      * @return model as returned from Niko Home Control
134      */
135     public String getModel() {
136         return model;
137     }
138
139     /**
140      * @return technology as returned from Niko Home Control
141      */
142     public String getTechnology() {
143         return technology;
144     }
145 }