]> git.basschouten.com Git - openhab-addons.git/blob
e281a6d16df4461615a25bd81b731f6f26c0d741
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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 deviceType;
39     private String deviceTechnology;
40     private String deviceModel;
41
42     NhcAction2(String id, String name, String deviceType, String deviceTechnology, String deviceModel,
43             @Nullable String location, ActionType type, NikoHomeControlCommunication nhcComm) {
44         super(id, name, type, location, nhcComm);
45         this.deviceType = deviceType;
46         this.deviceTechnology = deviceTechnology;
47         this.deviceModel = deviceModel;
48     }
49
50     /**
51      * Get on/off state of action.
52      * <p>
53      * true for on, false for off
54      *
55      * @return action on/off state
56      */
57     boolean booleanState() {
58         return booleanState;
59     }
60
61     @Override
62     public int getState() {
63         return booleanState ? state : 0;
64     }
65
66     /**
67      * Sets on/off state of action.
68      *
69      * @param state - boolean false for on, true for off
70      */
71     public void setBooleanState(boolean state) {
72         booleanState = state;
73         if (getType().equals(ActionType.DIMMER)) {
74             if (booleanState) {
75                 // only send stored brightness value if on
76                 updateState();
77             } else {
78                 updateState(0);
79             }
80         } else {
81             if (booleanState) {
82                 this.state = 100;
83                 updateState(100);
84             } else {
85                 this.state = 0;
86                 updateState(0);
87             }
88         }
89     }
90
91     /**
92      * Sets state of action. This version is used for Niko Home Control II.
93      *
94      * @param state - The allowed values depend on the action type.
95      *            switch action: 0 or 100
96      *            dimmer action: between 0 and 100
97      *            rollershutter action: between 0 and 100
98      */
99     @Override
100     public void setState(int state) {
101         this.state = state;
102         if (getType().equals(ActionType.DIMMER)) { // for dimmers, only send the update to the event
103                                                    // handler if on
104             if (booleanState) {
105                 updateState();
106             }
107         } else {
108             updateState();
109         }
110     }
111
112     /**
113      * Sends action to Niko Home Control. This version is used for Niko Home Control II, that has extra status options.
114      *
115      * @param command - The allowed values depend on the action type.
116      *            switch action: On or Off
117      *            dimmer action: between 0 and 100, On or Off
118      *            rollershutter action: between 0 and 100, Up, Down or Stop
119      */
120     @Override
121     public void execute(String command) {
122         logger.debug("execute action {} of type {} for {}", command, type, id);
123
124         String cmd;
125         if ("flag".equals(deviceModel)) {
126             cmd = NHCON.equals(command) ? NHCTRUE : NHCFALSE;
127         } else {
128             cmd = command;
129         }
130
131         nhcComm.executeAction(id, cmd);
132     }
133
134     /**
135      * @return type as returned from Niko Home Control
136      */
137     public String getDeviceType() {
138         return deviceType;
139     }
140
141     /**
142      * @return technology as returned from Niko Home Control
143      */
144     public String getDeviceTechnology() {
145         return deviceTechnology;
146     }
147
148     /**
149      * @return model as returned from Niko Home Control
150      */
151     public String getDeviceModel() {
152         return deviceModel;
153     }
154 }