]> git.basschouten.com Git - openhab-addons.git/blob
327b2b3148ae18738721639798887480fab312c0
[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
58                 handler.writeValue(variableName, "1");
59             }
60         } catch (HeliosException e) {
61             logger.warn("Error executing action triggering switch for variable {}: {}", variableName, e.getMessage());
62         } catch (InterruptedException e) {
63             logger.debug(
64                     "{} encountered Exception when trying to lock Semaphore for writing variable {} to the device: {}",
65                     HeliosEasyControlsActions.class.getSimpleName(), variableName, e.getMessage());
66         }
67     }
68
69     @RuleAction(label = "@text/action.resetFilterChangeTimer.label", description = "@text/action.resetFilterChangeTimer.description")
70     public void resetFilterChangeTimer() {
71         triggerSwitch(HeliosEasyControlsBindingConstants.FILTER_CHANGE_RESET);
72     }
73
74     public static void resetFilterChangeTimer(ThingActions actions) {
75         ((HeliosEasyControlsActions) actions).resetFilterChangeTimer();
76     }
77
78     @RuleAction(label = "@text/action.resetErrors.label", description = "@text/action.resetErrors.description")
79     public void resetErrors() {
80         triggerSwitch(HeliosEasyControlsBindingConstants.RESET_FLAG);
81     }
82
83     public static void resetErrors(ThingActions actions) {
84         ((HeliosEasyControlsActions) actions).resetErrors();
85     }
86
87     @RuleAction(label = "@text/action.resetToFactoryDefaults.label", description = "@text/action.resetToFactoryDefaults.description")
88     public void resetToFactoryDefaults() {
89         triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_RESET);
90     }
91
92     public static void resetToFactoryDefaults(ThingActions actions) {
93         ((HeliosEasyControlsActions) actions).resetToFactoryDefaults();
94     }
95
96     @RuleAction(label = "@text/action.resetSwitchingTimes.label", description = "@text/action.resetSwitchingTimes.description")
97     public void resetSwitchingTimes() {
98         triggerSwitch(HeliosEasyControlsBindingConstants.FACTORY_SETTING_WZU);
99     }
100
101     public static void resetSwitchingTimes(ThingActions actions) {
102         ((HeliosEasyControlsActions) actions).resetSwitchingTimes();
103     }
104
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) {
109             try {
110                 handler.setSysDateTime();
111             } catch (InterruptedException e) {
112                 logger.debug(
113                         "{} encountered Exception when trying to lock Semaphore for setting system date and time on the device: {}",
114                         HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
115             }
116         }
117     }
118
119     public static void setSysDateTime(ThingActions actions) {
120         ((HeliosEasyControlsActions) actions).setSysDateTime();
121     }
122
123     private void setBypass(boolean from, int day, int month) {
124         HeliosEasyControlsHandler handler = this.handler;
125         if (handler != null) {
126             try {
127                 handler.setBypass(from, day, month);
128             } catch (InterruptedException e) {
129                 logger.debug(
130                         "{} encountered Exception when trying to lock Semaphore for setting bypass date on the device: {}",
131                         HeliosEasyControlsActions.class.getSimpleName(), e.getMessage());
132             }
133         }
134     }
135
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);
141     }
142
143     public static void setBypassFrom(ThingActions actions, int day, int month) {
144         ((HeliosEasyControlsActions) actions).setBypassFrom(day, month);
145     }
146
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);
152     }
153
154     public static void setBypassTo(ThingActions actions, int day, int month) {
155         ((HeliosEasyControlsActions) actions).setBypassTo(day, month);
156     }
157
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>();
161     }
162
163     public static List<String> getErrorMessages(ThingActions actions) {
164         return ((HeliosEasyControlsActions) actions).getErrorMessages();
165     }
166
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>();
170     }
171
172     public static List<String> getWarningMessages(ThingActions actions) {
173         return ((HeliosEasyControlsActions) actions).getWarningMessages();
174     }
175
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>();
179     }
180
181     public static List<String> getInfoMessages(ThingActions actions) {
182         return ((HeliosEasyControlsActions) actions).getInfoMessages();
183     }
184
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>();
188     }
189
190     public static List<String> getStatusMessages(ThingActions actions) {
191         return ((HeliosEasyControlsActions) actions).getStatusMessages();
192     }
193
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());
203         }
204         return messages;
205     }
206
207     public static Map<String, Object> getMessages(ThingActions actions) {
208         return ((HeliosEasyControlsActions) actions).getMessages();
209     }
210 }