]> git.basschouten.com Git - openhab-addons.git/blob
a535caf52e689406099a2b0305d22c50928fa9b6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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     public static void pushNotification(@Nullable ThingActions actions, @Nullable String event,
61             @Nullable String description) {
62         if (actions instanceof ProwlActions) {
63             ((ProwlActions) actions).pushNotification(event, description);
64         } else {
65             throw new IllegalArgumentException("Instance is not a ProwlActions class.");
66         }
67     }
68 }