Class BaseCharacteristic<T>

    • Constructor Detail

      • BaseCharacteristic

        public BaseCharacteristic​(String type,
                                  String format,
                                  String description,
                                  boolean isReadable,
                                  boolean isWritable,
                                  Optional<Consumer<HomekitCharacteristicChangeCallback>> subscriber,
                                  Optional<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 changes
        unsubscriber - unsubscribes to changes
    • Method Detail

      • toJson

        public final 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 interface Characteristic
        Parameters:
        iid - The instance ID of the characteristic to be included in the serialization.
        Returns:
        the future completing with the resulting JSON.
      • makeBuilder

        protected 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 interface Characteristic
        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 interface Characteristic
        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 interface EventableCharacteristic
        Parameters:
        callback - a function to call when a change is made to the characteristic value.
      • 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

        protected abstract void setValue​(T value)
                                  throws Exception
        Update the characteristic value using a new value supplied by the connected client.
        Parameters:
        value - the new value to set.
        Throws:
        Exception - if the value cannot be set.
      • getValue

        protected abstract 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 to
        value - The value to add