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