]> git.basschouten.com Git - openhab-addons.git/blob
9d64feb5e7932a5170ea6f208d704e9937a56ea6
[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;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlConstants.ActionType;
18 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc1.NhcAction1;
19 import org.openhab.binding.nikohomecontrol.internal.protocol.nhc2.NhcAction2;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link NhcAction} class represents the action Niko Home Control 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. Specific implementation are {@link NhcAction1} and {@link NhcAction2}.
27  *
28  * @author Mark Herwege - Initial Contribution
29  */
30 @NonNullByDefault
31 public abstract class NhcAction {
32
33     private final Logger logger = LoggerFactory.getLogger(NhcAction.class);
34
35     protected NikoHomeControlCommunication nhcComm;
36
37     protected String id;
38     protected String name;
39     protected ActionType type;
40     protected @Nullable String location;
41     protected volatile int state;
42     protected volatile int closeTime = 0;
43     protected volatile int openTime = 0;
44
45     @Nullable
46     private NhcActionEvent eventHandler;
47
48     protected NhcAction(String id, String name, ActionType type, @Nullable String location,
49             NikoHomeControlCommunication nhcComm) {
50         this.id = id;
51         this.name = name;
52         this.type = type;
53         this.location = location;
54         this.nhcComm = nhcComm;
55     }
56
57     /**
58      * This method should be called when an object implementing the {@NhcActionEvent} interface is initialized.
59      * It keeps a record of the event handler in that object so it can be updated when the action receives an update
60      * from the Niko Home Control IP-interface.
61      *
62      * @param eventHandler
63      */
64     public void setEventHandler(NhcActionEvent eventHandler) {
65         this.eventHandler = eventHandler;
66     }
67
68     /**
69      * This method should be called when an object implementing the {@NhcActionEvent} interface is disposed.
70      * It resets the reference, so no updates go to the handler anymore.
71      *
72      */
73     public void unsetEventHandler() {
74         this.eventHandler = null;
75     }
76
77     /**
78      * Get id of action.
79      *
80      * @return id
81      */
82     public String getId() {
83         return id;
84     }
85
86     /**
87      * Get name of action.
88      *
89      * @return action name
90      */
91     public String getName() {
92         return name;
93     }
94
95     /**
96      * Set name of action.
97      *
98      * @param name action name
99      */
100     public void setName(String name) {
101         this.name = name;
102     }
103
104     /**
105      * Get type of action identified.
106      * <p>
107      * ActionType can be RELAY (for simple light or socket switch), DIMMER, ROLLERSHUTTER, TRIGGER or GENERIC.
108      *
109      * @return {@link ActionType}
110      */
111     public ActionType getType() {
112         return type;
113     }
114
115     /**
116      * Get location name of action.
117      *
118      * @return location name
119      */
120     public @Nullable String getLocation() {
121         return location;
122     }
123
124     /**
125      * Set location name of action.
126      *
127      * @param location action location name
128      */
129     public void setLocation(@Nullable String location) {
130         this.location = location;
131     }
132
133     /**
134      * Get state of action.
135      * <p>
136      * State is a value between 0 and 100 for a dimmer or rollershutter.
137      * Rollershutter state is 0 for fully closed and 100 for fully open.
138      * State is 0 or 100 for a switch.
139      *
140      * @return action state
141      */
142     public int getState() {
143         return state;
144     }
145
146     /**
147      * Set openTime and closeTime for rollershutter action.
148      * <p>
149      * Time is in seconds to fully open or close a rollershutter.
150      *
151      * @param openTime
152      * @param closeTime
153      */
154     public void setShutterTimes(int openTime, int closeTime) {
155         this.openTime = openTime;
156         this.closeTime = closeTime;
157     }
158
159     /**
160      * Get openTime of action.
161      * <p>
162      * openTime is the time in seconds to fully open a rollershutter.
163      *
164      * @return action openTime
165      */
166     public int getOpenTime() {
167         return openTime;
168     }
169
170     /**
171      * Get closeTime of action.
172      * <p>
173      * closeTime is the time in seconds to fully close a rollershutter.
174      *
175      * @return action closeTime
176      */
177     public int getCloseTime() {
178         return closeTime;
179     }
180
181     protected void updateState() {
182         updateState(state);
183     }
184
185     protected void updateState(int state) {
186         NhcActionEvent eventHandler = this.eventHandler;
187         if (eventHandler != null) {
188             logger.debug("update channel state for {} with {}", id, state);
189             eventHandler.actionEvent(state);
190         }
191     }
192
193     /**
194      * Method called when action is removed from the Niko Home Control Controller.
195      */
196     public void actionRemoved() {
197         logger.debug("action removed {}, {}", id, name);
198         NhcActionEvent eventHandler = this.eventHandler;
199         if (eventHandler != null) {
200             eventHandler.actionRemoved();
201             unsetEventHandler();
202         }
203     }
204
205     /**
206      * Sets state of action. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
207      *
208      * @param state - The allowed values depend on the action type.
209      *            switch action: 0 or 100
210      *            dimmer action: between 0 and 100
211      *            rollershutter action: between 0 and 100
212      */
213     public abstract void setState(int state);
214
215     /**
216      * Sends action to Niko Home Control. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
217      *
218      * @param command - The allowed values depend on the action type.
219      *            switch action: On or Off
220      *            dimmer action: between 0 and 100, On or Off
221      *            rollershutter action: between 0 and 100, Up, Down or Stop
222      */
223     public abstract void execute(String command);
224 }