예제 #1
0
        protected DvtkData.Dimse.DataSet Hl7ToDicom(Hl7Message message)
        {
            DvtkData.Dimse.DataSet dataset = new DvtkData.Dimse.DataSet("Transient");

            if (message != null)
            {
                // try to get the patient id
                CommonIdFormat patientId = new CommonIdFormat();
                patientId.FromHl7Format(message.Value(new Hl7Tag("PID", 3)));
                if (patientId.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENT_ID.GroupNumber, DvtkData.Dimse.Tag.PATIENT_ID.ElementNumber, DvtkData.Dimse.VR.LO, patientId.ToDicomFormat());
                }

                // try to get the patient's name
                CommonNameFormat patientName = new CommonNameFormat();
                patientName.FromHl7Format(message.Value(new Hl7Tag("PID", 5)));
                if (patientName.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENTS_NAME.GroupNumber, DvtkData.Dimse.Tag.PATIENTS_NAME.ElementNumber, DvtkData.Dimse.VR.PN, patientName.ToDicomFormat());
                }

                // try to get the patient's birth date
                CommonDateFormat patientBirthDate = new CommonDateFormat();
                patientBirthDate.FromHl7Format(message.Value(new Hl7Tag("PID", 7)));
                if (patientBirthDate.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENTS_BIRTH_DATE.GroupNumber, DvtkData.Dimse.Tag.PATIENTS_BIRTH_DATE.ElementNumber, DvtkData.Dimse.VR.DA, patientBirthDate.ToDicomFormat());
                }

                // try to get the patient's sex
                CommonStringFormat patientSex = new CommonStringFormat();
                patientSex.FromHl7Format(message.Value(new Hl7Tag("PID", 8)));
                if (patientSex.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.PATIENTS_SEX.GroupNumber, DvtkData.Dimse.Tag.PATIENTS_SEX.ElementNumber, DvtkData.Dimse.VR.CS, patientSex.ToDicomFormat());
                }

                // try to get the merge patient id
                CommonIdFormat mergePatientId = new CommonIdFormat();
                mergePatientId.FromHl7Format(message.Value(new Hl7Tag("MRG", 1)));
                if (mergePatientId.ToDicomFormat() != System.String.Empty)
                {
                    dataset.AddAttribute(DvtkData.Dimse.Tag.OTHER_PATIENT_IDS.GroupNumber, DvtkData.Dimse.Tag.OTHER_PATIENT_IDS.ElementNumber, DvtkData.Dimse.VR.LO, mergePatientId.ToDicomFormat());
                }
            }

            return(dataset);
        }
예제 #2
0
        private bool LoadTemplate(Hl7Message hl7Message)
        {
            if (hl7Message == null)
            {
                return(false);
            }

            // try to find the template tag in the dataset
            foreach (Hl7ComparisonTag comparisonTag in this.Template.ComparisonTags)
            {
                Hl7Tag        tag            = comparisonTag.Tag;
                System.String attributeValue = hl7Message.Value(tag.Segment, tag.FieldIndex);

                if (attributeValue != System.String.Empty)
                {
                    comparisonTag.DataFormat.FromHl7Format(attributeValue);
                }
            }

            return(true);
        }
예제 #3
0
        private void ProcessTrigger(Hl7Trigger trigger)
        {
            _hl7Mllp = new Hl7Mllp();

            // get the next transaction number - needed to sort the
            // transactions correctly
            int transactionNumber = TransactionNumber.GetNextTransactionNumber();

            System.String message = System.String.Format("HL7 Client thread - connecting to \"{0}\" on port {1}...", _config.ToActorIpAddress, _config.PortNumber);
            _hl7ThreadForHl7Client.LogInformation(message);

            if (_hl7Mllp.Connect(_config.ToActorIpAddress, _config.PortNumber))
            {
                Hl7Message hl7RequestMessage = trigger.Trigger;

                // Set the sending and receiving applications
                hl7RequestMessage.SendingApplication   = _config.FromActorAeTitle;
                hl7RequestMessage.ReceivingApplication = _config.ToActorAeTitle;

                // Add the control id and date/time of message
                hl7RequestMessage.MessageControlId = _messageControlId.ToString();
                _messageControlId++;
                hl7RequestMessage.DateTimeOfMessage = System.DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.CultureInfo.InvariantCulture);

                // get initial HL7 message delimiters from the config
                Hl7MessageDelimiters messageDelimiters = _config.MessageDelimiters;

                System.String messageType = hl7RequestMessage.Value(Hl7SegmentEnum.MSH, 9);
                _hl7ThreadForHl7Client.LogInformation(System.String.Format("HL7 Client thread - send message \"{0}\".", messageType));
                _hl7ThreadForHl7Client.LogInformation(hl7RequestMessage.ToString(messageDelimiters));

                if (_hl7Mllp.SendMessage(hl7RequestMessage, messageDelimiters) == true)
                {
                    Hl7Message hl7ResponseMessage = _hl7Mllp.ReceiveMessage(out messageDelimiters);

                    if (hl7ResponseMessage != null)
                    {
                        messageType = hl7ResponseMessage.Value(Hl7SegmentEnum.MSH, 9);
                        _hl7ThreadForHl7Client.LogInformation(System.String.Format("HL7 Client thread - received message \"{0}\".", messageType));
                        _hl7ThreadForHl7Client.LogInformation(hl7ResponseMessage.ToString(messageDelimiters));

                        // Validate the message
                        if (_config.AutoValidate == true)
                        {
                            ValidateMessage(hl7ResponseMessage, messageDelimiters);
                        }

                        // save the transaction
                        Hl7Transaction transaction = new Hl7Transaction(TransactionNameEnum.RAD_1, TransactionDirectionEnum.TransactionSent);
                        transaction.Request  = hl7RequestMessage;
                        transaction.Response = hl7ResponseMessage;

                        ActorsTransaction actorsTransaction = new ActorsTransaction(transactionNumber,
                                                                                    ActorName,             // from actor
                                                                                    ParentActor.ActorName, // to actor
                                                                                    transaction,
                                                                                    _hl7ThreadForHl7Client.Options.ResultsFileNameOnly,
                                                                                    _hl7ThreadForHl7Client.Options.ResultsFullFileName,
                                                                                    (uint)_hl7ThreadForHl7Client.NrErrors,
                                                                                    (uint)_hl7ThreadForHl7Client.NrWarnings);

                        // save the transaction in the Actor log
                        ParentActor.ActorsTransactionLog.Add(actorsTransaction);

                        // publish the transaction event to any interested parties
                        PublishTransactionAvailableEvent(ActorName, actorsTransaction);
                    }
                }

                _hl7Mllp.Stop();

                _hl7ThreadForHl7Client.StopResultsGathering();
                _hl7ThreadForHl7Client.StartResultsGathering();
            }

            if (_awaitCompletion == true)
            {
                _semaphore.Signal();
            }
        }
