]> git.basschouten.com Git - openhab-addons.git/blob
b0064aa200f13435efe64af834ae2eb9675076a6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.pushover.internal.handler;
14
15 import static org.openhab.binding.pushover.internal.PushoverBindingConstants.DEFAULT_SOUND;
16
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.eclipse.jetty.client.HttpClient;
24 import org.openhab.binding.pushover.internal.actions.PushoverActions;
25 import org.openhab.binding.pushover.internal.config.PushoverAccountConfiguration;
26 import org.openhab.binding.pushover.internal.config.PushoverConfigOptionProvider;
27 import org.openhab.binding.pushover.internal.connection.PushoverAPIConnection;
28 import org.openhab.binding.pushover.internal.connection.PushoverCommunicationException;
29 import org.openhab.binding.pushover.internal.connection.PushoverConfigurationException;
30 import org.openhab.binding.pushover.internal.connection.PushoverMessageBuilder;
31 import org.openhab.binding.pushover.internal.dto.Sound;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingStatus;
35 import org.openhab.core.thing.ThingStatusDetail;
36 import org.openhab.core.thing.binding.BaseThingHandler;
37 import org.openhab.core.thing.binding.ThingHandlerService;
38 import org.openhab.core.types.Command;
39
40 /**
41  * The {@link PushoverAccountHandler} is responsible for handling commands, which are sent to one of the channels.
42  *
43  * @author Christoph Weitkamp - Initial contribution
44  */
45 @NonNullByDefault
46 public class PushoverAccountHandler extends BaseThingHandler {
47
48     private static final Collection<Class<? extends ThingHandlerService>> SUPPORTED_THING_ACTIONS = Set
49             .of(PushoverActions.class, PushoverConfigOptionProvider.class);
50
51     private final HttpClient httpClient;
52
53     private PushoverAccountConfiguration config = new PushoverAccountConfiguration();
54     private @Nullable PushoverAPIConnection connection;
55
56     public PushoverAccountHandler(Thing thing, HttpClient httpClient) {
57         super(thing);
58         this.httpClient = httpClient;
59     }
60
61     @Override
62     public void handleCommand(ChannelUID channelUID, Command command) {
63         // nothing
64     }
65
66     @Override
67     public void initialize() {
68         config = getConfigAs(PushoverAccountConfiguration.class);
69
70         boolean configValid = true;
71         final String apikey = config.apikey;
72         if (apikey == null || apikey.isEmpty()) {
73             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
74                     "@text/offline.conf-error-missing-apikey");
75             configValid = false;
76         }
77         final String user = config.user;
78         if (user == null || user.isEmpty()) {
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
80                     "@text/offline.conf-error-missing-user");
81             configValid = false;
82         }
83
84         if (configValid) {
85             updateStatus(ThingStatus.UNKNOWN);
86
87             connection = new PushoverAPIConnection(httpClient, config);
88             scheduler.submit(this::asyncValidateUser);
89         }
90     }
91
92     @Override
93     public Collection<Class<? extends ThingHandlerService>> getServices() {
94         return SUPPORTED_THING_ACTIONS;
95     }
96
97     /**
98      * Retrieves the list of current sounds and their descriptions from the Pushover API.
99      *
100      * @return a list of {@link Sound}s
101      */
102     public List<Sound> getSounds() {
103         try {
104             return connection != null ? connection.getSounds() : PushoverAccountConfiguration.DEFAULT_SOUNDS;
105         } catch (PushoverCommunicationException e) {
106             // do nothing, causing exception is already logged
107         } catch (PushoverConfigurationException e) {
108             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
109         }
110         return PushoverAccountConfiguration.DEFAULT_SOUNDS;
111     }
112
113     /**
114      * Returns a preconfigured {@link PushoverMessageBuilder}.
115      *
116      * @param message the message
117      * @return a {@link PushoverMessageBuilder} instance
118      */
119     public PushoverMessageBuilder getDefaultPushoverMessageBuilder(String message) {
120         PushoverMessageBuilder builder = PushoverMessageBuilder.getInstance(config.apikey, config.user)
121                 .withMessage(message) //
122                 .withTitle(config.title) //
123                 .withRetry(config.retry) //
124                 .withExpire(config.expire);
125         // specify format if defined
126         switch (config.format) {
127             case PushoverMessageBuilder.MESSAGE_KEY_HTML:
128                 builder.withHtmlFormatting();
129                 break;
130             case PushoverMessageBuilder.MESSAGE_KEY_MONOSPACE:
131                 builder.withMonospaceFormatting();
132             default:
133                 break;
134         }
135         // add sound, if defined
136         if (!DEFAULT_SOUND.equals(config.sound)) {
137             builder.withSound(config.sound);
138         }
139         return builder;
140     }
141
142     public boolean sendMessage(PushoverMessageBuilder messageBuilder) {
143         if (connection != null) {
144             try {
145                 return connection.sendMessage(messageBuilder);
146             } catch (PushoverCommunicationException e) {
147                 // do nothing, causing exception is already logged
148             } catch (PushoverConfigurationException e) {
149                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
150             }
151             return false;
152         } else {
153             throw new IllegalArgumentException("PushoverAPIConnection is null!");
154         }
155     }
156
157     public String sendPriorityMessage(PushoverMessageBuilder messageBuilder) {
158         if (connection != null) {
159             try {
160                 return connection.sendPriorityMessage(messageBuilder);
161             } catch (PushoverCommunicationException e) {
162                 // do nothing, causing exception is already logged
163             } catch (PushoverConfigurationException e) {
164                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
165             }
166             return "";
167         } else {
168             throw new IllegalArgumentException("PushoverAPIConnection is null!");
169         }
170     }
171
172     public boolean cancelPriorityMessage(String receipt) {
173         if (connection != null) {
174             try {
175                 return connection.cancelPriorityMessage(receipt);
176             } catch (PushoverCommunicationException e) {
177                 // do nothing, causing exception is already logged
178             } catch (PushoverConfigurationException e) {
179                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
180             }
181             return false;
182         } else {
183             throw new IllegalArgumentException("PushoverAPIConnection is null!");
184         }
185     }
186
187     @SuppressWarnings("null")
188     private void asyncValidateUser() {
189         try {
190             connection.validateUser();
191             updateStatus(ThingStatus.ONLINE);
192         } catch (PushoverCommunicationException | PushoverConfigurationException e) {
193             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
194         }
195     }
196 }