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
96 changes: 96 additions & 0 deletions src/main/java/com/beowulfe/hap/accessories/SecuritySystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
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.CurrentSecuritySystemState;
import com.beowulfe.hap.accessories.properties.SecuritySystemAlarmType;
import com.beowulfe.hap.accessories.properties.TargetSecuritySystemState;
import com.beowulfe.hap.impl.services.SecuritySystemService;

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

/**
* <p>A security system that can be armed so that when a contact sensor is opened or a motion
* sensor detects movement, then a siren could be fired off. There are different modes for arming
* the system. See {@link TargetSecuritySystemState} for more information.</p>
*
* @author Gaston Dombiak
*/
public interface SecuritySystem extends HomekitAccessory {

/**
* Retrieves the current state of the security system. The state describes if the system
* is armed in any of its variations; or if the alarm has been triggered; or if the system
* is disarmed.
*
* @return current state of the security system.
*/
CompletableFuture<CurrentSecuritySystemState> getCurrentSecuritySystemState();

/**
* Subscribes to changes to the state of the security system.
*
* @param callback the function to call when the state changes.
*/
void subscribeCurrentSecuritySystemState(HomekitCharacteristicChangeCallback callback);

/**
* Unsubscribes from changes in the state of the security system.
*/
void unsubscribeCurrentSecuritySystemState();

/**
* Sets the state of the security system. The security system could be armed in any
* of its variations or disarmed.
*
* @param state target state of the security system.
* @throws Exception when the change cannot be made.
*/
void setTargetSecuritySystemState(TargetSecuritySystemState state) throws Exception;

/**
* Retrieves the pending, but not yet completed, state of the security system.
*
* @return target state of the security system.
*/
CompletableFuture<TargetSecuritySystemState> getTargetSecuritySystemState();

/**
* Subscribes to changes in the pending, but not yet completed, state of the security system.
*
* @param callback the function to call when the state changes.
*/
void subscribeTargetSecuritySystemState(HomekitCharacteristicChangeCallback callback);

/**
* Unsubscribes from changes in the pending, but not yet completed, state of the security system.
*/
void unsubscribeTargetSecuritySystemState();

/**
* Retrieves the alarm type of the security system.
*
* @return alarm type of the security system.
*/
CompletableFuture<SecuritySystemAlarmType> getAlarmTypeState();

/**
* Subscribes to changes to the alarm type of the security system.
*
* @param callback the function to call when the alarm type changes.
*/
void subscribeAlarmTypeState(HomekitCharacteristicChangeCallback callback);

/**
* Unsubscribes from changes in the alarm type of the security system.
*/
void unsubscribeAlarmTypeState();

@Override
default Collection<Service> getServices() {
return Collections.singleton(new SecuritySystemService(this));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.beowulfe.hap.accessories.properties;

import com.beowulfe.hap.accessories.SecuritySystem;

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

/**
* The current state of a {@link SecuritySystem}. Unlike {@link TargetSecuritySystemState}, this enum
* includes a triggered state.
*
* @author Gaston Dombiak
*/
public enum CurrentSecuritySystemState {

/**
* The home is occupied and residents are active.
*/
STAY_ARM(0),
/**
* The home is unoccupied.
*/
AWAY_ARM(1),
/**
* The home is occupied and residents are sleeping.
*/
NIGHT_ARM(2),
/**
* The security system is disarmed.
*/
DISARMED(3),
/**
* The security system is triggered.
*/
TRIGGERED(4);

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

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

private final int code;

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

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

import com.beowulfe.hap.accessories.SecuritySystem;

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

/**
* Type of alarm of a {@link SecuritySystem}.
*
* @author Gaston Dombiak
*/
public enum SecuritySystemAlarmType {

/**
* Alarm conditions are cleared
*/
CLEARED(0),
/**
* Alarm type is not known
*/
UNKNOWN(1);

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

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

private final int code;

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

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

import com.beowulfe.hap.accessories.SecuritySystem;

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

/**
* The target state of a {@link SecuritySystem}.
*
* @author Gaston Dombiak
*/
public enum TargetSecuritySystemState {

/**
* The home is occupied and residents are active.
*/
STAY_ARM(0),
/**
* The home is unoccupied.
*/
AWAY_ARM(1),
/**
* The home is occupied and residents are sleeping.
*/
NIGHT_ARM(2),
/**
* The security system is disarmed.
*/
DISARMED(3);

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

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

private final int code;

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

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

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.SecuritySystem;
import com.beowulfe.hap.accessories.properties.CurrentSecuritySystemState;
import com.beowulfe.hap.characteristics.EnumCharacteristic;
import com.beowulfe.hap.characteristics.EventableCharacteristic;

import java.util.concurrent.CompletableFuture;

public class CurrentSecuritySystemStateCharacteristic extends EnumCharacteristic implements EventableCharacteristic {

private final SecuritySystem securitySystem;

public CurrentSecuritySystemStateCharacteristic(SecuritySystem securitySystem) {
super("00000066-0000-1000-8000-0026BB765291", false, true, "Current security system state", 4);
this.securitySystem = securitySystem;
}

@Override
protected CompletableFuture<Integer> getValue() {
return securitySystem.getCurrentSecuritySystemState().thenApply(CurrentSecuritySystemState::getCode);
}

@Override
protected void setValue(Integer value) throws Exception {
//Not writable
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
securitySystem.subscribeCurrentSecuritySystemState(callback);
}

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

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.SecuritySystem;
import com.beowulfe.hap.accessories.properties.SecuritySystemAlarmType;
import com.beowulfe.hap.characteristics.EnumCharacteristic;
import com.beowulfe.hap.characteristics.EventableCharacteristic;

import java.util.concurrent.CompletableFuture;

public class SecuritySystemAlarmTypeCharacteristic extends EnumCharacteristic implements EventableCharacteristic {

private final SecuritySystem securitySystem;

public SecuritySystemAlarmTypeCharacteristic(SecuritySystem securitySystem) {
super("0000008E-0000-1000-8000-0026BB765291", false, true, "Security system alarm type", 1);
this.securitySystem = securitySystem;
}

@Override
protected CompletableFuture<Integer> getValue() {
return securitySystem.getAlarmTypeState().thenApply(SecuritySystemAlarmType::getCode);
}

@Override
protected void setValue(Integer value) throws Exception {
//Not writable
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
securitySystem.subscribeAlarmTypeState(callback);
}

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

import com.beowulfe.hap.HomekitCharacteristicChangeCallback;
import com.beowulfe.hap.accessories.SecuritySystem;
import com.beowulfe.hap.accessories.properties.TargetSecuritySystemState;
import com.beowulfe.hap.characteristics.EnumCharacteristic;
import com.beowulfe.hap.characteristics.EventableCharacteristic;

import java.util.concurrent.CompletableFuture;

public class TargetSecuritySystemStateCharacteristic extends EnumCharacteristic implements EventableCharacteristic {

private final SecuritySystem securitySystem;

public TargetSecuritySystemStateCharacteristic(SecuritySystem securitySystem) {
super("00000067-0000-1000-8000-0026BB765291", true, true, "Target security system state", 3);
this.securitySystem = securitySystem;
}

@Override
protected CompletableFuture<Integer> getValue() {
return securitySystem.getTargetSecuritySystemState().thenApply(TargetSecuritySystemState::getCode);
}

@Override
protected void setValue(Integer value) throws Exception {
securitySystem.setTargetSecuritySystemState(TargetSecuritySystemState.fromCode(value));
}

@Override
public void subscribe(HomekitCharacteristicChangeCallback callback) {
securitySystem.subscribeTargetSecuritySystemState(callback);
}

@Override
public void unsubscribe() {
securitySystem.unsubscribeTargetSecuritySystemState();
}
}
Loading