]> git.basschouten.com Git - openhab-addons.git/blob
8d9d9bd31b86a586efcaf9d2e770acc630074797
[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.loxone.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.jupnp.model.meta.DeviceDetails;
20 import org.jupnp.model.meta.RemoteDevice;
21 import org.openhab.core.config.discovery.DiscoveryResult;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingTypeUID;
26 import org.openhab.core.thing.ThingUID;
27 import org.osgi.service.component.annotations.Component;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * The {@link LxDiscoveryParticipant} class creates Miniserver things.
33  * It analyzes UPNP devices discovered by the framework and if Loxone Miniserver is found,
34  * a new thing discovery is reported, which in turn will result in creating a {@link Thing}
35  * and subsequently a new {@link LxServerHandler} object.
36  *
37  * @author Pawel Pieczul - Initial contribution
38  *
39  */
40 @Component
41 public class LxDiscoveryParticipant implements UpnpDiscoveryParticipant {
42
43     private final Logger logger = LoggerFactory.getLogger(LxDiscoveryParticipant.class);
44
45     @Override
46     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
47         return LxServerHandler.SUPPORTED_THING_TYPES_UIDS;
48     }
49
50     @Override
51     public DiscoveryResult createResult(RemoteDevice device) {
52         ThingUID uid = getThingUID(device);
53         if (uid != null) {
54             Map<String, Object> properties = new HashMap<>(2);
55
56             // After correct Thing UID is created, we have confidence that all following parameters exist and we don't
57             // need to check for null objects here in the device details
58             DeviceDetails details = device.getDetails();
59             String serial = details.getSerialNumber();
60             String host = details.getPresentationURI().getHost();
61             String label = details.getFriendlyName() + " @ " + host;
62             int port = details.getPresentationURI().getPort();
63             String vendor = details.getManufacturerDetails().getManufacturer();
64             String model = details.getModelDetails().getModelName();
65
66             logger.debug("Creating discovery result for serial {} label {} port {}", serial, label, port);
67             properties.put(LxBindingConstants.MINISERVER_PARAM_HOST, host);
68             properties.put(LxBindingConstants.MINISERVER_PARAM_PORT, port);
69             properties.put(Thing.PROPERTY_VENDOR, vendor);
70             properties.put(Thing.PROPERTY_MODEL_ID, model);
71             properties.put(Thing.PROPERTY_SERIAL_NUMBER, serial);
72
73             return DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
74                     .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build();
75         }
76         return null;
77     }
78
79     @Override
80     public ThingUID getThingUID(RemoteDevice device) {
81         String manufacturer = device.getDetails().getManufacturerDetails().getManufacturer();
82         if (manufacturer != null && manufacturer.toLowerCase().contains("loxone")) {
83             String model = device.getDetails().getModelDetails().getModelName();
84             if (model != null && model.toLowerCase().contentEquals("loxone miniserver")) {
85                 String serial = device.getDetails().getSerialNumber();
86                 if (serial == null) {
87                     serial = device.getIdentity().getUdn().getIdentifierString();
88                 }
89                 if (serial != null) {
90                     return new ThingUID(LxBindingConstants.THING_TYPE_MINISERVER, serial);
91                 }
92             }
93         }
94         return null;
95     }
96 }