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