package de.uniluebeck.mi.projmi6.hapi; import ca.uhn.hl7v2.DefaultHapiContext; import ca.uhn.hl7v2.HL7Exception; import ca.uhn.hl7v2.HapiContext; import ca.uhn.hl7v2.app.Connection; import ca.uhn.hl7v2.app.HL7Service; import ca.uhn.hl7v2.app.Initiator; import ca.uhn.hl7v2.llp.LLPException; import ca.uhn.hl7v2.model.Message; import ca.uhn.hl7v2.model.v251.message.ACK; import ca.uhn.hl7v2.model.v251.message.ADT_A01; import ca.uhn.hl7v2.model.v251.segment.*; import ca.uhn.hl7v2.parser.Parser; import de.uniluebeck.mi.projmi6.Main; import de.uniluebeck.mi.projmi6.model.Diagnose; import de.uniluebeck.mi.projmi6.model.Fall; import de.uniluebeck.mi.projmi6.model.HL7LogEntry; import de.uniluebeck.mi.projmi6.model.Patient; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.sql.SQLException; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; import java.util.List; import static de.uniluebeck.mi.projmi6.db.DBHandler.getDiagnosenByFall; /** * Created by taschi on 22.11.15. */ public class HL7Sender { /** * erstellt eine ADT_A0 nachricht, welche anschließend an die OPS Gruppe verschickt werden soll. * TODO Mit OPS Gruppe absprechen, welche Informationen sie auf jeden Fall benötigen und Code entsprechend anpassen. * * @param fall nach dem Erstellen eines neuen Falls wird diese Methode aufgerufen um Hl7 Nachricht zu erzeugen * @throws Exception */ public static void createMessageADT_A01(Fall fall) throws HL7Exception, IOException, SQLException { Patient patient = fall.getPatient(); ADT_A01 adt = new ADT_A01(); //default MSH Values (Sets Segments: 1,2,7,9,11 adt.initQuickstart("ADT", "A01", "P"); //MSH Segment: MSH mshSegment = adt.getMSH(); mshSegment.getMsh3_SendingApplication().getNamespaceID().parse("KISGruppe6"); mshSegment.getMsh5_ReceivingApplication().getNamespaceID().parse("OPS Gruppe von Maurice und Torben"); mshSegment.getMsh12_VersionID().getVersionID().parse("2.5.1"); mshSegment.getMsh15_AcceptAcknowledgmentType().parse("AL"); //TODO check ob segment 10 gesetzt wurde //EVN Segment: EVN evnSegment = adt.getEVN(); evnSegment.getEvn1_EventTypeCode().parse("A01"); evnSegment.getEvn2_RecordedDateTime().getTime().setValue(mshSegment.getDateTimeOfMessage().encode()); evnSegment.getEvn4_EventReasonCode().parse("01"); //PID PID pidSegment = adt.getPID(); pidSegment.getPid2_PatientID().getIDNumber().parse(String.valueOf(patient.getPatID())); pidSegment.getPid3_PatientIdentifierList(0).getIDNumber().parse(Integer.toString(patient.getPatID())); pidSegment.getPid5_PatientName(0).getFamilyName().getSurname().parse(patient.getNachname()); pidSegment.getPid5_PatientName(0).getGivenName().parse(patient.getVorname()); pidSegment.getPid7_DateTimeOfBirth().getTime().setValue(Date.from(patient.getGeburtsdatum().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())); pidSegment.getPid8_AdministrativeSex().parse(patient.getGeschlecht().toString()); pidSegment.getPid11_PatientAddress(0).getStreetAddress().getStreetName().parse(patient.getStrasse()); pidSegment.getPid11_PatientAddress(0).getStreetAddress().getDwellingNumber().parse(patient.getHausnummer()); pidSegment.getPid11_PatientAddress(0).getCity().parse(patient.getOrt()); pidSegment.getPid13_PhoneNumberHome(0).getTelephoneNumber().parse(patient.getTelefon()); pidSegment.getPid16_MaritalStatus().getAlternateIdentifier().parse(patient.getFamilienstand().toString()); //Diagnosen List diagnosen = getDiagnosenByFall(fall); diagnosen.add(fall.getHauptDiagnose()); PV1 pv1Segment = adt.getPV1(); pv1Segment.getPv12_PatientClass().parse("U"); if (!diagnosen.isEmpty()) { int i = 1; DG1 dg1Segment; for (Diagnose diagnose : diagnosen) { dg1Segment = adt.getDG1(i - 1); dg1Segment.getDg11_SetIDDG1().parse(String.valueOf(i)); dg1Segment.getDg13_DiagnosisCodeDG1().getIdentifier().parse(diagnose.getIcd10code().getCode()); // dg1Segment.getDg116_DiagnosingClinician ()[0].parse ( diagnose.getArzt ().getEinweisenderArzt () ); dg1Segment.getDg14_DiagnosisDescription().parse(diagnose.getFreiText()); dg1Segment.getDg16_DiagnosisType().parse(diagnose.getDiagArt().toString()); i++; //Segment 2 Verschluesselung ? } } sendMessage(adt); } private static void sendMessage(Message message) throws HL7Exception { /* * The following section of code establishes a server listening * on port 1011 for new connections, and then "handles" them by */ if (message instanceof ADT_A01) { ADT_A01 adt = (ADT_A01) message; int port = Main.OPS_PORT; HapiContext context = new DefaultHapiContext(); HL7Service server = context.newServer(port, false); /* * Create a client, which will connect to our waiting * server and send messages to it. */ // A connection object represents a socket attached to an HL7 server Connection connection = null; try { connection = context.newClient(Main.OPS_IP, port, false); } catch (HL7Exception e) { e.printStackTrace(); } // The initiator is used to transmit unsolicited messages Initiator initiator = connection.getInitiator(); Message response = null; HL7LogEntry messageEntry = new HL7LogEntry(); messageEntry.setMessage(message.encode()); messageEntry.setSource("127.0.0.1:1234"); // TODO: naja... messageEntry.setDirection(HL7LogEntry.Direction.OUT); messageEntry.setTimestamp(LocalDateTime.now()); HL7Util.logHL7MessageToDatabase(messageEntry); try { while (!(response instanceof ACK)) { response = initiator.sendAndReceive(adt); Thread.sleep(5000); } } catch (HL7Exception | LLPException | IOException | InterruptedException e) { e.printStackTrace(); } HL7LogEntry responseEntry = new HL7LogEntry(); responseEntry.setMessage(response.encode()); responseEntry.setSource(Main.OPS_IP + ":" + Main.OPS_PORT); responseEntry.setDirection(HL7LogEntry.Direction.IN); responseEntry.setTimestamp(LocalDateTime.now()); HL7Util.logHL7MessageToDatabase(responseEntry); /* * Close the connection when you are done with it. */ connection.close(); // Stop the receiving server and client server.stop(); } } /** * gibt erzeugte hl7 Nachricht auf der Konsole aus (Kann nach Debug phase wieder geloescht werden) * * @param msg * @throws HL7Exception */ public void printXMLEncodedMessageADT(Message msg) throws HL7Exception, IOException { HapiContext context = new DefaultHapiContext(); Parser parser = context.getXMLParser(); String encodedMessage = parser.encode(msg); LocalDateTime ldt = LocalDateTime.now(); File file = new File("ADTA01Messafge" + LocalDateTime.now().toString() + "xml"); if (!file.exists()) { file.createNewFile(); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(encodedMessage); bw.close(); System.out.println(encodedMessage); } }