mirror of
https://github.com/jambonz/jambonz-feature-server.git
synced 2025-12-20 08:40:38 +00:00
major revamp of http client functionalit
This commit is contained in:
42
lib/utils/dtmf-collector.js
Normal file
42
lib/utils/dtmf-collector.js
Normal file
@@ -0,0 +1,42 @@
|
||||
class DtmfEntry {
|
||||
constructor(key, time) {
|
||||
this.key = key;
|
||||
this.time = time;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @classdesc Represents an object that collects dtmf key entries and
|
||||
* reports when a match is detected
|
||||
*/
|
||||
class DtmfCollector {
|
||||
constructor({logger, patterns, interDigitTimeout}) {
|
||||
this.logger = logger;
|
||||
this.patterns = patterns;
|
||||
this.idt = interDigitTimeout || 3000;
|
||||
this.buffer = [];
|
||||
}
|
||||
|
||||
keyPress(key) {
|
||||
const now = Date.now();
|
||||
|
||||
// age out previous entries if interdigit timer has elapsed
|
||||
const lastDtmf = this.buffer.pop();
|
||||
if (lastDtmf) {
|
||||
if (now - lastDtmf.time < this.idt) this.buffer.push(lastDtmf);
|
||||
else {
|
||||
this.buffer = [];
|
||||
}
|
||||
}
|
||||
// add new entry
|
||||
this.buffer.push(new DtmfEntry(key, now));
|
||||
|
||||
// check for a match
|
||||
const collectedDigits = this.buffer
|
||||
.map((entry) => entry.key)
|
||||
.join('');
|
||||
return this.patterns.find((pattern) => collectedDigits.endsWith(pattern));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DtmfCollector;
|
||||
Reference in New Issue
Block a user