Ejemplo n.º 1
0
        //     public System.Timers.Timer t = new System.Timers.Timer(1000);
        public bool Run(int nCommandIndex, string strParamater)
        {
            Debug.WriteLine("run section   nCommandIndex: " + nCommandIndex.ToString() + "  strParamater: " + strParamater);
            //  if (ConnectionCompleted!=null)
            //      ConnectionCompleted();


            if ((strParamater == ""))
            {
                strParamater = "7000;192.168.123.34;com1;10000;70000";
            }

            str = strParamater.Split(';');



            int API_TimeOut_ms = Convert.ToInt32(str[4]) + 10000;

            // 0: port
            // 1: ip
            // 2: usb port
            // 3: price
            // 4: timeout  (ms)

            int nCommandIndex_Provider = nCommandIndex / 100;
            int temp = nCommandIndex % 100;
            int nCommandIndex_TransMode = temp / 10;

            temp = temp % 10;
            int nCommandIndex_ConType = temp;



            if (PEP_Pos != null)
            {
                PEP_Pos.Dispose();
            }

            // t.Elapsed += T_Elapsed;

            try
            {
                switch (nCommandIndex_Provider)
                {
                case 11:    //pep

                    switch (nCommandIndex_TransMode)
                    {
                    case 1:            //async
                        switch (nCommandIndex_ConType)
                        {
                        case 1:                //lan (sync)

                            PEP_Pos = new PcPosClassLibrary.PCPOS(Convert.ToInt32(str[0]), str[1]);
                            PEP_Pos.SetLanReceiveTimeout(Convert.ToInt32(Convert.ToInt32(str[4]) / 1000));
                            if (Convert.ToInt32(POS_Count) > 1)
                            {
                                semSepehr.Release();                                                 //release Sepehr semaphore
                            }
                            CallWithTimeout(() => PEP_RD = PEP_Pos.SyncSale(Convert.ToInt32(str[3])), API_TimeOut_ms);
                            semRecieveMessage.WaitOne();                 //every thread recieved data block  others
                            //CallWithTimeout(() => RD = pos.SyncSale(Convert.ToInt32(str[3])),Convert.ToInt32( str[4])); // causing incomplete session in  sync operation
                            RefNo = PEP_RD.ReferenceNumber;
                            Err   = PEP_RD.ErrorCode.ToString();
                            break;

                        case 2:                     //usb
                            PEP_Pos = new PcPosClassLibrary.PCPOS(str[2]);
                            //  t.AutoReset = true;
                            //  t.Enabled = true;
                            PEP_Pos.DataRecieved += new PcPosClassLibrary.DataRecievedEventHandler(this.Pep_pos_DataReceived);
                            if (Convert.ToInt32(POS_Count) > 1)
                            {
                                semSepehr.Release();                                                 //release Sepehr semaphore
                            }
                            PEP_Pos.Sale(Convert.ToInt32(str[3]));
                            a.WaitOne();
                            //  t.Stop();
                            break;
                        }
                        break;

                    case 2:             //sync
                        switch (nCommandIndex_ConType)
                        {
                        case 1:                //lan
                            Debug.WriteLine("pep : " + nCommandIndex.ToString() + " : " + strParamater);
                            string str1 = "";
                            for (int i = 0; i < 5; i++)
                            {
                                str1 += str[i];
                            }
                            Debug.WriteLine("pep : " + str1);

                            PEP_Pos = new PcPosClassLibrary.PCPOS(Convert.ToInt32(str[0]), str[1]);
                            PEP_Pos.SetLanReceiveTimeout(Convert.ToInt32(Convert.ToInt32(str[4]) / 1000));
                            if (Convert.ToInt32(POS_Count) > 1)
                            {
                                semSepehr.Release();                                                 //release Sepehr semaphore
                            }
                            CallWithTimeout(() => PEP_RD = PEP_Pos.SyncSale(Convert.ToInt32(str[3])), API_TimeOut_ms);
                            semRecieveMessage.WaitOne();                 //every thread recieved data blocked  others
                            Debug.WriteLine("------------- semRecieveMessage  waitOne ");
                            //CallWithTimeout(() => RD = pos.SyncSale(Convert.ToInt32(str[3])),Convert.ToInt32( str[4])); // causing incomplete session in  sync operation
                            RefNo = PEP_RD.ReferenceNumber;
                            Err   = PEP_RD.ErrorCode.ToString();
                            break;

                        case 2:                 //usb
                            PEP_Pos = new PcPosClassLibrary.PCPOS(str[2]);
                            PEP_Pos.SetLanReceiveTimeout(Convert.ToInt32(Convert.ToInt32(str[4]) / 1000));
                            semSepehr.Release();                 //release Sepehr semaphore
                            CallWithTimeout(() => PEP_RD = PEP_Pos.SyncSale(Convert.ToInt32(str[3])), API_TimeOut_ms);
                            semRecieveMessage.WaitOne();         //every thread recieved data block  others
                            //CallWithTimeout(() => RD = pos.SyncSale(Convert.ToInt32(str[3])),Convert.ToInt32( str[4])); // causing incomplete session in  sync operation
                            RefNo = PEP_RD.ReferenceNumber;
                            Err   = PEP_RD.ErrorCode.ToString();
                            break;
                        }

                        break;
                    }
                    break;


                case 12:     //sepehr

                    switch (nCommandIndex_TransMode)
                    {
                    case 1:            //async
                        switch (nCommandIndex_ConType)
                        {
                        case 1:                //lan (sync)
                            return(false);

                        case 2:                 //usb
                            return(false);
                        }
                        break;

                    case 2:             //sync
                        switch (nCommandIndex_ConType)
                        {
                        case 1:                //lan
                            Debug.WriteLine("sepehr  : " + nCommandIndex.ToString() + " : " + strParamater);
                            string str1 = "";
                            for (int i = 0; i < 5; i++)
                            {
                                str1 += str[i];
                            }
                            Debug.WriteLine("sepehr  : " + str1);

                            clsCommunication clsCommunicationObj = new clsCommunication();
                            clsCommunicationObj.ConnType  = (int)clsCommunication.connectionType.ETHERNET;
                            clsCommunicationObj.IPAddress = str[1];
                            clsCommunicationObj.IPPort    = Convert.ToInt32(str[0]);
                            clsCommunicationObj.TimeOut   = Convert.ToInt32(Convert.ToInt32(str[4]) / 1000);               // force 20 sec
                            Sepehr_transaction            = new clsMessage();
                            Sepehr_req.msgTyp             = clsMessage.msgType.Sale;
                            Sepehr_req.terminalID         = "";
                            Sepehr_req.amount             = str[3];
                            Sepehr_transaction.request    = Sepehr_req;

                            if (Convert.ToInt32(POS_Count) > 1)
                            {
                                semSepehr.WaitOne();                   // wait until others begin
                            }
                            int SendretCode = Sepehr_transaction.SendMessage(0);
                            if (SendretCode != 0)
                            {
                                Err = (100).ToString();
                                semRecieveMessage.WaitOne();
                                Debug.WriteLine("------------- semRecieveMessage  waitOne ");
                                goto end;
                            }             // not send
                            int RecieveretCode = 0;
                            CallWithTimeout(() => RecieveretCode = Sepehr_transaction.ReceiveMessage(0), API_TimeOut_ms);
                            semRecieveMessage.WaitOne();                 //every thread recieved data block  others
                            Debug.WriteLine("------------- semRecieveMessage  waitOne ");
                            if (RecieveretCode != 0)
                            {
                                Err = (RecieveretCode + 110).ToString(); goto end;
                            }                   //not recieved  //  110-1 : 109 : not recived  //  110-5:105 : timeout // else : Message MAC Error
                            string POSErrCode = Sepehr_transaction.response.AppResponseCode;
                            if (Sepehr_transaction.response.AppResponseCode != "00")
                            {
                                Err = (Convert.ToInt32(POSErrCode) + 200).ToString(); goto end;
                            }                                                                                                                            //POS Err code:  +200

                            RefNo = Sepehr_transaction.response.SystemTraceNumber == "000000000000" ? "0":Sepehr_transaction.response.SystemTraceNumber; // 000000000000 -> 0

                            break;

                        case 2:                 //usb

                            break;
                        }

                        break;
                    }
                    break;
                }
end:
                Debug.WriteLine("Error:" + Err + "\n RefNo:" + RefNo);
            }
            catch (Exception ex)
            {
                if (ex.Message == "The operation has timed out.") //handle  Exception  (CallWithTimeout)
                {
                    if (nCommandIndex_Provider == 11)             //PEP
                    {
                        Err = "132";
                    }
                    else if (nCommandIndex_Provider == 12) //Sepehr
                    {
                        Err = "105";
                    }
                }
                Debug.WriteLine("Run Api Func Exception:" + ex.ToString() + "\n API_TimeOut_ms: " + API_TimeOut_ms);

                if (PEP_Pos != null)
                {
                    PEP_Pos.cancelTrans();
                }
                return(false);
            }

            return(true);
        }