Skip to content

Latest commit

 

History

History
100 lines (62 loc) · 2.1 KB

charging-state.rst

File metadata and controls

100 lines (62 loc) · 2.1 KB

cap:charging-state - monitor if charging

The charging-state capability is used for things that have a battery and can report if they are being charged or not. Some of these things will also have the :doc:`battery-level <battery-level>` capability.

if(thing.matches('cap:charging-state')) {
        if(await thing.charging()) {
                // This thing is charging
        }
}

API

.. js:function:: charging()

        Get the current charging state as a :doc:`boolean </values/boolean>`.
        ``true`` indicates that the thing is charging.

        :returns: Promise that resolves to the current charging state.

        Example:

        .. sourcecode:: js

                thing.charging()
                        .then(isCharging => ...)
                        .catch(...);

                const isCharging = await thing.charging();

Events

.. describe:: chargingChanged

        The current charging state has changed. Payload will be the new state
        a :doc:`boolean </values/boolean>`.

        .. sourcecode:: js

                thing.on('chargingChanged', v => console.log('Charging:', v));

.. describe:: chargingStarted

        The thing is now being charged.

        .. sourcecode:: js

                thing.on('chargingStarted', () => console.log('Charging started'));

.. describe:: chargingStopped

        The thing is no longer being charged.

        .. sourcecode:: js

                thing.on('chargingStopped', () => console.log('Charging stopped'));

Protected methods

.. js:function:: updateCharging(chargingState)

        Update the current charging state. Should be called whenever a change in
        charging state is detected.

        :param boolean chargingState: The new charging state.

        Example:

        .. sourcecode:: js

                this.updateCharging(true);

Implementing capability

When implementing this capability the implementor needs to call updateCharging whenever the charging state changes.

const { Thing, ChargingState } = require('abstract-things');

class Example extends Thing.with(ChargingState) {

        initCallback() {
                return super.initCallback()
                        .then(readChargingStateSomehow)
                        .then(chargingState => {
                                this.updateCharging(chargingState);
                        });
        }

}