]> git.basschouten.com Git - openhab-addons.git/blob
ab2d6801f1f92b7a4a74910bee5719f4cb256dd0
[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.network.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.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;
27
28 /**
29  * The class is responsible to call corresponding actions on {@link NetworkHandler}.
30  * <p>
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.
35  *
36  * @author Wouter Born - Initial contribution
37  */
38 @ThingActionsScope(name = "network")
39 @NonNullByDefault
40 public class NetworkActions implements ThingActions, INetworkActions {
41
42     private final Logger logger = LoggerFactory.getLogger(NetworkActions.class);
43
44     private @Nullable NetworkHandler handler;
45
46     @Override
47     public void setThingHandler(@Nullable ThingHandler handler) {
48         if (handler instanceof NetworkHandler) {
49             this.handler = (NetworkHandler) handler;
50         }
51     }
52
53     @Override
54     public @Nullable ThingHandler getThingHandler() {
55         return handler;
56     }
57
58     @Override
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();
64         } else {
65             logger.warn("Failed to send Wake-on-LAN packet (handler null)");
66         }
67     }
68
69     public static void sendWakeOnLanPacket(@Nullable ThingActions actions) {
70         invokeMethodOf(actions).sendWakeOnLanPacket();
71     }
72
73     private static INetworkActions invokeMethodOf(@Nullable ThingActions actions) {
74         if (actions == null) {
75             throw new IllegalArgumentException("actions cannot be null");
76         }
77         if (actions.getClass().getName().equals(NetworkActions.class.getName())) {
78             if (actions instanceof INetworkActions) {
79                 return (INetworkActions) actions;
80             } else {
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);
86                         });
87             }
88         }
89         throw new IllegalArgumentException("Actions is not an instance of " + NetworkActions.class.getName());
90     }
91 }