Beispiel #1
0
        private void sendSocket()//通过socket发送
        {
            string ASTMMessage = string.Empty;
            string receiveString;
            byte   receiveByte;
            string EndJudge = string.Empty;//字段检查

            while (!ProcessASTMCancel.IsCancellationRequested)
            {
                if (astmManager.IsASTMAvailable)
                {
                    #region 向ASTM发送样本测试结果
                    Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy; // mini mode
                    ASTMMessage = astmManager.GetASTMMessage();          //先把要发送的数据取出来
                    if (!GlobalVariable.IsOneWay)
                    {
                        #region 双向通信
                        Connect.sendSocketByte(ASTM_Commands.ENQ);//先发送ENQ
                        receiveByte = Connect.receiveSocketByet();
                        if (receiveByte == ASTM_Commands.ENQ)
                        {
                            //此时双方都在发送ENQ命令争夺主模式,遵循仪器优先原则
                            Connect.sendSocketByte(ASTM_Commands.NAK); //发送NAK表示不接受LIS的主模式
                            receiveByte = Connect.receiveSocketByet(); //等待LIS发送ACK
                        }
                        if (receiveByte == ASTM_Commands.ACK)
                        {
                            //LIS应答ACK
                            //此时仪器为主模式,仪器是从模式
                            if (Connect.sendSocket(ASTMMessage))
                            {
                                ++Statusbar.SBar.SendNum;
                                receiveByte = Connect.receiveSocketByet();//等待LIS发送应答ACK
                                if (receiveByte == ASTM_Commands.ACK)
                                {
                                    //如果应答ACK,则表示接受成功
                                    //发送结束标识符EOT
                                    Connect.sendSocketByte(ASTM_Commands.EOT);
                                    ProcessASTMMessage.Invoke(astmManager.GetASTMSample_ID() + "Lis服务器接收成功\r\n", "LIS"); //发送成功

                                    Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;                              // mini mode
                                    Statusbar.SBar.SampleId   = astmManager.GetASTMSample_ID();                          // mini mode

                                    ++Statusbar.SBar.ReplyNum;
                                    UpdateDB.Invoke(astmManager.GetASTMSample_ID(), astmManager.GetASTMItem(), astmManager.GetASTMDevice(), astmManager.GetASTMSample_SendTime());//回调
                                }
                                else
                                {
                                    Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                    ProcessASTMMessage.Invoke(astmManager.GetASTMSample_ID() + "Lis服务器发送失败\r\n", "LIS");
                                }
                            }
                            else
                            {
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                ProcessASTMMessage.Invoke(astmManager.GetASTMSample_ID() + "Lis服务器发送失败\r\n", "LIS");
                            }
                        }
                        else
                        {
                            //LIS没有应答ACK,LIS连接错误
                            Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                            ProcessASTMMessage.Invoke(astmManager.GetASTMSample_ID() + "Lis服务器连接错误\r\n", "LIS");
                        }
                        #endregion
                    }
                    else
                    {
                        #region 单向通信
                        if (Connect.sendSocket(ASTMMessage))
                        {
                            ++Statusbar.SBar.SendNum;
                            ProcessASTMMessage.Invoke(astmManager.GetASTMSample_ID() + "Lis服务器发送成功\r\n", "LIS");                                                           //发送成功

                            Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;                                                                                        // mini mode
                            Statusbar.SBar.SampleId   = astmManager.GetASTMSample_ID();                                                                                    // mini mode

                            UpdateDB.Invoke(astmManager.GetASTMSample_ID(), astmManager.GetASTMItem(), astmManager.GetASTMDevice(), astmManager.GetASTMSample_SendTime()); //回调
                        }
                        else
                        {
                            Statusbar.SBar.SoftStatus = GlobalVariable.miniError;                                // mini mode
                            ProcessASTMMessage.Invoke(astmManager.GetASTMSample_ID() + "Lis服务器发送失败\r\n", "LIS"); //发送成功
                        }
                        #endregion
                    }
                    astmManager.RemoveASTM();//无论如何,最后都在队列删除掉这条数据
                    #endregion
                }
                else if (astmManager.IsASTMRequestSampleDataAvailable)
                {
                    #region 向ASTM请求样本信息

                    Connect.sendSocketByte(ASTM_Commands.ENQ);//先发送ENQ
                    receiveByte = Connect.receiveSocketByet();
                    if (receiveByte == ASTM_Commands.ENQ)
                    {
                        //此时双方都在发送ENQ命令争夺主模式,遵循仪器优先原则
                        Connect.sendSocketByte(ASTM_Commands.NAK);           //发送NAK表示不接受LIS的主模式
                        receiveByte = Connect.receiveSocketByet();           //等待LIS发送ACK

                        Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy; // mini mode
                    }
                    if (receiveByte == ASTM_Commands.ACK)
                    {
                        //LIS应答ACK
                        //此时仪器为主模式,仪器是从模式
                        ASTMMessage = astmManager.GetASTMRequestSampleDataMessage();
                        Connect.sendSocket(ASTMMessage);
                        ++Statusbar.SBar.SendNum;
                        receiveByte = Connect.receiveSocketByet();//等待LIS发送应答ACK
                        if (receiveByte == ASTM_Commands.ACK)
                        {
                            //如果应答ACK,则表示接受成功
                            //发送结束标识符EOT
                            Connect.sendSocketByte(ASTM_Commands.EOT);
                            //此时进入中立模式
                            receiveByte = Connect.receiveSocketByet();//测试等待接收LIS发回来样本信息
                            if (receiveByte == ASTM_Commands.ENQ)
                            {
                                //应答ACK
                                Connect.sendSocketByte(ASTM_Commands.ACK);
                                //LIS开始为主模式,仪器为从模式
                                receiveString = Connect.receiveSocket();//接收样本申请信息
                                //接收到样本消息后,应答ACK
                                Connect.sendSocketByte(ASTM_Commands.ACK);
                                //然后等待LIS发送EOT结束命令
                                receiveByte = Connect.receiveSocketByet();
                                if (receiveByte == ASTM_Commands.EOT)
                                {
                                    //样本查询和下载结束
                                    //此时来验证是否有相应的样本信息
                                    ASTM_Parser         parser  = new ASTM_Parser();
                                    List <ASTM_Message> message = new List <ASTM_Message>();
                                    try
                                    {
                                        parser.parse(receiveString, ref message);
                                        EndJudge = message[0].terminatorRecords[0].f_terminatorcode;//检查L字段
                                    }
                                    catch
                                    {
                                        //解析出现异常
                                        ProcessASTMMessage.Invoke(astmManager.GetASTMRequestSampleDataSample_ID() + "Lis服务器申请样本异常\r\n", "LIS");
                                        Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                    }

                                    if (EndJudge == "I")
                                    {
                                        //LIS无相应样本信息
                                        ProcessASTMMessage.Invoke(astmManager.GetASTMRequestSampleDataSample_ID() + "Lis服务器无相关样本信息\r\n", "LIS");
                                        Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                    }
                                    else if (EndJudge == "N")
                                    {
                                        //LIS内有相应样本信息
                                        ASTMManager.ASTMPatientInfo pi = ASTM_ParesrPatientInfo(receiveString);

                                        if (pi.SampleInfo.Count >= 1)
                                        {
                                            Statusbar.SBar.SampleId = pi.SampleInfo[0].SampleID; // mini mode
                                        }
                                        Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;  // mini mode

                                        RequestSampleData.BeginInvoke(pi, null, null);
                                        //委托出去
                                    }
                                    else
                                    {
                                        //LIS 异常
                                        Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                        ProcessASTMMessage.Invoke(astmManager.GetASTMRequestSampleDataSample_ID() + "Lis服务器申请样本异常\r\n", "LIS");
                                    }
                                }
                            }
                        }
                    }
                    astmManager.RemoveASTMRequestSampleData();

                    #endregion
                }
                else
                {
                    #region LIS主动发送样本信息

                    //astmManager.ASTMSignal.Reset();
                    //持续监听LIS服务器
                    //LIS为主模式 仪器为从模式
                    receiveString = Connect.receiveSocket();
                    //接收到样本消息后,应答ACK
                    Connect.sendSocketByte(ASTM_Commands.ACK);
                    if (receiveString.Length > 10 && receiveString.IndexOf("H") != -1)
                    {
                        Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy;// mini mode

                        //传回来标准消息
                        //然后等待LIS发送EOT结束命令
                        receiveByte = Connect.receiveSocketByet();
                        if (receiveByte == ASTM_Commands.EOT)
                        {
                            //样本查询和下载结束
                            //此时来验证是否有相应的样本信息
                            ASTM_Parser         parser  = new ASTM_Parser();
                            List <ASTM_Message> message = new List <ASTM_Message>();
                            try
                            {
                                parser.parse(receiveString, ref message);
                                EndJudge = message[0].patientRecords[0].orderRecords[0].f_report_type;//检查O字段的第26字段
                            }
                            catch
                            {
                                //解析出现异常
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                ProcessASTMMessage.Invoke(astmManager.GetASTMRequestSampleDataSample_ID() + "Lis服务器主动发送样本信息异常\r\n", "LIS");
                            }
                            if (EndJudge == "O")
                            {
                                //LIS内有相应样本信息
                                ASTMManager.ASTMPatientInfo pi = ASTM_ParesrPatientInfo(receiveString);

                                if (pi.SampleInfo.Count >= 1)
                                {
                                    Statusbar.SBar.SampleId = pi.SampleInfo[0].SampleID; // mini mode
                                }
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;  // mini mode

                                RequestSampleData.BeginInvoke(pi, null, null);           //委托出去
                            }
                            else
                            {
                                //LIS 异常
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                ProcessASTMMessage.Invoke(astmManager.GetASTMRequestSampleDataSample_ID() + "Lis服务器主动发送样本信息异常\r\n", "LIS");
                            }
                        }
                    }
                    #endregion
                }
            }
        }
