]> git.basschouten.com Git - openhab-addons.git/blob
a80dd8e6ec768fe9d089904355403cd8502b0737
[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.tr064.internal;
14
15 import static org.openhab.binding.tr064.internal.Tr064BindingConstants.*;
16
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.time.Duration;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27 import java.util.Optional;
28 import java.util.Set;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.ScheduledFuture;
31 import java.util.concurrent.TimeUnit;
32 import java.util.concurrent.TimeoutException;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35
36 import javax.xml.soap.SOAPException;
37 import javax.xml.soap.SOAPMessage;
38
39 import org.eclipse.jdt.annotation.NonNullByDefault;
40 import org.eclipse.jdt.annotation.Nullable;
41 import org.eclipse.jetty.client.HttpClient;
42 import org.eclipse.jetty.client.api.Authentication;
43 import org.eclipse.jetty.client.api.AuthenticationStore;
44 import org.eclipse.jetty.client.api.ContentResponse;
45 import org.eclipse.jetty.client.util.DigestAuthentication;
46 import org.openhab.binding.tr064.internal.config.Tr064ChannelConfig;
47 import org.openhab.binding.tr064.internal.config.Tr064RootConfiguration;
48 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDDeviceType;
49 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType;
50 import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDActionType;
51 import org.openhab.binding.tr064.internal.phonebook.Phonebook;
52 import org.openhab.binding.tr064.internal.phonebook.PhonebookProvider;
53 import org.openhab.binding.tr064.internal.phonebook.Tr064PhonebookImpl;
54 import org.openhab.binding.tr064.internal.soap.SOAPConnector;
55 import org.openhab.binding.tr064.internal.soap.SOAPRequest;
56 import org.openhab.binding.tr064.internal.soap.SOAPValueConverter;
57 import org.openhab.binding.tr064.internal.util.SCPDUtil;
58 import org.openhab.binding.tr064.internal.util.Util;
59 import org.openhab.core.cache.ExpiringCacheMap;
60 import org.openhab.core.thing.Bridge;
61 import org.openhab.core.thing.ChannelUID;
62 import org.openhab.core.thing.ThingStatus;
63 import org.openhab.core.thing.ThingStatusDetail;
64 import org.openhab.core.thing.ThingTypeUID;
65 import org.openhab.core.thing.ThingUID;
66 import org.openhab.core.thing.binding.BaseBridgeHandler;
67 import org.openhab.core.thing.binding.ThingHandlerCallback;
68 import org.openhab.core.thing.binding.ThingHandlerService;
69 import org.openhab.core.thing.binding.builder.ThingBuilder;
70 import org.openhab.core.types.Command;
71 import org.openhab.core.types.RefreshType;
72 import org.openhab.core.types.State;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
75
76 /**
77  * The {@link Tr064RootHandler} is responsible for handling commands, which are
78  * sent to one of the channels and update channel values
79  *
80  * @author Jan N. Klug - Initial contribution
81  */
82 @NonNullByDefault
83 public class Tr064RootHandler extends BaseBridgeHandler implements PhonebookProvider {
84     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_GENERIC, THING_TYPE_FRITZBOX);
85     private static final int RETRY_INTERVAL = 60;
86     private static final Set<String> PROPERTY_ARGUMENTS = Set.of("NewSerialNumber", "NewSoftwareVersion",
87             "NewModelName");
88
89     private final Logger logger = LoggerFactory.getLogger(Tr064RootHandler.class);
90     private final HttpClient httpClient;
91
92     private @Nullable SCPDUtil scpdUtil;
93     private SOAPConnector soapConnector;
94
95     // these are set when the config is available
96     private Tr064RootConfiguration config = new Tr064RootConfiguration();
97     private String endpointBaseURL = "";
98     private int timeout = Tr064RootConfiguration.DEFAULT_HTTP_TIMEOUT;
99
100     private String deviceType = "";
101
102     private final Map<ChannelUID, Tr064ChannelConfig> channels = new HashMap<>();
103     // caching is used to prevent excessive calls to the same action
104     private final ExpiringCacheMap<ChannelUID, State> stateCache = new ExpiringCacheMap<>(Duration.ofMillis(2000));
105     private Collection<Phonebook> phonebooks = List.of();
106
107     private @Nullable ScheduledFuture<?> connectFuture;
108     private @Nullable ScheduledFuture<?> pollFuture;
109     private @Nullable ScheduledFuture<?> phonebookFuture;
110
111     private boolean communicationEstablished = false;
112
113     Tr064RootHandler(Bridge bridge, HttpClient httpClient) {
114         super(bridge);
115         this.httpClient = httpClient;
116         this.soapConnector = new SOAPConnector(httpClient, endpointBaseURL, timeout);
117     }
118
119     @Override
120     public void handleCommand(ChannelUID channelUID, Command command) {
121         if (!communicationEstablished) {
122             logger.debug("Tried to process command, but thing is not yet ready: {} to {}", channelUID, command);
123         }
124         Tr064ChannelConfig channelConfig = channels.get(channelUID);
125         if (channelConfig == null) {
126             logger.trace("Channel {} not supported.", channelUID);
127             return;
128         }
129
130         if (command instanceof RefreshType) {
131             SOAPConnector soapConnector = this.soapConnector;
132             State state = stateCache.putIfAbsentAndGet(channelUID,
133                     () -> soapConnector.getChannelStateFromDevice(channelConfig, channels, stateCache));
134             if (state != null) {
135                 updateState(channelUID, state);
136             }
137             return;
138         }
139
140         if (channelConfig.getChannelTypeDescription().getSetAction() == null) {
141             logger.debug("Discarding command {} to {}, read-only channel", command, channelUID);
142             return;
143         }
144         scheduler.execute(() -> soapConnector.sendChannelCommandToDevice(channelConfig, command));
145     }
146
147     @Override
148     public void initialize() {
149         config = getConfigAs(Tr064RootConfiguration.class);
150         if (!config.isValid()) {
151             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
152                     "At least one mandatory configuration field is empty");
153             return;
154         }
155
156         endpointBaseURL = "http://" + config.host + ":49000";
157         soapConnector = new SOAPConnector(httpClient, endpointBaseURL, timeout);
158         timeout = config.timeout;
159         updateStatus(ThingStatus.UNKNOWN);
160
161         connectFuture = scheduler.scheduleWithFixedDelay(this::internalInitialize, 0, RETRY_INTERVAL, TimeUnit.SECONDS);
162     }
163
164     /**
165      * internal thing initializer (sets SCPDUtil and connects to remote device)
166      */
167     private void internalInitialize() {
168         try {
169             scpdUtil = new SCPDUtil(httpClient, endpointBaseURL, timeout);
170         } catch (SCPDException e) {
171             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
172                     "could not get device definitions from " + config.host);
173             return;
174         }
175
176         if (establishSecureConnectionAndUpdateProperties()) {
177             removeConnectScheduler();
178
179             // connection successful, check channels
180             ThingBuilder thingBuilder = editThing();
181             thingBuilder.withoutChannels(thing.getChannels());
182             final SCPDUtil scpdUtil = this.scpdUtil;
183             final ThingHandlerCallback callback = getCallback();
184             if (scpdUtil != null && callback != null) {
185                 Util.checkAvailableChannels(thing, callback, thingBuilder, scpdUtil, "", deviceType, channels);
186                 updateThing(thingBuilder.build());
187             }
188
189             communicationEstablished = true;
190             installPolling();
191             updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
192         }
193     }
194
195     private void removeConnectScheduler() {
196         final ScheduledFuture<?> connectFuture = this.connectFuture;
197         if (connectFuture != null) {
198             connectFuture.cancel(true);
199             this.connectFuture = null;
200         }
201     }
202
203     @Override
204     public void dispose() {
205         communicationEstablished = false;
206         removeConnectScheduler();
207         uninstallPolling();
208         stateCache.clear();
209         scpdUtil = null;
210
211         super.dispose();
212     }
213
214     /**
215      * poll remote device for channel values
216      */
217     private void poll() {
218         try {
219             channels.forEach((channelUID, channelConfig) -> {
220                 if (isLinked(channelUID)) {
221                     State state = stateCache.putIfAbsentAndGet(channelUID,
222                             () -> soapConnector.getChannelStateFromDevice(channelConfig, channels, stateCache));
223                     if (state != null) {
224                         updateState(channelUID, state);
225                     }
226                 }
227             });
228         } catch (RuntimeException e) {
229             logger.warn("Exception while refreshing remote data for thing '{}':", thing.getUID(), e);
230             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
231                     "Refresh exception: " + e.getMessage());
232         }
233     }
234
235     /**
236      * establish the connection - get secure port (if available), install authentication, get device properties
237      *
238      * @return true if successful
239      */
240     private boolean establishSecureConnectionAndUpdateProperties() {
241         final SCPDUtil scpdUtil = this.scpdUtil;
242         if (scpdUtil != null) {
243             try {
244                 SCPDDeviceType device = scpdUtil.getDevice("")
245                         .orElseThrow(() -> new SCPDException("Root device not found"));
246                 SCPDServiceType deviceService = device.getServiceList().stream()
247                         .filter(service -> service.getServiceId().equals("urn:DeviceInfo-com:serviceId:DeviceInfo1"))
248                         .findFirst().orElseThrow(() -> new SCPDException(
249                                 "service 'urn:DeviceInfo-com:serviceId:DeviceInfo1' not found"));
250
251                 this.deviceType = device.getDeviceType();
252
253                 // try to get security (https) port
254                 SOAPMessage soapResponse = soapConnector
255                         .doSOAPRequest(new SOAPRequest(deviceService, "GetSecurityPort"));
256                 if (!soapResponse.getSOAPBody().hasFault()) {
257                     SOAPValueConverter soapValueConverter = new SOAPValueConverter(httpClient, timeout);
258                     soapValueConverter.getStateFromSOAPValue(soapResponse, "NewSecurityPort", null)
259                             .ifPresentOrElse(port -> {
260                                 endpointBaseURL = "https://" + config.host + ":" + port;
261                                 soapConnector = new SOAPConnector(httpClient, endpointBaseURL, timeout);
262                                 logger.debug("endpointBaseURL is now '{}'", endpointBaseURL);
263                             }, () -> logger.warn("Could not determine secure port, disabling https"));
264                 } else {
265                     logger.warn("Could not determine secure port, disabling https");
266                 }
267
268                 // clear auth cache and force re-auth
269                 AuthenticationStore authStore = httpClient.getAuthenticationStore();
270                 authStore.clearAuthentications();
271                 authStore.clearAuthenticationResults();
272                 authStore.addAuthentication(new DigestAuthentication(new URI(endpointBaseURL), Authentication.ANY_REALM,
273                         config.user, config.password));
274
275                 // check & update properties
276                 SCPDActionType getInfoAction = scpdUtil.getService(deviceService.getServiceId())
277                         .orElseThrow(() -> new SCPDException(
278                                 "Could not get service definition for 'urn:DeviceInfo-com:serviceId:DeviceInfo1'"))
279                         .getActionList().stream().filter(action -> action.getName().equals("GetInfo")).findFirst()
280                         .orElseThrow(() -> new SCPDException("Action 'GetInfo' not found"));
281                 SOAPMessage soapResponse1 = soapConnector
282                         .doSOAPRequest(new SOAPRequest(deviceService, getInfoAction.getName()));
283                 SOAPValueConverter soapValueConverter = new SOAPValueConverter(httpClient, timeout);
284                 Map<String, String> properties = editProperties();
285                 PROPERTY_ARGUMENTS.forEach(argumentName -> getInfoAction.getArgumentList().stream()
286                         .filter(argument -> argument.getName().equals(argumentName)).findFirst()
287                         .ifPresent(argument -> soapValueConverter
288                                 .getStateFromSOAPValue(soapResponse1, argumentName, null).ifPresent(value -> properties
289                                         .put(argument.getRelatedStateVariable(), value.toString()))));
290                 properties.put("deviceType", device.getDeviceType());
291                 updateProperties(properties);
292
293                 return true;
294             } catch (SCPDException | SOAPException | Tr064CommunicationException | URISyntaxException e) {
295                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
296                 return false;
297             }
298         }
299         return false;
300     }
301
302     /**
303      * get all sub devices of this root device (used for discovery)
304      *
305      * @return the list
306      */
307     public List<SCPDDeviceType> getAllSubDevices() {
308         final SCPDUtil scpdUtil = this.scpdUtil;
309         return (scpdUtil == null) ? List.of() : scpdUtil.getAllSubDevices();
310     }
311
312     /**
313      * get the SOAP connector (used by sub devices for communication with the remote device)
314      *
315      * @return the SOAP connector
316      */
317     public SOAPConnector getSOAPConnector() {
318         return soapConnector;
319     }
320
321     /**
322      * return the result of an (authenticated) GET request
323      *
324      * @param url the requested URL
325      *
326      * @return a {@link ContentResponse} with the result of the request
327      * @throws ExecutionException
328      * @throws InterruptedException
329      * @throws TimeoutException
330      */
331     public ContentResponse getUrl(String url) throws ExecutionException, InterruptedException, TimeoutException {
332         httpClient.getAuthenticationStore().addAuthentication(
333                 new DigestAuthentication(URI.create(url), Authentication.ANY_REALM, config.user, config.password));
334         return httpClient.GET(URI.create(url));
335     }
336
337     /**
338      * get the SCPD processing utility
339      *
340      * @return the SCPD utility (or null if not available)
341      */
342     public @Nullable SCPDUtil getSCPDUtil() {
343         return scpdUtil;
344     }
345
346     /**
347      * uninstall the polling
348      */
349     private void uninstallPolling() {
350         final ScheduledFuture<?> pollFuture = this.pollFuture;
351         if (pollFuture != null) {
352             pollFuture.cancel(true);
353             this.pollFuture = null;
354         }
355         final ScheduledFuture<?> phonebookFuture = this.phonebookFuture;
356         if (phonebookFuture != null) {
357             phonebookFuture.cancel(true);
358             this.phonebookFuture = null;
359         }
360     }
361
362     /**
363      * install the polling
364      */
365     private void installPolling() {
366         uninstallPolling();
367         pollFuture = scheduler.scheduleWithFixedDelay(this::poll, 0, config.refresh, TimeUnit.SECONDS);
368         if (config.phonebookInterval > 0) {
369             phonebookFuture = scheduler.scheduleWithFixedDelay(this::retrievePhonebooks, 0, config.phonebookInterval,
370                     TimeUnit.SECONDS);
371         }
372     }
373
374     @SuppressWarnings("unchecked")
375     private Collection<Phonebook> processPhonebookList(SOAPMessage soapMessagePhonebookList,
376             SCPDServiceType scpdService) {
377         SOAPValueConverter soapValueConverter = new SOAPValueConverter(httpClient, timeout);
378         Optional<Stream<String>> phonebookStream = soapValueConverter
379                 .getStateFromSOAPValue(soapMessagePhonebookList, "NewPhonebookList", null)
380                 .map(phonebookList -> Arrays.stream(phonebookList.toString().split(",")));
381         return phonebookStream.map(stringStream -> (Collection<Phonebook>) stringStream.map(index -> {
382             try {
383                 SOAPMessage soapMessageURL = soapConnector
384                         .doSOAPRequest(new SOAPRequest(scpdService, "GetPhonebook", Map.of("NewPhonebookID", index)));
385                 return soapValueConverter.getStateFromSOAPValue(soapMessageURL, "NewPhonebookURL", null)
386                         .map(url -> (Phonebook) new Tr064PhonebookImpl(httpClient, url.toString(), timeout));
387             } catch (Tr064CommunicationException e) {
388                 logger.warn("Failed to get phonebook with index {}:", index, e);
389             }
390             return Optional.empty();
391         }).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList())).orElseGet(Set::of);
392     }
393
394     private void retrievePhonebooks() {
395         String serviceId = "urn:X_AVM-DE_OnTel-com:serviceId:X_AVM-DE_OnTel1";
396         SCPDUtil scpdUtil = this.scpdUtil;
397         if (scpdUtil == null) {
398             logger.warn("Cannot find SCPDUtil. This is most likely a programming error.");
399             return;
400         }
401         Optional<SCPDServiceType> scpdService = scpdUtil.getDevice("").flatMap(deviceType -> deviceType.getServiceList()
402                 .stream().filter(service -> service.getServiceId().equals(serviceId)).findFirst());
403
404         phonebooks = Objects.requireNonNull(scpdService.map(service -> {
405             try {
406                 return processPhonebookList(soapConnector.doSOAPRequest(new SOAPRequest(service, "GetPhonebookList")),
407                         service);
408             } catch (Tr064CommunicationException e) {
409                 return Collections.<Phonebook> emptyList();
410             }
411         }).orElse(List.of()));
412
413         if (phonebooks.isEmpty()) {
414             logger.warn("Could not get phonebooks for thing {}", thing.getUID());
415         }
416     }
417
418     @Override
419     public Optional<Phonebook> getPhonebookByName(String name) {
420         return phonebooks.stream().filter(p -> name.equals(p.getName())).findAny();
421     }
422
423     @Override
424     public Collection<Phonebook> getPhonebooks() {
425         return phonebooks;
426     }
427
428     @Override
429     public ThingUID getUID() {
430         return thing.getUID();
431     }
432
433     @Override
434     public String getFriendlyName() {
435         String friendlyName = thing.getLabel();
436         return friendlyName != null ? friendlyName : getUID().getId();
437     }
438
439     @Override
440     public Collection<Class<? extends ThingHandlerService>> getServices() {
441         if (THING_TYPE_FRITZBOX.equals(thing.getThingTypeUID())) {
442             return Set.of(Tr064DiscoveryService.class, FritzboxActions.class);
443         } else {
444             return Set.of(Tr064DiscoveryService.class);
445         }
446     }
447
448     /**
449      * get the backup configuration for this thing (only applies to FritzBox devices
450      *
451      * @return the configuration
452      */
453     public FritzboxActions.BackupConfiguration getBackupConfiguration() {
454         return new FritzboxActions.BackupConfiguration(config.backupDirectory,
455                 Objects.requireNonNullElse(config.backupPassword, config.password));
456     }
457 }