cap:battery-level
- monitor battery level¶
The battery-level
capability is used for things that have a battery that
can be monitored. Sometimes this capability is combined with
charging-state if the thing also can report when it is
being charged.
if(thing.matches('cap:battery-level')) {
console.log('Current battery level:', await thing.batteryLevel());
}
API¶
-
batteryLevel
()¶ Get the current battery level as a percentage between 0 and 100.
Returns: Promise that resolves to the battery level in percent. Example:
thing.batteryLevel() .then(level => ...) .catch(...); const level = await thing.batteryLevel();
Events¶
-
batteryLevelChanged
The current battery level has changed. Payload will be the new battery level as a percentage.
thing.on('batteryLevelChanged', batteryLevel => console.log('Battery level is now:', batteryLevel));
Protected methods¶
-
updateBatteryLevel
(batteryLevel)¶ Update the current battery level. Should be called whenever a change in battery level is detected.
Arguments: - batteryLevel (percentage) – The new battery level. Will be converted to a percentage.
Example:
this.updateBatteryLevel(20); this.updateBatteryLevel('10');
Implementing capability¶
When implementing this capability the implementor needs to call
updateBatteryLevel
whenever the battery level changes.
const { Thing, BatteryLevel } = require('abstract-things');
class Example extends Thing.with(BatteryLevel) {
initCallback() {
return super.initCallback()
.then(readBatteryLevelSomehow)
.then(batteryLevel => {
this.updateBatteryLevel(batteryLevel);
});
}
}