Commit 3309cfa1 authored by antirek's avatar antirek

remove some lines

parent fb764f1a
...@@ -3,15 +3,17 @@ var AGIServer = require('./../lib/index'); ...@@ -3,15 +3,17 @@ var AGIServer = require('./../lib/index');
var handler = function (context) { var handler = function (context) {
context.onEvent('variables') context.onEvent('variables')
.then(function (vars) { .then(function (vars) {
return context.streamFile('beep'); console.log('vars', vars);
return context.streamFile('beep', '#');
}) })
.then(function (result) { .then(function (result) {
return context.setVariable('RECOGNITION_RESULT', 'I\'m your father, Luc'); return context.setVariable('RECOGNITION_RESULT', 'I\'m your father, Luc');
}) })
.then(function (result) { .then(function (result) {
return context.end(); return context.end();
}); })
.fail(console.log);
}; };
var agi = new AGIServer(handler); var agi = new AGIServer(handler, {debug: true});
agi.start(3000); agi.start(3007);
\ No newline at end of file \ No newline at end of file
...@@ -330,6 +330,7 @@ module.exports = [ ...@@ -330,6 +330,7 @@ module.exports = [
} }
}, },
{ {
default: '#',
prepare: function (value) { prepare: function (value) {
return '"' + value + '"'; return '"' + value + '"';
} }
......
...@@ -4,28 +4,27 @@ var state = require('./state'); ...@@ -4,28 +4,27 @@ var state = require('./state');
var Q = require('q'); var Q = require('q');
var commands = require('./command'); var commands = require('./command');
//base context
var Context = function (stream, debug) { var Context = function (stream, debug) {
EventEmitter.call(this); EventEmitter.call(this);
this.debug = debug; this.debug = debug;
this.stream = new Readable(); this.stream = new Readable();
//this.stream = stream;
this.stream.setEncoding('utf8'); this.stream.setEncoding('utf8');
this.stream.wrap(stream); this.stream.wrap(stream);
this.state = state.init; this.state = state.init;
this.msg = ""; this.msg = "";
this.variables = {};
this.pending = null;
var self = this; var self = this;
this.stream.on('readable', function () { this.stream.on('readable', function () {
//always keep the 'leftover' part of the message //always keep the 'leftover' part of the message
self.msg = self.read(); self.msg = self.read();
}); });
//this.msg = this.read();
this.variables = {};
this.pending = null;
this.stream.on('error', this.emit.bind(this, 'error')); this.stream.on('error', this.emit.bind(this, 'error'));
this.stream.on('close', this.emit.bind(this, 'close')); this.stream.on('close', this.emit.bind(this, 'close'));
...@@ -33,34 +32,25 @@ var Context = function (stream, debug) { ...@@ -33,34 +32,25 @@ var Context = function (stream, debug) {
require('util').inherits(Context, EventEmitter); require('util').inherits(Context, EventEmitter);
Context.prototype.read = function() { Context.prototype.read = function () {
var buffer = this.stream.read(); var buffer = this.stream.read();
if (!buffer) return this.msg; if (!buffer) return this.msg;
this.msg += buffer; //.toString('utf8'); this.msg += buffer;
if (this.state === state.init) { if (this.state === state.init) {
if(this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message if (this.msg.indexOf('\n\n') < 0) return this.msg; //we don't have whole message
this.readVariables(this.msg); this.readVariables(this.msg);
} else if (this.state === state.waiting) { } else if (this.state === state.waiting) {
if(this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message if (this.msg.indexOf('\n') < 0) return this.msg; //we don't have whole message
this.readResponse(this.msg); this.readResponse(this.msg);
} }
return ""; return '';
}; };
Context.prototype.readVariables = function (msg) { Context.prototype.readVariables = function (msg) {
var lines = msg.split('\n'); var lines = msg.split('\n');
/*
for(var i = 0; i < lines.length; i++) {
var line = lines[i];
var split = line.split(':')
var name = split[0];
var value = split[1];
this.variables[name] = (value||'').trim();
}*/
lines.map(function (line) { lines.map(function (line) {
var split = line.split(':'); var split = line.split(':');
...@@ -71,33 +61,30 @@ Context.prototype.readVariables = function (msg) { ...@@ -71,33 +61,30 @@ Context.prototype.readVariables = function (msg) {
this.emit('variables', this.variables); this.emit('variables', this.variables);
this.setState(state.waiting); this.setState(state.waiting);
//return "";
}; };
Context.prototype.readResponse = function (msg) { Context.prototype.readResponse = function (msg) {
var lines = msg.split('\n'); var lines = msg.split('\n');
/*
for(var i = 0; i < lines.length; i++) {
this.readResponseLine(lines[i]);
}
*/
lines.map(function (line) { lines.map(function (line) {
this.readResponseLine(line); this.readResponseLine(line);
}, this); }, this);
//return "";
}; };
Context.prototype.readResponseLine = function (line) { Context.prototype.readResponseLine = function (line) {
if (!line) return; if (!line) return;
var parsed = /^(\d{3})(?: result=)(.*)/.exec(line); //var parsed = /^(\d{3})(?: result=)(.*)/.exec(line);
var parsed = /^(\d{3})(?: result=)([^(]*)(?:\((.*)\))?/.exec(line);
if (!parsed) { if (!parsed) {
return this.emit('hangup'); return this.emit('hangup');
} }
var response = { var response = {
code: parseInt(parsed[1]), code: parseInt(parsed[1]),
result: parsed[2] result: parsed[2].trim(),
}; };
//our last command had a pending callback //our last command had a pending callback
...@@ -109,11 +96,11 @@ Context.prototype.readResponseLine = function (line) { ...@@ -109,11 +96,11 @@ Context.prototype.readResponseLine = function (line) {
this.emit('response', response); this.emit('response', response);
} }
Context.prototype.setState = function(state) { Context.prototype.setState = function (state) {
this.state = state; this.state = state;
}; };
Context.prototype.send = function(msg, cb) { Context.prototype.send = function (msg, cb) {
this.pending = cb; this.pending = cb;
this.stream.write(msg); this.stream.write(msg);
}; };
...@@ -146,16 +133,14 @@ Context.prototype.onEvent = function (event) { ...@@ -146,16 +133,14 @@ Context.prototype.onEvent = function (event) {
return defer.promise; return defer.promise;
}; };
Context.prototype.dial = function (num, timeout, params) { //additional agi commands
return this.exec('Dial', num + ',' + timeout + ',' + params);
};
commands.map(function (command) { commands.map(function (command) {
var str = ''; var str = '';
Context.prototype[command.name] = function () { Context.prototype[command.name] = function () {
if (command.params > 0) { if (command.params > 0) {
var args = [].slice.call(arguments, 0, command.params); var args = [].slice.call(arguments, 0, command.params);
str = command.command + " " + prepareArgs(args, command.paramRules).join(" "); str = command.command + " " + prepareArgs(args, command.paramRules, command.params).join(" ");
} else { } else {
str = command.command; str = command.command;
} }
...@@ -163,16 +148,35 @@ commands.map(function (command) { ...@@ -163,16 +148,35 @@ commands.map(function (command) {
}; };
}); });
var prepareArgs = function (args, argsRules) { var prepareArgs = function (args, argsRules, count) {
var q; var q, argsP = [];
if (argsRules) { if (argsRules && count) {
q = args.map(function (arg, i) {
return (argsRules[i]) ? argsRules[i].prepare(arg) : arg; args = args.map(function (arg){
return arg.toString();
});
for (var i = 0; i < count; i++) {
argsP[i] = (args[i]) ?
args[i] :
((argsRules[i] && argsRules[i].default) ?
argsRules[i].default :
'');
}
q = argsP.map(function (arg, i) {
return (argsRules[i] && argsRules[i].prepare) ? argsRules[i].prepare(arg) : arg;
}); });
} else { } else {
q = args; q = args;
} }
return q; return q;
} };
//sugar commands
Context.prototype.dial = function (target, timeout, params) {
return this.exec('Dial', target + ',' + timeout + ',' + params);
};
module.exports = Context; module.exports = Context;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment