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.pushbullet.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.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;
31 * The {@link PushbulletActions} class defines rule actions for sending notifications
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.
38 * @author Hakan Tandogan - Initial contribution
40 @ThingActionsScope(name = "pushbullet")
42 public class PushbulletActions implements ThingActions, IPushbulletActions {
44 private final Logger logger = LoggerFactory.getLogger(PushbulletActions.class);
46 private @Nullable PushbulletHandler handler;
49 public void setThingHandler(@Nullable ThingHandler handler) {
50 this.handler = (PushbulletHandler) handler;
54 public @Nullable ThingHandler getThingHandler() {
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);
66 // Use local variable so the SAT check can do proper flow analysis
67 PushbulletHandler localHandler = handler;
69 if (localHandler == null) {
70 logger.warn("Pushbullet service Handler is null!");
74 return localHandler.sendPush(recipient, title, message, "note");
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);
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);
89 // Use local variable so the SAT check can do proper flow analysis
90 PushbulletHandler localHandler = handler;
92 if (localHandler == null) {
93 logger.warn("Pushbullet service Handler is null!");
97 return localHandler.sendPush(recipient, message, "note");
100 public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
101 @Nullable String message) {
102 return invokeMethodOf(actions).sendPushbulletNote(recipient, message);
105 private static IPushbulletActions invokeMethodOf(@Nullable ThingActions actions) {
106 if (actions == null) {
107 throw new IllegalArgumentException("actions cannot be null");
109 if (actions.getClass().getName().equals(PushbulletActions.class.getName())) {
110 if (actions instanceof IPushbulletActions) {
111 return (IPushbulletActions) actions;
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);
121 throw new IllegalArgumentException("Actions is not an instance of PushbulletActions");