public override void ExecuteCommand(MLLPSession session, HL7RequestInfo requestInfo)
        {
            Logging.Logger.Info(new PipeParser().Encode(requestInfo.Message));

            if (requestInfo.Message is ORU_R01 || requestInfo.Message is MDM_T02)
            {
                if (!GetBinaryDataMessageHL7(requestInfo.Message))
                {
                    requestInfo.ErrorMessage = "";
                }
            }

            //var messageId = Guid.NewGuid().ToString();

            //ConfigurationPublisherClient publisher = new ConfigurationPublisherClient
            //{
            //    Applicacion = "IntegrationAPI",
            //    Module = "accountMedicalDocuments",
            //    CallbackResponse = true,
            //    Action = (OperationType)1,
            //    CorrelationId = "0",
            //    Priority = 0,
            //    ReplyTo = "0",
            //    UserId = "",
            //    Entity = "accountMedicalDocuments",
            //    //PropertiesCustom = JsonConvert.DeserializeObject<Dictionary<string, object>>(metadata),
            //    Metadata = new Dictionary<string, object>(),
            //    MessageId = messageId,
            //};

            //var brokerClient = new RabbitMQBrokerClient("13.90.192.76", "developerAdmin", "developerAdmin", "C:\\Servinte\\configSynchronizationAPI.json", "/");
            //var result = brokerClient.SendMessage<object, object>(requestInfo.Message, null, publisher);

            base.ExecuteCommand(session, requestInfo);
        }
        public IActionResult HL7messageFromServer()
        {
            String hl7MessageRaw;

            using (Stream Body = HttpContext.Request.Body)
            {
                hl7MessageRaw = new StreamReader(HttpContext.Request.Body).ReadToEnd();
            }

            HL7RequestInfo info = hL7CommunicationService.ParseHL7RawMessage(hl7MessageRaw, "Http");

            NHapi.Model.V23.Message.ADT_A01 message = ((NHapi.Model.V23.Message.ADT_A01)info.Message);

            HadleHL7Message(info, hl7MessageRaw);
            IMessage response = HL7Acknowlege.MakeACK(info.Message, "AA");

            return(Accepted(response));
        }
        public IActionResult HL7messageSendToServer(int exmiantionId)
        {
            try
            {
                var examination = _patientInfoRepository.GetExamination(exmiantionId);

                if (examination == null)
                {
                    return(NotFound());
                }

                var patient = _patientInfoRepository.GetPatient(examination.PatientId);

                if (patient == null)
                {
                    return(NotFound());
                }

                var examDetail = _patientInfoRepository.GetExaminationDetail(patient.Id, examination.Id);

                if (examDetail == null)
                {
                    return(NotFound());
                }

                Message msg = CreateHL7Message(examination, "2.3", patient, examDetail);

                string           hl7msg = msg.Serialize();
                HL7RequestInfo   info   = hL7CommunicationService.ParseHL7RawMessage(hl7msg, "Http");
                SimpleMLLPClient client = new SimpleMLLPClient("localhost", 2021);
                //MLLPSession session = new MLLPSession();
                var result = client.SendHL7Message(info.Message);
                _patientInfoRepository.UpdateExaminationStatus(exmiantionId, true);

                return(Ok());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal Server Error"));
            }
        }
        /// <summary>
        ///  Sort and save to DB HL7 incoming Messages.
        /// </summary>
        /// <param name="request"></param>
        void HadleHL7Message(HL7RequestInfo request, string createdFromHl7Message)
        {
            try
            {
                // Handle ADT_A01 - incoming patient registration. Also save the raw HL7 Messages. To log and store it.
                if (request.Message.ToString().Contains("ADT_A01"))
                {
                    var pateitnToAdd = new Entities.Patient()
                    {
                        Archived   = true,
                        Name       = ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.PatientName.GivenName.Value + " " + ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.PatientName.FamilyName.Value,
                        Gender     = ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.Sex.Value,
                        ExternalId = Convert.ToInt32(((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.PatientAccountNumber.ID.Value),
                        Age        = ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.DateOfBirth.TimeOfAnEvent.Year,
                        BirthDate  = new DateTime(
                            ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.DateOfBirth.TimeOfAnEvent.Year,
                            ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.DateOfBirth.TimeOfAnEvent.Month,
                            ((NHapi.Model.V23.Message.ADT_A01)request.Message).PID.DateOfBirth.TimeOfAnEvent.Day),
                        OriginalHL7Message = createdFromHl7Message,
                    };

                    _patientInfoRepository.AddPatient(pateitnToAdd);
                    _patientInfoRepository.Save();
                }
                // Handle  ORU_R01 - incoming Examination data.
                if (request.Message.ToString().Contains("ORU_R01"))
                {
                    int                 externalID        = Convert.ToInt32(((NHapi.Model.V23.Message.ORU_R01)request.Message).GetRESPONSE().PATIENT.PID.PatientAccountNumber.ID.Value);
                    Patient             patient           = new Patient();
                    Examination         examToAdd         = new Examination();
                    BloodPressureData   newExamData       = new BloodPressureData();
                    SpOData             newExaamSPo       = new SpOData();
                    BodyTemperatureData newExamBTD        = new BodyTemperatureData();
                    bool                BloodPressureFalg = false;
                    if (_patientInfoRepository.PatientExistsByExtId(externalID))
                    {
                        patient = _patientInfoRepository.GetPatientByExtID(externalID);
                    }
                    else
                    {
                        // Todo Save Patient.
                    }

                    int obsCount = ((NHapi.Model.V23.Message.ORU_R01)request.Message).GetRESPONSE().ORDER_OBSERVATIONRepetitionsUsed;
                    for (int i = 0; i < obsCount; i++)
                    {
                        var orderObservation = ((NHapi.Model.V23.Message.ORU_R01)request.Message).GetRESPONSE().GetORDER_OBSERVATION(i);
                        int obxCount         = ((NHapi.Model.V23.Message.ORU_R01)request.Message).GetRESPONSE().GetORDER_OBSERVATION(i).OBSERVATIONRepetitionsUsed;
                        for (int j = 0; j < obxCount; j++)
                        {
                            NHapi.Model.V23.Segment.OBX obx = orderObservation.GetOBSERVATION(j).OBX;
                            var obxVaries = orderObservation.GetOBSERVATION(j).OBX.GetObservationValue();

                            if (obx.ObservationIdentifier.Text.Value == "Body temperature")
                            {
                                Examination examToAddBDT = new Examination();
                                examToAddBDT.Description     = String.Empty;
                                examToAddBDT.Archived        = true;
                                examToAddBDT.PatientId       = patient.Id;
                                examToAddBDT.Value           = DateTime.Now.ToString();
                                examToAddBDT.ExaminationType = "Body temperature";
                                newExamBTD.TemperatureValue  = Convert.ToInt32(((NHapi.Base.Model.AbstractPrimitive)obx.GetObservationValue(0).Data).Value);
                                _patientInfoRepository.AddExaminationToPatient(patient.Id, examToAddBDT, ExaminationType.BodyTemperature, newExamBTD, null);
                            }
                            if (obx.ObservationIdentifier.Text.Value == "SpO2")
                            {
                                Examination examToAddSPO = new Examination();
                                examToAddSPO.PatientId       = patient.Id;
                                examToAddSPO.Description     = String.Empty;
                                examToAddSPO.Archived        = true;
                                examToAddSPO.Value           = DateTime.Now.ToString();
                                examToAddSPO.ExaminationType = "SpO2";
                                newExaamSPo.SPOValue         = Convert.ToInt32(((NHapi.Base.Model.AbstractPrimitive)obx.GetObservationValue(0).Data).Value);
                                _patientInfoRepository.AddExaminationToPatient(patient.Id, examToAddSPO, ExaminationType.BloodSpO2, newExaamSPo, null);
                            }
                            if (obx.ObservationIdentifier.Text.Value == "Mean blood pressure")
                            {
                                BloodPressureFalg             = true;
                                examToAdd.PatientId           = patient.Id;
                                examToAdd.Description         = String.Empty;
                                examToAdd.Archived            = true;
                                examToAdd.Value               = DateTime.Now.ToString();
                                examToAdd.ExaminationType     = "BloodPressure";
                                newExamData.MeanBloodPressure = Convert.ToInt32(((NHapi.Base.Model.AbstractPrimitive)obx.GetObservationValue(0).Data).Value);
                            }
                            if (obx.ObservationIdentifier.Text.Value == "Pulse rate")
                            {
                                BloodPressureFalg         = true;
                                examToAdd.PatientId       = patient.Id;
                                examToAdd.Archived        = true;
                                examToAdd.Description     = String.Empty;
                                examToAdd.Value           = DateTime.Now.ToString();
                                examToAdd.ExaminationType = "BloodPressure";
                                newExamData.PulseRate     = Convert.ToInt32(((NHapi.Base.Model.AbstractPrimitive)obx.GetObservationValue(0).Data).Value);
                            }
                            if (obx.ObservationIdentifier.Text.Value == "Diastolic blood pressure")
                            {
                                BloodPressureFalg          = true;
                                examToAdd.PatientId        = patient.Id;
                                examToAdd.Archived         = true;
                                examToAdd.Description      = String.Empty;
                                examToAdd.Value            = DateTime.Now.ToString();
                                examToAdd.ExaminationType  = "BloodPressure";
                                newExamData.DiastolicValue = Convert.ToInt32(((NHapi.Base.Model.AbstractPrimitive)obx.GetObservationValue(0).Data).Value);
                            }
                            if (obx.ObservationIdentifier.Text.Value == "Systolic blood pressure")
                            {
                                BloodPressureFalg         = true;
                                examToAdd.PatientId       = patient.Id;
                                examToAdd.Archived        = true;
                                examToAdd.Description     = String.Empty;
                                examToAdd.Value           = DateTime.Now.ToString();
                                examToAdd.ExaminationType = "BloodPressure";
                                newExamData.SystolicValue = Convert.ToInt32(((NHapi.Base.Model.AbstractPrimitive)obx.GetObservationValue(0).Data).Value);
                            }
                        }
                        if (BloodPressureFalg)
                        {
                            _patientInfoRepository.AddExaminationToPatient(patient.Id, examToAdd, ExaminationType.BloodPressure, newExamData, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogInformation("HadleHL7Message error: " + ex.Message);
            }
        }