]> git.basschouten.com Git - openhab-addons.git/blob
f05f46b8b352733fd35b24100c64635adb36c4be
[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.autelis.internal.discovery;
14
15 import java.net.URL;
16 import java.util.HashMap;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.jupnp.model.meta.RemoteDevice;
23 import org.openhab.binding.autelis.internal.AutelisBindingConstants;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  *
35  * Discovery Service for Autelis Pool Controllers.
36  *
37  * @author Dan Cunningham - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 @Component
42 public class AutelisDiscoveryParticipant implements UpnpDiscoveryParticipant {
43
44     private final Logger logger = LoggerFactory.getLogger(AutelisDiscoveryParticipant.class);
45
46     private static final String MANUFACTURER = "autelis";
47     private static final String MODEL_PENTAIR = "pc100p";
48     private static final String MODEL_JANDY = "pc100j";
49
50     @Override
51     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52         return AutelisBindingConstants.SUPPORTED_THING_TYPES_UIDS;
53     }
54
55     @Override
56     public @Nullable DiscoveryResult createResult(RemoteDevice device) {
57         ThingUID uid = getThingUID(device);
58         if (uid != null) {
59             Map<String, Object> properties = new HashMap<>(3);
60
61             URL url = device.getDetails().getBaseURL();
62             String label = device.getDetails().getFriendlyName();
63             int port = url.getPort() > 0 ? url.getPort() : 80;
64
65             properties.put("host", url.getHost());
66             properties.put("user", "admin");
67             properties.put("password", "admin");
68             properties.put("port", Integer.valueOf(port));
69
70             DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel(label)
71                     .build();
72
73             logger.debug("Created a DiscoveryResult for device '{}' with UDN '{}'",
74                     device.getDetails().getFriendlyName(), device.getIdentity().getUdn().getIdentifierString());
75             return result;
76         } else {
77             return null;
78         }
79     }
80
81     @Override
82     public @Nullable ThingUID getThingUID(RemoteDevice device) {
83         if (device.getDetails().getManufacturerDetails().getManufacturer() != null
84                 && device.getDetails().getModelDetails().getModelNumber() != null) {
85             logger.trace("UPNP {} : {}", device.getDetails().getManufacturerDetails().getManufacturer(),
86                     device.getDetails().getModelDetails().getModelNumber());
87             if (device.getDetails().getManufacturerDetails().getManufacturer().toLowerCase().startsWith(MANUFACTURER)) {
88                 logger.debug("Autelis Pool Control Found at {}", device.getDetails().getBaseURL());
89                 String id = device.getIdentity().getUdn().getIdentifierString().replace(":", "").toUpperCase();
90                 if (device.getDetails().getModelDetails().getModelNumber().toLowerCase().startsWith(MODEL_PENTAIR)) {
91                     return new ThingUID(AutelisBindingConstants.PENTAIR_THING_TYPE_UID, id);
92                 }
93                 if (device.getDetails().getModelDetails().getModelNumber().toLowerCase().startsWith(MODEL_JANDY)) {
94                     return new ThingUID(AutelisBindingConstants.JANDY_THING_TYPE_UID, id);
95                 }
96             }
97         }
98         return null;
99     }
100 }