]> git.basschouten.com Git - openhab-addons.git/blob
f1ae9e55f14f605de724f1a7a7800e90396ce3ae
[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.pushbullet.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.pushbullet.internal.handler.PushbulletHandler;
21 import org.openhab.core.automation.annotation.ActionInput;
22 import org.openhab.core.automation.annotation.ActionOutput;
23 import org.openhab.core.automation.annotation.RuleAction;
24 import org.openhab.core.thing.binding.ThingActions;
25 import org.openhab.core.thing.binding.ThingActionsScope;
26 import org.openhab.core.thing.binding.ThingHandler;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link PushbulletActions} class defines rule actions for sending notifications
32  * <p>
33  * <b>Note:</b>The static method <b>invokeMethodOf</b> handles the case where
34  * the test <i>actions instanceof PushbulletActions</i> fails. This test can fail
35  * due to an issue in openHAB core v2.5.0 where the {@link PushbulletActions} class
36  * can be loaded by a different classloader than the <i>actions</i> instance.
37  *
38  * @author Hakan Tandogan - Initial contribution
39  */
40 @ThingActionsScope(name = "pushbullet")
41 @NonNullByDefault
42 public class PushbulletActions implements ThingActions, IPushbulletActions {
43
44     private final Logger logger = LoggerFactory.getLogger(PushbulletActions.class);
45
46     private @Nullable PushbulletHandler handler;
47
48     @Override
49     public void setThingHandler(@Nullable ThingHandler handler) {
50         this.handler = (PushbulletHandler) handler;
51     }
52
53     @Override
54     public @Nullable ThingHandler getThingHandler() {
55         return this.handler;
56     }
57
58     @Override
59     @RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc")
60     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote(
61             @ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient,
62             @ActionInput(name = "title", label = "@text/actionSendPushbulletNoteInputTitleLabel", description = "@text/actionSendPushbulletNoteInputTitleDesc") @Nullable String title,
63             @ActionInput(name = "message", label = "@text/actionSendPushbulletNoteInputMessageLabel", description = "@text/actionSendPushbulletNoteInputMessageDesc") @Nullable String message) {
64         logger.trace("sendPushbulletNote '{}', '{}', '{}'", recipient, title, message);
65
66         // Use local variable so the SAT check can do proper flow analysis
67         PushbulletHandler localHandler = handler;
68
69         if (localHandler == null) {
70             logger.warn("Pushbullet service Handler is null!");
71             return false;
72         }
73
74         return localHandler.sendPush(recipient, title, message, "note");
75     }
76
77     public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
78             @Nullable String title, @Nullable String message) {
79         return invokeMethodOf(actions).sendPushbulletNote(recipient, title, message);
80     }
81
82     @Override
83     @RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc")
84     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote(
85             @ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient,
86             @ActionInput(name = "message", label = "@text/actionSendPushbulletNoteInputMessageLabel", description = "@text/actionSendPushbulletNoteInputMessageDesc") @Nullable String message) {
87         logger.trace("sendPushbulletNote '{}', '{}'", recipient, message);
88
89         // Use local variable so the SAT check can do proper flow analysis
90         PushbulletHandler localHandler = handler;
91
92         if (localHandler == null) {
93             logger.warn("Pushbullet service Handler is null!");
94             return false;
95         }
96
97         return localHandler.sendPush(recipient, message, "note");
98     }
99
100     public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
101             @Nullable String message) {
102         return invokeMethodOf(actions).sendPushbulletNote(recipient, message);
103     }
104
105     private static IPushbulletActions invokeMethodOf(@Nullable ThingActions actions) {
106         if (actions == null) {
107             throw new IllegalArgumentException("actions cannot be null");
108         }
109         if (actions.getClass().getName().equals(PushbulletActions.class.getName())) {
110             if (actions instanceof IPushbulletActions) {
111                 return (IPushbulletActions) actions;
112             } else {
113                 return (IPushbulletActions) Proxy.newProxyInstance(IPushbulletActions.class.getClassLoader(),
114                         new Class[] { IPushbulletActions.class }, (Object proxy, Method method, Object[] args) -> {
115                             Method m = actions.getClass().getDeclaredMethod(method.getName(),
116                                     method.getParameterTypes());
117                             return m.invoke(actions, args);
118                         });
119             }
120         }
121         throw new IllegalArgumentException("Actions is not an instance of PushbulletActions");
122     }
123 }