Beispiel #2
0
        private void sendSocket()
        {
            string     HL7Message = string.Empty;
            string     receiveString;
            PipeParser parser;
            IMessage   m;
            int        SendNum = 0;

            log.Info("HL7 send socket start.");
            while (!ProcessHL7Cancel.IsCancellationRequested && GlobalVariable.IsSocketRun)
            {
                if (hl7Manager.IsHL7Available)
                {
                    #region 向HL7发送样本测试结果
                    if (SendNum == GlobalVariable.ReSendNum)
                    {
                        //连续发送失败超过规定次数,不再发送
                        hl7Manager.RemoveHL7();//移除队列中开始处的HL7
                        SendNum = 0;
                        continue;
                    }
                    Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy;// mini mode
                    try
                    {
                        HL7Message = hl7Manager.GetHL7Message();
                        Connect.sendSocket(HL7Message);
                        log.Info("HL7 send" + hl7Manager.GetHL7Sample_ID());
                        if (SendNum == 0)
                        {
                            ++Statusbar.SBar.SendNum;
                        }
                        ++SendNum;
                    }
                    catch
                    {
                        Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                        ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器发送失败\r\n请重新打开软件\r\n", "LIS");
                        break;
                    }
                    if (!GlobalVariable.IsOneWay)//双向
                    {
                        receiveString = Connect.receiveSocket();
                        if (receiveString.Substring(0, 3) == "MSH")//相当于一个判断
                        {
                            parser = new PipeParser();
                            m      = parser.Parse(receiveString);
                            ACK ack = m as ACK;
                            if (ack.MSA.AcknowledgementCode.Value == "AA")
                            {
                                ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器接收成功\r\n", "LIS");
                                log.Info("LIS receive success" + hl7Manager.GetHL7Sample_ID());
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;   // mini mode
                                Statusbar.SBar.SampleId   = hl7Manager.GetHL7Sample_ID(); //mini mode
                                ++Statusbar.SBar.ReplyNum;
                                SendNum = 0;
                                UpdateDB.Invoke(hl7Manager.GetHL7Sample_ID(), hl7Manager.GetHL7Item(), hl7Manager.GetHL7Device(), hl7Manager.GetHL7Sample_SendTime());
                                hl7Manager.RemoveHL7();//移除队列中开始处的HL7
                            }
                            else
                            {
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器第" + SendNum.ToString() + "次接收失败\r\n", "LIS");
                                log.Info("LIS " + SendNum + " receive fail");
                            }
                        }
                        else
                        {
                            //接收异常
                            Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                            ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器第" + SendNum.ToString() + "次接收失败\r\n", "LIS");
                            log.Info("LIS " + SendNum + " receive fail");
                        }
                    }
                    else//单向
                    {
                        Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;// mini mode
                        Statusbar.SBar.SampleId   = hl7Manager.GetHL7Sample_ID();//mini mode

                        ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器发送成功\r\n", "LIS");
                        log.Info("LIS receive success" + hl7Manager.GetHL7Sample_ID());
                        UpdateDB.Invoke(hl7Manager.GetHL7Sample_ID(), hl7Manager.GetHL7Item(), hl7Manager.GetHL7Device(), hl7Manager.GetHL7Sample_SendTime()); //直接回调
                        hl7Manager.RemoveHL7();                                                                                                                //移除队列中开始处的HL7
                    }
                    #endregion
                }
                else if (hl7Manager.IsHL7RequestSampleDataAvailable)
                {
                    #region 向LIS请求样本信息
                    Statusbar.SBar.SoftStatus = GlobalVariable.miniBusy;// mini mode
                    string HL7Apply;
                    //申请信息发送
                    try
                    {
                        HL7Apply = hl7Manager.GetHL7RequestSampleDataMessage();
                        Connect.sendSocket(HL7Apply);
                        ++Statusbar.SBar.SendNum;
                    }
                    catch
                    {
                        Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                        ProcessHL7Message.Invoke(hl7Manager.GetHL7Sample_ID() + "Lis服务器发送失败\r\n请重新打开软件\r\n", "LIS");
                        break;
                    }
                    receiveString = Connect.receiveSocket();
                    if (receiveString.Substring(0, 3) == "MSH")
                    {
                        //传回为标准消息
                        parser = new PipeParser();
                        m      = parser.Parse(receiveString);
                        QCK_Q02 qck = m as QCK_Q02;
                        if (qck.QAK.QueryResponseStatus.Value == "OK")//代表有样本数据
                        {
                            string receiveStringDSR;
                            int    DSRIndex = receiveString.IndexOf("DSR");
                            if (DSRIndex == -1)                             //如果刚才接收过来的没有DSR数据
                            {
                                receiveStringDSR = Connect.receiveSocket(); //重新来接受DSR数据
                            }
                            else
                            {
                                receiveStringDSR = receiveString.Substring(DSRIndex);//截取后面一部分得为DSR数据
                            }
                            //解析DSR
                            //解析完DSR数据就要回复ACKQ03消息,此时设备信息传出去
                            PipeParser parserDSR = new PipeParser();
                            IMessage   mDSR      = parserDSR.Parse(receiveStringDSR);
                            DSR_Q03    dsr       = mDSR as DSR_Q03;
                            if (dsr.QAK.QueryResponseStatus.Value == "OK") //相当于再做了一层判断
                            {
                                HL7Manager.HL7SampleInfo hl7info = HL7_ParserSampleInfo(receiveStringDSR);
                                //接收成功后就要发送应答信号
                                RequestSampleData.BeginInvoke(hl7info, null, null);
                                Connect.sendSocket(CreatACKQ03(dsr.MSH.ReceivingFacility.NamespaceID.Value));
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniWaiting;                    // mini mode
                                Statusbar.SBar.SampleId   = hl7Manager.GetHL7RequestSampleDataSample_ID(); //mini mode
                                ProcessHL7Message.Invoke(hl7Manager.GetHL7RequestSampleDataSample_ID() + "LIS服务器申请样本成功\r\n", "LIS");
                                log.Info("Lis request sample success " + hl7Manager.GetHL7RequestSampleDataSample_ID());
                            }
                            else
                            {
                                Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                                ProcessHL7Message.Invoke(hl7Manager.GetHL7RequestSampleDataSample_ID() + "LIS服务器申请样本异常\r\n", "LIS");
                                log.Info("Lis request sample fail " + hl7Manager.GetHL7RequestSampleDataSample_ID());
                            }
                        }
                        else if (qck.QAK.QueryResponseStatus.Value == "NF")//代表没有样本数据
                        {
                            //此时不再回复消息
                            //结束
                            Statusbar.SBar.SoftStatus = GlobalVariable.miniError;// mini mode
                            ProcessHL7Message.Invoke(hl7Manager.GetHL7RequestSampleDataSample_ID() + "LIS服务器无相关样本信息\r\n", "LIS");
                            log.Info("Lis no request sample " + hl7Manager.GetHL7RequestSampleDataSample_ID());
                        }
                    }
                    #endregion
                    hl7Manager.RemoveHL7RequestSampleData();//移除掉队列中的申请信息
                }
                else
                {
                    #region LIS主动发送样本信息
                    //hl7Manager.HL7Signal.Reset();
                    //持续监听LIS服务器
                    receiveString = Connect.receiveSocket();
                    if (receiveString.Length > 10 && receiveString.Substring(0, 3) == "MSH")
                    {
                        //传回来为标准信息
                        hl7Manager.AddHL7ApplySample(receiveString); //扔给队列交给线程处理
                        hl7Manager.HL7ApplySampleSignal.Set();       //唤醒线程
                    }
                    #endregion
                }
            }
        }