-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dialer.js
114 lines (95 loc) · 3 KB
/
Dialer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
var PulseDetector = require('./PulseDetector');
var ButtonDetector = require('./ButtonDetector');
var _ = require('lodash');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function Dialer(pulsePin, hookPin, dialExcursionPin) {
_.bindAll(this);
EventEmitter.call(this);
//this.waitForDialingDone = _.debounce(this.dialingDone, 5*1000);
this.pulsePin = pulsePin;
this.hookPin = hookPin;
this.dialExcursionPin = dialExcursionPin;
this.isOnHook = true;
this.dialExcursion = false;
this.digitsDialed = "";
this.inCall = false;
this.hookDetector = new ButtonDetector(hookPin);
this.hookDetector.on("buttonState", this.onHookDetected);
this.dialExcursionDetector = new ButtonDetector(dialExcursionPin);
this.dialExcursionDetector.on("buttonState", this.onDialExcursionDetected);
this.pulseDetector = new PulseDetector(pulsePin);
this.pulseDetector.on("pulses", this.onPulsesDetected);
this.dumpState();
}
util.inherits(Dialer, EventEmitter);
module.exports = Dialer;
Dialer.prototype.onHookDetected = function(buttonState) {
var isOnHook = !buttonState;
if (isOnHook !== this.isOnHook) {
this.isOnHook = isOnHook;
console.log(this.isOnHook ? "on hook" : "off hook");
if (this.isOnHook){
if (this.digitsDialed.length) {
this.digitsDialed = "";
} else {
this.hangUp();
}
}
}
this.dumpState();
};
Dialer.prototype.onDialExcursionDetected = function(buttonState) {
if (buttonState !== this.dialExcursion) {
this.dialExcursion = buttonState;
// console.log(this.dialExcursion ? "dial excursion" : "dial at rest position");
// if (this.dialExcursion) {
// this.waitForDialingDone.cancel();
// }
}
this.dumpState();
};
Dialer.prototype.onPulsesDetected = function(pulses) {
if (!this.isOnHook && !this.inCall) {
if (pulses > 9) {
pulses = 0;
}
this.digitsDialed += pulses;
console.log("digit "+pulses);
this.waitForDialingDone();
}
this.dumpState();
};
Dialer.prototype.dialingDone = function() {
if (!this.isOnHook && !this.inCall && !this.dialExcursion && this.digitsDialed.length >= 5) {
this.call(this.digitsDialed);
this.digitsDialed = "";
}
this.dumpState();
};
Dialer.prototype.waitForDialingDone = _.debounce(function() {
this.dialingDone();
}, 5 * 1000);
Dialer.prototype.call = function(dialString) {
console.log("Calling "+dialString);
this.inCall = true;
};
Dialer.prototype.hangUp = function() {
this.inCall = false;
console.log("hanging up");
this.dumpState();
};
Dialer.prototype.dumpState = function() {
var stateMessage = [
this.isOnHook ? "on hook" : "off hook",
this.dialExcursion ? "dial excursion" : "dial in rest position",
this.inCall ? "in call" : "not in call",
"digits dialed: "+this.digitsDialed
].join(", ");
console.log("Dialer state = "+stateMessage);
};
Dialer.prototype.close = function() {
this.pulseDetector.close();
this.hookDetector.close();
this.dialExcursionDetector.close();
};