]> git.basschouten.com Git - openhab-addons.git/blob
103828e3785e1caa434afdc8b4fa725493a5f2dc
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.pushbullet.internal.handler.PushbulletHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.ActionOutput;
20 import org.openhab.core.automation.annotation.RuleAction;
21 import org.openhab.core.thing.binding.ThingActions;
22 import org.openhab.core.thing.binding.ThingActionsScope;
23 import org.openhab.core.thing.binding.ThingHandler;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * The {@link PushbulletActions} class defines rule actions for sending notifications
29  *
30  * @author Hakan Tandogan - Initial contribution
31  */
32 @ThingActionsScope(name = "pushbullet")
33 @NonNullByDefault
34 public class PushbulletActions implements ThingActions {
35
36     private final Logger logger = LoggerFactory.getLogger(PushbulletActions.class);
37
38     private @Nullable PushbulletHandler handler;
39
40     @Override
41     public void setThingHandler(@Nullable ThingHandler handler) {
42         this.handler = (PushbulletHandler) handler;
43     }
44
45     @Override
46     public @Nullable ThingHandler getThingHandler() {
47         return handler;
48     }
49
50     @RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc")
51     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote(
52             @ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient,
53             @ActionInput(name = "title", label = "@text/actionSendPushbulletNoteInputTitleLabel", description = "@text/actionSendPushbulletNoteInputTitleDesc") @Nullable String title,
54             @ActionInput(name = "message", label = "@text/actionSendPushbulletNoteInputMessageLabel", description = "@text/actionSendPushbulletNoteInputMessageDesc") @Nullable String message) {
55         logger.trace("sendPushbulletNote '{}', '{}', '{}'", recipient, title, message);
56
57         PushbulletHandler localHandler = handler;
58         if (localHandler == null) {
59             logger.warn("Pushbullet service Handler is null!");
60             return false;
61         }
62
63         return localHandler.sendPush(recipient, title, message, "note");
64     }
65
66     public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
67             @Nullable String title, @Nullable String message) {
68         if (actions instanceof PushbulletActions) {
69             return ((PushbulletActions) actions).sendPushbulletNote(recipient, title, message);
70         } else {
71             throw new IllegalArgumentException("Actions is not an instance of PushbulletActions");
72         }
73     }
74
75     @RuleAction(label = "@text/actionSendPushbulletNoteLabel", description = "@text/actionSendPushbulletNoteDesc")
76     public @ActionOutput(name = "success", type = "java.lang.Boolean") Boolean sendPushbulletNote(
77             @ActionInput(name = "recipient", label = "@text/actionSendPushbulletNoteInputRecipientLabel", description = "@text/actionSendPushbulletNoteInputRecipientDesc") @Nullable String recipient,
78             @ActionInput(name = "message", label = "@text/actionSendPushbulletNoteInputMessageLabel", description = "@text/actionSendPushbulletNoteInputMessageDesc") @Nullable String message) {
79         logger.trace("sendPushbulletNote '{}', '{}'", recipient, message);
80
81         PushbulletHandler localHandler = handler;
82         if (localHandler == null) {
83             logger.warn("Pushbullet service Handler is null!");
84             return false;
85         }
86
87         return localHandler.sendPush(recipient, message, "note");
88     }
89
90     public static boolean sendPushbulletNote(@Nullable ThingActions actions, @Nullable String recipient,
91             @Nullable String message) {
92         if (actions instanceof PushbulletActions) {
93             return ((PushbulletActions) actions).sendPushbulletNote(recipient, message);
94         } else {
95             throw new IllegalArgumentException("Actions is not an instance of PushbulletActions");
96         }
97     }
98 }