2 * Copyright (c) 2010-2023 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.modbus.helioseasycontrols.internal;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.ActionOutput;
24 import org.openhab.core.automation.annotation.RuleAction;
25 import org.openhab.core.thing.binding.ThingActions;
26 import org.openhab.core.thing.binding.ThingActionsScope;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This class provides the actions available for the Helios device
34 * @author Bernhard Bauer - Initial contribution
36 @ThingActionsScope(name = "modbus.helioseasycontrols")
38 public class HeliosEasyControlsActions implements ThingActions {
40 private @Nullable HeliosEasyControlsHandler handler;
42 private final Logger logger = LoggerFactory.getLogger(HeliosEasyControlsActions.class);
45 public void setThingHandler(@Nullable ThingHandler handler) {
46 this.handler = (HeliosEasyControlsHandler) handler;
50 public @Nullable ThingHandler getThingHandler() {
54 private void triggerSwitch(String variableName) {
56 if (handler != null) {
58 handler.writeValue(variableName, "1");
60 } catch (HeliosException e) {
61 logger.warn("Error executing action triggering switch for variable {}: {}", variableName, e.getMessage());
62 } catch (InterruptedException e) {
64 "{} encountered Exception when trying to lock Semaphore for writing variable {} to the device: {}",
65 HeliosEasyControlsActions.class.getSimpleName(), variableName, e.getMessage());
69 @RuleAction(label = "@text/action.resetFilterChangeTimer.label", description = "@text/action.resetFilterChangeTimer.description")
70 public void resetFilterChangeTimer() {
71 triggerSwitch(HeliosEasyControlsBindingConstants.FILTER_CHANGE_RESET);
74 public static void resetFilterChangeTimer(ThingActions actions) {
75 ((HeliosEasyControlsActions) actions).resetFilterChangeTimer();
78 @RuleAction(label = "@text/action.resetErrors.label", description = "@text/action.resetErrors.description")
79 public void resetErrors() {
80 triggerSwitch(HeliosEasyControlsBindingConstants.RESET_FLAG);
83 public static void resetErrors(ThingActions actions) {
84 ((HeliosEasyControlsActions) actions).resetErrors();
87 @RuleAction(label = "@text/action.resetToFactoryDefaults.label", description = "@text/action.resetToFactoryDefaults.description")
88 public void resetToFactoryDefaults() {
89 triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_RESET);
92 public static void resetToFactoryDefaults(ThingActions actions) {
93 ((HeliosEasyControlsActions) actions).resetToFactoryDefaults();
96 @RuleAction(label = "@text/action.resetSwitchingTimes.label", description = "@text/action.resetSwitchingTimes.description")
97 public void resetSwitchingTimes() {
98 triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_SETTING_WZU);
101 public static void resetSwitchingTimes(ThingActions actions) {
102 ((HeliosEasyControlsActions) actions).resetSwitchingTimes();
105 @RuleAction(label = "@text/action.setSysDateTime.label", description = "@text/action.setSysDateTime.description")
106 public void setSysDateTime() {
107 HeliosEasyControlsHandler handler = this.handler;
108 if (handler != null) {
110 handler.setSysDateTime();
111 } catch (InterruptedException e) {
113 "{} encountered Exception when trying to lock Semaphore for setting system date and time on the device: {}",
114 HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
119 public static void setSysDateTime(ThingActions actions) {
120 ((HeliosEasyControlsActions) actions).setSysDateTime();
123 private void setBypass(boolean from, int day, int month) {
124 HeliosEasyControlsHandler handler = this.handler;
125 if (handler != null) {
127 handler.setBypass(from, day, month);
128 } catch (InterruptedException e) {
130 "{} encountered Exception when trying to lock Semaphore for setting bypass date on the device: {}",
131 HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
136 @RuleAction(label = "@text/action.setBypassFrom.label", description = "@text/action.setBypassFrom.description")
137 public void setBypassFrom(
138 @ActionInput(name = "day", label = "@text/action.setBypassFrom.inputParams.day.label", description = "@text/action.setBypassFrom.inputParams.day.description") int day,
139 @ActionInput(name = "month", label = "@text/action.setBypassFrom.inputParams.month.label", description = "@text/action.setBypassFrom.inputParams.month.description") int month) {
140 setBypass(true, day, month);
143 public static void setBypassFrom(ThingActions actions, int day, int month) {
144 ((HeliosEasyControlsActions) actions).setBypassFrom(day, month);
147 @RuleAction(label = "@text/action.setBypassTo.label", description = "@text/action.setBypassTo.description")
148 public void setBypassTo(
149 @ActionInput(name = "day", label = "@text/action.setBypassTo.inputParams.day.label", description = "@text/action.setBypassTo.inputParams.day.description") int day,
150 @ActionInput(name = "month", label = "@text/action.setBypassTo.inputParams.day.label", description = "@text/action.setBypassTo.inputParams.day.description") int month) {
151 setBypass(false, day, month);
154 public static void setBypassTo(ThingActions actions, int day, int month) {
155 ((HeliosEasyControlsActions) actions).setBypassTo(day, month);
158 @RuleAction(label = "@text/action.getErrorMessages.label", description = "@text/action.getErrorMessages.description")
159 public @ActionOutput(name = "errorMessages", type = "java.util.List<String>") List<String> getErrorMessages() {
160 return (handler != null) ? handler.getErrorMessages() : new ArrayList<String>();
163 public static List<String> getErrorMessages(ThingActions actions) {
164 return ((HeliosEasyControlsActions) actions).getErrorMessages();
167 @RuleAction(label = "@text/action.getWarningMessages.label", description = "@text/action.getWarningMessages.description")
168 public @ActionOutput(name = "warningMessages", type = "java.util.List<String>") List<String> getWarningMessages() {
169 return (handler != null) ? handler.getWarningMessages() : new ArrayList<String>();
172 public static List<String> getWarningMessages(ThingActions actions) {
173 return ((HeliosEasyControlsActions) actions).getWarningMessages();
176 @RuleAction(label = "@text/action.getInfoMessages.label", description = "@text/action.getInfoMessages.description")
177 public @ActionOutput(name = "infoMessages", type = "java.util.List<String>") List<String> getInfoMessages() {
178 return (handler != null) ? handler.getInfoMessages() : new ArrayList<String>();
181 public static List<String> getInfoMessages(ThingActions actions) {
182 return ((HeliosEasyControlsActions) actions).getInfoMessages();
185 @RuleAction(label = "@text/action.getStatusMessages.label", description = "@text/action.getStatusMessages.description")
186 public @ActionOutput(name = "statusMessages", type = "java.util.List<String>") List<String> getStatusMessages() {
187 return (handler != null) ? handler.getStatusMessages() : new ArrayList<String>();
190 public static List<String> getStatusMessages(ThingActions actions) {
191 return ((HeliosEasyControlsActions) actions).getStatusMessages();
194 @RuleAction(label = "@text/action.getMessages.label", description = "@text/action.getMessages.description")
195 public @ActionOutput(name = "errorMessages", type = "java.util.List<String>") @ActionOutput(name = "warningMessages", type = "java.util.List<String>") @ActionOutput(name = "infoMessages", type = "java.util.List<String>") @ActionOutput(name = "statusMessages", type = "java.util.List<String>") Map<String, Object> getMessages() {
196 Map<String, Object> messages = new HashMap<>();
197 HeliosEasyControlsHandler handler = this.handler;
198 if (handler != null) {
199 messages.put("errorMessages", handler.getErrorMessages());
200 messages.put("warningMessages", handler.getWarningMessages());
201 messages.put("infoMessages", handler.getInfoMessages());
202 messages.put("statusMessages", handler.getStatusMessages());
207 public static Map<String, Object> getMessages(ThingActions actions) {
208 return ((HeliosEasyControlsActions) actions).getMessages();