]> git.basschouten.com Git - openhab-addons.git/blob
fd4e8d6a23af359bf12df04dc5e798b1f32fd883
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.hdpowerview.internal.discovery;
14
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
16
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
22 import org.openhab.core.config.discovery.DiscoveryResult;
23 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
24 import org.openhab.core.config.discovery.sddp.SddpDevice;
25 import org.openhab.core.config.discovery.sddp.SddpDiscoveryParticipant;
26 import org.openhab.core.thing.ThingTypeUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.osgi.service.component.annotations.Component;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Discovers HD PowerView hubs by means of SDDP
34  *
35  * @author Andrew Fiddian-Green - Initial contribution
36  */
37 @NonNullByDefault
38 @Component
39 public class HDPowerViewHubDiscoveryParticipantSddp implements SddpDiscoveryParticipant {
40
41     private static final String LABEL_KEY_HUB = "discovery.hub.label";
42     private static final String LABEL_KEY_GATEWAY = "discovery.gateway.label";
43
44     private static final String HUNTER_DOUGLAS = "hunterdouglas:";
45     private static final String POWERVIEW_HUB_ID = "hub:powerview";
46     private static final String POWERVIEW_GEN3_ID = "powerview:gen3:gateway";
47
48     private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipantSddp.class);
49
50     @Override
51     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
52         return Set.of(THING_TYPE_HUB, THING_TYPE_GATEWAY);
53     }
54
55     @Override
56     public @Nullable DiscoveryResult createResult(SddpDevice device) {
57         final ThingUID thingUID = getThingUID(device);
58         if (thingUID != null) {
59             try {
60                 DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
61                         .withProperty(HDPowerViewHubConfiguration.HOST, device.ipAddress)
62                         .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
63                         .withLabel(String.format("@text/%s [\"%s\"]",
64                                 isGateway(device) ? LABEL_KEY_GATEWAY : LABEL_KEY_HUB, device.ipAddress))
65                         .build();
66                 logger.debug("SDDP discovered hub/gateway '{}' on host '{}'", thingUID, device.ipAddress);
67                 return hub;
68             } catch (IllegalArgumentException e) {
69                 // error already logged, so fall through
70             }
71         }
72         return null;
73     }
74
75     @Override
76     public @Nullable ThingUID getThingUID(SddpDevice device) {
77         if (device.type.startsWith(HUNTER_DOUGLAS)) {
78             try {
79                 if (VALID_IP_V4_ADDRESS.matcher(device.ipAddress).matches()) {
80                     return new ThingUID(isGateway(device) ? THING_TYPE_GATEWAY : THING_TYPE_HUB,
81                             device.ipAddress.replace('.', '_'));
82                 }
83             } catch (IllegalArgumentException e) {
84                 // error already logged, so fall through
85             }
86         }
87         return null;
88     }
89
90     /**
91      * Check if the device 'type' property represents a Gen 3 gateway or a Gen 1/2 hub.
92      *
93      * @return true if a Gen 3 gateway or false if a Gen 1/2 hub.
94      * @throws IllegalArgumentException if neither Gen 3, 2 or 1.
95      */
96     private boolean isGateway(SddpDevice device) throws IllegalArgumentException {
97         if (device.type.contains(POWERVIEW_GEN3_ID)) {
98             return true;
99         }
100         if (device.type.contains(POWERVIEW_HUB_ID)) {
101             return false;
102         }
103         final IllegalArgumentException e = new IllegalArgumentException("Device has unexpected 'type' property");
104         logger.debug("{}", e.getMessage());
105         throw e;
106     }
107 }