@pst-on-npm/homebridge-enocean
Version:
Integrate EnOcean® devices into Homebridge.
64 lines • 2.17 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ButtonEventManager = void 0;
/**
* ButtonEventManager class for handling button events
*/
class ButtonEventManager {
onButtonPress;
hap;
singleClickTimeout = null;
longPressTimeout = null;
clickCount = 0;
isLongPress = false;
isButtonPressed = false;
singleClickDelay = 350; // milliseconds
longPressDelay = 600; // milliseconds
constructor(onButtonPress, hap) {
this.onButtonPress = onButtonPress;
this.hap = hap;
}
/**
* Handle button press - single, double or long press.
* If the button is pressed for longer than the long press delay, it is considered a long press
*/
buttonPressed() {
this.isButtonPressed = true;
this.isLongPress = false;
this.longPressTimeout = setTimeout(() => {
this.isLongPress = true;
this.onButtonPress(this.hap.Characteristic.ProgrammableSwitchEvent.LONG_PRESS);
}, this.longPressDelay);
}
/**
* Handle button release - single or double press.
* If the button is released before the long press delay, it is considered a single or double press
*/
buttonReleased() {
if (!this.isButtonPressed) {
return;
}
this.isButtonPressed = false;
if (this.longPressTimeout) {
clearTimeout(this.longPressTimeout);
}
if (this.isLongPress) {
return;
}
this.clickCount++;
if (this.singleClickTimeout) {
clearTimeout(this.singleClickTimeout);
}
this.singleClickTimeout = setTimeout(() => {
if (this.clickCount === 1) {
this.onButtonPress(this.hap.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS);
}
else if (this.clickCount === 2) {
this.onButtonPress(this.hap.Characteristic.ProgrammableSwitchEvent.DOUBLE_PRESS);
}
this.clickCount = 0;
}, this.singleClickDelay);
}
}
exports.ButtonEventManager = ButtonEventManager;
//# sourceMappingURL=ButtonEventManager.js.map