2 * Copyright (c) 2010-2020 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.automower.internal.actions;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.Proxy;
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;
31 * @author Markus Pfleger - Initial contribution
33 @ThingActionsScope(name = "automower")
35 public class AutomowerActions implements ThingActions, IAutomowerActions {
36 private final Logger logger = LoggerFactory.getLogger(AutomowerActions.class);
37 private @Nullable AutomowerHandler handler;
40 public void setThingHandler(@Nullable ThingHandler handler) {
41 this.handler = (AutomowerHandler) handler;
45 public @Nullable AutomowerHandler getThingHandler() {
50 @RuleAction(label = "@text/action-start-label", description = "@text/action-start-desc")
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!");
57 automowerHandler.sendAutomowerCommand(AutomowerCommand.START, durationMin);
61 public static void start(@Nullable ThingActions actions, int durationMin) {
62 invokeMethodOf(actions).start(durationMin);
66 @RuleAction(label = "@text/action-pause-label", description = "@text/action-pause-desc")
68 AutomowerHandler automowerHandler = handler;
69 if (automowerHandler == null) {
70 logger.warn("Automower Action service ThingHandler is null!");
72 automowerHandler.sendAutomowerCommand(AutomowerCommand.PAUSE);
76 public static void pause(@Nullable ThingActions actions) {
77 invokeMethodOf(actions).pause();
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!");
87 automowerHandler.sendAutomowerCommand(AutomowerCommand.PARK_UNTIL_NEXT_SCHEDULE);
91 public static void parkUntilNextSchedule(@Nullable ThingActions actions) {
92 invokeMethodOf(actions).parkUntilNextSchedule();
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!");
102 automowerHandler.sendAutomowerCommand(AutomowerCommand.PARK_UNTIL_FURTHER_NOTICE);
106 public static void parkUntilFurtherNotice(@Nullable ThingActions actions) {
107 invokeMethodOf(actions).parkUntilFurtherNotice();
111 @RuleAction(label = "@text/action-park-label", description = "@text/action-park-desc")
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!");
118 automowerHandler.sendAutomowerCommand(AutomowerCommand.PARK, durationMin);
122 public static void park(@Nullable ThingActions actions, int durationMin) {
123 invokeMethodOf(actions).park(durationMin);
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!");
133 automowerHandler.sendAutomowerCommand(AutomowerCommand.RESUME_SCHEDULE);
137 public static void resumeSchedule(@Nullable ThingActions actions) {
138 invokeMethodOf(actions).resumeSchedule();
141 private static IAutomowerActions invokeMethodOf(@Nullable ThingActions actions) {
142 if (actions == null) {
143 throw new IllegalArgumentException("actions cannot be null");
145 if (actions.getClass().getName().equals(AutomowerActions.class.getName())) {
146 if (actions instanceof AutomowerActions) {
147 return (IAutomowerActions) actions;
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);
157 throw new IllegalArgumentException("Actions is not an instance of IAutomowerActions");