Skip to content

Commit f4eef87

Browse files
committed
Refactored AbstractServiceImpl so that Name characteristic is always added and BatteryLevelCharacteristic is added if accessory implements BatteryAccessory interface. Old constructor has been deprecated.
1 parent de167b0 commit f4eef87

14 files changed

+66
-76
lines changed

src/main/java/com/beowulfe/hap/impl/services/AbstractServiceImpl.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,62 @@
11
package com.beowulfe.hap.impl.services;
22

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+
310
import java.util.Collections;
411
import java.util.LinkedList;
512
import java.util.List;
613

7-
import com.beowulfe.hap.Service;
8-
import com.beowulfe.hap.characteristics.Characteristic;
9-
1014
abstract class AbstractServiceImpl implements Service {
1115

1216
private final String type;
1317
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
1527
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) {
1644
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+
}
1760
}
1861

1962
@Override

src/main/java/com/beowulfe/hap/impl/services/AccessoryInformationService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.beowulfe.hap.impl.services;
22

33
import com.beowulfe.hap.HomekitAccessory;
4-
import com.beowulfe.hap.impl.characteristics.common.Name;
54
import com.beowulfe.hap.impl.characteristics.information.Identify;
65
import com.beowulfe.hap.impl.characteristics.information.Manufacturer;
76
import com.beowulfe.hap.impl.characteristics.information.Model;
@@ -10,8 +9,7 @@
109
public class AccessoryInformationService extends AbstractServiceImpl {
1110

1211
public AccessoryInformationService(HomekitAccessory accessory) throws Exception {
13-
super("0000003E-0000-1000-8000-0026BB765291");
14-
addCharacteristic(new Name(accessory));
12+
super("0000003E-0000-1000-8000-0026BB765291", accessory);
1513
addCharacteristic(new Manufacturer(accessory));
1614
addCharacteristic(new Model(accessory));
1715
addCharacteristic(new SerialNumber(accessory));
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
package com.beowulfe.hap.impl.services;
22

3-
import com.beowulfe.hap.accessories.BatteryAccessory;
43
import com.beowulfe.hap.accessories.ContactSensor;
5-
import com.beowulfe.hap.impl.characteristics.common.BatteryLevelCharacteristic;
6-
import com.beowulfe.hap.impl.characteristics.common.Name;
74
import com.beowulfe.hap.impl.characteristics.contactsensor.ContactSensorStateCharacteristic;
85

96
public class ContactSensorService extends AbstractServiceImpl {
107

118
public ContactSensorService(ContactSensor contactSensor) {
12-
super("00000080-0000-1000-8000-0026BB765291");
13-
addCharacteristic(new Name(contactSensor));
9+
super("00000080-0000-1000-8000-0026BB765291", contactSensor);
1410
addCharacteristic(new ContactSensorStateCharacteristic(contactSensor));
15-
16-
if (contactSensor instanceof BatteryAccessory) {
17-
BatteryAccessory batteryAccessory = (BatteryAccessory) contactSensor;
18-
addCharacteristic(new BatteryLevelCharacteristic(
19-
batteryAccessory::getBatteryLevelState,
20-
batteryAccessory::subscribeBatteryLevelState,
21-
batteryAccessory::unsubscribeBatteryLevelState
22-
));
23-
}
2411
}
2512
}

src/main/java/com/beowulfe/hap/impl/services/FanService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.beowulfe.hap.impl.services;
22

33
import com.beowulfe.hap.accessories.Fan;
4-
import com.beowulfe.hap.impl.characteristics.common.Name;
54
import com.beowulfe.hap.impl.characteristics.common.PowerStateCharacteristic;
65
import com.beowulfe.hap.impl.characteristics.fan.RotationDirectionCharacteristic;
76
import com.beowulfe.hap.impl.characteristics.fan.RotationSpeedCharacteristic;
87

98
public class FanService extends AbstractServiceImpl {
109

1110
public FanService(Fan fan) {
12-
super("00000040-0000-1000-8000-0026BB765291");
13-
addCharacteristic(new Name(fan));
11+
super("00000040-0000-1000-8000-0026BB765291", fan);
1412
addCharacteristic(new PowerStateCharacteristic(
1513
() -> fan.getFanPower(),
1614
v -> fan.setFanPower(v),

src/main/java/com/beowulfe/hap/impl/services/GarageDoorService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
package com.beowulfe.hap.impl.services;
22

33
import com.beowulfe.hap.accessories.GarageDoor;
4-
import com.beowulfe.hap.impl.characteristics.common.Name;
54
import com.beowulfe.hap.impl.characteristics.common.ObstructionDetectedCharacteristic;
65
import com.beowulfe.hap.impl.characteristics.garage.CurrentDoorStateCharacteristic;
76
import com.beowulfe.hap.impl.characteristics.garage.TargetDoorStateCharacteristic;
87

98
public class GarageDoorService extends AbstractServiceImpl {
109

1110
public GarageDoorService(GarageDoor door) {
12-
super("00000041-0000-1000-8000-0026BB765291");
13-
addCharacteristic(new Name(door));
11+
super("00000041-0000-1000-8000-0026BB765291", door);
1412
addCharacteristic(new CurrentDoorStateCharacteristic(door));
1513
addCharacteristic(new TargetDoorStateCharacteristic(door));
1614
addCharacteristic(new ObstructionDetectedCharacteristic(() -> door.getObstructionDetected(),

src/main/java/com/beowulfe/hap/impl/services/HumiditySensorService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package com.beowulfe.hap.impl.services;
22

33
import com.beowulfe.hap.accessories.HumiditySensor;
4-
import com.beowulfe.hap.impl.characteristics.common.Name;
54
import com.beowulfe.hap.impl.characteristics.humiditysensor.CurrentRelativeHumidityCharacteristic;
65

76
public class HumiditySensorService extends AbstractServiceImpl {
87

98
public HumiditySensorService(HumiditySensor sensor) {
10-
super("00000082-0000-1000-8000-0026BB765291");
11-
addCharacteristic(new Name(sensor));
9+
super("00000082-0000-1000-8000-0026BB765291", sensor);
1210
addCharacteristic(new CurrentRelativeHumidityCharacteristic(sensor));
1311
}
1412

src/main/java/com/beowulfe/hap/impl/services/LightbulbService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.beowulfe.hap.accessories.ColorfulLightbulb;
44
import com.beowulfe.hap.accessories.DimmableLightbulb;
55
import com.beowulfe.hap.accessories.Lightbulb;
6-
import com.beowulfe.hap.impl.characteristics.common.Name;
76
import com.beowulfe.hap.impl.characteristics.common.PowerStateCharacteristic;
87
import com.beowulfe.hap.impl.characteristics.lightbulb.BrightnessCharacteristic;
98
import com.beowulfe.hap.impl.characteristics.lightbulb.HueCharacteristic;
@@ -12,8 +11,7 @@
1211
public class LightbulbService extends AbstractServiceImpl {
1312

1413
public LightbulbService(Lightbulb lightbulb) {
15-
super("00000043-0000-1000-8000-0026BB765291");
16-
addCharacteristic(new Name(lightbulb));
14+
super("00000043-0000-1000-8000-0026BB765291", lightbulb);
1715
addCharacteristic(new PowerStateCharacteristic(
1816
() -> lightbulb.getLightbulbPowerState(),
1917
v -> lightbulb.setLightbulbPowerState(v),
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
package com.beowulfe.hap.impl.services;
22

3-
import com.beowulfe.hap.accessories.BatteryAccessory;
43
import com.beowulfe.hap.accessories.LockMechanism;
54
import com.beowulfe.hap.accessories.LockableLockMechanism;
6-
import com.beowulfe.hap.impl.characteristics.common.BatteryLevelCharacteristic;
7-
import com.beowulfe.hap.impl.characteristics.common.Name;
85
import com.beowulfe.hap.impl.characteristics.lock.mechanism.CurrentLockMechanismStateCharacteristic;
96
import com.beowulfe.hap.impl.characteristics.lock.mechanism.TargetLockMechanismStateCharacteristic;
107

118
public class LockMechanismService extends AbstractServiceImpl {
129

1310
public LockMechanismService(LockMechanism lock) {
14-
super("00000045-0000-1000-8000-0026BB765291");
15-
addCharacteristic(new Name(lock));
11+
super("00000045-0000-1000-8000-0026BB765291", lock);
1612
addCharacteristic(new CurrentLockMechanismStateCharacteristic(lock));
1713

1814
if (lock instanceof LockableLockMechanism) {
1915
addCharacteristic(new TargetLockMechanismStateCharacteristic((LockableLockMechanism) lock));
2016
}
21-
22-
if (lock instanceof BatteryAccessory) {
23-
BatteryAccessory batteryAccessory = (BatteryAccessory) lock;
24-
addCharacteristic(new BatteryLevelCharacteristic(
25-
batteryAccessory::getBatteryLevelState,
26-
batteryAccessory::subscribeBatteryLevelState,
27-
batteryAccessory::unsubscribeBatteryLevelState
28-
));
29-
}
3017
}
3118
}
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,12 @@
11
package com.beowulfe.hap.impl.services;
22

3-
import com.beowulfe.hap.accessories.BatteryAccessory;
43
import com.beowulfe.hap.accessories.MotionSensor;
5-
import com.beowulfe.hap.impl.characteristics.common.BatteryLevelCharacteristic;
6-
import com.beowulfe.hap.impl.characteristics.common.Name;
74
import com.beowulfe.hap.impl.characteristics.motionsensor.MotionDetectedStateCharacteristic;
85

96
public class MotionSensorService extends AbstractServiceImpl {
107

118
public MotionSensorService(MotionSensor motionSensor) {
12-
super("00000085-0000-1000-8000-0026BB765291");
13-
addCharacteristic(new Name(motionSensor));
9+
super("00000085-0000-1000-8000-0026BB765291", motionSensor);
1410
addCharacteristic(new MotionDetectedStateCharacteristic(motionSensor));
15-
16-
if (motionSensor instanceof BatteryAccessory) {
17-
BatteryAccessory batteryAccessory = (BatteryAccessory) motionSensor;
18-
addCharacteristic(new BatteryLevelCharacteristic(
19-
batteryAccessory::getBatteryLevelState,
20-
batteryAccessory::subscribeBatteryLevelState,
21-
batteryAccessory::unsubscribeBatteryLevelState
22-
));
23-
}
2411
}
2512
}

src/main/java/com/beowulfe/hap/impl/services/OutletService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.beowulfe.hap.impl.services;
22

33
import com.beowulfe.hap.accessories.Outlet;
4-
import com.beowulfe.hap.impl.characteristics.common.Name;
54
import com.beowulfe.hap.impl.characteristics.common.PowerStateCharacteristic;
65
import com.beowulfe.hap.impl.characteristics.outlet.OutletInUseCharacteristic;
76

87
public class OutletService extends AbstractServiceImpl {
98

109
public OutletService(Outlet outlet) {
11-
super("00000047-0000-1000-8000-0026BB765291");
12-
addCharacteristic(new Name(outlet));
10+
super("00000047-0000-1000-8000-0026BB765291", outlet);
1311
addCharacteristic(new PowerStateCharacteristic(
1412
() -> outlet.getPowerState(),
1513
v -> outlet.setPowerState(v),

0 commit comments

Comments
 (0)