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 battery-level capability.

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

API

charging()

Get the current charging state as a boolean. true indicates that the thing is charging.

Returns:Promise that resolves to the current charging state.

Example:

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

const isCharging = await thing.charging();

Events

chargingChanged

The current charging state has changed. Payload will be the new state a boolean.

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

The thing is now being charged.

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

The thing is no longer being charged.

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

Protected methods

updateCharging(chargingState)

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

Arguments:
  • chargingState (boolean) – The new charging state.

Example:

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);
      });
  }

}