cap:power - monitor power state

The power-capability is used for any thing that can monitor its power state.

if(thing.matches('cap:power')) {
        console.log('Power is', await thing.power());

        thing.on('powerChanged', power => console.log('Power is now', power));
}

Related capabilities: switchable-power, state

API

power()

Get the current power state.

Returns:Promise that resolves to a boolean representing the current power state.

Example:

thing.power()
        .then(power => ...)
        .catch(...);

const powerIsOn = await thing.power();

Events

powerChanged

The current power state has changed. Payload will be current power state as a boolean.

thing.on('powerChanged', power => console.log('power is now:', power));

Protected methods

updatePower(power)

Update the current power state of the thing. Will change the state key power and emit the power event.

Arguments:
  • power (boolean) – The current power state.

Implementing capability

The power-capability has no functions that need to be implemented. Call updatePower whenever the monitored power state changes.

Example:

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

class Example extends Thing.with(Power) {
        constructor() {
                super();

                // Indicate that power has been switched every second
                setInterval(() => {
                        this.updatePower(! this.getState('power'));
                }, 1000);
        }
}