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.volvooncall.internal.action;
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.volvooncall.internal.handler.VehicleHandler;
21 import org.openhab.core.automation.annotation.ActionInput;
22 import org.openhab.core.automation.annotation.RuleAction;
23 import org.openhab.core.thing.binding.ThingActions;
24 import org.openhab.core.thing.binding.ThingActionsScope;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@VehicleAction } class is responsible to call corresponding
31 * action on Vehicle Handler
33 * @author Gaƫl L'hopital - Initial contribution
35 @ThingActionsScope(name = "volvooncall")
37 public class VolvoOnCallActions implements ThingActions, IVolvoOnCallActions {
39 private final Logger logger = LoggerFactory.getLogger(VolvoOnCallActions.class);
41 private @Nullable VehicleHandler handler;
43 public VolvoOnCallActions() {
44 logger.info("Volvo On Call actions service instanciated");
48 public void setThingHandler(@Nullable ThingHandler handler) {
49 if (handler instanceof VehicleHandler) {
50 this.handler = (VehicleHandler) handler;
55 public @Nullable ThingHandler getThingHandler() {
60 @RuleAction(label = "Volvo On Call : Close", description = "Closes the car")
61 public void closeCarCommand() {
62 logger.debug("closeCarCommand called");
63 VehicleHandler handler = this.handler;
64 if (handler != null) {
65 handler.actionClose();
67 logger.warn("VolvoOnCall Action service ThingHandler is null!");
71 public static void closeCarCommand(@Nullable ThingActions actions) {
72 invokeMethodOf(actions).closeCarCommand();
76 @RuleAction(label = "Volvo On Call : Open", description = "Opens the car")
77 public void openCarCommand() {
78 logger.debug("openCarCommand called");
79 VehicleHandler handler = this.handler;
80 if (handler != null) {
83 logger.warn("VolvoOnCall Action service ThingHandler is null!");
87 public static void openCarCommand(@Nullable ThingActions actions) {
88 invokeMethodOf(actions).openCarCommand();
92 @RuleAction(label = "Volvo On Call : Start Engine", description = "Starts the engine")
93 public void engineStartCommand(@ActionInput(name = "runtime", label = "Runtime") @Nullable Integer runtime) {
94 logger.debug("engineStartCommand called");
95 VehicleHandler handler = this.handler;
96 if (handler != null) {
97 handler.actionStart(runtime != null ? runtime : 5);
99 logger.warn("VolvoOnCall Action service ThingHandler is null!");
103 public static void engineStartCommand(@Nullable ThingActions actions, @Nullable Integer runtime) {
104 invokeMethodOf(actions).engineStartCommand(runtime);
108 @RuleAction(label = "Volvo On Call : Heater Start", description = "Starts car heater")
109 public void heaterStartCommand() {
110 logger.debug("heaterStartCommand called");
111 VehicleHandler handler = this.handler;
112 if (handler != null) {
113 handler.actionHeater(true);
115 logger.warn("VolvoOnCall Action service ThingHandler is null!");
119 public static void heaterStartCommand(@Nullable ThingActions actions) {
120 invokeMethodOf(actions).heaterStartCommand();
124 @RuleAction(label = "Volvo On Call : Preclimatization Start", description = "Starts car heater")
125 public void preclimatizationStartCommand() {
126 logger.debug("preclimatizationStartCommand called");
127 VehicleHandler handler = this.handler;
128 if (handler != null) {
129 handler.actionPreclimatization(true);
131 logger.warn("VolvoOnCall Action service ThingHandler is null!");
135 public static void preclimatizationStartCommand(@Nullable ThingActions actions) {
136 invokeMethodOf(actions).preclimatizationStartCommand();
140 @RuleAction(label = "Volvo On Call : Heater Stop", description = "Stops car heater")
141 public void heaterStopCommand() {
142 logger.debug("heaterStopCommand called");
143 VehicleHandler handler = this.handler;
144 if (handler != null) {
145 handler.actionHeater(false);
147 logger.warn("VolvoOnCall Action service ThingHandler is null!");
151 public static void heaterStopCommand(@Nullable ThingActions actions) {
152 invokeMethodOf(actions).heaterStopCommand();
156 @RuleAction(label = "Volvo On Call : Preclimatization Stop", description = "Stops car heater")
157 public void preclimatizationStopCommand() {
158 logger.debug("preclimatizationStopCommand called");
159 VehicleHandler handler = this.handler;
160 if (handler != null) {
161 handler.actionPreclimatization(false);
163 logger.warn("VolvoOnCall Action service ThingHandler is null!");
167 public static void preclimatizationStopCommand(@Nullable ThingActions actions) {
168 invokeMethodOf(actions).preclimatizationStopCommand();
172 @RuleAction(label = "Volvo On Call : Honk-blink", description = "Activates the horn and or lights of the car")
173 public void honkBlinkCommand(@ActionInput(name = "honk", label = "Honk") Boolean honk,
174 @ActionInput(name = "blink", label = "Blink") Boolean blink) {
175 logger.debug("honkBlinkCommand called");
176 VehicleHandler handler = this.handler;
177 if (handler != null) {
178 handler.actionHonkBlink(honk, blink);
180 logger.warn("VolvoOnCall Action service ThingHandler is null!");
184 public static void honkBlinkCommand(@Nullable ThingActions actions, Boolean honk, Boolean blink) {
185 invokeMethodOf(actions).honkBlinkCommand(honk, blink);
188 private static IVolvoOnCallActions invokeMethodOf(@Nullable ThingActions actions) {
189 if (actions == null) {
190 throw new IllegalArgumentException("actions cannot be null");
192 if (actions.getClass().getName().equals(VolvoOnCallActions.class.getName())) {
193 if (actions instanceof IVolvoOnCallActions) {
194 return (IVolvoOnCallActions) actions;
196 return (IVolvoOnCallActions) Proxy.newProxyInstance(IVolvoOnCallActions.class.getClassLoader(),
197 new Class[] { IVolvoOnCallActions.class }, (Object proxy, Method method, Object[] args) -> {
198 Method m = actions.getClass().getDeclaredMethod(method.getName(),
199 method.getParameterTypes());
200 return m.invoke(actions, args);
204 throw new IllegalArgumentException("Actions is not an instance of VolvoOnCallActions");