2 * Copyright (c) 2010-2024 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.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.ServiceScope;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This class provides the actions available for the Helios device
36 * @author Bernhard Bauer - Initial contribution
38 @Component(scope = ServiceScope.PROTOTYPE, service = HeliosEasyControlsActions.class)
39 @ThingActionsScope(name = "modbus.helioseasycontrols")
41 public class HeliosEasyControlsActions implements ThingActions {
43 private @Nullable HeliosEasyControlsHandler handler;
45 private final Logger logger = LoggerFactory.getLogger(HeliosEasyControlsActions.class);
48 public void setThingHandler(@Nullable ThingHandler handler) {
49 this.handler = (HeliosEasyControlsHandler) handler;
53 public @Nullable ThingHandler getThingHandler() {
57 private void triggerSwitch(String variableName) {
59 if (handler != null) {
60 handler.writeValue(variableName, "1");
62 } catch (HeliosException e) {
63 logger.warn("Error executing action triggering switch for variable {}: {}", variableName, e.getMessage());
64 } catch (InterruptedException e) {
66 "{} encountered Exception when trying to lock Semaphore for writing variable {} to the device: {}",
67 HeliosEasyControlsActions.class.getSimpleName(), variableName, e.getMessage());
71 @RuleAction(label = "@text/action.resetFilterChangeTimer.label", description = "@text/action.resetFilterChangeTimer.description")
72 public void resetFilterChangeTimer() {
73 triggerSwitch(HeliosEasyControlsBindingConstants.FILTER_CHANGE_RESET);
76 public static void resetFilterChangeTimer(ThingActions actions) {
77 ((HeliosEasyControlsActions) actions).resetFilterChangeTimer();
80 @RuleAction(label = "@text/action.resetErrors.label", description = "@text/action.resetErrors.description")
81 public void resetErrors() {
82 triggerSwitch(HeliosEasyControlsBindingConstants.RESET_FLAG);
85 public static void resetErrors(ThingActions actions) {
86 ((HeliosEasyControlsActions) actions).resetErrors();
89 @RuleAction(label = "@text/action.resetToFactoryDefaults.label", description = "@text/action.resetToFactoryDefaults.description")
90 public void resetToFactoryDefaults() {
91 triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_RESET);
94 public static void resetToFactoryDefaults(ThingActions actions) {
95 ((HeliosEasyControlsActions) actions).resetToFactoryDefaults();
98 @RuleAction(label = "@text/action.resetSwitchingTimes.label", description = "@text/action.resetSwitchingTimes.description")
99 public void resetSwitchingTimes() {
100 triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_SETTING_WZU);
103 public static void resetSwitchingTimes(ThingActions actions) {
104 ((HeliosEasyControlsActions) actions).resetSwitchingTimes();
107 @RuleAction(label = "@text/action.setSysDateTime.label", description = "@text/action.setSysDateTime.description")
108 public void setSysDateTime() {
109 HeliosEasyControlsHandler handler = this.handler;
110 if (handler != null) {
112 handler.setSysDateTime();
113 } catch (InterruptedException e) {
115 "{} encountered Exception when trying to lock Semaphore for setting system date and time on the device: {}",
116 HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
121 public static void setSysDateTime(ThingActions actions) {
122 ((HeliosEasyControlsActions) actions).setSysDateTime();
125 private void setBypass(boolean from, int day, int month) {
126 HeliosEasyControlsHandler handler = this.handler;
127 if (handler != null) {
129 handler.setBypass(from, day, month);
130 } catch (InterruptedException e) {
132 "{} encountered Exception when trying to lock Semaphore for setting bypass date on the device: {}",
133 HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
138 @RuleAction(label = "@text/action.setBypassFrom.label", description = "@text/action.setBypassFrom.description")
139 public void setBypassFrom(
140 @ActionInput(name = "day", label = "@text/action.setBypassFrom.inputParams.day.label", description = "@text/action.setBypassFrom.inputParams.day.description") int day,
141 @ActionInput(name = "month", label = "@text/action.setBypassFrom.inputParams.month.label", description = "@text/action.setBypassFrom.inputParams.month.description") int month) {
142 setBypass(true, day, month);
145 public static void setBypassFrom(ThingActions actions, int day, int month) {
146 ((HeliosEasyControlsActions) actions).setBypassFrom(day, month);
149 @RuleAction(label = "@text/action.setBypassTo.label", description = "@text/action.setBypassTo.description")
150 public void setBypassTo(
151 @ActionInput(name = "day", label = "@text/action.setBypassTo.inputParams.day.label", description = "@text/action.setBypassTo.inputParams.day.description") int day,
152 @ActionInput(name = "month", label = "@text/action.setBypassTo.inputParams.day.label", description = "@text/action.setBypassTo.inputParams.day.description") int month) {
153 setBypass(false, day, month);
156 public static void setBypassTo(ThingActions actions, int day, int month) {
157 ((HeliosEasyControlsActions) actions).setBypassTo(day, month);
160 @RuleAction(label = "@text/action.getErrorMessages.label", description = "@text/action.getErrorMessages.description")
161 public @ActionOutput(name = "errorMessages", type = "java.util.List<String>") List<String> getErrorMessages() {
162 return (handler != null) ? handler.getErrorMessages() : new ArrayList<>();
165 public static List<String> getErrorMessages(ThingActions actions) {
166 return ((HeliosEasyControlsActions) actions).getErrorMessages();
169 @RuleAction(label = "@text/action.getWarningMessages.label", description = "@text/action.getWarningMessages.description")
170 public @ActionOutput(name = "warningMessages", type = "java.util.List<String>") List<String> getWarningMessages() {
171 return (handler != null) ? handler.getWarningMessages() : new ArrayList<>();
174 public static List<String> getWarningMessages(ThingActions actions) {
175 return ((HeliosEasyControlsActions) actions).getWarningMessages();
178 @RuleAction(label = "@text/action.getInfoMessages.label", description = "@text/action.getInfoMessages.description")
179 public @ActionOutput(name = "infoMessages", type = "java.util.List<String>") List<String> getInfoMessages() {
180 return (handler != null) ? handler.getInfoMessages() : new ArrayList<>();
183 public static List<String> getInfoMessages(ThingActions actions) {
184 return ((HeliosEasyControlsActions) actions).getInfoMessages();
187 @RuleAction(label = "@text/action.getStatusMessages.label", description = "@text/action.getStatusMessages.description")
188 public @ActionOutput(name = "statusMessages", type = "java.util.List<String>") List<String> getStatusMessages() {
189 return (handler != null) ? handler.getStatusMessages() : new ArrayList<>();
192 public static List<String> getStatusMessages(ThingActions actions) {
193 return ((HeliosEasyControlsActions) actions).getStatusMessages();
196 @RuleAction(label = "@text/action.getMessages.label", description = "@text/action.getMessages.description")
197 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() {
198 Map<String, Object> messages = new HashMap<>();
199 HeliosEasyControlsHandler handler = this.handler;
200 if (handler != null) {
201 messages.put("errorMessages", handler.getErrorMessages());
202 messages.put("warningMessages", handler.getWarningMessages());
203 messages.put("infoMessages", handler.getInfoMessages());
204 messages.put("statusMessages", handler.getStatusMessages());
209 public static Map<String, Object> getMessages(ThingActions actions) {
210 return ((HeliosEasyControlsActions) actions).getMessages();