]> git.basschouten.com Git - openhab-addons.git/blob
696790b716b360a3c53ef5987f207c31dd1b485b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.prowl.internal.action;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.prowl.internal.ProwlHandler;
18 import org.openhab.core.automation.annotation.ActionInput;
19 import org.openhab.core.automation.annotation.RuleAction;
20 import org.openhab.core.thing.binding.ThingActions;
21 import org.openhab.core.thing.binding.ThingActionsScope;
22 import org.openhab.core.thing.binding.ThingHandler;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * The {@link ProwlActions} class contains methods for use in DSL.
28  *
29  * @author Ondrej Pecta - Initial contribution
30  */
31 @ThingActionsScope(name = "prowl")
32 @NonNullByDefault
33 public class ProwlActions implements ThingActions {
34     private final Logger logger = LoggerFactory.getLogger(ProwlActions.class);
35     private @Nullable ProwlHandler handler;
36
37     @Override
38     public void setThingHandler(ThingHandler thingHandler) {
39         this.handler = (ProwlHandler) thingHandler;
40     }
41
42     @Override
43     public @Nullable ThingHandler getThingHandler() {
44         return handler;
45     }
46
47     @RuleAction(label = "@text/pushNotificationActionLabel", description = "@text/pushNotificationActionDescription")
48     public void pushNotification(
49             @ActionInput(name = "event", label = "@text/pushNotificationActionEventLabel", description = "@text/pushNotificationActionEventDescription") @Nullable String event,
50             @ActionInput(name = "message", label = "@text/pushNotificationActionMessageLabel", description = "@text/pushNotificationActionMessageDescription") @Nullable String message) {
51         ProwlHandler clientHandler = handler;
52         if (clientHandler == null) {
53             logger.warn("Prowl ThingHandler is null");
54             return;
55         }
56
57         handler.pushNotification(event, message);
58     }
59
60     @RuleAction(label = "@text/pushNotificationActionLabel", description = "@text/pushNotificationActionDescription")
61     public void pushNotification(
62             @ActionInput(name = "event", label = "@text/pushNotificationActionEventLabel", description = "@text/pushNotificationActionEventDescription") @Nullable String event,
63             @ActionInput(name = "message", label = "@text/pushNotificationActionMessageLabel", description = "@text/pushNotificationActionMessageDescription") @Nullable String message,
64             @ActionInput(name = "priority", label = "@text/pushNotificationActionPriorityLabel", description = "@text/pushNotificationActionPriorityDescription") int priority) {
65         ProwlHandler clientHandler = handler;
66         if (clientHandler == null) {
67             logger.warn("Prowl ThingHandler is null");
68             return;
69         }
70
71         handler.pushNotification(event, message, priority);
72     }
73
74     public static void pushNotification(@Nullable ThingActions actions, @Nullable String event,
75             @Nullable String description) {
76         pushNotification(actions, event, description, 0);
77     }
78
79     public static void pushNotification(@Nullable ThingActions actions, @Nullable String event,
80             @Nullable String description, int priority) {
81         if (actions instanceof ProwlActions prowlActions) {
82             prowlActions.pushNotification(event, description, priority);
83         } else {
84             throw new IllegalArgumentException("Instance is not a ProwlActions class.");
85         }
86     }
87 }