Im Rahmen der Veranstaltung "CS3330 - Projektpraktikum MedizinischeInformatik" an der Universität zu Lübeck entstandenes Krankenhausinformationssystem (KIS).
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

242 rindas
10 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.ADT_A01;
  11. import ca.uhn.hl7v2.model.v251.segment.*;
  12. import ca.uhn.hl7v2.parser.Parser;
  13. import ca.uhn.hl7v2.parser.PipeParser;
  14. import ca.uhn.hl7v2.validation.ValidationContext;
  15. import ca.uhn.hl7v2.validation.impl.ValidationContextFactory;
  16. import de.uniluebeck.mi.projmi6.model.Diagnose;
  17. import de.uniluebeck.mi.projmi6.model.Fall;
  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.util.List;
  26. import static de.uniluebeck.mi.projmi6.db.DBHandler.getDiagnosenByFall;
  27. /**
  28. * Created by taschi on 22.11.15.
  29. */
  30. public class HL7Sender {
  31. /**
  32. * erstellt eine ADT_A0 nachricht, welche anschließend an die OPS Gruppe verschickt werden soll.
  33. * TODO Mit OPS Gruppe absprechen, welche Informationen sie auf jeden Fall benötigen und Code entsprechend anpassen.
  34. * @param fall nach dem Erstellen eines neuen Falls wird diese Methode aufgerufen um Hl7 Nachricht zu erzeugen
  35. * @throws Exception
  36. */
  37. public void createMessageADTA01( Fall fall) throws HL7Exception, IOException, SQLException {
  38. Patient patient = fall.getPatient ();
  39. ADT_A01 adt = new ADT_A01 ();
  40. //default MSH Values (Sets Segments: 1,2,7,9,11
  41. adt.initQuickstart ("ADT", "A01", "P");
  42. //MSH Segment:
  43. MSH mshSegment = adt.getMSH();
  44. mshSegment.getMsh3_SendingApplication ().getNamespaceID ().parse ( "KISGruppe6" );
  45. mshSegment.getMsh5_ReceivingApplication ().getNamespaceID ().parse("OPS Gruppe von Maurice und Torben");
  46. mshSegment.getMsh12_VersionID ().getVersionID ().parse ( "2.51" );
  47. mshSegment.getMsh15_AcceptAcknowledgmentType ().parse ( "AL" );
  48. //TODO check ob segment 10 gesetzt wurde
  49. //EVN Segment:
  50. EVN evnSegment = adt.getEVN ();
  51. evnSegment.getEvn1_EventTypeCode ().parse("A01");
  52. evnSegment.getEvn2_RecordedDateTime ().parse ( String.valueOf ( LocalDateTime.now () ) );
  53. evnSegment.getEvn4_EventReasonCode ().parse ( "01" );
  54. //PID
  55. PID pidSegment = adt.getPID ();
  56. pidSegment.getPid2_PatientID ().getIDNumber ().parse ( String.valueOf ( patient.getPatID () ) );
  57. pidSegment.getPid3_PatientIdentifierList (0).getIDNumber ().parse ( Integer.toString(patient.getPatID ()) );
  58. pidSegment.getPid5_PatientName (0).getFamilyName ().getSurname ().parse ( patient.getNachname () );
  59. pidSegment.getPid5_PatientName (0).getGivenName ().parse ( patient.getVorname () );
  60. pidSegment.getPid7_DateTimeOfBirth ().getTime ().parse( patient.getGeburtsdatum ().toString () );
  61. pidSegment.getPid8_AdministrativeSex ().parse ( patient.getGeschlecht ().toString () );
  62. pidSegment.getPid11_PatientAddress (0).getStreetAddress ().getStreetName().parse ( patient.getStrasse () );
  63. pidSegment.getPid11_PatientAddress (0).getStreetAddress ().getDwellingNumber ( ).parse( patient.getHausnummer () );
  64. pidSegment.getPid11_PatientAddress (0).getCity ().parse (patient.getOrt () );
  65. pidSegment.getPid13_PhoneNumberHome (0).getTelephoneNumber ().parse ( patient.getTelefon () );
  66. pidSegment.getPid16_MaritalStatus ().getAlternateIdentifier ().parse ( patient.getFamilienstand ().toString());
  67. //Diagnosen
  68. List<Diagnose> diagnosen = getDiagnosenByFall ( fall );
  69. diagnosen.add(fall.getHauptDiagnose ());
  70. PV1 pv1Segment = adt.getPV1 ();
  71. pv1Segment.getPv12_PatientClass ().parse ( "U" );
  72. if(!diagnosen.isEmpty ()) {
  73. int i =1;
  74. for (Diagnose diagnose : diagnosen) {
  75. DG1 dg1Segment = adt.getDG1 ();
  76. dg1Segment.getDg11_SetIDDG1 ().parse ( String.valueOf ( i ) );
  77. dg1Segment.getDg13_DiagnosisCodeDG1 ().getIdentifier ().parse ( diagnose.getIcd10code ().getCode () );
  78. dg1Segment.getDg116_DiagnosingClinician ()[0].parse ( diagnose.getArzt ().getEinweisenderArzt () );
  79. dg1Segment.getDg14_DiagnosisDescription ().parse ( diagnose.getFreiText () );
  80. dg1Segment.getDg16_DiagnosisType ().parse ( diagnose.getDiagArt ().toString () );
  81. adt.getDG1All ().add ( dg1Segment );
  82. i++;
  83. //Segment 2 Verschluesselung ?
  84. }
  85. }
  86. printXMLEncodedMessageADT(adt);
  87. }
  88. public void sendMessage(Message message){
  89. /*
  90. * The following section of code establishes a server listening
  91. * on port 1011 for new connections, and then "handles" them by
  92. */
  93. int port = 1011; // The port to listen on
  94. boolean useTls = false; // Don't use TLS/SSL
  95. HapiContext context = new DefaultHapiContext();
  96. HL7Service server = context.newServer(port, useTls);
  97. /*
  98. * Create a client, which will connect to our waiting
  99. * server and send messages to it.
  100. */
  101. // A connection object represents a socket attached to an HL7 server
  102. Connection connection = null;
  103. try {
  104. connection = context.newClient("localhost", port, useTls);
  105. } catch (HL7Exception e) {
  106. e.printStackTrace();
  107. }
  108. // Create a message to send
  109. String msg = "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01|12345|P|2.2\r"
  110. + "PID|0001|00009874|00001122|A00977|SMITH^JOHN^M|MOM|19581119|F|NOTREAL^LINDA^M|C|564 SPRING ST^^NEEDHAM^MA^02494^US|0002|(818)565-1551|(425)828-3344|E|S|C|0000444444|252-00-4414||||SA|||SA||||NONE|V1|0001|I|D.ER^50A^M110^01|ER|P00055|11B^M011^02|070615^BATMAN^GEORGE^L|555888^NOTREAL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^NOTREAL^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|199904101200||||5555112333|||666097^NOTREAL^MANNY^P\r"
  111. + "NK1|0222555|NOTREAL^JAMES^R|FA|STREET^OTHER STREET^CITY^ST^55566|(222)111-3333|(888)999-0000|||||||ORGANIZATION\r"
  112. + "PV1|0001|I|D.ER^1F^M950^01|ER|P000998|11B^M011^02|070615^BATMAN^GEORGE^L|555888^OKNEL^BOB^K^DR^MD|777889^NOTREAL^SAM^T^DR^MD^PHD|ER|D.WT^1A^M010^01|||ER|AMB|02|070615^VOICE^BILL^L|ER|000001916994|D||||||||||||||||GDD|WA|NORM|02|O|02|E.IN^02D^M090^01|E.IN^01D^M080^01|199904072124|199904101200|||||5555112333|||666097^DNOTREAL^MANNY^P\r"
  113. + "PV2|||0112^TESTING|55555^PATIENT IS NORMAL|NONE|||19990225|19990226|1|1|TESTING|555888^NOTREAL^BOB^K^DR^MD||||||||||PROD^003^099|02|ER||NONE|19990225|19990223|19990316|NONE\r"
  114. + "AL1||SEV|001^POLLEN\r"
  115. + "GT1||0222PL|NOTREAL^BOB^B||STREET^OTHER STREET^CITY^ST^77787|(444)999-3333|(222)777-5555||||MO|111-33-5555||||NOTREAL GILL N|STREET^OTHER STREET^CITY^ST^99999|(111)222-3333\r"
  116. + "IN1||022254P|4558PD|BLUE CROSS|STREET^OTHER STREET^CITY^ST^00990||(333)333-6666||221K|LENIX|||19980515|19990515|||PATIENT01 TEST D||||||||||||||||||02LL|022LP554";
  117. Parser p = context.getPipeParser();
  118. Message adt = null;
  119. try {
  120. adt = p.parse(msg);
  121. } catch (HL7Exception e) {
  122. e.printStackTrace();
  123. }
  124. // The initiator is used to transmit unsolicited messages
  125. Initiator initiator = connection.getInitiator();
  126. Message response = null;
  127. try {
  128. response = initiator.sendAndReceive(adt);
  129. } catch (HL7Exception e) {
  130. e.printStackTrace();
  131. } catch (LLPException e) {
  132. e.printStackTrace();
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. String responseString = null;
  137. try {
  138. responseString = p.encode(response);
  139. } catch (HL7Exception e) {
  140. e.printStackTrace();
  141. }
  142. System.out.println("Received response:\n" + responseString);
  143. /*
  144. * MSH|^~\&|||||20070218200627.515-0500||ACK|54|P|2.2 MSA|AA|12345
  145. */
  146. /*
  147. * If you want to send another message to the same destination, it's fine
  148. * to ask the context again for a client to attach to the same host/port.
  149. * The context will be smart about it and return the same (already
  150. * connected) client Connection instance, assuming it hasn't been closed.
  151. */
  152. try {
  153. connection = context.newClient("localhost", port, useTls);
  154. } catch (HL7Exception e) {
  155. e.printStackTrace();
  156. }
  157. initiator = connection.getInitiator();
  158. try {
  159. response = initiator.sendAndReceive(adt);
  160. } catch (HL7Exception e) {
  161. e.printStackTrace();
  162. } catch (LLPException e) {
  163. e.printStackTrace();
  164. } catch (IOException e) {
  165. e.printStackTrace();
  166. }
  167. /*
  168. * Close the connection when you are done with it.
  169. */
  170. connection.close();
  171. // Stop the receiving server and client
  172. server.stopAndWait();
  173. }
  174. /**
  175. * gibt erzeugte hl7 Nachricht auf der Konsole aus (Kann nach Debug phase wieder geloescht werden)
  176. * @param msg
  177. * @throws HL7Exception
  178. */
  179. public void printXMLEncodedMessageADT(Message msg) throws HL7Exception, IOException {
  180. HapiContext context = new DefaultHapiContext ();
  181. Parser parser = context.getXMLParser();
  182. String encodedMessage = parser.encode(msg);
  183. LocalDateTime ldt = LocalDateTime.now();
  184. File file = new File("ADTA01Messafge" +LocalDateTime.now().toString() +"xml" );
  185. if (!file.exists()) {
  186. file.createNewFile();
  187. }
  188. FileWriter fw = new FileWriter(file.getAbsoluteFile());
  189. BufferedWriter bw = new BufferedWriter(fw);
  190. bw.write(encodedMessage);
  191. bw.close();
  192. System.out.println (encodedMessage);
  193. }
  194. /**
  195. *
  196. * @param msg
  197. */
  198. public void validateMessage(Message msg) {
  199. HapiContext context = new DefaultHapiContext();
  200. context.setValidationContext( ValidationContextFactory.<ValidationContext>defaultValidation());
  201. PipeParser parser = context.getPipeParser();
  202. try {
  203. parser.encode(msg);
  204. } catch (HL7Exception e) {
  205. System.out.println("invaid message!" +e);
  206. }
  207. }
  208. }