|
1 | 1 | package com.beowulfe.hap.impl.services; |
2 | 2 |
|
| 3 | +import com.beowulfe.hap.HomekitAccessory; |
| 4 | +import com.beowulfe.hap.Service; |
| 5 | +import com.beowulfe.hap.accessories.BatteryAccessory; |
| 6 | +import com.beowulfe.hap.characteristics.Characteristic; |
| 7 | +import com.beowulfe.hap.impl.characteristics.common.BatteryLevelCharacteristic; |
| 8 | +import com.beowulfe.hap.impl.characteristics.common.Name; |
| 9 | + |
3 | 10 | import java.util.Collections; |
4 | 11 | import java.util.LinkedList; |
5 | 12 | import java.util.List; |
6 | 13 |
|
7 | | -import com.beowulfe.hap.Service; |
8 | | -import com.beowulfe.hap.characteristics.Characteristic; |
9 | | - |
10 | 14 | abstract class AbstractServiceImpl implements Service { |
11 | 15 |
|
12 | 16 | private final String type; |
13 | 17 | private final List<Characteristic> characteristics = new LinkedList<>(); |
14 | | - |
| 18 | + |
| 19 | + /** |
| 20 | + * This constructor has been deprecated and replaced with {@link #AbstractServiceImpl(String, HomekitAccessory)}. |
| 21 | + * Usages of this constructor will need to manually configure {@link Name} characteristic and |
| 22 | + * {@link BatteryLevelCharacteristic} if needed. |
| 23 | + * |
| 24 | + * @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator. |
| 25 | + */ |
| 26 | + @Deprecated |
15 | 27 | public AbstractServiceImpl(String type) { |
| 28 | + this(type, null); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * <p>Creates a new instance of this class with the specified UUID and {@link HomekitAccessory}. |
| 33 | + * Download and install <i>HomeKit Accessory Simulator</i> to discover the corresponding UUID for |
| 34 | + * the specific service.</p> |
| 35 | + * |
| 36 | + * <p>The new service will automatically add {@link Name} characteristic. If the accessory |
| 37 | + * is battery operated then it must implement {@link BatteryAccessory} and {@link BatteryLevelCharacteristic} |
| 38 | + * will be added too.</p> |
| 39 | + * |
| 40 | + * @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator. |
| 41 | + * @param accessory HomeKit accessory exposed as a service. |
| 42 | + */ |
| 43 | + public AbstractServiceImpl(String type, HomekitAccessory accessory) { |
16 | 44 | this.type = type; |
| 45 | + |
| 46 | + if (accessory != null) { |
| 47 | + // Add name characteristic |
| 48 | + addCharacteristic(new Name(accessory)); |
| 49 | + |
| 50 | + // If battery operated accessory then add BatteryLevelCharacteristic |
| 51 | + if (accessory instanceof BatteryAccessory) { |
| 52 | + BatteryAccessory batteryAccessory = (BatteryAccessory) accessory; |
| 53 | + addCharacteristic(new BatteryLevelCharacteristic( |
| 54 | + batteryAccessory::getBatteryLevelState, |
| 55 | + batteryAccessory::subscribeBatteryLevelState, |
| 56 | + batteryAccessory::unsubscribeBatteryLevelState |
| 57 | + )); |
| 58 | + } |
| 59 | + } |
17 | 60 | } |
18 | 61 |
|
19 | 62 | @Override |
|
0 commit comments