]> git.basschouten.com Git - openhab-addons.git/blob
6423bca169c1dd2c7fb9e632e34bcce62cbec398
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.smsmodem.internal.actions;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.smsmodem.internal.handler.SMSModemBridgeHandler;
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 import org.smslib.message.AbstractMessage.Encoding;
26
27 /**
28  * The {@link SMSModemActions} exposes some actions
29  *
30  * @author Gwendal ROULLEAU - Initial contribution
31  */
32 @ThingActionsScope(name = "smsmodem")
33 @NonNullByDefault
34 public class SMSModemActions implements ThingActions {
35
36     private @NonNullByDefault({}) SMSModemBridgeHandler handler;
37
38     private final Logger logger = LoggerFactory.getLogger(SMSModemActions.class);
39
40     @Override
41     public void setThingHandler(@Nullable ThingHandler handler) {
42         this.handler = (SMSModemBridgeHandler) handler;
43     }
44
45     @Override
46     public @Nullable ThingHandler getThingHandler() {
47         return handler;
48     }
49
50     @RuleAction(label = "Send Message With Special Encoding", description = "Send a message and specify encoding")
51     public void sendSMS(
52             @ActionInput(name = "recipient", label = "recipient", description = "Recipient of the message") @Nullable String recipient,
53             @ActionInput(name = "message", label = "message", description = "Message to send") @Nullable String message,
54             @ActionInput(name = "encoding", label = "encoding", description = "Encoding") @Nullable String encoding) {
55         if (recipient != null && !recipient.isEmpty() && message != null) {
56             handler.send(recipient, message, false, encoding);
57         } else {
58             logger.warn("SMSModem cannot send a message with no recipient or text");
59         }
60     }
61
62     @RuleAction(label = "Send Message", description = "Send a message")
63     public void sendSMS(
64             @ActionInput(name = "recipient", label = "recipient", description = "Recipient of the message") @Nullable String recipient,
65             @ActionInput(name = "message", label = "message", description = "Message to send") @Nullable String message) {
66         sendSMS(recipient, message, Encoding.Enc7.toString());
67     }
68
69     public static void sendSMS(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message,
70             @Nullable String encoding) {
71         if (actions instanceof SMSModemActions smsModemActions) {
72             smsModemActions.sendSMS(recipient, message, encoding);
73         } else {
74             throw new IllegalArgumentException("Instance is not an SMSModemActions class.");
75         }
76     }
77
78     public static void sendSMS(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message) {
79         sendSMS(actions, recipient, message, Encoding.Enc7.toString());
80     }
81 }