]> git.basschouten.com Git - openhab-addons.git/blob
e63f453ce09ee016ae6d6dd2e9d3861cf16f86f8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.automower.internal.actions;
14
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.automower.internal.things.AutomowerCommand;
21 import org.openhab.binding.automower.internal.things.AutomowerHandler;
22 import org.openhab.core.automation.annotation.ActionInput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * @author Markus Pfleger - Initial contribution
32  */
33 @ThingActionsScope(name = "automower")
34 @NonNullByDefault
35 public class AutomowerActions implements ThingActions, IAutomowerActions {
36     private final Logger logger = LoggerFactory.getLogger(AutomowerActions.class);
37     private @Nullable AutomowerHandler handler;
38
39     @Override
40     public void setThingHandler(@Nullable ThingHandler handler) {
41         this.handler = (AutomowerHandler) handler;
42     }
43
44     @Override
45     public @Nullable AutomowerHandler getThingHandler() {
46         return handler;
47     }
48
49     @Override
50     @RuleAction(label = "@text/action-start-label", description = "@text/action-start-desc")
51     public void start(
52             @ActionInput(name = "duration", label = "@text/action-input-duration-label", description = "@text/action-input-duration-desc") int durationMin) {
53         AutomowerHandler automowerHandler = handler;
54         if (automowerHandler == null) {
55             logger.warn("Automower Action service ThingHandler is null!");
56         } else {
57             automowerHandler.sendAutomowerCommand(AutomowerCommand.START, durationMin);
58         }
59     }
60
61     public static void start(@Nullable ThingActions actions, int durationMin) {
62         invokeMethodOf(actions).start(durationMin);
63     }
64
65     @Override
66     @RuleAction(label = "@text/action-pause-label", description = "@text/action-pause-desc")
67     public void pause() {
68         AutomowerHandler automowerHandler = handler;
69         if (automowerHandler == null) {
70             logger.warn("Automower Action service ThingHandler is null!");
71         } else {
72             automowerHandler.sendAutomowerCommand(AutomowerCommand.PAUSE);
73         }
74     }
75
76     public static void pause(@Nullable ThingActions actions) {
77         invokeMethodOf(actions).pause();
78     }
79
80     @Override
81     @RuleAction(label = "@text/action-parkuntilnextschedule-label", description = "@text/action-parkuntilnextschedule-desc")
82     public void parkUntilNextSchedule() {
83         AutomowerHandler automowerHandler = handler;
84         if (automowerHandler == null) {
85             logger.warn("Automower Action service ThingHandler is null!");
86         } else {
87             automowerHandler.sendAutomowerCommand(AutomowerCommand.PARK_UNTIL_NEXT_SCHEDULE);
88         }
89     }
90
91     public static void parkUntilNextSchedule(@Nullable ThingActions actions) {
92         invokeMethodOf(actions).parkUntilNextSchedule();
93     }
94
95     @Override
96     @RuleAction(label = "@text/action-parkuntilfurthernotice-label", description = "@text/action-parkuntilfurthernotice-desc")
97     public void parkUntilFurtherNotice() {
98         AutomowerHandler automowerHandler = handler;
99         if (automowerHandler == null) {
100             logger.warn("Automower Action service ThingHandler is null!");
101         } else {
102             automowerHandler.sendAutomowerCommand(AutomowerCommand.PARK_UNTIL_FURTHER_NOTICE);
103         }
104     }
105
106     public static void parkUntilFurtherNotice(@Nullable ThingActions actions) {
107         invokeMethodOf(actions).parkUntilFurtherNotice();
108     }
109
110     @Override
111     @RuleAction(label = "@text/action-park-label", description = "@text/action-park-desc")
112     public void park(
113             @ActionInput(name = "duration", label = "@text/action-input-duration-label", description = "@text/action-input-duration-desc") int durationMin) {
114         AutomowerHandler automowerHandler = handler;
115         if (automowerHandler == null) {
116             logger.warn("Automower Action service ThingHandler is null!");
117         } else {
118             automowerHandler.sendAutomowerCommand(AutomowerCommand.PARK, durationMin);
119         }
120     }
121
122     public static void park(@Nullable ThingActions actions, int durationMin) {
123         invokeMethodOf(actions).park(durationMin);
124     }
125
126     @Override
127     @RuleAction(label = "@text/action-resumeschedule-label", description = "@text/action-resumeschedule-desc")
128     public void resumeSchedule() {
129         AutomowerHandler automowerHandler = handler;
130         if (automowerHandler == null) {
131             logger.warn("Automower Action service ThingHandler is null!");
132         } else {
133             automowerHandler.sendAutomowerCommand(AutomowerCommand.RESUME_SCHEDULE);
134         }
135     }
136
137     public static void resumeSchedule(@Nullable ThingActions actions) {
138         invokeMethodOf(actions).resumeSchedule();
139     }
140
141     private static IAutomowerActions invokeMethodOf(@Nullable ThingActions actions) {
142         if (actions == null) {
143             throw new IllegalArgumentException("actions cannot be null");
144         }
145         if (actions.getClass().getName().equals(AutomowerActions.class.getName())) {
146             if (actions instanceof AutomowerActions) {
147                 return (IAutomowerActions) actions;
148             } else {
149                 return (IAutomowerActions) Proxy.newProxyInstance(IAutomowerActions.class.getClassLoader(),
150                         new Class[] { IAutomowerActions.class }, (Object proxy, Method method, Object[] args) -> {
151                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
152                                     method.getParameterTypes());
153                             return m.invoke(actions, args);
154                         });
155             }
156         }
157         throw new IllegalArgumentException("Actions is not an instance of IAutomowerActions");
158     }
159 }