Skip to content

Commit 7fbdf84

Browse files
committed
Added light sensor that measures luminance in LUX.
1 parent 5a69d28 commit 7fbdf84

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.beowulfe.hap.accessories;
2+
3+
import com.beowulfe.hap.HomekitAccessory;
4+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
5+
import com.beowulfe.hap.Service;
6+
import com.beowulfe.hap.impl.services.LightSensorService;
7+
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.concurrent.CompletableFuture;
11+
12+
/**
13+
* A light sensor that reports current ambient light level.
14+
*
15+
* @author Gaston Dombiak
16+
*/
17+
public interface LightSensor extends HomekitAccessory {
18+
19+
/**
20+
* Retrieves the current ambient light level.
21+
*
22+
* @return a future that will contain the luminance level expressed in LUX.
23+
*/
24+
CompletableFuture<Double> getCurrentAmbientLightLevel();
25+
26+
@Override
27+
default Collection<Service> getServices() {
28+
return Collections.singleton(new LightSensorService(this));
29+
}
30+
31+
/**
32+
* Subscribes to changes in the current ambient light level.
33+
*
34+
* @param callback the function to call when the state changes.
35+
*/
36+
void subscribeCurrentAmbientLightLevel(HomekitCharacteristicChangeCallback callback);
37+
38+
/**
39+
* Unsubscribes from changes in the current ambient light level.
40+
*/
41+
void unsubscribeCurrentAmbientLightLevel();
42+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.beowulfe.hap.impl.characteristics.light;
2+
3+
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
4+
import com.beowulfe.hap.accessories.LightSensor;
5+
import com.beowulfe.hap.characteristics.EventableCharacteristic;
6+
import com.beowulfe.hap.characteristics.FloatCharacteristic;
7+
8+
import java.util.concurrent.CompletableFuture;
9+
10+
public class AmbientLightLevelCharacteristic extends FloatCharacteristic implements EventableCharacteristic {
11+
12+
private final LightSensor lightSensor;
13+
14+
public AmbientLightLevelCharacteristic(LightSensor lightSensor) {
15+
super("0000006B-0000-1000-8000-0026BB765291", false, true, "Current ambient light level", 0.0001, 100000,
16+
0.0001, "lux");
17+
this.lightSensor = lightSensor;
18+
}
19+
20+
@Override
21+
protected void setValue(Double value) throws Exception {
22+
//Read Only
23+
}
24+
25+
@Override
26+
public void subscribe(HomekitCharacteristicChangeCallback callback) {
27+
lightSensor.subscribeCurrentAmbientLightLevel(callback);
28+
}
29+
30+
@Override
31+
public void unsubscribe() {
32+
lightSensor.unsubscribeCurrentAmbientLightLevel();
33+
}
34+
35+
@Override
36+
protected CompletableFuture<Double> getDoubleValue() {
37+
return lightSensor.getCurrentAmbientLightLevel();
38+
}
39+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.beowulfe.hap.impl.services;
2+
3+
import com.beowulfe.hap.accessories.LightSensor;
4+
import com.beowulfe.hap.impl.characteristics.light.AmbientLightLevelCharacteristic;
5+
6+
public class LightSensorService extends AbstractServiceImpl {
7+
8+
public LightSensorService(LightSensor lightSensor) {
9+
this(lightSensor, lightSensor.getLabel());
10+
}
11+
12+
public LightSensorService(LightSensor lightSensor, String serviceName) {
13+
super("00000084-0000-1000-8000-0026BB765291", lightSensor, serviceName);
14+
addCharacteristic(new AmbientLightLevelCharacteristic(lightSensor));
15+
}
16+
}

0 commit comments

Comments
 (0)