Im Rahmen der Veranstaltung "CS3330 - Projektpraktikum MedizinischeInformatik" an der Universität zu Lübeck entstandenes Krankenhausinformationssystem (KIS).
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

191 linhas
7.8 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.app.HL7Service;
  7. import ca.uhn.hl7v2.app.Initiator;
  8. import ca.uhn.hl7v2.llp.LLPException;
  9. import ca.uhn.hl7v2.model.Message;
  10. import ca.uhn.hl7v2.model.v251.message.ACK;
  11. import ca.uhn.hl7v2.model.v251.message.ADT_A01;
  12. import ca.uhn.hl7v2.model.v251.segment.*;
  13. import ca.uhn.hl7v2.parser.Parser;
  14. import de.uniluebeck.mi.projmi6.Main;
  15. import de.uniluebeck.mi.projmi6.model.Diagnose;
  16. import de.uniluebeck.mi.projmi6.model.Fall;
  17. import de.uniluebeck.mi.projmi6.model.HL7LogEntry;
  18. import de.uniluebeck.mi.projmi6.model.Patient;
  19. import java.io.BufferedWriter;
  20. import java.io.File;
  21. import java.io.FileWriter;
  22. import java.io.IOException;
  23. import java.sql.SQLException;
  24. import java.time.LocalDateTime;
  25. import java.time.ZoneId;
  26. import java.util.Date;
  27. import java.util.List;
  28. import static de.uniluebeck.mi.projmi6.db.DBHandler.getDiagnosenByFall;
  29. /**
  30. * Created by taschi on 22.11.15.
  31. */
  32. public class HL7Sender {
  33. /**
  34. * erstellt eine ADT_A0 nachricht, welche anschließend an die OPS Gruppe verschickt werden soll.
  35. * TODO Mit OPS Gruppe absprechen, welche Informationen sie auf jeden Fall benötigen und Code entsprechend anpassen.
  36. *
  37. * @param fall nach dem Erstellen eines neuen Falls wird diese Methode aufgerufen um Hl7 Nachricht zu erzeugen
  38. * @throws Exception
  39. */
  40. public static void createMessageADT_A01(Fall fall) throws HL7Exception, IOException, SQLException {
  41. Patient patient = fall.getPatient();
  42. ADT_A01 adt = new ADT_A01();
  43. //default MSH Values (Sets Segments: 1,2,7,9,11
  44. adt.initQuickstart("ADT", "A01", "P");
  45. //MSH Segment:
  46. MSH mshSegment = adt.getMSH();
  47. mshSegment.getMsh3_SendingApplication().getNamespaceID().parse("KISGruppe6");
  48. mshSegment.getMsh5_ReceivingApplication().getNamespaceID().parse("OPS Gruppe von Maurice und Torben");
  49. mshSegment.getMsh12_VersionID().getVersionID().parse("2.5.1");
  50. mshSegment.getMsh15_AcceptAcknowledgmentType().parse("AL");
  51. //TODO check ob segment 10 gesetzt wurde
  52. //EVN Segment:
  53. EVN evnSegment = adt.getEVN();
  54. evnSegment.getEvn1_EventTypeCode().parse("A01");
  55. evnSegment.getEvn2_RecordedDateTime().getTime().setValue(mshSegment.getDateTimeOfMessage().encode());
  56. evnSegment.getEvn4_EventReasonCode().parse("01");
  57. //PID
  58. PID pidSegment = adt.getPID();
  59. pidSegment.getPid2_PatientID().getIDNumber().parse(String.valueOf(patient.getPatID()));
  60. pidSegment.getPid3_PatientIdentifierList(0).getIDNumber().parse(Integer.toString(patient.getPatID()));
  61. pidSegment.getPid5_PatientName(0).getFamilyName().getSurname().parse(patient.getNachname());
  62. pidSegment.getPid5_PatientName(0).getGivenName().parse(patient.getVorname());
  63. pidSegment.getPid7_DateTimeOfBirth().getTime().setValue(Date.from(patient.getGeburtsdatum().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
  64. pidSegment.getPid8_AdministrativeSex().parse(patient.getGeschlecht().toString());
  65. pidSegment.getPid11_PatientAddress(0).getStreetAddress().getStreetName().parse(patient.getStrasse());
  66. pidSegment.getPid11_PatientAddress(0).getStreetAddress().getDwellingNumber().parse(patient.getHausnummer());
  67. pidSegment.getPid11_PatientAddress(0).getCity().parse(patient.getOrt());
  68. pidSegment.getPid13_PhoneNumberHome(0).getTelephoneNumber().parse(patient.getTelefon());
  69. pidSegment.getPid16_MaritalStatus().getAlternateIdentifier().parse(patient.getFamilienstand().toString());
  70. //Diagnosen
  71. List<Diagnose> diagnosen = getDiagnosenByFall(fall);
  72. diagnosen.add(fall.getHauptDiagnose());
  73. PV1 pv1Segment = adt.getPV1();
  74. pv1Segment.getPv12_PatientClass().parse("U");
  75. if (!diagnosen.isEmpty()) {
  76. int i = 1;
  77. DG1 dg1Segment;
  78. for (Diagnose diagnose : diagnosen) {
  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. //Segment 2 Verschluesselung ?
  87. }
  88. }
  89. sendMessage(adt);
  90. }
  91. private static void sendMessage(Message message) throws HL7Exception {
  92. /*
  93. * The following section of code establishes a server listening
  94. * on port 1011 for new connections, and then "handles" them by
  95. */
  96. if (message instanceof ADT_A01) {
  97. ADT_A01 adt = (ADT_A01) message;
  98. int port = Main.OPS_PORT;
  99. HapiContext context = new DefaultHapiContext();
  100. HL7Service server = context.newServer(port, false);
  101. /*
  102. * Create a client, which will connect to our waiting
  103. * server and send messages to it.
  104. */
  105. // A connection object represents a socket attached to an HL7 server
  106. Connection connection = null;
  107. try {
  108. connection = context.newClient(Main.OPS_IP, port, false);
  109. } catch (HL7Exception e) {
  110. e.printStackTrace();
  111. }
  112. // The initiator is used to transmit unsolicited messages
  113. Initiator initiator = connection.getInitiator();
  114. Message response = null;
  115. HL7LogEntry messageEntry = new HL7LogEntry();
  116. messageEntry.setMessage(message.encode());
  117. messageEntry.setSource("127.0.0.1:1234"); // TODO: naja...
  118. messageEntry.setDirection(HL7LogEntry.Direction.OUT);
  119. messageEntry.setTimestamp(LocalDateTime.now());
  120. HL7Util.logHL7MessageToDatabase(messageEntry);
  121. try {
  122. while (!(response instanceof ACK)) {
  123. response = initiator.sendAndReceive(adt);
  124. Thread.sleep(5000);
  125. }
  126. } catch (HL7Exception | LLPException | IOException | InterruptedException e) {
  127. e.printStackTrace();
  128. }
  129. HL7LogEntry responseEntry = new HL7LogEntry();
  130. responseEntry.setMessage(response.encode());
  131. responseEntry.setSource(Main.OPS_IP + ":" + Main.OPS_PORT);
  132. responseEntry.setDirection(HL7LogEntry.Direction.IN);
  133. responseEntry.setTimestamp(LocalDateTime.now());
  134. HL7Util.logHL7MessageToDatabase(responseEntry);
  135. /*
  136. * Close the connection when you are done with it.
  137. */
  138. connection.close();
  139. // Stop the receiving server and client
  140. server.stop();
  141. }
  142. }
  143. /**
  144. * gibt erzeugte hl7 Nachricht auf der Konsole aus (Kann nach Debug phase wieder geloescht werden)
  145. *
  146. * @param msg
  147. * @throws HL7Exception
  148. */
  149. public void printXMLEncodedMessageADT(Message msg) throws HL7Exception, IOException {
  150. HapiContext context = new DefaultHapiContext();
  151. Parser parser = context.getXMLParser();
  152. String encodedMessage = parser.encode(msg);
  153. LocalDateTime ldt = LocalDateTime.now();
  154. File file = new File("ADTA01Messafge" + LocalDateTime.now().toString() + "xml");
  155. if (!file.exists()) {
  156. file.createNewFile();
  157. }
  158. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  159. BufferedWriter bw = new BufferedWriter(fw);
  160. bw.write(encodedMessage);
  161. bw.close();
  162. System.out.println(encodedMessage);
  163. }
  164. }