Пример #1
0
 private void AddActiveCall(string uniqueid, AsteriskCall dest)
 {
     if (!this.activeCalls.ContainsKey(uniqueid))
     {
         this.activeCalls.Add(uniqueid, dest);
     }
     else
     {
         this.activeCalls[uniqueid] = dest;
     }
     if (Server.debug)
     {
         Console.WriteLine("Active channels: " + activeCalls.Count.ToString());
     }
 }
Пример #2
0
 public void ParseDestination()
 {
     if (this.Channel != null)
     {
         if (this.Channel.Contains("-"))
         {
             this.ParsedChannel = AsteriskCall.ParseChannel(this.Channel);
         }
         else
         {
             this.ParsedChannel = this.Channel;  // BI (Beppe Innamorato) 16/10/08 Fix the server crash when Agent are used
         }
     }
     else
     {
         this.ParsedChannel = "";
     }
 }
Пример #3
0
        private void EvaluateEvent(Hashtable evt)
        {
            string        szEvent     = (string)evt["Event"];
            string        Uniqueid    = null;
            string        Uniqueid2   = null;
            string        Context     = null;
            string        Destination = null;
            AsteriskCall  call        = null;
            AsteriskCall  call1       = null;
            AsteriskEvent astevt      = null;

            switch (szEvent)
            {
            /*
             * This event always happens when a new channel is created
             * So is generated everytime a call bengins
             */
            case "Newchannel":
                Uniqueid     = (string)evt["Uniqueid"];
                Context      = (string)evt["Context"];
                call         = new AsteriskCall();
                call.Channel = (string)evt["Channel"];
                call.ParseDestination();
                call.Uniqueid = Uniqueid;
                if (Context != null)
                {
                    call.Context = Context;
                }

                if (Server.debug)
                {
                    Console.WriteLine("Newchannel - " + call.ToString());
                }
                Server.logger.WriteLine(LogType.Debug, "Newchannel - " + call.ToString());
                AddActiveCall(Uniqueid, call);
                break;

            /*
             * Ringing: some phone is ringing? This should be trigger callerid
             */
            case "Dial":
                Uniqueid  = (string)evt["SrcUniqueID"];  // Caller
                Uniqueid2 = (string)evt["DestUniqueID"]; // Called

                call  = GetActiveCall(Uniqueid);         // caller extension..
                call1 = GetActiveCall(Uniqueid2);        // called extension..

                if ((call != null) && (call1 != null))
                {
                    string callerid     = (string)evt["CallerID"];
                    string calleridname = (string)evt["CallerIDName"];

                    // Here we set called call as dialed ;)
                    call1.isDialed     = true;
                    call1.CallerIDNum  = callerid;
                    call1.CallerIDName = calleridname;
                    astevt             = new AsteriskEvent();
                    astevt.Call        = call1;
                    astevt.Event       = "Newcallerid";
                    if (call1.ParsedChannel != null)     //BI (Beppe Innamorato) 16/10/08 inserted if
                    {
                        SendMessage(call1.ParsedChannel, astevt.ToXML());
                    }
                }
                break;

            /*
             * Example
             * Event: Newstate
             * Privilege: call,all
             * Channel: SIP/201-0080e6e0
             * State: Ringing
             * CallerID: 202
             * CallerIDName: Test 202
             * Uniqueid: asterisk-5559-1224799548.57
             *
             */
            case "Newstate":
                Destination = AsteriskCall.ParseChannel((string)evt["Channel"]);
                if (Destination != null)
                {
                    string state = (string)evt["State"];
                    if ((state != null) & (state.Equals("Ringing")))
                    {
                        Uniqueid = (string)evt["Uniqueid"];     // Caller
                        call     = GetActiveCall(Uniqueid);

                        // If the call is dialed then we can't
                        // send callerid event on Newstate..
                        if (!call.isDialed)
                        {
                            string callerid     = (string)evt["CallerID"];
                            string calleridname = (string)evt["CallerIDName"];

                            call.CallerIDNum  = callerid;
                            call.CallerIDName = calleridname;
                            astevt            = new AsteriskEvent();
                            astevt.Call       = call;
                            astevt.Event      = "Newcallerid";

                            SendMessage(Destination, astevt.ToXML());
                        }
                    }
                    else
                    {
                        AsteriskEvent tmpEvt = new AsteriskEvent();
                        tmpEvt.Event = "Newstate";

                        AsteriskCall tmpCall = new AsteriskCall();
                        tmpCall.Uniqueid     = (string)evt["Uniqueid"];
                        tmpCall.CallerIDNum  = (string)evt["CallerID"];
                        tmpCall.CallerIDName = (string)evt["CallerIDName"];
                        tmpCall.Channel      = (string)evt["Channel"];
                        tmpCall.State        = (string)evt["State"];

                        tmpEvt.Call = tmpCall;
                        SendMessage(Destination, tmpEvt.ToXML());

                        tmpCall = null;
                        tmpEvt  = null;
                    }
                }
                break;

            /*
             * Event: OriginateResponse
             * Privilege: call,all
             * Response: Success
             * Channel: SIP/201-082141f8
             * Context: astctidemo
             * Exten: 200
             * Reason: 4
             * Uniqueid: 1193986538.26
             * CallerID: <unknown>
             * CallerIDNum: <unknown>
             * CallerIDName: SIP/201
             */
            case "OriginateResponse":
                string originateResponse = (string)evt["Response"];
                if (originateResponse == null)
                {
                    return;
                }
                if (originateResponse.Equals("Success"))
                {
                    Uniqueid = (string)evt["Uniqueid"];
                    Context  = (string)evt["Context"];

                    call = GetActiveCall(Uniqueid);
                    if (call == null)
                    {
                        call = new AsteriskCall();
                    }
                    call.Context  = Context;
                    call.Uniqueid = Uniqueid;
                }
                break;

            /*
             * This event is generated everytime a new step in the dialplan
             * is done: a new extension of type set may contains calldata
             */
            case "Newexten":
                Uniqueid = (string)evt["Uniqueid"];
                Context  = (string)evt["Context"];



                call = GetActiveCall(Uniqueid);
                if (call == null)
                {
                    call = new AsteriskCall();
                }
                if (Context != null)
                {
                    call.Context = Context;
                }
                if (Uniqueid != null)
                {
                    call.Uniqueid = Uniqueid;
                }
                AddActiveCall(Uniqueid, call);

                string Application = (string)evt["Application"];
                if (Application == null)
                {
                    break;
                }
                switch (Application)
                {
                case "Set":
                    string AppData = ParseCallData((string)evt["AppData"]);
                    if (AppData != null)
                    {
                        call = GetActiveCall(Uniqueid);
                        if (call != null)
                        {
                            if (Context != null)
                            {
                                call.Context = Context;
                            }
                            call.AppData = AppData;
                            if (Server.debug)
                            {
                                Console.WriteLine("AppData - " + call.ToString());
                            }
                            Server.logger.WriteLine(LogType.Debug, "AppData - " + call.ToString());
                        }
                    }
                    break;
                }
                break;

            /*
             * Here we should notify the client..
             *
             * >> Example -> SIP/202 calls SIP/201
             * Event: Link
             * Privilege: call,all
             * Channel1: SIP/202-082173d0
             * Channel2: SIP/201-0821bb20
             * Uniqueid1: 1194000842.70
             * Uniqueid2: 1194000842.71
             * CallerID1: 202
             * CallerID2: 201
             *
             * >> Example -> SIP/202 calls Extension 100
             * Event: Link
             * Privilege: call,all
             * Channel1: SIP/202-082173d0
             * Channel2: SIP/201-0821bb20
             * Uniqueid1: 1194001092.74
             * Uniqueid2: 1194001092.75
             * CallerID1: 202
             * CallerID2: 100
             */
            case "Link":
                Uniqueid  = (string)evt["Uniqueid1"];
                Uniqueid2 = (string)evt["Uniqueid2"];
                string callerid1 = (string)evt["CallerID1"];
                string callerid2 = (string)evt["CallerID2"];
                call  = GetActiveCall(Uniqueid);    // caller
                call1 = GetActiveCall(Uniqueid2);   // called

                if (call1 != null)
                {
                    if (Server.debug)
                    {
                        Console.WriteLine("Link - " + call1.ToString());
                    }
                    Server.logger.WriteLine(LogType.Debug, "Link - " + call1.ToString());

                    if (call1.AppData == null)
                    {
                        if (call.AppData != null)
                        {
                            call1.AppData = call.AppData;
                        }
                    }
                    if (call1.Context == null)
                    {
                        if (call.Context != null)
                        {
                            call1.Context = call.Context;
                        }
                    }
                    // call1.CallerIDNum = callerid2;
                }
                if ((call != null) & (call1 != null))
                {
                    if (Server.debug)
                    {
                        Console.WriteLine("Link");
                    }
                    astevt       = new AsteriskEvent();
                    astevt.Call  = call1;
                    astevt.Event = "Link";

                    Destination = AsteriskCall.ParseChannel((string)evt["Channel2"]);
                    if (Destination != null)
                    {
                        Server.logger.WriteLine(LogType.Debug, string.Format("Sending Link notification to {0}", Destination));
                        SendMessage(Destination, astevt.ToXML());
                    }
                }
                break;

            /*
             * This event happens after a Dial command and usually before a Ringing event
             * and it's unique id is of the dialed extension. So we can notify when this happens
             */
            case "Newcallerid":
                //Uniqueid = (string)evt["Uniqueid"];
                //call = GetActiveCall(Uniqueid); // called extension..

                //if (call != null)
                //{
                //    string callerid = (string)evt["CallerID"];
                //    string calleridname = (string)evt["CallerIDName"];
                //    call.CallerIDNum = callerid;
                //    call.CallerIDName = calleridname;
                //    AddActiveCall(Uniqueid, call);   // Update the call info..
                //    if (Server.debug) Console.WriteLine("Newcallerid - " + call.ToString());

                //    astevt = new AsteriskEvent();
                //    astevt.Call = call;
                //    astevt.Event = "Newcallerid";
                //    if (call.ParsedChannel != null) //BI (Beppe Innamorato) 16/10/08 inserted if
                //    {
                //        SendMessage(call.ParsedChannel, astevt.ToXML());
                //    }
                //}

                break;

            /*
             * When an hangup event is fired, we've to remove
             * the call with the uniqueid matched
             */
            case "Hangup":
                Uniqueid = (string)evt["Uniqueid"];
                call     = GetActiveCall(Uniqueid);
                if (call != null)
                {
                    astevt       = new AsteriskEvent();
                    astevt.Call  = call;
                    astevt.Event = "Hangup";
                    if (call.ParsedChannel != null)     //BI (Beppe Innamorato) 16/10/08 inserted if
                    {
                        SendMessage(call.ParsedChannel, astevt.ToXML());
                    }
                    if (Server.debug)
                    {
                        Console.WriteLine("Hangup - " + call.ToString());
                    }
                    Server.logger.WriteLine(LogType.Debug, "Hangup - " + call.ToString());
                    RemoveActiveCall(Uniqueid);
                }

                break;

            default:

                break;
            }
        }