var AmiIo = require("ami-io");
var config = {host:'127.0.0.1',port:5038,login:'admin',password:'123456'};
var amiio = AmiIo.createClient(config);
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : '127.0.0.1',
user : 'root',
password : '123456',
database : 'test'
});
Date.prototype.Format = function(fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
};
amiio.on('rawEvent', function(event){
console.log("-----------------begin");
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
var logdate = new Date().Format("yyyy-MM-dd hh:mm:ss");
var addSql = 'insert into asterisk_log(log_date,log_text) VALUES(?,?)';
var addSqlParams = [logdate, JSON.stringify(event)];
// Use the connection
connection.query(addSql,addSqlParams, function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});
console.log(event.event);
console.log("-----------------end");
});
amiio.connect();