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