2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.smsmodem.internal.actions;
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;
28 * The {@link SMSModemActions} exposes some actions
30 * @author Gwendal ROULLEAU - Initial contribution
32 @ThingActionsScope(name = "smsmodem")
34 public class SMSModemActions implements ThingActions {
36 private @NonNullByDefault({}) SMSModemBridgeHandler handler;
38 private final Logger logger = LoggerFactory.getLogger(SMSModemActions.class);
41 public void setThingHandler(@Nullable ThingHandler handler) {
42 this.handler = (SMSModemBridgeHandler) handler;
46 public @Nullable ThingHandler getThingHandler() {
50 @RuleAction(label = "Send Message With Special Encoding", description = "Send a message and specify encoding")
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);
58 logger.warn("SMSModem cannot send a message with no recipient or text");
62 @RuleAction(label = "Send Message", description = "Send a message")
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());
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);
74 throw new IllegalArgumentException("Instance is not an SMSModemActions class.");
78 public static void sendSMS(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message) {
79 sendSMS(actions, recipient, message, Encoding.Enc7.toString());