提交 2053c87b authored 作者: Jonas Gauffin's avatar Jonas Gauffin

Been working on the IvrInterface.

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk/scripts/contrib@8245 d0543943-73ff-0310-b7d9-9358b9ac24b2
上级 d3612263
......@@ -222,20 +222,20 @@ namespace EventWatcher
{
GetVariableReply gvr = (GetVariableReply) reply;
if (!gvr.Success)
Debug.WriteLine("OnBridgeCallId " + reply.ErrCode);
Debug.WriteLine("OnBridgeCallId " + reply.ErrorMessage);
string otherChannelId = (string) command.ContextData;
SetVariable sv = new SetVariable(otherChannelId, "gate_callid", gvr.Value);
sv.OnReply += OnCallIdReply;
sv.ReplyReceived += OnCallIdReply;
_eventMgr.Send(sv);
command.OnReply -= OnBridgeCallId;
command.ReplyReceived -= OnBridgeCallId;
}
private void OnCallIdReply(CmdBase command, CommandReply reply)
{
if (!reply.Success)
Debug.WriteLine("OnCallIdReply " + reply.ErrCode);
command.OnReply -= OnCallIdReply;
Debug.WriteLine("OnCallIdReply " + reply.ErrorMessage);
command.ReplyReceived -= OnCallIdReply;
}
#region Nested type: ExtensionMapper
......
using System;
using System.Collections.Generic;
using System.Text;
namespace FreeSwitch.EventSocket
{
public class EventChannelExecute : EventChannelState
{
private string _appName;
private string _arguments;
public string AppName
{
get { return _appName; }
set { _appName = value; }
}
public string Arguments
{
get { return _arguments; }
set { _arguments = value; }
}
public override bool ParseCommand(string name, string value)
{
if (name == "application")
AppName = value;
else if (name == "application-data")
Arguments = value;
else
return base.ParseCommand(name, value);
return true;
}
}
}
......@@ -2,6 +2,32 @@ namespace FreeSwitch.EventSocket
{
public class EventChannelExecuteComplete : EventChannelState
{
private string _appName;
private string _arguments;
public string AppName
{
get { return _appName; }
set { _appName = value; }
}
public string Arguments
{
get { return _arguments; }
set { _arguments = value; }
}
public override bool ParseCommand(string name, string value)
{
if (name == "application")
AppName = value;
else if (name == "application-data")
Arguments = value;
else
return base.ParseCommand(name, value);
return true;
}
}
}
......@@ -28,7 +28,7 @@ namespace FreeSwitch.EventSocket.Commands
else
{
CommandReply reply = new CommandReply(false);
reply.ErrCode = dataToParse;
reply.ErrorMessage = dataToParse;
return reply;
}
}
......
......@@ -26,7 +26,7 @@ namespace FreeSwitch.EventSocket.Commands
else
{
CommandReply reply = new CommandReply(false);
reply.ErrCode = dataToParse;
reply.ErrorMessage = dataToParse;
return reply;
}
}
......
......@@ -8,7 +8,7 @@ namespace FreeSwitch.EventSocket.Commands
public abstract class CmdBase
{
public event ReplyHandler OnReply;
public event ReplyHandler ReplyReceived;
private object _contextData;
......@@ -35,16 +35,21 @@ namespace FreeSwitch.EventSocket.Commands
public virtual void HandleReply(CommandReply reply)
{
// a command is only invoked once.
if (OnReply != null)
if (ReplyReceived != null)
{
OnReply(this, reply);
OnReply = null;
ReplyReceived(this, reply);
ReplyReceived = null;
}
}
public virtual CommandReply CreateReply(string dataToParse)
{
return null;
if (string.IsNullOrEmpty(dataToParse.Trim()))
return new CommandReply(false);
CommandReply reply = new CommandReply(dataToParse.Trim()[0] == '+');
reply.ErrorMessage = dataToParse;
return reply;
}
public override string ToString()
......
......@@ -6,7 +6,7 @@ namespace FreeSwitch.EventSocket.Commands
public class CommandReply
{
private bool _success;
private string _errCode;
private string _ErrorMessage;
public CommandReply(bool success)
{
......@@ -18,10 +18,10 @@ namespace FreeSwitch.EventSocket.Commands
get { return _success; }
}
public string ErrCode
public string ErrorMessage
{
get { return _errCode; }
set { _errCode = value; }
get { return _ErrorMessage; }
set { _ErrorMessage = value; }
}
public override string ToString()
......@@ -29,7 +29,7 @@ namespace FreeSwitch.EventSocket.Commands
if (_success)
return "Success";
else
return "Failed: " + _errCode;
return "Failed: " + _ErrorMessage;
}
}
}
......@@ -47,7 +47,7 @@ namespace FreeSwitch.EventSocket.Commands
else
{
CommandReply reply = new CommandReply(false);
reply.ErrCode = dataToParse;
reply.ErrorMessage = dataToParse;
return reply;
}
}
......
......@@ -39,7 +39,7 @@ namespace FreeSwitch.EventSocket.Commands
else
{
GetVariableReply reply = new GetVariableReply(false, string.Empty);
reply.ErrCode = dataToParse;
reply.ErrorMessage = dataToParse;
return reply;
}
}
......
......@@ -97,9 +97,9 @@ namespace FreeSwitch.EventSocket.Commands
{
OriginateReply reply = new OriginateReply(false, string.Empty);
if (nameValue.Length > 1)
reply.ErrCode = nameValue[1];
reply.ErrorMessage = nameValue[1];
else
reply.ErrCode = dataToParse;
reply.ErrorMessage = dataToParse;
return reply;
}
}
......
......@@ -6,6 +6,7 @@ namespace FreeSwitch.EventSocket.Commands
public class PlaybackCmd : SendMsg
{
private readonly string _fileName;
private readonly string _dtmfAbort = string.Empty;
/// <summary>
///
......@@ -21,6 +22,12 @@ namespace FreeSwitch.EventSocket.Commands
_fileName = fileName;
}
public PlaybackCmd(string uuid, string fileName, string dtmfAbort) : base(uuid)
{
_fileName = fileName;
_dtmfAbort = dtmfAbort;
}
public override string Command
{
get { return "playback"; }
......@@ -30,5 +37,10 @@ namespace FreeSwitch.EventSocket.Commands
{
get { return _fileName; }
}
public string DtmfAbort
{
get { return _dtmfAbort; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace FreeSwitch.EventSocket.Commands
{
public class SendEventCmd : CmdBase
{
private EventBase _event;
/// <summary>
///
/// </summary>
/// <param name="evt">Note that all events have NOT being developed to be sent, only to be received.</param>
public SendEventCmd(EventBase evt)
{
_event = evt;
}
public override string Command
{
get { return "sendevent " + _event.Name + "\n"; }
}
public override string Arguments
{
get { return _event.ToString(); }
}
}
}
......@@ -25,7 +25,17 @@ namespace FreeSwitch.EventSocket.Commands
public override string Arguments
{
get { return _channelId + " " + _name + " " + _value; }
get { return _channelId + " " + Name + " " + Value; }
}
public string Name
{
get { return _name; }
}
public string Value
{
get { return _value; }
}
public override CommandReply CreateReply(string dataToParse)
......@@ -35,7 +45,7 @@ namespace FreeSwitch.EventSocket.Commands
else
{
CommandReply reply = new CommandReply(false);
reply.ErrCode = dataToParse;
reply.ErrorMessage = dataToParse;
return reply;
}
}
......
......@@ -79,7 +79,6 @@ namespace FreeSwitch.EventSocket
}
catch (SocketException)
{
Console.WriteLine("EventSocket: Connect failed");
HandleDisconnect();
}
}
......@@ -88,8 +87,6 @@ namespace FreeSwitch.EventSocket
{
lock (_lockobj)
{
Console.WriteLine("EventSocket.TryConnect: Connecting to " + _hostName + ":" + _port);
_socket.Connect(_hostName, _port);
if (_stream == null)
_stream = new NetworkStream(_socket, false);
......@@ -167,9 +164,7 @@ namespace FreeSwitch.EventSocket
{
try
{
Console.WriteLine("EventSocket.Write: Trying to connect eventsocket.");
TryConnect();
Console.WriteLine("EventSocket.Write: Connected.");
}
catch (SocketException)
{
......@@ -189,7 +184,7 @@ namespace FreeSwitch.EventSocket
if (msg.ContentType == "auth/request")
{
AuthCommand cmd = new AuthCommand(_password);
cmd.OnReply += OnAuthed;
cmd.ReplyReceived += OnAuthed;
_commands.Enqueue(cmd);
Write(cmd + "\n\n");
}
......@@ -199,7 +194,6 @@ namespace FreeSwitch.EventSocket
if (_commands.Count > 0)
{
CmdBase cmd = _commands.Dequeue();
Console.WriteLine("Reply to " + cmd.GetType().Name + ": " + msg.Body);
cmd.HandleReply(cmd.CreateReply(msg.Body));
}
else
......@@ -285,14 +279,11 @@ namespace FreeSwitch.EventSocket
EventSocket client = (EventSocket) state;
try
{
Console.WriteLine("EventSocket.Timer: Trying to connect eventsocket.");
client.TryConnect();
Console.WriteLine("EventSocket.Timer: Connected.");
}
catch (SocketException err)
{
Console.WriteLine("Eventsocket.TryConnect: " + err.Message);
}
catch (SocketException)
{}
}
}
}
\ No newline at end of file
......@@ -19,7 +19,27 @@ namespace FreeSwitch.EventSocket
public string Name
{
get { return m_name; }
get
{
lock (this)
{
if (string.IsNullOrEmpty(m_name))
{
string name = GetType().Name.Substring(5); //remove (Event)
m_name = string.Empty;
m_name += char.ToUpper(name[0]);
for (int i = 1; i < name.Length; ++i)
{
if (char.IsUpper(name[i]))
m_name += '_';
else
m_name += char.ToUpper(name[i]);
}
}
}
return m_name;
}
set { m_name = value; }
}
......
......@@ -8,6 +8,18 @@ namespace FreeSwitch.EventSocket
private int _totalMessages;
private int _totalUrgentMessages;
public EventMessageWaiting()
{
}
public EventMessageWaiting(string account, int newMessages, int totalMessages)
{
_account = account;
_newMessages = newMessages;
_totalMessages = totalMessages;
}
/// <summary>
/// Messages are for this account
/// </summary>
......
......@@ -35,10 +35,6 @@
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Tiny.Net, Version=1.0.0.1, Culture=neutral, PublicKeyToken=15480ee9e0247fcb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\dlls\Debug\Tiny.Net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ChannelEvents\EventChannelApplication.cs" />
......@@ -65,12 +61,16 @@
<Compile Include="Commands\ParkCmd.cs" />
<Compile Include="Commands\Playback.cs" />
<Compile Include="Commands\RecordCmd.cs" />
<Compile Include="Commands\SendEventCmd.cs" />
<Compile Include="Commands\SendMsg.cs" />
<Compile Include="Commands\SetVariable.cs" />
<Compile Include="Commands\SleepCmd.cs" />
<Compile Include="EventManager.cs" />
<Compile Include="ChannelEvents\EventDtmfStatus.cs" />
<Compile Include="Ivr\DtmfEventArgs.cs" />
<Compile Include="Ivr\IvrInterface.cs" />
<Compile Include="Ivr\IvrQueue.cs" />
<Compile Include="Ivr\PrivateWaitDtmf.cs" />
<Compile Include="PlainEventMsg.cs" />
<Compile Include="EventParser.cs" />
<Compile Include="Events.cs" />
......@@ -118,6 +118,7 @@
</Target>
-->
<PropertyGroup>
<PostBuildEvent>copy $(TargetPath) $(SolutionDir)..\..\dlls\$(ConfigurationName)</PostBuildEvent>
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
\ No newline at end of file
using System;
namespace FreeSwitch.EventSocket.Ivr
{
public class DtmfEventArgs : EventArgs
{
private string _dtmf;
private string _dtmfCopy;
public DtmfEventArgs(string dtmf)
{
_dtmf = dtmf;
_dtmfCopy = (string)_dtmf.Clone();
}
/// <summary>
/// You cannot remove digits from the buffer (since it's a copy of the real buffer).
/// </summary>
/// <seealso cref="RemoveDigits"/>
public string Dtmf
{
get { return _dtmfCopy; }
}
/// <summary>
/// Remove digits from the real buffer.
/// </summary>
/// <param name="count">number of digits to remove.</param>
public void RemoveDigits(int count)
{
lock (_dtmf)
{
if (count > _dtmf.Length)
_dtmf = string.Empty;
else
_dtmf = _dtmf.Remove(0, count);
_dtmfCopy = (string)_dtmf.Clone();
}
}
}
public delegate void DtmfHandler(object source, DtmfEventArgs args);
}
using System;
using System.Collections.Generic;
using FreeSwitch.EventSocket.Commands;
namespace FreeSwitch.EventSocket.Ivr
{
internal class IvrQueue
{
private readonly Queue<CmdBase> _items = new Queue<CmdBase>();
private readonly EventManager _mgr;
private CmdBase _currentCommand;
public IvrQueue(EventManager mgr)
{
if (mgr == null)
throw new ArgumentNullException("mgr");
_mgr = mgr;
}
public bool CommandRunning
{
get { return _currentCommand != null; }
}
public void Add(CmdBase cmd)
{
lock (_items)
_items.Enqueue(cmd);
}
public void Execute(EventChannelExecute evt)
{
}
public void ExecuteComplete(EventChannelExecuteComplete evt)
{
Console.WriteLine(evt.AppName + "(" + evt.Arguments + ") is done.");
if (_currentCommand != null && evt.AppName != _currentCommand.Command)
{
Console.WriteLine("Executed something that is not ours: " + _currentCommand.Command);
return;
}
// run next command
lock (_items)
{
_currentCommand = null;
if (_items.Count > 0)
{
// check if next cmd is waiting on dtmf, then trigger it to start.
if (_items.Peek().Command == "privdtmf")
((PrivateWaitDtmf) _items.Peek()).Trigger();
SendNextCommand();
}
}
}
/// <summary>
/// a DTMF was pressed. Validate it towards the queue
/// </summary>
/// <param name="digit"></param>
/// <returns>true if it can be processed, false if not.</returns>
public bool GotDtmf(char digit)
{
bool isValid = true;
lock (_items)
{
// dequeue all play commands if correct digit is pressed.
while (_items.Count > 0)
{
if (_items.Peek().GetType() == typeof (SetVariable))
{
Console.WriteLine("Is SetVariable");
SetVariable var = (SetVariable) _items.Peek();
string digits = string.Empty;
digits += digit;
if (var.Name == "playback_terminators" )
{
if (var.Value.Contains(digits))
{
_items.Dequeue();
if (_items.Count > 0 && _items.Peek().GetType() == typeof(PlaybackCmd))
_items.Dequeue();
}
else
isValid = false; //digit was not one of the terminators. skip it.
}
}
else if (_items.Peek().GetType() == typeof(PlaybackCmd))
{
Console.WriteLine("Is PlaybackCmd");
PlaybackCmd cmd = (PlaybackCmd)_items.Peek();
string digits = string.Empty;
digits += digit;
if (cmd.DtmfAbort.Contains(digits))
_items.Dequeue();
else
{
isValid = false; //digit was not one of the terminators, skip it.
break;
}
}
else
break;
}
// and dequeue the dtmf command.
if (_items.Count > 0 && _items.Peek().GetType() == typeof(PrivateWaitDtmf))
((PrivateWaitDtmf) _items.Dequeue()).Dispose();
}
return isValid;
}
private static void OnCommandReply(CmdBase command, CommandReply reply)
{
command.ReplyReceived -= OnCommandReply;
if (!reply.Success)
{
Console.WriteLine("Command failed: " + reply.ErrorMessage);
}
}
public CmdBase Peek()
{
lock (_items)
{
return _items.Peek();
}
}
public CmdBase Pop()
{
lock (_items)
{
return _items.Dequeue();
}
}
private void SendNextCommand()
{
lock (_items)
{
if (_currentCommand != null)
throw new InvalidOperationException("A command is already running.");
_currentCommand = _items.Dequeue();
}
_currentCommand.ReplyReceived += OnCommandReply;
_mgr.Send(_currentCommand);
}
}
}
\ No newline at end of file
using System;
using System.Threading;
using FreeSwitch.EventSocket.Commands;
namespace FreeSwitch.EventSocket.Ivr
{
internal delegate void DtmfTimeoutHandler(PrivateWaitDtmf dtmf);
internal class PrivateWaitDtmf : CmdBase, IDisposable
{
private Timer _timer;
private readonly int _waitTime;
private readonly DtmfTimeoutHandler _handler;
/// <summary>
/// DTMF timeouts should be counted AFTER a phrase has been said.
/// this small command allows us to start the timer when so.
/// </summary>
/// <param name="handler">Method that will be signalled when the DTMF command goes on timeout.</param>
/// <param name="time">Number of seconds to wait on input.</param>
public PrivateWaitDtmf(DtmfTimeoutHandler handler, int time)
{
if (handler == null)
throw new ArgumentNullException("handler");
if (time == 0)
throw new ArgumentException("time cannot be 0.", "time");
if (time < 100)
time *= 1000;
_waitTime = time;
_handler = handler;
}
public override string Command
{
get { return "privdtmf"; }
}
public override string Arguments
{
get { return _waitTime.ToString(); }
}
public void Trigger()
{
_timer = new Timer(OnTimeout, null, _waitTime, Timeout.Infinite);
}
private void OnTimeout(object state)
{
_timer.Dispose();
_timer = null;
_handler(this);
}
#region IDisposable Members
///<summary>
///Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///</summary>
///<filterpriority>2</filterpriority>
public void Dispose()
{
if (_timer != null)
_timer.Dispose();
_timer = null;
}
#endregion
}
}
using System;
using System.Threading;
using FreeSwitch.EventSocket;
using FreeSwitch.EventSocket.Commands;
namespace IvrSocket
{
......@@ -12,17 +14,13 @@ namespace IvrSocket
mgr.EventReceived += IvrManager;
mgr.Subscribe(Event.All);
mgr.Start("localhost");
Thread.Sleep(1000);
//mgr.Send(new SendEventCmd(new EventMessageWaiting("jonas@192.168.0.58", 1, 2)));
Console.ReadLine();
}
private static void IvrManager(EventBase receivedEvent)
{
Console.WriteLine(receivedEvent.Name);
if (receivedEvent is ChannelEvent)
{
ChannelEvent evt = (ChannelEvent) receivedEvent;
Console.WriteLine(evt.Name + ": " + evt.UniqueId);
}
if (receivedEvent is EventChannelAnswer)
{
EventChannelAnswer answer = (EventChannelAnswer)receivedEvent;
......
......@@ -21,6 +21,10 @@ namespace IvrSocket
private void OnIvr(IvrInterface app)
{
_ivr.Play("sv\\system\\record_after_beep.wav", IvrInterface.AllDigits);
_ivr.Play("sv\\system\\record_after_beep.wav", IvrInterface.AllDigits);
_ivr.Play("sv\\system\\record_after_beep.wav", "2");
string dtmf = _ivr.GetDtmf(4);
Console.WriteLine("Got DTMF: " + dtmf);
//_ivr.Sleep(5000);
//_ivr.Record("voicemails\\" + Guid.NewGuid() + ".wav", 60);
}
......
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论