-
Notifications
You must be signed in to change notification settings - Fork 17
/
ari_client.js
334 lines (307 loc) · 11.6 KB
/
ari_client.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
var ari = require('ari-client');
var uuid = require('uuid')
module.exports = function(RED) {
"use strict";
var ariConnectionPool = (function() {
var connections = {};
var channels = {}
var obj = {
setconn: function(url,username,password, app, node) {
var id = uuid.v4()
ari.connect(url, username, password, function(err, client){
if (err) {
node.error(err);
}
var id = client.id
connections[id] =client
clientLoaded(client, app, node,id)
});
//connections[id]._id = id;
//connections[id]._nodeCount = 0;
//connections[id]._nodeCount += 1;
return connections[id];
},
getconn : function(id){
return connections[id]
},
close: function(connection) {
connection._nodeCount -= 1;
if (connection._nodeCount === 0) {
delete connections[connection._id];
}
},
setchan: function(channel){
var id = channel.id
channels[id] = channel
return id
},
getchan: function(id){
return channels[id]
}
};
return obj;
}());
function clientLoaded (client, app, node, id) {
node.status({fill:"green",shape:"dot",text:"connected"});
function stasisStart(event, channel) {
var dialed = event.args[0] === 'dialed';
if (!dialed){
var channelid = ariConnectionPool.setchan(channel)
var msg = {}
msg.channel = channelid
msg.client = id
msg.payload = event
node.send([msg, null])
}
}
function stasisEnd(event, channel){
//console.log(event)
}
function dtmfEvent(event, channel){
var msg = {}
msg.channel = channel.id
msg.client = id
msg.payload = event
node.send([null, msg])
}
client.on('StasisStart', stasisStart);
//client.on('StasisEnd', stasisEnd);
client.on('ChannelDtmfReceived', dtmfEvent);
client.start(app);
}
function ari_client(n) {
RED.nodes.createNode(this,n);
var node = this;
this.sip_user = n.sip_user
this.sip_password = n.sip_password
this.server = RED.nodes.getNode(n.server);
provision(this.server.credentials.url, this.server.credentials.username, this.server.credentials.password, this.sip_user, this.sip_password)
this.conn = ariConnectionPool.setconn(this.server.credentials.url, this.server.credentials.username, this.server.credentials.password, this.sip_password, node)
this.on("close", function() {
//deprovision(this.server.credentials.url, this.server.credentials.username, this.server.credentials.password, this.sip_user)
//this.conn.close()
});
}
RED.nodes.registerType("ari_client",ari_client);
function ari_playback(n) {
RED.nodes.createNode(this,n);
var node = this;
this.media = n.media
node.on('input', function (msg) {
node.status({fill:"blue",shape:"dot"});
var client = ariConnectionPool.getconn(msg.client)
var channel = ariConnectionPool.getchan(msg.channel)
var playback = client.Playback();
channel.play({media: this.media},
playback, function(err, newPlayback) {if (err) {throw err;}});
playback.on('PlaybackFinished', function(event, completedPlayback) {
msg.payload = event
node.send(msg)
node.status({})
});
});
this.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
// Allows ports to be closed, connections dropped etc.
// eg: node.client.disconnect();
});
}
RED.nodes.registerType("ari_playback",ari_playback);
function ari_hangup(n) {
RED.nodes.createNode(this,n);
var node = this;
node.on('input', function (msg) {
node.status({fill:"blue",shape:"dot"});
var channel = ariConnectionPool.getchan(msg.channel)
channel.hangup(function(err) {
if (err) {node.error(err);}
node.status({})
});
});
}
RED.nodes.registerType("ari_hangup",ari_hangup);
function ari_answer(n) {
RED.nodes.createNode(this,n);
var node = this;
node.on('input', function (msg) {
node.status({fill:"blue",shape:"dot"});
var client = ariConnectionPool.getconn(msg.client)
var channel = ariConnectionPool.getchan(msg.channel)
client.channels.answer({channelId: channel.id},function (err) {
if (err) {node.error(err);}
msg.payload = 'answered'
node.send(msg)
node.status({})
});
});
this.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
// Allows ports to be closed, connections dropped etc.
// eg: node.client.disconnect();
});
}
RED.nodes.registerType("ari_answer",ari_answer);
function ari_bridgedial(n){
RED.nodes.createNode(this,n);
var node = this;
this.destination = n.destination
this.callerId = n.callerId
node.on('input', function (msg) {
node.status({fill:"blue",shape:"dot"});
var client = ariConnectionPool.getconn(msg.client)
var channel = ariConnectionPool.getchan(msg.channel)
// Create outbound channel
var dialed = client.Channel();
var bridge = client.Bridge();
var bridgeid = bridge.id
bridge.create({type: 'mixing, dtmf_events'}, function(err) {if (err) {throw err;}})
client.start(bridgeid);
dialed.on('StasisStart', function(event, dialed) {
dialed.answer(function(err) {if (err) {throw err;}})
bridge.addChannel({channel: [channel.id, dialed.id]}, function(err) {if (err) {throw err;}});
var channelid = ariConnectionPool.setchan(dialed)
var bmsg = {}
bmsg.channel = channelid
bmsg.client = client.id
msg.type = "connected"
bmsg.type = "connected"
bmsg.payload = {bridge : bridge.id}
msg.payload = {bridge : bridge.id}
if (n.connected_event) {
node.send([msg, bmsg])
}
});
dialed.on('StasisEnd', function(event, dialed) {
bridge.destroy(function(err) {});
msg.type = "ended"
msg.payload = event
if (n.ended_event) {
node.send([msg, null])
}
node.status({});
});
channel.on('StasisEnd', function(event, channel) {
var msg = {}
msg.type = "ended"
msg.channel = dialed.id
msg.client = client.id
msg.payload = event
bridge.destroy(function(err) {});
if (n.ended_event) {
node.send([null, msg])
}
node.status({});
});
channel.on('ChannelDtmfReceived', function(event, channel){
var msg = {}
msg.type = "DTMF"
msg.channel = channel.id
msg.client = client.id
msg.payload = event
node.send([msg, null])
});
dialed.on('ChannelDtmfReceived', function(event, dialled){
var msg = {}
msg.type = "DTMF"
msg.channel = dialled.id
msg.client = client.id
msg.payload = event
node.send([null, msg])
});
dialed.originate({endpoint: this.destination, callerId: this.callerId, app: bridgeid, appArgs: 'dialed'}, function(err, response) {
if (err) {throw err;}
});
});
this.on("close", function() {
// Called when the node is shutdown - eg on redeploy.
// Allows ports to be closed, connections dropped etc.
// eg: node.client.disconnect();
});
}
RED.nodes.registerType("ari_bridgedial",ari_bridgedial);
}
function provision(url,username,password, sip_user, sip_password){
ari.connect(url, username, password)
.then(function (client){
client.asterisk.updateObject({
configClass: 'res_pjsip',
id: sip_user,
objectType: 'auth',
fields : [
{ attribute: 'auth_type', value: 'userpass' },
{ attribute: 'username', value: sip_user },
{ attribute: 'password', value: sip_password }
]
})
.then (function (configTuples){
//console.log(configTuples)
client.asterisk.updateObject({
configClass: 'res_pjsip',
id: sip_user,
objectType: 'aor',
fields : [
{ attribute: 'support_path', value: "yes" },
{ attribute: 'remove_existing', value: "yes" },
{ attribute: 'max_contacts', value: "1" }
]
})
.then (function (configTuples){
//console.log(configTuples)
client.asterisk.updateObject({
configClass: 'res_pjsip',
id: sip_user,
objectType: 'endpoint',
fields : [
{ attribute: 'from_user', value: sip_user },
{ attribute: 'allow', value: "!all,g722,ulaw,alaw" },
{ attribute: 'ice_support', value: "yes" },
{ attribute: 'force_rport', value: "yes" },
{ attribute: 'rewrite_contact', value: "yes" },
{ attribute: 'rtp_symmetric', value: "yes" },
{ attribute: 'context', value: "applications" },
{ attribute: 'auth', value: sip_user },
{ attribute: 'aors', value: sip_user }
]
})
.then (function (configTuples){
//console.log(configTuples)
})
})
})
})
.catch(function (err) {
console.log(err)
});
}
function deprovision(url,username,password, sip_user){
ari.connect(url, username, password)
.then(function (client){
client.asterisk.deleteObject({
configClass: 'res_pjsip',
id: sip_user,
objectType: 'endpoint'
})
.then (function (configTuples){
//console.log(configTuples)
client.asterisk.deleteObject({
configClass: 'res_pjsip',
id: sip_user,
objectType: 'aor'
})
.then (function (configTuples){
console.log(configTuples)
client.asterisk.deleteObject({
configClass: 'res_pjsip',
id: sip_user,
objectType: 'auth'
})
.then (function (configTuples){
console.log(configTuples)
})
})
})
})
.catch(function (err) {
console.log(err)
});
}