]> git.basschouten.com Git - openhab-addons.git/blob
58fd3f3da43716862eb239812a99d786619e885a
[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.Collections;
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.openhab.binding.bluetooth.BluetoothBindingConstants;
23 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryDevice;
24 import org.openhab.binding.bluetooth.discovery.BluetoothDiscoveryParticipant;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31
32 /**
33  * This discovery participant is able to recognize Airthings devices and create discovery results for them.
34  *
35  * @author Pauli Anttila - Initial contribution
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
46     @Override
47     public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
48         return Collections.singleton(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS);
49     }
50
51     @Override
52     public @Nullable ThingUID getThingUID(BluetoothDiscoveryDevice device) {
53         if (isAirthingsDevice(device)) {
54             if (WAVE_PLUS_MODEL.equals(device.getModel())) {
55                 return new ThingUID(AirthingsBindingConstants.THING_TYPE_AIRTHINGS_WAVE_PLUS,
56                         device.getAdapter().getUID(), device.getAddress().toString().toLowerCase().replace(":", ""));
57             }
58         }
59         return null;
60     }
61
62     @Override
63     public @Nullable DiscoveryResult createResult(BluetoothDiscoveryDevice device) {
64         if (!isAirthingsDevice(device)) {
65             return null;
66         }
67         ThingUID thingUID = getThingUID(device);
68         if (thingUID == null) {
69             return null;
70         }
71         if (WAVE_PLUS_MODEL.equals(device.getModel())) {
72             return createWavePlus(device, thingUID);
73         }
74         return null;
75     }
76
77     @Override
78     public boolean requiresConnection(BluetoothDiscoveryDevice device) {
79         return isAirthingsDevice(device);
80     }
81
82     private boolean isAirthingsDevice(BluetoothDiscoveryDevice device) {
83         Integer manufacturerId = device.getManufacturerId();
84         if (manufacturerId != null && manufacturerId == AIRTHINGS_COMPANY_ID) {
85             return true;
86         }
87         return false;
88     }
89
90     private DiscoveryResult createWavePlus(BluetoothDiscoveryDevice device, ThingUID thingUID) {
91         Map<String, Object> properties = new HashMap<>();
92         properties.put(BluetoothBindingConstants.CONFIGURATION_ADDRESS, device.getAddress().toString());
93         properties.put(Thing.PROPERTY_VENDOR, "Airthings AS");
94         String serialNumber = device.getSerialNumber();
95         String firmwareRevision = device.getFirmwareRevision();
96         String model = device.getModel();
97         String hardwareRevision = device.getHardwareRevision();
98         Integer txPower = device.getTxPower();
99         if (serialNumber != null) {
100             properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
101         }
102         if (firmwareRevision != null) {
103             properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareRevision);
104         }
105         if (model != null) {
106             properties.put(Thing.PROPERTY_MODEL_ID, model);
107         }
108         if (hardwareRevision != null) {
109             properties.put(Thing.PROPERTY_HARDWARE_VERSION, hardwareRevision);
110         }
111         if (txPower != null) {
112             properties.put(BluetoothBindingConstants.PROPERTY_TXPOWER, Integer.toString(txPower));
113         }
114         properties.put(Thing.PROPERTY_MAC_ADDRESS, device.getAddress().toString());
115
116         // Create the discovery result and add to the inbox
117         return DiscoveryResultBuilder.create(thingUID).withProperties(properties)
118                 .withRepresentationProperty(BluetoothBindingConstants.CONFIGURATION_ADDRESS)
119                 .withBridge(device.getAdapter().getUID()).withLabel("Airthings Wave+").build();
120     }
121 }