2 * Copyright (c) 2010-2023 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.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.smsmodem.internal.SMSConversationConfiguration;
18 import org.openhab.binding.smsmodem.internal.SMSModemBindingConstants;
19 import org.openhab.core.i18n.ConfigurationException;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.binding.BaseThingHandler;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.types.Command;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link SMSConversationHandler} is responsible for managing
36 * discussion channels.
38 * @author Gwendal ROULLEAU - Initial contribution
41 public class SMSConversationHandler extends BaseThingHandler {
43 public static final ThingTypeUID SUPPORTED_THING_TYPES_UIDS = SMSModemBindingConstants.SMSCONVERSATION_THING_TYPE;
45 private final Logger logger = LoggerFactory.getLogger(SMSConversationHandler.class);
47 private @Nullable SMSModemBridgeHandler bridgeHandler;
49 private SMSConversationConfiguration config;
51 public SMSConversationHandler(Thing thing) {
53 this.config = new SMSConversationConfiguration();
56 public String getRecipient() {
57 return config.recipient.trim();
60 private synchronized void checkBridgeHandler() {
61 if (this.bridgeHandler == null) {
62 Bridge bridge = getBridge();
64 throw new ConfigurationException("Required bridge not defined for SMSconversation {} with {}.",
65 thing.getUID(), getRecipient());
67 ThingHandler handler = bridge.getHandler();
68 if (handler instanceof SMSModemBridgeHandler) {
69 this.bridgeHandler = (SMSModemBridgeHandler) handler;
71 throw new ConfigurationException("No available bridge handler found for SMSConversation {} bridge {} .",
72 thing.getUID(), bridge.getUID());
77 protected void checkAndReceive(String sender, String text) {
78 String conversationRecipient = config.recipient.trim();
79 // is the recipient the one handled by this conversation ? :
80 if (conversationRecipient.equals(sender)) {
81 updateState(SMSModemBindingConstants.CHANNEL_RECEIVED, new StringType(text));
85 protected void checkAndUpdateDeliveryStatus(String messageRecipient, DeliveryStatus sentStatus) {
86 String conversationRecipient = config.recipient.trim();
87 // is the recipient the one handled by this conversation ? :
88 if (conversationRecipient.equals(messageRecipient)) {
89 updateState(SMSModemBindingConstants.CHANNEL_DELIVERYSTATUS, new StringType(sentStatus.name()));
94 public void handleCommand(ChannelUID channelUID, Command command) {
95 if (command instanceof RefreshType) {
98 if (channelUID.getId().equals(SMSModemBindingConstants.CHANNEL_SEND)) {
99 send(command.toString());
100 updateState(SMSModemBindingConstants.CHANNEL_SEND, new StringType(command.toString()));
104 public void send(String text) {
105 SMSModemBridgeHandler bridgeHandlerFinal = bridgeHandler;
106 if (bridgeHandlerFinal != null) {
107 bridgeHandlerFinal.send(getRecipient(), text, config.deliveryReport, config.encoding);
109 logger.warn("Only channel 'send' in SMSConversation can receive command");
114 public void initialize() {
115 config = getConfigAs(SMSConversationConfiguration.class);
117 checkBridgeHandler();
118 updateStatus(ThingStatus.ONLINE);
119 } catch (ConfigurationException confe) {
120 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, confe.getMessage());