]> git.basschouten.com Git - openhab-addons.git/blob
91eb0ee9558c2e2b7a9f9d7da7760948d8c228dd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.bluetooth.airthings.internal;
14
15 import java.util.HashMap;
16 import java.util.Map;
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.bluetooth.BluetoothBindingConstants;
22 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
23 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.Thing;
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  * This discovery participant is able to recognize Airthings devices and create discovery results for them.
33  *
34  * @author Pauli Anttila - Initial contribution
35  * @author Kai Kreuzer - Added Airthings Wave Mini support
36  *
37  */
38 @NonNullByDefault
39 @Component
40 public class AirthingsDiscoveryParticipant implements BluetoothDiscoveryParticipant {
41
42     private static final int AIRTHINGS_COMPANY_ID = 820; // Formerly Corentium AS
43
44     private static final String WAVE_PLUS_MODEL = "2930";
45     private static final String WAVE_MINI_MODEL = "2920";
46
47     @Override
48     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49         return AirthingsBindingConstants.SUPPORTED_THING_TYPES_UIDS;
50     }
51
52     @Override
53     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
54         if (isAirthingsDevice(device)) {
55             if (WAVE_PLUS_MODEL.equals(device.getModel())) {
56                 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS,
57                         device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
58             }
59             if (WAVE_MINI_MODEL.equals(device.getModel())) {
60                 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_MINI,
61                         device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
62             }
63         }
64         return null;
65     }
66
67     @Override
68     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
69         if (!isAirthingsDevice(device)) {
70             return null;
71         }
72         ThingUID thingUID = getThingUID(device);
73         if (thingUID == null) {
74             return null;
75         }
76         if (WAVE_PLUS_MODEL.equals(device.getModel())) {
77             return createResult(device, thingUID, "Airthings Wave Plus");
78         }
79         if (WAVE_MINI_MODEL.equals(device.getModel())) {
80             return createResult(device, thingUID, "Airthings Wave Mini");
81         }
82         return null;
83     }
84
85     @Override
86     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
87         return isAirthingsDevice(device);
88     }
89
90     private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
91         Integer manufacturerId = device.getManufacturerId();
92         if (manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID) {
93             return true;
94         }
95         return false;
96     }
97
98     private DiscoveryResult createResult(BluetoothDiscoveryDevice device, ThingUID thingUID, String label) {
99         Map<String, Object> properties = new HashMap<>();
100         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
101         properties.put(Thing.PROPERTY_VENDOR, "Airthings AS");
102         String serialNumber = device.getSerialNumber();
103         String firmwareRevision = device.getFirmwareRevision();
104         String model = device.getModel();
105         String hardwareRevision = device.getHardwareRevision();
106         Integer txPower = device.getTxPower();
107         if (serialNumber != null) {
108             properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
109         }
110         if (firmwareRevision != null) {
111             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
112         }
113         if (model != null) {
114             properties.put(Thing.PROPERTY_MODEL_ID, model);
115         }
116         if (hardwareRevision != null) {
117             properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
118         }
119         if (txPower != null) {
120             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
121         }
122         properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
123
124         // Create the discovery result and add to the inbox
125         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
126                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
127                 .withBridge(device.getAdapter().getUID()).withLabel(label).build();
128     }
129 }