-
Notifications
You must be signed in to change notification settings - Fork 5
/
cdrReceiver.js
executable file
·143 lines (129 loc) · 4.86 KB
/
cdrReceiver.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
#!/usr/bin/env node
var fs = require('fs');
if (fs.existsSync('./config.js')) {
var config = require('./config.js')
} else {
console.log('No config.js file found: please edit config.default.js and save as config.js')
process.exit()
}
var express = require('express'),
bodyParser = require('body-parser'),
mysql = require('mysql')
require('body-parser-xml')(bodyParser)
var mysqlPoolConfig = Object.assign({timezone : 'z'},config.mysqlConfig)
var mysqlPool = mysql.createPool(mysqlPoolConfig)
var dbWorkers = []
var app = express()
app.use(bodyParser.xml({
limit: '1MB', // Reject payload bigger than 1 MB
xmlParseOptions: {
normalize: true, // Trim whitespace inside text nodes
normalizeTags: false, // Transform tags to lowercase
explicitArray: false, // Only put nodes in array if >1
mergeAttrs: true
}
}))
app.post('/cdr', function(req, res, body) {
// Any request with an XML payload will be parsed
// and a JavaScript object produced on req.body
// corresponding to the request payload.
if ( req.body.records.record.length > 0 ) {
var myRecords = req.body.records.record
} else {
var myRecords = [ req.body.records.record ]
}
console.log('session: ' + req.body.records.session)
for ( var i = 0; i < myRecords.length; i++){
console.log(myRecords[i])
if ( typeof myRecords[i].numPreceedingRecordsMissing != 'undefined' ){
console.log('Records missing: ' + myRecords[i].numPreceedingRecordsMissing)
delete myRecords[i].numPreceedingRecordsMissing
}
myRecords[i].session = req.body.records.session
var dbWorker = new RecordWriter(myRecords[i])
dbWorkers.push(dbWorker)
console.log('--------end record-----------')
res.status(200).end()
}
console.log('--------end records---------')
})
app.listen(config.listenPort, function () {
console.log('cdrReceiver is listening on port: ',config.listenPort)
})
function RecordWriter(record){
if ( record.type == 'callStart' ){
var post = record.call
post.start = record.time
mysqlPool.query('INSERT INTO calls SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted call')
post = record
tmp = record.call.id
post.call = tmp
mysqlPool.query('INSERT INTO callEvents SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted callEvent')
})
}.bind(this))
} else if ( record.type == 'callEnd' ){
var post = record.call
id = mysqlPool.escape(post.id)
post.end = record.time
mysqlPool.query('UPDATE calls SET ? WHERE id = ' + id , post, function (error, results, fields) {
if (error) console.log(error)
console.log('updated call')
post = record
tmp = record.call.id
post.call = tmp
mysqlPool.query('INSERT INTO callEvents SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted callEvent')
})
}.bind(this))
} else if ( record.type == 'callLegStart' ){
var post = record.callLeg
post.start = record.time
mysqlPool.query('INSERT INTO callLegs SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted callLeg')
post = record
tmp = record.callLeg.id
post.callLeg = tmp
mysqlPool.query('INSERT INTO callLegEvents SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted callLegEvent')
})
}.bind(this))
} else if ( record.type == 'callLegUpdate' ){
var post = record.callLeg
id = mysqlPool.escape(post.id)
if ( post.state == 'connected' ) post.connected = record.time
mysqlPool.query('UPDATE callLegs SET ? WHERE id = ' + id , post, function (error, results, fields) {
if (error) console.log(error)
console.log('updated callLeg')
post = record
tmp = record.callLeg.id
post.callLeg = tmp
mysqlPool.query('INSERT INTO callLegEvents SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted callLegEvent')
})
}.bind(this))
} else if ( record.type == 'callLegEnd' ){
var post = record.callLeg
id = mysqlPool.escape(post.id)
post.end = record.time
for (var j in post ){ if (typeof post[j] == "object") post[j] = JSON.stringify(post[j])}
mysqlPool.query('UPDATE callLegs SET ? WHERE id = ' + id , post, function (error, results, fields) {
if (error) console.log(error)
console.log('updated callLeg')
post = record
tmp = record.callLeg.id
post.callLeg = tmp
mysqlPool.query('INSERT INTO callLegEvents SET ?', post, function (error, results, fields) {
if (error) console.log(error)
console.log('inserted callLegEvent')
})
}.bind(this))
}
}