Class BaseCharacteristic<T>
- java.lang.Object
-
- io.github.hapjava.characteristics.impl.base.BaseCharacteristic<T>
-
- All Implemented Interfaces:
Characteristic
,EventableCharacteristic
- Direct Known Subclasses:
BooleanCharacteristic
,EnumCharacteristic
,FloatCharacteristic
,IntegerCharacteristic
,StaticStringCharacteristic
,StringCharacteristic
public abstract class BaseCharacteristic<T> extends java.lang.Object implements Characteristic, EventableCharacteristic
Base class for implementingCharacteristic
.- Author:
- Andy Lintner
-
-
Constructor Summary
Constructors Constructor Description BaseCharacteristic(java.lang.String type, java.lang.String format, java.lang.String description, boolean isReadable, boolean isWritable, java.util.Optional<java.util.function.Consumer<HomekitCharacteristicChangeCallback>> subscriber, java.util.Optional<java.lang.Runnable> unsubscriber)
Default constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract T
convert(javax.json.JsonValue jsonValue)
Converts from the JSON value to a Java object of the type Tabstract T
getDefault()
Supplies a default value for the characteristic to send to connected clients when the real value.java.lang.String
getType()
abstract java.util.concurrent.CompletableFuture<T>
getValue()
Retrieves the current value of the characteristic.protected java.util.concurrent.CompletableFuture<javax.json.JsonObjectBuilder>
makeBuilder(int instanceId)
Creates the JSON serialized form of the accessory for use over the HomeKit Accessory Protocol.protected void
setJsonValue(javax.json.JsonObjectBuilder builder, T value)
Writes the value key to the serialized characteristicvoid
setValue(javax.json.JsonValue jsonValue)
Invoked by the remote client, this updates the current value of the characteristic.abstract void
setValue(T value)
Update the characteristic value using a new value supplied by the connected client.void
subscribe(HomekitCharacteristicChangeCallback callback)
Begin listening to changes to this characteristic.void
supplyValue(javax.json.JsonObjectBuilder builder)
Adds an attribute to the passed JsonObjectBuilder named "value" with the current value of the characteristic.java.util.concurrent.CompletableFuture<javax.json.JsonObject>
toJson(int iid)
Creates the JSON representation of the characteristic, in accordance with the HomeKit Accessory Protocol.void
unsubscribe()
Stop listening to changes to this characteristic.
-
-
-
Constructor Detail
-
BaseCharacteristic
public BaseCharacteristic(java.lang.String type, java.lang.String format, java.lang.String description, boolean isReadable, boolean isWritable, java.util.Optional<java.util.function.Consumer<HomekitCharacteristicChangeCallback>> subscriber, java.util.Optional<java.lang.Runnable> unsubscriber)
Default constructor- Parameters:
type
- a string containing a UUID that indicates the type of characteristic. Apple defines a set of these, however implementors can create their own as well.format
- a string indicating the value type, which must be a recognized type by the consuming device.isWritable
- indicates whether the value can be changed.isReadable
- indicates whether the value can be retrieved.description
- a description of the characteristic to be passed to the consuming device.subscriber
- subscribes to changesunsubscriber
- unsubscribes to changes
-
-
Method Detail
-
getType
public java.lang.String getType()
- Specified by:
getType
in interfaceCharacteristic
- Returns:
- The UUID type for this characteristic.
-
toJson
public final java.util.concurrent.CompletableFuture<javax.json.JsonObject> toJson(int iid)
Description copied from interface:Characteristic
Creates the JSON representation of the characteristic, in accordance with the HomeKit Accessory Protocol.- Specified by:
toJson
in interfaceCharacteristic
- Parameters:
iid
- The instance ID of the characteristic to be included in the serialization.- Returns:
- the future completing with the resulting JSON.
-
makeBuilder
protected java.util.concurrent.CompletableFuture<javax.json.JsonObjectBuilder> makeBuilder(int instanceId)
Creates the JSON serialized form of the accessory for use over the HomeKit Accessory Protocol.- Parameters:
instanceId
- the static id of the accessory.- Returns:
- a future that will complete with the JSON builder for the object.
-
setValue
public final void setValue(javax.json.JsonValue jsonValue)
Invoked by the remote client, this updates the current value of the characteristic.- Specified by:
setValue
in interfaceCharacteristic
- Parameters:
jsonValue
- the JSON serialized value to set.
-
supplyValue
public void supplyValue(javax.json.JsonObjectBuilder builder)
Adds an attribute to the passed JsonObjectBuilder named "value" with the current value of the characteristic.- Specified by:
supplyValue
in interfaceCharacteristic
- Parameters:
builder
- the JsonObjectBuilder to add the value attribute to.
-
subscribe
public void subscribe(HomekitCharacteristicChangeCallback callback)
Begin listening to changes to this characteristic. When a change is made, call the provided function.- Specified by:
subscribe
in interfaceEventableCharacteristic
- Parameters:
callback
- a function to call when a change is made to the characteristic value.
-
unsubscribe
public void unsubscribe()
Stop listening to changes to this characteristic.- Specified by:
unsubscribe
in interfaceEventableCharacteristic
-
convert
protected abstract T convert(javax.json.JsonValue jsonValue)
Converts from the JSON value to a Java object of the type T- Parameters:
jsonValue
- the JSON value to convert from.- Returns:
- the converted Java object.
-
setValue
public abstract void setValue(T value) throws java.lang.Exception
Update the characteristic value using a new value supplied by the connected client.- Parameters:
value
- the new value to set.- Throws:
java.lang.Exception
- if the value cannot be set.
-
getValue
public abstract java.util.concurrent.CompletableFuture<T> getValue()
Retrieves the current value of the characteristic.- Returns:
- a future that will complete with the current value.
-
getDefault
public abstract T getDefault()
Supplies a default value for the characteristic to send to connected clients when the real value. cannot be retrieved.- Returns:
- a sensible default value.
-
setJsonValue
protected void setJsonValue(javax.json.JsonObjectBuilder builder, T value)
Writes the value key to the serialized characteristic- Parameters:
builder
- The JSON builder to add the value tovalue
- The value to add
-
-