]> git.basschouten.com Git - openhab-addons.git/blob
6014e40b1055a7e32f086e6032d4c7d8e314f1c4
[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.handler;
14
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;
33
34 /**
35  * The {@link SMSConversationHandler} is responsible for managing
36  * discussion channels.
37  *
38  * @author Gwendal ROULLEAU - Initial contribution
39  */
40 @NonNullByDefault
41 public class SMSConversationHandler extends BaseThingHandler {
42
43     public static final ThingTypeUID SUPPORTED_THING_TYPES_UIDS = SMSModemBindingConstants.SMSCONVERSATION_THING_TYPE;
44
45     private final Logger logger = LoggerFactory.getLogger(SMSConversationHandler.class);
46
47     private @Nullable SMSModemBridgeHandler bridgeHandler;
48
49     private SMSConversationConfiguration config;
50
51     public SMSConversationHandler(Thing thing) {
52         super(thing);
53         this.config = new SMSConversationConfiguration();
54     }
55
56     public String getRecipient() {
57         return config.recipient.trim();
58     }
59
60     private synchronized void checkBridgeHandler() {
61         if (this.bridgeHandler == null) {
62             Bridge bridge = getBridge();
63             if (bridge == null) {
64                 throw new ConfigurationException("Required bridge not defined for SMSconversation {} with {}.",
65                         thing.getUID(), getRecipient());
66             }
67             ThingHandler handler = bridge.getHandler();
68             if (handler instanceof SMSModemBridgeHandler) {
69                 this.bridgeHandler = (SMSModemBridgeHandler) handler;
70             } else {
71                 throw new ConfigurationException("No available bridge handler found for SMSConversation {} bridge {} .",
72                         thing.getUID(), bridge.getUID());
73             }
74         }
75     }
76
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));
82         }
83     }
84
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()));
90         }
91     }
92
93     @Override
94     public void handleCommand(ChannelUID channelUID, Command command) {
95         if (command instanceof RefreshType) {
96             return;
97         }
98         if (channelUID.getId().equals(SMSModemBindingConstants.CHANNEL_SEND)) {
99             send(command.toString());
100             updateState(SMSModemBindingConstants.CHANNEL_SEND, new StringType(command.toString()));
101         }
102     }
103
104     public void send(String text) {
105         SMSModemBridgeHandler bridgeHandlerFinal = bridgeHandler;
106         if (bridgeHandlerFinal != null) {
107             bridgeHandlerFinal.send(getRecipient(), text, config.deliveryReport, config.encoding);
108         } else {
109             logger.warn("Only channel 'send' in SMSConversation can receive command");
110         }
111     }
112
113     @Override
114     public void initialize() {
115         config = getConfigAs(SMSConversationConfiguration.class);
116         try {
117             checkBridgeHandler();
118             updateStatus(ThingStatus.ONLINE);
119         } catch (ConfigurationException confe) {
120             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, confe.getMessage());
121         }
122     }
123 }