]> git.basschouten.com Git - openhab-addons.git/blob
f5540299daa00e53850a85a2086d383e8fe2ff98
[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.util;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jetty.client.HttpClient;
25 import org.openhab.binding.tr064.internal.SCPDException;
26 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDDeviceType;
27 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDRootType;
28 import org.openhab.binding.tr064.internal.dto.scpd.root.SCPDServiceType;
29 import org.openhab.binding.tr064.internal.dto.scpd.service.SCPDScpdType;
30
31 /**
32  * The {@link SCPDUtil} is responsible for handling commands, which are
33  * sent to one of the channels.
34  *
35  * @author Jan N. Klug - Initial contribution
36  */
37 @NonNullByDefault
38 public class SCPDUtil {
39     private final SCPDRootType scpdRoot;
40     private final List<SCPDDeviceType> scpdDevicesList = new ArrayList<>();
41     private final Map<String, SCPDScpdType> serviceMap = new HashMap<>();
42
43     public SCPDUtil(HttpClient httpClient, String endpoint, int timeout) throws SCPDException {
44         SCPDRootType scpdRoot = Util.getAndUnmarshalXML(httpClient, endpoint + "/tr64desc.xml", SCPDRootType.class,
45                 timeout);
46         if (scpdRoot == null) {
47             throw new SCPDException("could not get SCPD root");
48         }
49         this.scpdRoot = scpdRoot;
50
51         scpdDevicesList.addAll(flatDeviceList(scpdRoot.getDevice()).toList());
52         for (SCPDDeviceType device : scpdDevicesList) {
53             for (SCPDServiceType service : device.getServiceList()) {
54                 SCPDScpdType scpd = serviceMap.computeIfAbsent(service.getServiceId(), serviceId -> Util
55                         .getAndUnmarshalXML(httpClient, endpoint + service.getSCPDURL(), SCPDScpdType.class, timeout));
56                 if (scpd == null) {
57                     throw new SCPDException("could not get SCPD service");
58                 }
59             }
60         }
61     }
62
63     /**
64      * recursively flatten the device tree to a stream
65      *
66      * @param device a device
67      * @return stream of sub-devices
68      */
69     private Stream<SCPDDeviceType> flatDeviceList(SCPDDeviceType device) {
70         return Stream.concat(Stream.of(device), device.getDeviceList().stream().flatMap(this::flatDeviceList));
71     }
72
73     /**
74      * get a list of all sub-devices (root device not included)
75      *
76      * @return the device list
77      */
78     public List<SCPDDeviceType> getAllSubDevices() {
79         return scpdDevicesList.stream().filter(device -> !device.getUDN().equals(scpdRoot.getDevice().getUDN()))
80                 .collect(Collectors.toList());
81     }
82
83     /**
84      * get a single device by its UDN
85      *
86      * @param udn the device UDN
87      * @return the device
88      */
89     public Optional<SCPDDeviceType> getDevice(String udn) {
90         if (udn.isEmpty()) {
91             return Optional.of(scpdRoot.getDevice());
92         } else {
93             return getAllSubDevices().stream().filter(device -> udn.equals(device.getUDN())).findFirst();
94         }
95     }
96
97     /**
98      * get a single service by its serviceId
99      *
100      * @param serviceId the service id
101      * @return the service
102      */
103     public Optional<SCPDScpdType> getService(String serviceId) {
104         return Optional.ofNullable(serviceMap.get(serviceId));
105     }
106 }