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.network.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.network.internal.handler.NetworkHandler;
21 import org.openhab.core.automation.annotation.RuleAction;
22 import org.openhab.core.thing.binding.ThingActions;
23 import org.openhab.core.thing.binding.ThingActionsScope;
24 import org.openhab.core.thing.binding.ThingHandler;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * The class is responsible to call corresponding actions on {@link NetworkHandler}.
31 * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
32 * the test <i>actions instanceof NetworkActions</i> fails. This test can fail
33 * due to an issue in openHAB core v2.5.0 where the {@link NetworkActions} class
34 * can be loaded by a different classloader than the <i>actions</i> instance.
36 * @author Wouter Born - Initial contribution
38 @ThingActionsScope(name = "network")
40 public class NetworkActions implements ThingActions, INetworkActions {
42 private final Logger logger = LoggerFactory.getLogger(NetworkActions.class);
44 private @Nullable NetworkHandler handler;
47 public void setThingHandler(@Nullable ThingHandler handler) {
48 if (handler instanceof NetworkHandler) {
49 this.handler = (NetworkHandler) handler;
54 public @Nullable ThingHandler getThingHandler() {
59 @RuleAction(label = "Send WoL Packet", description = "Send a Wake-on-LAN packet to wake the device")
60 public void sendWakeOnLanPacket() {
61 NetworkHandler localHandler = handler;
62 if (localHandler != null) {
63 localHandler.sendWakeOnLanPacket();
65 logger.warn("Failed to send Wake-on-LAN packet (handler null)");
69 public static void sendWakeOnLanPacket(@Nullable ThingActions actions) {
70 invokeMethodOf(actions).sendWakeOnLanPacket();
73 private static INetworkActions invokeMethodOf(@Nullable ThingActions actions) {
74 if (actions == null) {
75 throw new IllegalArgumentException("actions cannot be null");
77 if (actions.getClass().getName().equals(NetworkActions.class.getName())) {
78 if (actions instanceof INetworkActions) {
79 return (INetworkActions) actions;
81 return (INetworkActions) Proxy.newProxyInstance(INetworkActions.class.getClassLoader(),
82 new Class[] { INetworkActions.class }, (Object proxy, Method method, Object[] args) -> {
83 Method m = actions.getClass().getDeclaredMethod(method.getName(),
84 method.getParameterTypes());
85 return m.invoke(actions, args);
89 throw new IllegalArgumentException("Actions is not an instance of " + NetworkActions.class.getName());