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