]> git.basschouten.com Git - openhab-addons.git/blob
101d46f0cf60f51f598cbadc23d36c0df2fcb747
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.modbus.helioseasycontrols.internal;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
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;
32
33 /**
34  * This class provides the actions available for the Helios device
35  *
36  * @author Bernhard Bauer - Initial contribution
37  */
38 @Component(scope = ServiceScope.PROTOTYPE, service = HeliosEasyControlsActions.class)
39 @ThingActionsScope(name = "modbus.helioseasycontrols")
40 @NonNullByDefault
41 public class HeliosEasyControlsActions implements ThingActions {
42
43     private @Nullable HeliosEasyControlsHandler handler;
44
45     private final Logger logger = LoggerFactory.getLogger(HeliosEasyControlsActions.class);
46
47     @Override
48     public void setThingHandler(@Nullable ThingHandler handler) {
49         this.handler = (HeliosEasyControlsHandler) handler;
50     }
51
52     @Override
53     public @Nullable ThingHandler getThingHandler() {
54         return handler;
55     }
56
57     private void triggerSwitch(String variableName) {
58         try {
59             if (handler != null) {
60                 handler.writeValue(variableName, "1");
61             }
62         } catch (HeliosException e) {
63             logger.warn("Error executing action triggering switch for variable {}: {}", variableName, e.getMessage());
64         } catch (InterruptedException e) {
65             logger.debug(
66                     "{} encountered Exception when trying to lock Semaphore for writing variable {} to the device: {}",
67                     HeliosEasyControlsActions.class.getSimpleName(), variableName, e.getMessage());
68         }
69     }
70
71     @RuleAction(label = "@text/action.resetFilterChangeTimer.label", description = "@text/action.resetFilterChangeTimer.description")
72     public void resetFilterChangeTimer() {
73         triggerSwitch(HeliosEasyControlsBindingConstants.FILTER_CHANGE_RESET);
74     }
75
76     public static void resetFilterChangeTimer(ThingActions actions) {
77         ((HeliosEasyControlsActions) actions).resetFilterChangeTimer();
78     }
79
80     @RuleAction(label = "@text/action.resetErrors.label", description = "@text/action.resetErrors.description")
81     public void resetErrors() {
82         triggerSwitch(HeliosEasyControlsBindingConstants.RESET_FLAG);
83     }
84
85     public static void resetErrors(ThingActions actions) {
86         ((HeliosEasyControlsActions) actions).resetErrors();
87     }
88
89     @RuleAction(label = "@text/action.resetToFactoryDefaults.label", description = "@text/action.resetToFactoryDefaults.description")
90     public void resetToFactoryDefaults() {
91         triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_RESET);
92     }
93
94     public static void resetToFactoryDefaults(ThingActions actions) {
95         ((HeliosEasyControlsActions) actions).resetToFactoryDefaults();
96     }
97
98     @RuleAction(label = "@text/action.resetSwitchingTimes.label", description = "@text/action.resetSwitchingTimes.description")
99     public void resetSwitchingTimes() {
100         triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_SETTING_WZU);
101     }
102
103     public static void resetSwitchingTimes(ThingActions actions) {
104         ((HeliosEasyControlsActions) actions).resetSwitchingTimes();
105     }
106
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) {
111             try {
112                 handler.setSysDateTime();
113             } catch (InterruptedException e) {
114                 logger.debug(
115                         "{} encountered Exception when trying to lock Semaphore for setting system date and time on the device: {}",
116                         HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
117             }
118         }
119     }
120
121     public static void setSysDateTime(ThingActions actions) {
122         ((HeliosEasyControlsActions) actions).setSysDateTime();
123     }
124
125     private void setBypass(boolean from, int day, int month) {
126         HeliosEasyControlsHandler handler = this.handler;
127         if (handler != null) {
128             try {
129                 handler.setBypass(from, day, month);
130             } catch (InterruptedException e) {
131                 logger.debug(
132                         "{} encountered Exception when trying to lock Semaphore for setting bypass date on the device: {}",
133                         HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
134             }
135         }
136     }
137
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);
143     }
144
145     public static void setBypassFrom(ThingActions actions, int day, int month) {
146         ((HeliosEasyControlsActions) actions).setBypassFrom(day, month);
147     }
148
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);
154     }
155
156     public static void setBypassTo(ThingActions actions, int day, int month) {
157         ((HeliosEasyControlsActions) actions).setBypassTo(day, month);
158     }
159
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<String>();
163     }
164
165     public static List<String> getErrorMessages(ThingActions actions) {
166         return ((HeliosEasyControlsActions) actions).getErrorMessages();
167     }
168
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<String>();
172     }
173
174     public static List<String> getWarningMessages(ThingActions actions) {
175         return ((HeliosEasyControlsActions) actions).getWarningMessages();
176     }
177
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<String>();
181     }
182
183     public static List<String> getInfoMessages(ThingActions actions) {
184         return ((HeliosEasyControlsActions) actions).getInfoMessages();
185     }
186
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<String>();
190     }
191
192     public static List<String> getStatusMessages(ThingActions actions) {
193         return ((HeliosEasyControlsActions) actions).getStatusMessages();
194     }
195
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());
205         }
206         return messages;
207     }
208
209     public static Map<String, Object> getMessages(ThingActions actions) {
210         return ((HeliosEasyControlsActions) actions).getMessages();
211     }
212 }