]> git.basschouten.com Git - openhab-addons.git/blob
acdb937a1b81848565229b2cc2e8c7bdcd726655
[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.gce.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.gce.internal.handler.Ipx800v3Handler;
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 {Ipx800Actions } defines rule actions for the GCE binding.
31  * <p>
32  * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
33  * the test <i>actions instanceof Ipx800Actions</i> fails. This test can fail
34  * due to an issue in openHAB core v2.5.0 where the {@link Ipx800Actions} class
35  * can be loaded by a different classloader than the <i>actions</i> instance.
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  */
39 @ThingActionsScope(name = "gce")
40 @NonNullByDefault
41 public class Ipx800Actions implements ThingActions, IIpx800Actions {
42     private final Logger logger = LoggerFactory.getLogger(Ipx800Actions.class);
43
44     protected @Nullable Ipx800v3Handler handler;
45
46     public Ipx800Actions() {
47         logger.debug("IPX800 actions service instanciated");
48     }
49
50     @Override
51     public void setThingHandler(@Nullable ThingHandler handler) {
52         if (handler instanceof Ipx800v3Handler) {
53             this.handler = (Ipx800v3Handler) handler;
54         }
55     }
56
57     @Override
58     public @Nullable ThingHandler getThingHandler() {
59         return this.handler;
60     }
61
62     @Override
63     @RuleAction(label = "GCE : Reset counter", description = "Resets to 0 value of a given counter")
64     public void resetCounter(
65             @ActionInput(name = "counter", label = "Counter", required = true, description = "Id of the counter", type = "java.lang.Integer") Integer counter) {
66         logger.debug("IPX800 action 'resetCounter' called");
67         Ipx800v3Handler theHandler = this.handler;
68         if (theHandler != null) {
69             theHandler.resetCounter(counter);
70         } else {
71             logger.warn("Method call resetCounter failed because IPX800 action service ThingHandler is null!");
72         }
73     }
74
75     @Override
76     @RuleAction(label = "GCE : Reset PLC", description = "Restarts the IPX800")
77     public void reset(
78             @ActionInput(name = "placeholder", label = "Placeholder", required = false, description = "This parameter is not used", type = "java.lang.Integer") @Nullable Integer placeholder) {
79         logger.debug("IPX800 action 'reset' called");
80         Ipx800v3Handler theHandler = this.handler;
81         if (theHandler != null) {
82             theHandler.reset();
83         } else {
84             logger.warn("Method call reset failed because IPX800 action service ThingHandler is null!");
85         }
86     }
87
88     public static void resetCounter(@Nullable ThingActions actions, Integer counter) {
89         invokeMethodOf(actions).resetCounter(counter);
90     }
91
92     public static void reset(@Nullable ThingActions actions, @Nullable Integer placeholder) {
93         invokeMethodOf(actions).reset(placeholder);
94     }
95
96     private static IIpx800Actions invokeMethodOf(@Nullable ThingActions actions) {
97         if (actions == null) {
98             throw new IllegalArgumentException("actions cannot be null");
99         }
100         if (actions.getClass().getName().equals(Ipx800Actions.class.getName())) {
101             if (actions instanceof IIpx800Actions) {
102                 return (IIpx800Actions) actions;
103             } else {
104                 return (IIpx800Actions) Proxy.newProxyInstance(IIpx800Actions.class.getClassLoader(),
105                         new Class[] { IIpx800Actions.class }, (Object proxy, Method method, Object[] args) -> {
106                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
107                                     method.getParameterTypes());
108                             return m.invoke(actions, args);
109                         });
110             }
111         }
112         throw new IllegalArgumentException("Actions is not an instance of Ipx800Actions");
113     }
114 }