|
1 | 1 | package com.beowulfe.hap.impl.services;
|
2 | 2 |
|
| 3 | +import java.util.Collections; |
| 4 | +import java.util.LinkedList; |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
3 | 10 | import com.beowulfe.hap.HomekitAccessory;
|
4 | 11 | import com.beowulfe.hap.Service;
|
5 | 12 | import com.beowulfe.hap.accessories.BatteryAccessory;
|
| 13 | +import com.beowulfe.hap.accessories.BatteryStatusAccessory; |
6 | 14 | import com.beowulfe.hap.characteristics.Characteristic;
|
7 | 15 | import com.beowulfe.hap.impl.characteristics.common.BatteryLevelCharacteristic;
|
| 16 | +import com.beowulfe.hap.impl.characteristics.common.LowBatteryStatusCharacteristic; |
8 | 17 | import com.beowulfe.hap.impl.characteristics.common.Name;
|
9 | 18 |
|
10 |
| -import java.util.Collections; |
11 |
| -import java.util.LinkedList; |
12 |
| -import java.util.List; |
13 |
| - |
14 | 19 | abstract class AbstractServiceImpl implements Service {
|
15 |
| - |
16 |
| - private final String type; |
17 |
| - private final List<Characteristic> characteristics = new LinkedList<>(); |
| 20 | + private final Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 21 | + private final String type; |
| 22 | + private final List<Characteristic> characteristics = new LinkedList<>(); |
18 | 23 |
|
19 |
| - /** |
20 |
| - * This constructor has been deprecated and replaced with |
21 |
| - * {@link #AbstractServiceImpl(String, HomekitAccessory, String)}. Usages of |
22 |
| - * this constructor will need to manually configure {@link Name} characteristic |
23 |
| - * and {@link BatteryLevelCharacteristic} if needed. |
24 |
| - * |
25 |
| - * @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator. |
26 |
| - */ |
27 |
| - @Deprecated |
28 |
| - public AbstractServiceImpl(String type) { |
29 |
| - this(type, null, null); |
30 |
| - } |
| 24 | + /** |
| 25 | + * This constructor has been deprecated and replaced with |
| 26 | + * {@link #AbstractServiceImpl(String, HomekitAccessory, String)}. Usages of |
| 27 | + * this constructor will need to manually configure {@link Name} characteristic |
| 28 | + * and {@link BatteryLevelCharacteristic} if needed. |
| 29 | + * |
| 30 | + * @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator. |
| 31 | + */ |
| 32 | + @Deprecated |
| 33 | + public AbstractServiceImpl(String type) { |
| 34 | + this(type, null, null); |
| 35 | + } |
31 | 36 |
|
32 |
| - /** |
33 |
| - * <p>Creates a new instance of this class with the specified UUID and {@link HomekitAccessory}. |
34 |
| - * Download and install <i>HomeKit Accessory Simulator</i> to discover the corresponding UUID for |
35 |
| - * the specific service.</p> |
36 |
| - * |
37 |
| - * <p>The new service will automatically add {@link Name} characteristic. If the accessory |
38 |
| - * is battery operated then it must implement {@link BatteryAccessory} and {@link BatteryLevelCharacteristic} |
39 |
| - * will be added too.</p> |
40 |
| - * |
41 |
| - * @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator. |
42 |
| - * @param accessory HomeKit accessory exposed as a service. |
43 |
| - * @param serviceName name of the service. This information is usually the name of the accessory. |
| 37 | + /** |
| 38 | + * <p> |
| 39 | + * Creates a new instance of this class with the specified UUID and {@link HomekitAccessory}. |
| 40 | + * Download and install <i>HomeKit Accessory Simulator</i> to discover the corresponding UUID for |
| 41 | + * the specific service. |
| 42 | + * </p> |
| 43 | + * |
| 44 | + * <p> |
| 45 | + * The new service will automatically add {@link Name} characteristic. If the accessory |
| 46 | + * is battery operated then it must implement {@link BatteryAccessory} and {@link BatteryLevelCharacteristic} |
| 47 | + * will be added too. |
| 48 | + * </p> |
| 49 | + * |
| 50 | + * @param type unique UUID of the service. This information can be obtained from HomeKit Accessory Simulator. |
| 51 | + * @param accessory HomeKit accessory exposed as a service. |
| 52 | + * @param serviceName name of the service. This information is usually the name of the accessory. |
44 | 53 | */
|
45 |
| - public AbstractServiceImpl(String type, HomekitAccessory accessory, String serviceName) { |
46 |
| - this.type = type; |
| 54 | + public AbstractServiceImpl(String type, HomekitAccessory accessory, String serviceName) { |
| 55 | + this.type = type; |
| 56 | + |
| 57 | + if (accessory != null) { |
| 58 | + // Add name characteristic |
| 59 | + addCharacteristic(new Name(serviceName)); |
| 60 | + |
| 61 | + // If battery operated accessory then add BatteryLevelCharacteristic |
| 62 | + if (accessory instanceof BatteryAccessory) { |
| 63 | + logger.warn( |
| 64 | + "Accessory {} implements BatteryAccessory, which was incorrectly used to advertise battery state and is not recognized by HomeKit. " |
| 65 | + + "Battery-powered devices should report their battery status using LowBatteryStatusAccessory", |
| 66 | + accessory.getClass()); |
| 67 | + } |
| 68 | + |
| 69 | + // If battery operated accessory then add LowBatteryStatusAccessory |
| 70 | + if (accessory instanceof BatteryStatusAccessory) { |
| 71 | + BatteryStatusAccessory batteryStatusAccessory = (BatteryStatusAccessory) accessory; |
| 72 | + addCharacteristic(new LowBatteryStatusCharacteristic(batteryStatusAccessory::getLowBatteryState, |
| 73 | + batteryStatusAccessory::subscribeLowBatteryState, |
| 74 | + batteryStatusAccessory::unsubscribeLowBatteryState)); |
| 75 | + |
| 76 | + } |
| 77 | + } |
| 78 | + } |
47 | 79 |
|
48 |
| - if (accessory != null) { |
49 |
| - // Add name characteristic |
50 |
| - addCharacteristic(new Name(serviceName)); |
| 80 | + @Override |
| 81 | + public List<Characteristic> getCharacteristics() { |
| 82 | + return Collections.unmodifiableList(characteristics); |
| 83 | + } |
51 | 84 |
|
52 |
| - // If battery operated accessory then add BatteryLevelCharacteristic |
53 |
| - if (accessory instanceof BatteryAccessory) { |
54 |
| - BatteryAccessory batteryAccessory = (BatteryAccessory) accessory; |
55 |
| - addCharacteristic(new BatteryLevelCharacteristic( |
56 |
| - batteryAccessory::getBatteryLevelState, |
57 |
| - batteryAccessory::subscribeBatteryLevelState, |
58 |
| - batteryAccessory::unsubscribeBatteryLevelState |
59 |
| - )); |
60 |
| - } |
61 |
| - } |
62 |
| - } |
| 85 | + @Override |
| 86 | + public String getType() { |
| 87 | + return type; |
| 88 | + } |
63 | 89 |
|
64 |
| - @Override |
65 |
| - public List<Characteristic> getCharacteristics() { |
66 |
| - return Collections.unmodifiableList(characteristics); |
67 |
| - } |
68 |
| - |
69 |
| - @Override |
70 |
| - public String getType() { |
71 |
| - return type; |
72 |
| - } |
73 |
| - |
74 |
| - protected void addCharacteristic(Characteristic characteristic) { |
75 |
| - this.characteristics.add(characteristic); |
76 |
| - } |
| 90 | + protected void addCharacteristic(Characteristic characteristic) { |
| 91 | + this.characteristics.add(characteristic); |
| 92 | + } |
77 | 93 | }
|
0 commit comments