]> git.basschouten.com Git - openhab-addons.git/blob
bda23e28dfb6b352f06c303942c1621c3fc825a3
[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      * Get id of action.
70      *
71      * @return id
72      */
73     public String getId() {
74         return id;
75     }
76
77     /**
78      * Get name of action.
79      *
80      * @return action name
81      */
82     public String getName() {
83         return name;
84     }
85
86     /**
87      * Set name of action.
88      *
89      * @param name action name
90      */
91     public void setName(String name) {
92         this.name = name;
93     }
94
95     /**
96      * Get type of action identified.
97      * <p>
98      * ActionType can be RELAY (for simple light or socket switch), DIMMER, ROLLERSHUTTER, TRIGGER or GENERIC.
99      *
100      * @return {@link ActionType}
101      */
102     public ActionType getType() {
103         return type;
104     }
105
106     /**
107      * Get location name of action.
108      *
109      * @return location name
110      */
111     public @Nullable String getLocation() {
112         return location;
113     }
114
115     /**
116      * Set location name of action.
117      *
118      * @param location action location name
119      */
120     public void setLocation(@Nullable String location) {
121         this.location = location;
122     }
123
124     /**
125      * Get state of action.
126      * <p>
127      * State is a value between 0 and 100 for a dimmer or rollershutter.
128      * Rollershutter state is 0 for fully closed and 100 for fully open.
129      * State is 0 or 100 for a switch.
130      *
131      * @return action state
132      */
133     public int getState() {
134         return state;
135     }
136
137     /**
138      * Set openTime and closeTime for rollershutter action.
139      * <p>
140      * Time is in seconds to fully open or close a rollershutter.
141      *
142      * @param openTime
143      * @param closeTime
144      */
145     public void setShutterTimes(int openTime, int closeTime) {
146         this.openTime = openTime;
147         this.closeTime = closeTime;
148     }
149
150     /**
151      * Get openTime of action.
152      * <p>
153      * openTime is the time in seconds to fully open a rollershutter.
154      *
155      * @return action openTime
156      */
157     public int getOpenTime() {
158         return openTime;
159     }
160
161     /**
162      * Get closeTime of action.
163      * <p>
164      * closeTime is the time in seconds to fully close a rollershutter.
165      *
166      * @return action closeTime
167      */
168     public int getCloseTime() {
169         return closeTime;
170     }
171
172     protected void updateState() {
173         updateState(state);
174     }
175
176     protected void updateState(int state) {
177         NhcActionEvent eventHandler = this.eventHandler;
178         if (eventHandler != null) {
179             logger.debug("update channel state for {} with {}", id, state);
180             eventHandler.actionEvent(state);
181         }
182     }
183
184     /**
185      * Method called when action is removed from the Niko Home Control Controller.
186      */
187     public void actionRemoved() {
188         logger.debug("action removed {}, {}", id, name);
189         NhcActionEvent eventHandler = this.eventHandler;
190         if (eventHandler != null) {
191             eventHandler.actionRemoved();
192         }
193     }
194
195     /**
196      * Sets state of action. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
197      *
198      * @param state - The allowed values depend on the action type.
199      *            switch action: 0 or 100
200      *            dimmer action: between 0 and 100
201      *            rollershutter action: between 0 and 100
202      */
203     public abstract void setState(int state);
204
205     /**
206      * Sends action to Niko Home Control. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
207      *
208      * @param command - The allowed values depend on the action type.
209      *            switch action: On or Off
210      *            dimmer action: between 0 and 100, On or Off
211      *            rollershutter action: between 0 and 100, Up, Down or Stop
212      */
213     public abstract void execute(String command);
214 }