-
Notifications
You must be signed in to change notification settings - Fork 1
/
phone.advanced.js
151 lines (128 loc) · 4.04 KB
/
phone.advanced.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
let EventEmitter = require('events').EventEmitter;
let bluebird = require('bluebird');
let promisifyAll = bluebird.promisifyAll;
let SerialPort = require('serialport');
let moment = require('moment');
let codes = {
ring: '\u0010R',
number: 'NMBR=',
dial: '\u0010d',
busy: '\u0010b',
};
/** WIP **/
function extractPhoneNumber(string) {
let number = string.indexOf(codes.number);
if (number < 0) {
return null;
}
let phoneNumber = string.substring(number + 5, string.length - 1);
phoneNumber = phoneNumber.replace(/\D/g, '');
return phoneNumber;
}
class Phone extends EventEmitter {
constructor(port) {
super();
this.serialPort = new SerialPort(port);
promisifyAll(this.serialPort);
this.serialPort.on('open', this.init.bind(this));
}
async init() {
await this.setToVoiceMode();
await this.setUpListener();
this.emit('init');
}
async setToVoiceMode() {
await this.serialPort.writeAsync('AT+FCLASS=8\r');
await this.serialPort.drainAsync();
await this.serialPort.writeAsync('AT+VCID=1\r');
await this.serialPort.drainAsync();
await this.hangUp();
}
async setUpListener() {
this.serialPort.on('data', async data => {
let msg = Buffer.from(data, 'ascii').toString();
this.lastEventAt = moment();
// If data contains a phone number extract it and trigger current call.
let phoneNumber = extractPhoneNumber(msg);
if (phoneNumber) {
this.startCall(phoneNumber);
return;
}
// console.log(JSON.stringify(msg));
if (msg === codes.ring) {
this.ring();
return;
}
if (msg === codes.dial) {
this.endCall();
}
if (msg === codes.busy) {
this.endCall();
}
});
}
async startCall(phoneNumber) {
this.ongoingCall = {
number: phoneNumber,
startedAt: moment(),
rings: 1,
pickedUpAt: null,
hungUpAt: null,
};
this.emit('call.incoming', this.ongoingCall);
this.emit('ringing', this.ongoingCall);
this.setCheck();
}
async endCall() {
if (this.ongoingCall) {
if (this.ongoingCall.pickedUpAt) {
this.ongoingCall.hungUpAt = moment();
this.ongoingCall.duration = this.ongoingCall.pickedUpAt.diff(this.ongoingCall.hungUpAt, 'seconds');
this.emit('call.end', this.ongoingCall);
} else {
this.emit('ringing.end', this.ongoingCall);
}
}
this.ongoingCall = null;
await this.hangUp();
}
async ring() {
if (this.lastRingAt) {
let lastRingAgo = moment().diff(this.lastRingAt, 'seconds');
if (lastRingAgo === 4) {
this.ongoingCall.rings++;
this.emit('ringing', this.ongoingCall);
}
}
this.lastRingAt = moment();
this.setCheck();
}
async pickUp() {
await this.serialPort.writeAsync('AT+VLS=5\r');
await this.serialPort.drainAsync();
}
async hangUp() {
await this.serialPort.writeAsync('AT+VLS=0\r');
await this.serialPort.drainAsync();
}
async setCheck() {
if (this.check) {
clearTimeout(this.check);
}
this.check = setTimeout(async () => {
let ringSecondsAgo = moment().diff(this.lastRingAt, 'seconds');
if (ringSecondsAgo > 4) {
await this.pickUp();
setTimeout(() => {
if (this.ongoingCall) {
this.ongoingCall.pickedUpAt = moment();
this.emit('call.start', this.ongoingCall);
}
}, 4000);
}
}, 5000);
}
async checkIfPickedUp() {
}
}
module.exports = Phone;