예제 #4
0
        /// <summary>
        /// Process Hl7 requests and responses.
        /// </summary>
        public void ProcessMessages()
        {
            _hl7Mllp = new Hl7Mllp();

            for (;;)
            {
                System.String message = System.String.Format("Listening for incoming HL7 connection on port {0}...", _config.PortNumber);
                _hl7ThreadForHl7Server.LogInformation(message);

                try
                {
                    // listen for a connection
                    _hl7Mllp.Listen(_config.PortNumber);

                    for (;;)
                    {
                        // accept the connection - only one at a time
                        _hl7Mllp.Accept();

                        // the message delimiters will be filled in by the ReceiveMessage method
                        Hl7MessageDelimiters messageDelimiters = null;

                        // receive an HL7 message
                        Hl7Message hl7RequestMessage = _hl7Mllp.ReceiveMessage(out messageDelimiters);
                        if (hl7RequestMessage != null)
                        {
                            System.String messageType = hl7RequestMessage.Value(Hl7SegmentEnum.MSH, 9);
                            message = System.String.Format("HL7 Server thread - received message \"{0}\".", messageType);
                            _hl7ThreadForHl7Server.LogInformation(message);
                            _hl7ThreadForHl7Server.LogInformation(hl7RequestMessage.ToString(messageDelimiters));

                            // Validate the message
                            if (_config.AutoValidate == true)
                            {
                                ValidateMessage(hl7RequestMessage, messageDelimiters);
                            }

                            // call the message handler to get a response
                            Hl7Message hl7ResponseMessage = MessageHandler(hl7RequestMessage);

                            // copy some fields from the request
                            hl7ResponseMessage.SendingApplication   = hl7RequestMessage.ReceivingApplication;
                            hl7ResponseMessage.SendingFacility      = hl7RequestMessage.ReceivingFacility;
                            hl7ResponseMessage.ReceivingApplication = hl7RequestMessage.SendingApplication;
                            hl7ResponseMessage.ReceivingFacility    = hl7RequestMessage.SendingFacility;
                            hl7ResponseMessage.MessageControlId     = hl7RequestMessage.MessageControlId;

                            // set the date/time of message
                            hl7ResponseMessage.DateTimeOfMessage = System.DateTime.Now.ToString("yyyyMMddhhmmss", System.Globalization.CultureInfo.InvariantCulture);

                            // send the response
                            _hl7Mllp.SendMessage(hl7ResponseMessage, messageDelimiters);

                            messageType = hl7ResponseMessage.Value(Hl7SegmentEnum.MSH, 9);
                            message     = System.String.Format("HL7 Server thread - sent message \"{0}\".", messageType);
                            _hl7ThreadForHl7Server.LogInformation(message);
                            _hl7ThreadForHl7Server.LogInformation(hl7ResponseMessage.ToString(messageDelimiters));

                            // get the next transaction number - needed to sort the
                            // transactions correctly
                            int transactionNumber = TransactionNumber.GetNextTransactionNumber();

                            // save the transaction
                            Hl7Transaction transaction = new Hl7Transaction(TransactionNameEnum.RAD_1, TransactionDirectionEnum.TransactionReceived);
                            transaction.Request  = hl7RequestMessage;
                            transaction.Response = hl7ResponseMessage;

                            _hl7ThreadForHl7Server.LogInformation(String.Format("{0} - received Hl7 event from {1}", ParentActor.ActorName, ActorName));

                            ActorsTransaction actorsTransaction = new ActorsTransaction(transactionNumber,
                                                                                        ActorName,             // from actor
                                                                                        ParentActor.ActorName, // to actor
                                                                                        transaction,
                                                                                        _hl7ThreadForHl7Server.Options.ResultsFileNameOnly,
                                                                                        _hl7ThreadForHl7Server.Options.ResultsFullFileName,
                                                                                        (uint)_hl7ThreadForHl7Server.NrErrors,
                                                                                        (uint)_hl7ThreadForHl7Server.NrWarnings);

                            // save the transaction in the Actor log
                            ParentActor.ActorsTransactionLog.Add(actorsTransaction);

                            // publish the transaction event to any interested parties
                            PublishTransactionAvailableEvent(ActorName, actorsTransaction);
                        }

                        _hl7Mllp.Close();

                        _hl7ThreadForHl7Server.StopResultsGathering();
                        _hl7ThreadForHl7Server.StartResultsGathering();
                    }
                }
                catch
                {
                    // System.Net.Sockets.Socket.Accept() exception thrown when the server is stopped by closing the listen socket.
                    break;
                }
            }
        }