Im Rahmen der Veranstaltung "CS3330 - Projektpraktikum MedizinischeInformatik" an der Universität zu Lübeck entstandenes Krankenhausinformationssystem (KIS).
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

176 righe
7.3 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.message.ACK;
  9. import ca.uhn.hl7v2.model.v251.message.ADT_A01;
  10. import ca.uhn.hl7v2.model.v251.segment.*;
  11. import ca.uhn.hl7v2.parser.Parser;
  12. import de.uniluebeck.mi.projmi6.Main;
  13. import de.uniluebeck.mi.projmi6.model.Diagnose;
  14. import de.uniluebeck.mi.projmi6.model.Fall;
  15. import de.uniluebeck.mi.projmi6.model.HL7LogEntry;
  16. import de.uniluebeck.mi.projmi6.model.Patient;
  17. import org.slf4j.LoggerFactory;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileWriter;
  21. import java.io.IOException;
  22. import java.sql.SQLException;
  23. import java.time.LocalDateTime;
  24. import java.time.ZoneId;
  25. import java.util.Date;
  26. import java.util.List;
  27. import static de.uniluebeck.mi.projmi6.db.DBHandler.getDiagnosenByFall;
  28. /**
  29. * Created by taschi on 22.11.15.
  30. */
  31. public class HL7Sender {
  32. /**
  33. * erstellt eine ADT_A0 nachricht, welche anschließend an die OPS Gruppe verschickt werden soll.
  34. * TODO Mit OPS Gruppe absprechen, welche Informationen sie auf jeden Fall benötigen und Code entsprechend anpassen.
  35. *
  36. * @param fall nach dem Erstellen eines neuen Falls wird diese Methode aufgerufen um Hl7 Nachricht zu erzeugen
  37. * @throws Exception
  38. */
  39. public static void createMessageADT_A01(Fall fall) throws HL7Exception, IOException, SQLException {
  40. Patient patient = fall.getPatient();
  41. ADT_A01 adt = new ADT_A01();
  42. //default MSH Values (Sets Segments: 1,2,7,9,11
  43. adt.initQuickstart("ADT", "A01", "P");
  44. //MSH Segment:
  45. MSH mshSegment = adt.getMSH();
  46. mshSegment.getMsh3_SendingApplication().getNamespaceID().parse("KISGruppe6");
  47. mshSegment.getMsh5_ReceivingApplication().getNamespaceID().parse("OPS Gruppe von Maurice und Torben");
  48. mshSegment.getMsh12_VersionID().getVersionID().parse("2.5.1");
  49. mshSegment.getMsh15_AcceptAcknowledgmentType().parse("AL");
  50. //TODO check ob segment 10 gesetzt wurde
  51. //EVN Segment:
  52. EVN evnSegment = adt.getEVN();
  53. evnSegment.getEvn1_EventTypeCode().parse("A01");
  54. evnSegment.getEvn2_RecordedDateTime().getTime().setValue(mshSegment.getDateTimeOfMessage().encode());
  55. evnSegment.getEvn4_EventReasonCode().parse("01");
  56. //PID
  57. PID pidSegment = adt.getPID();
  58. pidSegment.getPid2_PatientID().getIDNumber().parse(String.valueOf(patient.getPatID()));
  59. pidSegment.getPid3_PatientIdentifierList(0).getIDNumber().parse(Integer.toString(patient.getPatID()));
  60. pidSegment.getPid5_PatientName(0).getFamilyName().getSurname().parse(patient.getNachname());
  61. pidSegment.getPid5_PatientName(0).getGivenName().parse(patient.getVorname());
  62. pidSegment.getPid7_DateTimeOfBirth().getTime().setValue(Date.from(patient.getGeburtsdatum().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
  63. pidSegment.getPid8_AdministrativeSex().parse(patient.getGeschlecht().toString());
  64. pidSegment.getPid11_PatientAddress(0).getStreetAddress().getStreetName().parse(patient.getStrasse());
  65. pidSegment.getPid11_PatientAddress(0).getStreetAddress().getDwellingNumber().parse(patient.getHausnummer());
  66. pidSegment.getPid11_PatientAddress(0).getCity().parse(patient.getOrt());
  67. pidSegment.getPid13_PhoneNumberHome(0).getTelephoneNumber().parse(patient.getTelefon());
  68. pidSegment.getPid16_MaritalStatus().getAlternateIdentifier().parse(patient.getFamilienstand().toString());
  69. //Diagnosen
  70. List<Diagnose> diagnosen = getDiagnosenByFall(fall);
  71. PV1 pv1Segment = adt.getPV1();
  72. pv1Segment.getPv12_PatientClass().parse("U");
  73. if (!diagnosen.isEmpty()) {
  74. diagnosen.add(fall.getHauptDiagnose());
  75. int i = 1;
  76. DG1 dg1Segment;
  77. for (Diagnose diagnose : diagnosen) {
  78. System.out.println(diagnose);
  79. dg1Segment = adt.getDG1(i - 1);
  80. dg1Segment.getDg11_SetIDDG1().parse(String.valueOf(i));
  81. dg1Segment.getDg13_DiagnosisCodeDG1().getIdentifier().parse(diagnose.getIcd10code().getCode());
  82. // dg1Segment.getDg116_DiagnosingClinician ()[0].parse ( diagnose.getArzt ().getEinweisenderArzt () );
  83. dg1Segment.getDg14_DiagnosisDescription().parse(diagnose.getFreiText());
  84. dg1Segment.getDg16_DiagnosisType().parse(diagnose.getDiagArt().toString());
  85. i++;
  86. }
  87. }
  88. sendMessage(adt);
  89. }
  90. private static void sendMessage(Message message) throws HL7Exception {
  91. if (message instanceof ADT_A01) {
  92. sendMessageADT_A01(message);
  93. }
  94. }
  95. private static void sendMessageADT_A01(Message message) throws HL7Exception {
  96. ADT_A01 adt_a01 = (ADT_A01) message;
  97. HapiContext context = new DefaultHapiContext();
  98. Connection connection;
  99. try {
  100. connection = context.newClient(Main.OPS_IP, Main.OPS_PORT, false);
  101. } catch (HL7Exception e) {
  102. LoggerFactory.getLogger(HL7Sender.class).warn("Konnte aus irgendeinem Grund keine HL7 Nachricht senden. OPS Server down?");
  103. return;
  104. }
  105. Message response = null;
  106. HL7LogEntry messageEntry = new HL7LogEntry();
  107. HL7LogEntry responseEntry = new HL7LogEntry();
  108. messageEntry.setMessage(message.encode());
  109. messageEntry.setSource("127.0.0.1:1234"); // TODO: Kann man den lokal genutzen Port raus finden?
  110. messageEntry.setDirection(HL7LogEntry.Direction.OUT);
  111. messageEntry.setTimestamp(LocalDateTime.now());
  112. messageEntry.setTimestamp(HL7Utils.parseLocalDateTime(adt_a01.getMSH().getDateTimeOfMessage().getTime()));
  113. HL7Utils.logHL7MessageToDatabase(messageEntry);
  114. try {
  115. while (!(response instanceof ACK)) {
  116. response = connection.getInitiator().sendAndReceive(adt_a01);
  117. Thread.sleep(5000);
  118. }
  119. } catch (HL7Exception | LLPException | IOException | InterruptedException e) {
  120. // e.printStackTrace();
  121. }
  122. responseEntry.setMessage(response.encode());
  123. responseEntry.setSource(Main.OPS_IP + ":" + Main.OPS_PORT);
  124. responseEntry.setDirection(HL7LogEntry.Direction.IN);
  125. responseEntry.setTimestamp(LocalDateTime.now());
  126. HL7Utils.logHL7MessageToDatabase(responseEntry);
  127. connection.close();
  128. }
  129. /**
  130. * gibt erzeugte hl7 Nachricht auf der Konsole aus (Kann nach Debug phase wieder geloescht werden)
  131. *
  132. * @param msg
  133. * @throws HL7Exception
  134. */
  135. public void printXMLEncodedMessageADT(Message msg) throws HL7Exception, IOException {
  136. HapiContext context = new DefaultHapiContext();
  137. Parser parser = context.getXMLParser();
  138. String encodedMessage = parser.encode(msg);
  139. LocalDateTime ldt = LocalDateTime.now();
  140. File file = new File("ADTA01Messafge" + LocalDateTime.now().toString() + "xml");
  141. if (!file.exists()) {
  142. file.createNewFile();
  143. }
  144. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  145. BufferedWriter bw = new BufferedWriter(fw);
  146. bw.write(encodedMessage);
  147. bw.close();
  148. System.out.println(encodedMessage);
  149. }
  150. }