]> git.basschouten.com Git - openhab-addons.git/blob
e866e1f9264d3632606c5a2876676f77d1c7f18c
[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.io.openhabcloud.internal.actions;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.core.automation.Action;
18 import org.openhab.core.automation.handler.BaseActionModuleHandler;
19 import org.openhab.core.automation.handler.ModuleHandler;
20 import org.openhab.core.config.core.ConfigParser;
21 import org.openhab.io.openhabcloud.internal.CloudService;
22
23 /**
24  * This is a base {@link ModuleHandler} implementation for {@link Action}s to send a notifications via openHAB Cloud.
25  *
26  * @author Christoph Weitkamp - Initial contribution
27  * @author Dan Cunningham - Extended notification enhancements
28  */
29 @NonNullByDefault
30 public abstract class BaseNotificationActionHandler extends BaseActionModuleHandler {
31
32     public static final String PARAM_MESSAGE = "message";
33     public static final String PARAM_ICON = "icon";
34     public static final String PARAM_SEVERITY = "severity";
35     public static final String PARAM_TITLE = "title";
36     public static final String PARAM_ON_CLICK_ACTION = "onClickAction";
37     public static final String PARAM_MEDIA_ATTACHMENT_URL = "mediaAttachmentUrl";
38     public static final String PARAM_ACTION_BUTTON_1 = "actionButton1";
39     public static final String PARAM_ACTION_BUTTON_2 = "actionButton2";
40     public static final String PARAM_ACTION_BUTTON_3 = "actionButton3";
41
42     protected final CloudService cloudService;
43
44     protected final String message;
45     protected final @Nullable String icon;
46     protected final @Nullable String severity;
47     protected final @Nullable String title;
48     protected final @Nullable String onClickAction;
49     protected final @Nullable String mediaAttachmentUrl;
50     protected final @Nullable String actionButton1;
51     protected final @Nullable String actionButton2;
52     protected final @Nullable String actionButton3;
53
54     public BaseNotificationActionHandler(Action module, CloudService cloudService) {
55         super(module);
56         this.cloudService = cloudService;
57
58         Object messageParam = module.getConfiguration().get(PARAM_MESSAGE);
59         if (messageParam instanceof String) {
60             this.message = messageParam.toString();
61         } else {
62             throw new IllegalArgumentException(String.format("Param '%s' should be of type String.", PARAM_MESSAGE));
63         }
64
65         this.icon = stringConfig(PARAM_ICON);
66         this.severity = stringConfig(PARAM_SEVERITY);
67         this.title = stringConfig(PARAM_TITLE);
68         this.onClickAction = stringConfig(PARAM_ON_CLICK_ACTION);
69         this.mediaAttachmentUrl = stringConfig(PARAM_MEDIA_ATTACHMENT_URL);
70         this.actionButton1 = stringConfig(PARAM_ACTION_BUTTON_1);
71         this.actionButton2 = stringConfig(PARAM_ACTION_BUTTON_2);
72         this.actionButton3 = stringConfig(PARAM_ACTION_BUTTON_3);
73     }
74
75     private @Nullable String stringConfig(String key) {
76         return ConfigParser.valueAs(module.getConfiguration().get(key), String.class);
77     }
78 }