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;
73 public String getId() {
82 public String getName() {
89 * @param name action name
91 public void setName(String name) {
96 * Get type of action identified.
98 * ActionType can be RELAY (for simple light or socket switch), DIMMER, ROLLERSHUTTER, TRIGGER or GENERIC.
100 * @return {@link ActionType}
102 public ActionType getType() {
107 * Get location name of action.
109 * @return location name
111 public @Nullable String getLocation() {
116 * Set location name of action.
118 * @param location action location name
120 public void setLocation(@Nullable String location) {
121 this.location = location;
125 * Get state of action.
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.
131 * @return action state
133 public int getState() {
138 * Set openTime and closeTime for rollershutter action.
140 * Time is in seconds to fully open or close a rollershutter.
145 public void setShutterTimes(int openTime, int closeTime) {
146 this.openTime = openTime;
147 this.closeTime = closeTime;
151 * Get openTime of action.
153 * openTime is the time in seconds to fully open a rollershutter.
155 * @return action openTime
157 public int getOpenTime() {
162 * Get closeTime of action.
164 * closeTime is the time in seconds to fully close a rollershutter.
166 * @return action closeTime
168 public int getCloseTime() {
172 protected void updateState() {
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);
185 * Method called when action is removed from the Niko Home Control Controller.
187 public void actionRemoved() {
188 logger.debug("action removed {}, {}", id, name);
189 NhcActionEvent eventHandler = this.eventHandler;
190 if (eventHandler != null) {
191 eventHandler.actionRemoved();
196 * Sets state of action. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
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
203 public abstract void setState(int state);
206 * Sends action to Niko Home Control. This method is implemented in {@link NhcAction1} and {@link NhcAction2}.
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
213 public abstract void execute(String command);