cap:contact-detection
- contact sensing¶
This capability is used to mark sensors that report if contact is detected, such as for door and window sensors that detect if the door or window is open.
if(thing.matches('cap:contact-detection')) {
console.log('Has contact:', await thing.contactDetected());
thing.on('contactDetectedChanged', v => console.log('Contact is now:', c));
}
API¶
-
contactDetected
()¶ Boolean representing if the sensor is currently detecting contact.
Returns: Promise that resolves to if the sensor is detecting contact. Example:
if(await thing.contactDetected()) { console.log('Thing has detected contact'); }
Events¶
-
contactDetectedChanged
¶ The contact value has changed. Payload is the new contact state as a boolean.
Example:
thing.on('contactDetectedChanged', v => console.log('Contact is now:', c));
-
opened
¶ The sensor has detected it is does not have contact and is now opened.
Example:
thing.on('opened', v => console.log('Sensor is now open'));
Protected methods¶
-
updateContactDetected
(value)¶ Update if the sensor is currently detecting contact.
Arguments: - value – The new contact status as a boolean.
- autoIdleTimeout (duration) – Optional duration to switch back the contact detection status to
false
.
Example:
// Set the sensor to contact not detected (open) this.updateContactDetected(false); this.updateContactDetected(true, '1m');
Implementing capability¶
Implementors of this capability should call updateContact
whenever the
contact state changes.
const { Sensor, ContactDetection } = require('abstract-things/sensors');
class Example extends Sensor.with(ContactDetection) {
constructor() {
super();
this.updateContactDetected(true);
}
}