]> git.basschouten.com Git - openhab-addons.git/blob
337e4f7df144bfe77a296065943ec7984bfd4cd4
[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.enocean.internal.discovery;
14
15 import static org.openhab.binding.enocean.internal.EnOceanBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.HashSet;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.usbserial.UsbSerialDeviceInformation;
26 import org.openhab.core.config.discovery.usbserial.UsbSerialDiscoveryParticipant;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Component;
30
31 /**
32  * Discovery for Enocean USB dongles, integrated in USB-serial discovery by implementing a component of type
33  * {@link UsbSerialDiscoveryParticipant}.
34  * <p/>
35  * Currently, this {@link UsbSerialDiscoveryParticipant} supports the Enocean USB300 dongles.
36  *
37  * @author Aitor Iturrioz - initial contribution
38  */
39 @NonNullByDefault
40 @Component(service = UsbSerialDiscoveryParticipant.class)
41 public class EnOceanUsbSerialDiscoveryParticipant implements UsbSerialDiscoveryParticipant {
42
43     private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = new HashSet<>(Arrays.asList(THING_TYPE_BRIDGE));
44
45     public static final int ENOCEAN_USB300_DONGLE_VENDOR_ID = 0x0403;
46     public static final int ENOCEAN_USB300_DONGLE_PRODUCT_ID = 0x6001;
47     public static final String ENOCEAN_USB300_DONGLE_MANUFACTURER = "EnOcean GmbH";
48     public static final String ENOCEAN_USB300_DONGLE_PRODUCT = "usb 300";
49     public static final String ENOCEAN_USB300_DONGLE_DEFAULT_LABEL = "Enocean USB300 Dongle";
50
51     @Override
52     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
53         return SUPPORTED_THING_TYPES;
54     }
55
56     @Override
57     public @Nullable DiscoveryResult createResult(UsbSerialDeviceInformation deviceInformation) {
58         if (isEnoceanUSB300Dongle(deviceInformation)) {
59             return DiscoveryResultBuilder.create(createBridgeThingType(deviceInformation))
60                     .withLabel(createEnoceanUSB300DongleLabel(deviceInformation)).withRepresentationProperty(PATH)
61                     .withProperty(PATH, deviceInformation.getSerialPort()).build();
62         } else {
63             return null;
64         }
65     }
66
67     @Override
68     public @Nullable ThingUID getThingUID(UsbSerialDeviceInformation deviceInformation) {
69         if (isEnoceanUSB300Dongle(deviceInformation)) {
70             return createBridgeThingType(deviceInformation);
71         } else {
72             return null;
73         }
74     }
75
76     private ThingUID createBridgeThingType(UsbSerialDeviceInformation deviceInformation) {
77         String serialNumber = deviceInformation.getSerialNumber();
78         if (serialNumber != null) {
79             return new ThingUID(THING_TYPE_BRIDGE, serialNumber);
80         } else {
81             return new ThingUID(THING_TYPE_BRIDGE, String.valueOf(deviceInformation.getProductId()));
82         }
83     }
84
85     private boolean isEnoceanUSB300Dongle(UsbSerialDeviceInformation deviceInformation) {
86         String manufacturer = deviceInformation.getManufacturer();
87         String product = deviceInformation.getProduct();
88
89         return deviceInformation.getVendorId() == ENOCEAN_USB300_DONGLE_VENDOR_ID
90                 && deviceInformation.getProductId() == ENOCEAN_USB300_DONGLE_PRODUCT_ID && manufacturer != null
91                 && manufacturer.equalsIgnoreCase(ENOCEAN_USB300_DONGLE_MANUFACTURER) && product != null
92                 && product.toLowerCase().contains(ENOCEAN_USB300_DONGLE_PRODUCT);
93     }
94
95     private @Nullable String createEnoceanUSB300DongleLabel(UsbSerialDeviceInformation deviceInformation) {
96         String serialNumber = deviceInformation.getSerialNumber();
97
98         if (serialNumber != null && !serialNumber.isEmpty()) {
99             return String.format("%s (%s)", ENOCEAN_USB300_DONGLE_DEFAULT_LABEL, serialNumber);
100         } else {
101             return ENOCEAN_USB300_DONGLE_DEFAULT_LABEL;
102         }
103     }
104 }