Created
July 30, 2013 21:47
-
-
Save mikelopez/6117304 to your computer and use it in GitHub Desktop.
sample code for .net asterisk ami originating calls and tracing taken from http://caruizdiaz.com/?p=247 and fixed indenting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Dialer | |
{ | |
ManagerConnection _managerConnection; | |
Timer _timer; | |
public delegate void CallFinalizedCallback(Dialer sender); | |
string _uniqueID = string.Empty; | |
bool _wasOriginated = false; | |
object _locker = new object(); | |
List<HangupEvent> _hangupList = new List<HangupEvent>(); | |
CallFinalizedCallback _callFinalized; | |
public CallFinalizedCallback CallFinalized | |
{ | |
get { | |
return this._callFinalized; | |
} | |
set { | |
_callFinalized = value; | |
} | |
} | |
CallInfo _callInfo; | |
public CallInfo CallInfo | |
{ | |
get | |
{ | |
return this._callInfo; | |
} | |
} | |
public Dialer() | |
{ | |
_managerConnection = new ManagerConnection(Configuration.ManagerHost, | |
Configuration.ManagerPort, | |
Configuration.ManagerUserName, | |
Configuration.ManagerPassword); | |
//_timer = new Timer(Handle_timeoutCallback, Configuration.CallNotificationTimeout); | |
AddEventHandlers(); | |
} | |
void Handle_managerConnectionOriginateResponse (object sender, OriginateResponseEvent e) | |
{ | |
if (!e.CallerIdNum.Equals(_callInfo.CallId.ToString())) | |
return; | |
Logger.Print(string.Format("Call to {0}-{1} was originated", _callInfo.CallId, e.Exten)); | |
bool succeeded = e.Response.ToLower().Equals("success"); | |
if (succeeded) | |
UpdateDialoutQueue(CallStatus.Established); | |
else | |
UpdateDialoutQueue(CallStatus.FinishedWithError); | |
lock(_locker) | |
{ | |
_wasOriginated = true; | |
_uniqueID = _callInfo.CallId.ToString(); | |
} | |
foreach(HangupEvent hEvent in _hangupList) | |
{ | |
if (hEvent.CallerIdNum.Equals(e.CallerIdNum)) // the call failed for some reason | |
{ | |
Handle_managerConnectionHangup(this, hEvent); | |
break; | |
} | |
} | |
_hangupList.Clear(); | |
} | |
public Dialer(CallInfo callInfo, CallFinalizedCallback callback) : this() | |
{ | |
_callInfo = callInfo; | |
if (callback == null) | |
throw new ArgumentException("CallFinalizedCallback cannot be null"); | |
_callFinalized = callback; | |
} | |
void AddEventHandlers() | |
{ | |
_managerConnection.Hangup += Handle_managerConnectionHangup; | |
_managerConnection.OriginateResponse += Handle_managerConnectionOriginateResponse; | |
} | |
public void DisconnectFromAMI() | |
{ | |
_managerConnection.Hangup -= Handle_managerConnectionHangup; | |
_managerConnection.OriginateResponse -= Handle_managerConnectionOriginateResponse; | |
} | |
void Handle_timeoutCallback() | |
{ | |
try | |
{ | |
Logger.Print(string.Format("Call {0}-{1} forced to finalize after timeout", _callInfo.CallId, _callInfo.Extension)); | |
UpdateDialoutQueue(CallStatus.Timedout); | |
//_timer.CancelAlarm(); | |
if (_callFinalized != null) | |
_callFinalized(this); | |
DisconnectFromAMI(); | |
} | |
catch(Exception ex) | |
{ | |
Logger.Error(string.Format("Handle_timeoutCallback: {0}", ex.Message)); | |
DisconnectFromAMI(); | |
} | |
} | |
void Handle_managerConnectionHangup(object sender, HangupEvent e) | |
{ | |
try | |
{ | |
lock(_locker) | |
if (!_wasOriginated || !_uniqueID.Equals(e.CallerIdNum)) | |
{ | |
_hangupList.Add(e); // register this event in case it was fired before the OriginateAction event | |
return; | |
} | |
UpdateDialoutQueue(CallStatus.Finished, e.Cause, e.CauseTxt); | |
//_timer.CancelAlarm(); | |
Logger.Print(string.Format("Call {0}-{1} was hung up", _callInfo.CallId, e.Channel)); | |
if (_callFinalized != null) | |
_callFinalized(this); | |
DisconnectFromAMI(); | |
} | |
catch(Exception ex) | |
{ | |
Logger.Error(string.Format("Handle_managerConnectionHangup: {0}", ex.Message)); | |
DisconnectFromAMI(); | |
} | |
} | |
public void Call() | |
{ | |
Call(_callInfo); | |
} | |
public void Call(CallInfo callInfo) | |
{ | |
try | |
{ | |
OriginateAction originate = new OriginateAction(); | |
originate.Channel = callInfo.Channel; | |
originate.Priority = callInfo.Priority; | |
originate.Context = callInfo.Context; | |
originate.Exten = callInfo.Extension; | |
originate.CallerId = callInfo.CallId.ToString(); // <-------- | |
originate.Timeout = 500000; | |
originate.Async = true; | |
_managerConnection.Login(); | |
MarkCallAsProcessed(); | |
//_timer.SetAlarm(); | |
UpdateDialoutQueue(CallStatus.Trying); | |
ManagerResponse r = _managerConnection.SendAction(originate, originate.Timeout); | |
} | |
catch(Exception ex) | |
{ | |
Logger.Error(string.Format("Call: {0}", ex.Message)); | |
//_timer.CancelAlarm(); | |
if (ex is Asterisk.NET.Manager.TimeoutException) | |
{ | |
UpdateDialoutQueue(CallStatus.Timedout); | |
DisconnectFromAMI(); | |
} | |
else | |
UpdateDialoutQueue(CallStatus.FinishedWithError); | |
} | |
} | |
} | |
- See more at: http://caruizdiaz.com/?p=247#sthash.o4b3D15N.dpuf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment