Im Rahmen der Veranstaltung "CS3330 - Projektpraktikum MedizinischeInformatik" an der Universität zu Lübeck entstandenes Krankenhausinformationssystem (KIS).
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

163 行
7.0 KiB

  1. package de.uniluebeck.mi.projmi6.hapi;
  2. import ca.uhn.hl7v2.DefaultHapiContext;
  3. import ca.uhn.hl7v2.HL7Exception;
  4. import ca.uhn.hl7v2.HapiContext;
  5. import ca.uhn.hl7v2.app.Connection;
  6. import ca.uhn.hl7v2.llp.LLPException;
  7. import ca.uhn.hl7v2.model.Message;
  8. import ca.uhn.hl7v2.model.v251.datatype.XCN;
  9. import ca.uhn.hl7v2.model.v251.message.ACK;
  10. import ca.uhn.hl7v2.model.v251.message.ADT_A01;
  11. import ca.uhn.hl7v2.model.v251.segment.*;
  12. import de.uniluebeck.mi.projmi6.Main;
  13. import de.uniluebeck.mi.projmi6.db.DBHandler;
  14. import de.uniluebeck.mi.projmi6.model.*;
  15. import org.slf4j.LoggerFactory;
  16. import java.io.IOException;
  17. import java.sql.SQLException;
  18. import java.time.LocalDateTime;
  19. import java.time.ZoneId;
  20. import java.util.Date;
  21. import java.util.List;
  22. /**
  23. * Manages all HL7 sending tasks. This includes: creating a message, sending the message,
  24. * Created by taschi on 22.11.15.
  25. *
  26. */
  27. public class HL7Sender {
  28. /**
  29. * generates an ADT_A05 message for data exchange with OPS group
  30. *
  31. *
  32. * @param fall to create the message from this content. patient and diagnosis are linked to fall
  33. * @throws HL7Exception, IOException, SQLException
  34. */
  35. public static void createMessageADT_A01(Fall fall) throws HL7Exception, IOException, SQLException {
  36. Patient patient = fall.getPatient();
  37. ADT_A01 adt = new ADT_A01();
  38. //default MSH Values (Sets Segments: 1,2,7,9,10,11
  39. adt.initQuickstart("ADT", "A01", "P");
  40. //MSH Segment:
  41. MSH mshSegment = adt.getMSH();
  42. mshSegment.getMsh3_SendingApplication().getNamespaceID().parse("KISGruppe6");
  43. mshSegment.getMsh5_ReceivingApplication().getNamespaceID().parse("OPS Gruppe von Maurice und Torben");
  44. mshSegment.getMsh12_VersionID().getVersionID().parse("2.5.1");
  45. mshSegment.getMsh15_AcceptAcknowledgmentType().parse("AL");
  46. //EVN Segment:
  47. EVN evnSegment = adt.getEVN();
  48. evnSegment.getEvn1_EventTypeCode().parse("A01");
  49. evnSegment.getEvn2_RecordedDateTime().getTime().setValue(mshSegment.getDateTimeOfMessage().encode());
  50. evnSegment.getEvn4_EventReasonCode().parse("01");
  51. //PID
  52. PID pidSegment = adt.getPID();
  53. pidSegment.getPid2_PatientID().getIDNumber().parse(String.valueOf(patient.getPatID()));
  54. pidSegment.getPid3_PatientIdentifierList(0).getIDNumber().parse(Integer.toString(patient.getPatID()));
  55. pidSegment.getPid5_PatientName(0).getFamilyName().getSurname().parse(patient.getNachname());
  56. pidSegment.getPid5_PatientName(0).getGivenName().parse(patient.getVorname());
  57. pidSegment.getPid7_DateTimeOfBirth().getTime().setValue(Date.from(patient.getGeburtsdatum().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
  58. pidSegment.getPid8_AdministrativeSex().parse(String.valueOf(patient.getGeschlecht().id()));
  59. pidSegment.getPid11_PatientAddress(0).getStreetAddress().getStreetName().parse(patient.getStrasse());
  60. pidSegment.getPid11_PatientAddress(0).getStreetAddress().getDwellingNumber().parse(patient.getHausnummer());
  61. pidSegment.getPid11_PatientAddress(0).getCity().parse(patient.getOrt());
  62. pidSegment.getPid13_PhoneNumberHome(0).getTelephoneNumber().parse(patient.getTelefon());
  63. pidSegment.getPid16_MaritalStatus().getAlternateIdentifier().parse(patient.getFamilienstand().toString());
  64. //Diagnosen (DG1 Segmente des PV1 Segments
  65. List<Diagnose> diagnosen = DBHandler.getDiagnosenByFall(fall);
  66. PV1 pv1Segment = adt.getPV1();
  67. pv1Segment.getPv12_PatientClass().parse("U");
  68. int i = 1;
  69. for (Diagnose diagnose : diagnosen) {
  70. Mitarbeiter arzt = diagnose.getArzt();
  71. DG1 dg1 = adt.getDG1(i - 1);
  72. dg1.getDg11_SetIDDG1().setValue(String.valueOf(i));
  73. dg1.getDg13_DiagnosisCodeDG1().getIdentifier().parse(diagnose.getIcd10code().getCode());
  74. XCN xcn = dg1.getDg116_DiagnosingClinician(0);
  75. xcn.getIDNumber().setValue(arzt.getEinweisenderArzt());
  76. xcn.getGivenName().setValue(arzt.getVorname());
  77. xcn.getFamilyName().getSurname().setValue(arzt.getNachname());
  78. xcn.getPrefixEgDR().setValue(arzt.getTitel());
  79. dg1.getDg14_DiagnosisDescription().parse(diagnose.getFreiText());
  80. dg1.getDg16_DiagnosisType().parse(diagnose.getDiagArt().id());
  81. i++;
  82. }
  83. sendMessage(adt);
  84. }
  85. /**
  86. * Sending message handler. Calls function to send current message
  87. * @param message to send
  88. * @throws HL7Exception
  89. */
  90. private static void sendMessage(Message message) throws HL7Exception {
  91. if (message instanceof ADT_A01) {
  92. sendMessageADT_A01(message);
  93. }
  94. }
  95. /**
  96. * sends an ADT_A01 message and waits ca 5 seconds for an ACK. If no ACK occurs it sends the message ONLY 5 times again
  97. * if after 5 times no ack occurs log message
  98. * write log historie in database
  99. * @param message you want to send
  100. * @throws HL7Exception
  101. */
  102. private static void sendMessageADT_A01(Message message) throws HL7Exception {
  103. ADT_A01 adt_a01 = (ADT_A01) message;
  104. HapiContext context = new DefaultHapiContext();
  105. Connection connection;
  106. try {
  107. connection = context.newClient(Main.OPS_IP, Main.OPS_PORT, false);
  108. } catch (HL7Exception e) {
  109. LoggerFactory.getLogger(HL7Sender.class).error("Konnte aus irgendeinem Grund keine HL7 Nachricht senden. OPS Server down?");
  110. return;
  111. }
  112. Message response = null;
  113. HL7LogEntry messageEntry = new HL7LogEntry();
  114. HL7LogEntry responseEntry = new HL7LogEntry();
  115. messageEntry.setMessage(message.encode());
  116. messageEntry.setSource("127.0.0.1:1234"); // TODO: Kann man den lokal genutzen Port raus finden?
  117. messageEntry.setDirection(HL7LogEntry.Direction.OUT);
  118. messageEntry.setTimestamp(LocalDateTime.now());
  119. messageEntry.setTimestamp(HL7Utils.parseLocalDateTime(adt_a01.getMSH().getDateTimeOfMessage().getTime()));
  120. HL7Utils.logHL7MessageToDatabase(messageEntry);
  121. try {
  122. int count = 0;
  123. // 5 Versuche starten und schauen ob man ein ACK bekommt.
  124. while (!(response instanceof ACK) && count < 5) {
  125. if (count > 0) {
  126. Thread.sleep(5000);
  127. }
  128. response = connection.getInitiator().sendAndReceive(adt_a01);
  129. count++;
  130. }
  131. } catch (HL7Exception | LLPException | IOException | InterruptedException e) {
  132. LoggerFactory.getLogger(HL7Sender.class).warn("Bekomme aus irgendeinem Grund kein ACK.");
  133. connection.close();
  134. return;
  135. }
  136. responseEntry.setMessage(response.encode());
  137. responseEntry.setSource(Main.OPS_IP + ":" + Main.OPS_PORT);
  138. responseEntry.setDirection(HL7LogEntry.Direction.IN);
  139. responseEntry.setTimestamp(LocalDateTime.now());
  140. HL7Utils.logHL7MessageToDatabase(responseEntry);
  141. connection.close();
  142. }
  143. }