2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nikohomecontrol.internal.protocol;
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;
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}.
28 * @author Mark Herwege - Initial Contribution
31 public abstract class NhcAction {
33 private final Logger logger = LoggerFactory.getLogger(NhcAction.class);
35 protected NikoHomeControlCommunication nhcComm;
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;
46 private NhcActionEvent eventHandler;
48 protected NhcAction(String id, String name, ActionType type, @Nullable String location,
49 NikoHomeControlCommunication nhcComm) {
53 this.location = location;
54 this.nhcComm = nhcComm;
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.
64 public void setEventHandler(NhcActionEvent eventHandler) {
65 this.eventHandler = eventHandler;
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.
73 public void unsetEventHandler() {
74 this.eventHandler = null;
82 public String getId() {
91 public String getName() {
98 * @param name action name
100 public void setName(String name) {
105 * Get type of action identified.
107 * ActionType can be RELAY (for simple light or socket switch), DIMMER, ROLLERSHUTTER, TRIGGER or GENERIC.
109 * @return {@link ActionType}
111 public ActionType getType() {
116 * Get location name of action.
118 * @return location name
120 public @Nullable String getLocation() {
125 * Set location name of action.
127 * @param location action location name
129 public void setLocation(@Nullable String location) {
130 this.location = location;
134 * Get state of action.
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.
140 * @return action state
142 public int getState() {
147 * Set openTime and closeTime for rollershutter action.
149 * Time is in seconds to fully open or close a rollershutter.
154 public void setShutterTimes(int openTime, int closeTime) {
155 this.openTime = openTime;
156 this.closeTime = closeTime;
160 * Get openTime of action.
162 * openTime is the time in seconds to fully open a rollershutter.
164 * @return action openTime
166 public int getOpenTime() {
171 * Get closeTime of action.
173 * closeTime is the time in seconds to fully close a rollershutter.
175 * @return action closeTime
177 public int getCloseTime() {
181 protected void updateState() {
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);
194 * Method called when action is removed from the Niko Home Control Controller.
196 public void actionRemoved() {
197 logger.debug("action removed {}, {}", id, name);
198 NhcActionEvent eventHandler = this.eventHandler;
199 if (eventHandler != null) {
200 eventHandler.actionRemoved();
206 * Sets state of action. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
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
213 public abstract void setState(int state);
216 * Sends action to Niko Home Control. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
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
223 public abstract void execute(String command);