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.gce.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.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;
30 * The {Ipx800Actions } defines rule actions for the GCE binding.
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.
37 * @author Gaƫl L'hopital - Initial contribution
39 @ThingActionsScope(name = "gce")
41 public class Ipx800Actions implements ThingActions, IIpx800Actions {
42 private final Logger logger = LoggerFactory.getLogger(Ipx800Actions.class);
44 protected @Nullable Ipx800v3Handler handler;
46 public Ipx800Actions() {
47 logger.debug("IPX800 actions service instanciated");
51 public void setThingHandler(@Nullable ThingHandler handler) {
52 if (handler instanceof Ipx800v3Handler) {
53 this.handler = (Ipx800v3Handler) handler;
58 public @Nullable ThingHandler getThingHandler() {
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);
71 logger.warn("Method call resetCounter failed because IPX800 action service ThingHandler is null!");
76 @RuleAction(label = "GCE : Reset PLC", description = "Restarts the IPX800")
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) {
84 logger.warn("Method call reset failed because IPX800 action service ThingHandler is null!");
88 public static void resetCounter(@Nullable ThingActions actions, Integer counter) {
89 invokeMethodOf(actions).resetCounter(counter);
92 public static void reset(@Nullable ThingActions actions, @Nullable Integer placeholder) {
93 invokeMethodOf(actions).reset(placeholder);
96 private static IIpx800Actions invokeMethodOf(@Nullable ThingActions actions) {
97 if (actions == null) {
98 throw new IllegalArgumentException("actions cannot be null");
100 if (actions.getClass().getName().equals(Ipx800Actions.class.getName())) {
101 if (actions instanceof IIpx800Actions) {
102 return (IIpx800Actions) actions;
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);
112 throw new IllegalArgumentException("Actions is not an instance of Ipx800Actions");