]> git.basschouten.com Git - openhab-addons.git/blob
2d4e72cae7b776598f5374c1f72c0e1f956ad2db
[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.volvooncall.internal.action;
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.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;
28
29 /**
30  * The {@VehicleAction } class is responsible to call corresponding
31  * action on Vehicle Handler
32  *
33  * @author GaĆ«l L'hopital - Initial contribution
34  */
35 @ThingActionsScope(name = "volvooncall")
36 @NonNullByDefault
37 public class VolvoOnCallActions implements ThingActions, IVolvoOnCallActions {
38
39     private final Logger logger = LoggerFactory.getLogger(VolvoOnCallActions.class);
40
41     private @Nullable VehicleHandler handler;
42
43     public VolvoOnCallActions() {
44         logger.info("Volvo On Call actions service instanciated");
45     }
46
47     @Override
48     public void setThingHandler(@Nullable ThingHandler handler) {
49         if (handler instanceof VehicleHandler) {
50             this.handler = (VehicleHandler) handler;
51         }
52     }
53
54     @Override
55     public @Nullable ThingHandler getThingHandler() {
56         return this.handler;
57     }
58
59     @Override
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();
66         } else {
67             logger.warn("VolvoOnCall Action service ThingHandler is null!");
68         }
69     }
70
71     public static void closeCarCommand(@Nullable ThingActions actions) {
72         invokeMethodOf(actions).closeCarCommand();
73     }
74
75     @Override
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) {
81             handler.actionOpen();
82         } else {
83             logger.warn("VolvoOnCall Action service ThingHandler is null!");
84         }
85     }
86
87     public static void openCarCommand(@Nullable ThingActions actions) {
88         invokeMethodOf(actions).openCarCommand();
89     }
90
91     @Override
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);
98         } else {
99             logger.warn("VolvoOnCall Action service ThingHandler is null!");
100         }
101     }
102
103     public static void engineStartCommand(@Nullable ThingActions actions, @Nullable Integer runtime) {
104         invokeMethodOf(actions).engineStartCommand(runtime);
105     }
106
107     @Override
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);
114         } else {
115             logger.warn("VolvoOnCall Action service ThingHandler is null!");
116         }
117     }
118
119     public static void heaterStartCommand(@Nullable ThingActions actions) {
120         invokeMethodOf(actions).heaterStartCommand();
121     }
122
123     @Override
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);
130         } else {
131             logger.warn("VolvoOnCall Action service ThingHandler is null!");
132         }
133     }
134
135     public static void preclimatizationStartCommand(@Nullable ThingActions actions) {
136         invokeMethodOf(actions).preclimatizationStartCommand();
137     }
138
139     @Override
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);
146         } else {
147             logger.warn("VolvoOnCall Action service ThingHandler is null!");
148         }
149     }
150
151     public static void heaterStopCommand(@Nullable ThingActions actions) {
152         invokeMethodOf(actions).heaterStopCommand();
153     }
154
155     @Override
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);
162         } else {
163             logger.warn("VolvoOnCall Action service ThingHandler is null!");
164         }
165     }
166
167     public static void preclimatizationStopCommand(@Nullable ThingActions actions) {
168         invokeMethodOf(actions).preclimatizationStopCommand();
169     }
170
171     @Override
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);
179         } else {
180             logger.warn("VolvoOnCall Action service ThingHandler is null!");
181         }
182     }
183
184     public static void honkBlinkCommand(@Nullable ThingActions actions, Boolean honk, Boolean blink) {
185         invokeMethodOf(actions).honkBlinkCommand(honk, blink);
186     }
187
188     private static IVolvoOnCallActions invokeMethodOf(@Nullable ThingActions actions) {
189         if (actions == null) {
190             throw new IllegalArgumentException("actions cannot be null");
191         }
192         if (actions.getClass().getName().equals(VolvoOnCallActions.class.getName())) {
193             if (actions instanceof IVolvoOnCallActions) {
194                 return (IVolvoOnCallActions) actions;
195             } else {
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);
201                         });
202             }
203         }
204         throw new IllegalArgumentException("Actions is not an instance of VolvoOnCallActions");
205     }
206 }