|
| 1 | +package io.github.hapjava.services.impl; |
| 2 | + |
| 3 | +import io.github.hapjava.accessories.BasicFanAccessory; |
| 4 | +import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithName; |
| 5 | +import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithRotationDirection; |
| 6 | +import io.github.hapjava.accessories.optionalcharacteristic.AccessoryWithRotationSpeed; |
| 7 | +import io.github.hapjava.characteristics.impl.common.NameCharacteristic; |
| 8 | +import io.github.hapjava.characteristics.impl.common.OnCharacteristic; |
| 9 | +import io.github.hapjava.characteristics.impl.fan.RotationDirectionCharacteristic; |
| 10 | +import io.github.hapjava.characteristics.impl.fan.RotationSpeedCharacteristic; |
| 11 | + |
| 12 | +/** |
| 13 | + * This service describes a fan. |
| 14 | + * |
| 15 | + * <p>In the R1 release of the HAP specification, this is simply described as Fan. It is no longer |
| 16 | + * present in the R2 release. |
| 17 | + */ |
| 18 | +public class BasicFanService extends AbstractServiceImpl { |
| 19 | + |
| 20 | + public BasicFanService(OnCharacteristic on) { |
| 21 | + super("00000040-0000-1000-8000-0026BB765291"); |
| 22 | + addCharacteristic(on); |
| 23 | + } |
| 24 | + |
| 25 | + public BasicFanService(BasicFanAccessory accessory) { |
| 26 | + this( |
| 27 | + new OnCharacteristic( |
| 28 | + () -> accessory.isOn(), |
| 29 | + (v) -> accessory.setOn(v), |
| 30 | + accessory::subscribeOn, |
| 31 | + accessory::unsubscribeOn)); |
| 32 | + if (accessory instanceof AccessoryWithName) { |
| 33 | + addOptionalCharacteristic(new NameCharacteristic(((AccessoryWithName) accessory)::getName)); |
| 34 | + } |
| 35 | + |
| 36 | + if (accessory instanceof AccessoryWithRotationDirection) { |
| 37 | + addOptionalCharacteristic( |
| 38 | + new RotationDirectionCharacteristic( |
| 39 | + ((AccessoryWithRotationDirection) accessory)::getRotationDirection, |
| 40 | + ((AccessoryWithRotationDirection) accessory)::setRotationDirection, |
| 41 | + ((AccessoryWithRotationDirection) accessory)::subscribeRotationDirection, |
| 42 | + ((AccessoryWithRotationDirection) accessory)::unsubscribeRotationDirection)); |
| 43 | + } |
| 44 | + if (accessory instanceof AccessoryWithRotationSpeed) { |
| 45 | + addOptionalCharacteristic( |
| 46 | + new RotationSpeedCharacteristic( |
| 47 | + ((AccessoryWithRotationSpeed) accessory)::getRotationSpeed, |
| 48 | + ((AccessoryWithRotationSpeed) accessory)::setRotationSpeed, |
| 49 | + ((AccessoryWithRotationSpeed) accessory)::subscribeRotationSpeed, |
| 50 | + ((AccessoryWithRotationSpeed) accessory)::unsubscribeRotationSpeed)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public void addOptionalCharacteristic(NameCharacteristic name) { |
| 55 | + addCharacteristic(name); |
| 56 | + } |
| 57 | + |
| 58 | + public void addOptionalCharacteristic(RotationDirectionCharacteristic direction) { |
| 59 | + addCharacteristic(direction); |
| 60 | + } |
| 61 | + |
| 62 | + public void addOptionalCharacteristic(RotationSpeedCharacteristic speed) { |
| 63 | + addCharacteristic(speed); |
| 64 | + } |
| 65 | +} |
0 commit comments