Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.beowulfe.hap.accessories;

import com.beowulfe.hap.HomekitAccessory;
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.Service;
import com.beowulfe.hap.accessories.properties.CarbonMonoxideDetectedState;
import com.beowulfe.hap.impl.services.CarbonMonoxideSensorService;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

/**
* <p>A carbon monoxide sensor reports whether carbon monoxide has been detected or not.</p>
*
* <p>Carbon monoxide sensors that run on batteries will need to implement this interface
* and also implement {@link BatteryAccessory}.</p>
*
* @author Gaston Dombiak
*/
public interface CarbonMonoxideSensor extends HomekitAccessory {

/**
* Retrieves the state of the sensor that indicates if carbon monoxide has been detected.
*
* @return a future that will contain the carbon monoxide sensor's state
*/
CompletableFuture<CarbonMonoxideDetectedState> getCarbonMonoxideDetectedState();

@Override
default Collection<Service> getServices() {
return Collections.singleton(new CarbonMonoxideSensorService(this));
}

/**
* Subscribes to changes in the carbon monoxide's state.
*
* @param callback the function to call when the state changes.
*/
void subscribeCarbonMonoxideDetectedState(HomekitCharacteristicChangeCallback callback);

/**
* Unsubscribes from changes in the carbon monoxide's state.
*/
void unsubscribeCarbonMonoxideDetectedState();
}
46 changes: 46 additions & 0 deletions src/main/java/com/beowulfe/hap/accessories/SmokeSensor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.beowulfe.hap.accessories;

import com.beowulfe.hap.HomekitAccessory;
import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.Service;
import com.beowulfe.hap.accessories.properties.SmokeDetectedState;
import com.beowulfe.hap.impl.services.SmokeSensorService;

import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;

/**
* <p>A smoke sensor reports whether smoke has been detected or not.</p>
*
* <p>Smoke sensors that run on batteries will need to implement this interface
* and also implement {@link BatteryAccessory}.</p>
*
* @author Gaston Dombiak
*/
public interface SmokeSensor extends HomekitAccessory {

/**
* Retrieves the state of the smoke sensor. This is whether smoke has been detected or not.
*
* @return a future that will contain the smoke sensor's state
*/
CompletableFuture<SmokeDetectedState> getSmokeDetectedState();

@Override
default Collection<Service> getServices() {
return Collections.singleton(new SmokeSensorService(this));
}

/**
* Subscribes to changes in the smoke sensor's state.
*
* @param callback the function to call when the state changes.
*/
void subscribeSmokeDetectedState(HomekitCharacteristicChangeCallback callback);

/**
* Unsubscribes from changes in the smoke sensor's state.
*/
void unsubscribeSmokeDetectedState();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.beowulfe.hap.accessories.properties;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public enum CarbonMonoxideDetectedState {

NORMAL(0),
ABNORMAL(1);

private final static Map<Integer, CarbonMonoxideDetectedState> reverse;
static {
reverse = Arrays.stream(CarbonMonoxideDetectedState.values()).collect(Collectors.toMap(CarbonMonoxideDetectedState::getCode, t -> t));
}

public static CarbonMonoxideDetectedState fromCode(Integer code) {
return reverse.get(code);
}

private final int code;

CarbonMonoxideDetectedState(int code) {
this.code = code;
}

public int getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.beowulfe.hap.accessories.properties;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public enum SmokeDetectedState {

NOT_DETECTED(0),
DETECTED(1);

private final static Map<Integer, SmokeDetectedState> reverse;
static {
reverse = Arrays.stream(SmokeDetectedState.values()).collect(Collectors.toMap(SmokeDetectedState::getCode, t -> t));
}

public static SmokeDetectedState fromCode(Integer code) {
return reverse.get(code);
}

private final int code;

SmokeDetectedState(int code) {
this.code = code;
}

public int getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.beowulfe.hap.impl.characteristics.carbonmonoxide.Carbon;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.CarbonMonoxideSensor;
import com.beowulfe.hap.accessories.properties.CarbonMonoxideDetectedState;
import com.beowulfe.hap.characteristics.EnumCharacteristic;
import com.beowulfe.hap.characteristics.EventableCharacteristic;

import java.util.concurrent.CompletableFuture;

public class CarbonMonoxideDetectedCharacteristic extends EnumCharacteristic implements EventableCharacteristic {

private final CarbonMonoxideSensor carbonMonoxideSensor;

public CarbonMonoxideDetectedCharacteristic(CarbonMonoxideSensor carbonMonoxideSensor) {
super("00000069-0000-1000-8000-0026BB765291", false, true, "Carbon Monoxide Detected", 1);
this.carbonMonoxideSensor = carbonMonoxideSensor;
}

@Override
protected CompletableFuture<Integer> getValue() {
return carbonMonoxideSensor.getCarbonMonoxideDetectedState().thenApply(CarbonMonoxideDetectedState::getCode);
}

@Override
protected void setValue(Integer value) throws Exception {
//Read Only
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
carbonMonoxideSensor.subscribeCarbonMonoxideDetectedState(callback);
}

@Override
public void unsubscribe() {
carbonMonoxideSensor.unsubscribeCarbonMonoxideDetectedState();
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.beowulfe.hap.impl.characteristics.smokesensor;

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.SmokeSensor;
import com.beowulfe.hap.accessories.properties.SmokeDetectedState;
import com.beowulfe.hap.characteristics.EnumCharacteristic;
import com.beowulfe.hap.characteristics.EventableCharacteristic;

import java.util.concurrent.CompletableFuture;

public class SmokeDetectedCharacteristic extends EnumCharacteristic implements EventableCharacteristic {

private final SmokeSensor smokeSensor;

public SmokeDetectedCharacteristic(SmokeSensor smokeSensor) {
super("00000076-0000-1000-8000-0026BB765291", false, true, "Smoke Detected", 1);
this.smokeSensor = smokeSensor;
}

@Override
protected CompletableFuture<Integer> getValue() {
return smokeSensor.getSmokeDetectedState().thenApply(SmokeDetectedState::getCode);
}

@Override
protected void setValue(Integer value) throws Exception {
//Read Only
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
smokeSensor.subscribeSmokeDetectedState(callback);
}

@Override
public void unsubscribe() {
smokeSensor.unsubscribeSmokeDetectedState();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.beowulfe.hap.impl.services;

import com.beowulfe.hap.accessories.CarbonMonoxideSensor;
import com.beowulfe.hap.impl.characteristics.carbonmonoxide.Carbon.CarbonMonoxideDetectedCharacteristic;

public class CarbonMonoxideSensorService extends AbstractServiceImpl {

public CarbonMonoxideSensorService(CarbonMonoxideSensor carbonMonoxideSensor) {
super("0000007F-0000-1000-8000-0026BB765291", carbonMonoxideSensor);
addCharacteristic(new CarbonMonoxideDetectedCharacteristic(carbonMonoxideSensor));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.beowulfe.hap.impl.services;

import com.beowulfe.hap.accessories.SmokeSensor;
import com.beowulfe.hap.impl.characteristics.smokesensor.SmokeDetectedCharacteristic;

public class SmokeSensorService extends AbstractServiceImpl {

public SmokeSensorService(SmokeSensor smokeSensor) {
super("00000087-0000-1000-8000-0026BB765291", smokeSensor);
addCharacteristic(new SmokeDetectedCharacteristic(smokeSensor));
}
}