private void ProcessHL7Event(HL7Message message) { GetHL7Events(); string sMessageHL7Event = message.GetHL7Item("MSH-9.2")[0]; // create HL7Processor to update SQL DB with HL7! <ala: Mike B> //HL7Processor hl7Processor = new HL7Processor(); //hl7Processor.ProcessData(message); string sError = "Error Processing Transaction " + message.GetHL7Item("MSH-9")[0]; ErrorLogger oErrorLogger = new ErrorLogger(); oErrorLogger.ProcessError(sError); //int iEventTypeIndex = Array.IndexOf(sHL7Events, sMessageHL7Event); // Console.WriteLine("Event Type index = {0}",iEventTypeIndex.ToString()); /* * if (iEventTypeIndex = -1) // -1 == array item not found * { * * } */ Console.WriteLine("Ready to process event: {0}", sMessageHL7Event); //ProcessPatient(message); }
// MB added 11/28/16 string GenerateNAK(string originalMessage) { // create a HL7Message object using the original message as the source to obtain details to reflect back in the ACK message HL7Message tmpMsg = new HL7Message(originalMessage); string trigger = tmpMsg.GetHL7Item("MSH-9.2")[0]; string originatingApp = tmpMsg.GetHL7Item("MSH-3")[0]; string originatingSite = tmpMsg.GetHL7Item("MSH-4")[0]; string messageID = tmpMsg.GetHL7Item("MSH-10")[0]; string processingID = "T"; //tmpMsg.GetHL7Item("MSH-11")[0]; string hl7Version = "2.3"; //tmpMsg.GetHL7Item("MSH-12")[0]; string ackTimestamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString(); StringBuilder NAKString = new StringBuilder(); NAKString.Append((char)0x0B); NAKString.Append("MSH|^~\\&|HL7Listener|HL7Listener|" + originatingSite + "|" + originatingApp + "|" + ackTimestamp + "||ACK^" + trigger + "|" + messageID + "|" + processingID + "|" + hl7Version); NAKString.Append((char)0x0D); NAKString.Append("MSA|AR|" + messageID); NAKString.Append((char)0x1C); NAKString.Append((char)0x0D); return(NAKString.ToString()); }
public void ProcessData(HL7Message message) { //Console.WriteLine("processing message!"); //string sSex = null; // SQL SP Writer //ProcessHL7Event(message); Console.WriteLine("============================================"); Console.WriteLine("Patient First Name = {0}", message.GetHL7Item("PID-5.2")[0]); Console.WriteLine("Patient Last Name = {0}", message.GetHL7Item("PID-5.1")[0]); Console.WriteLine("Patient Med Rec # = {0}", message.GetHL7Item("PID-3.1")[0]); //Console.WriteLine("Patient Sex = {0}", sSex); Console.WriteLine("Patient Sex = {0}", message.GetHL7Item("PID-8")[0]); Console.WriteLine("Patient Race = {0}", message.GetHL7Item("PID-10")[0]); Console.WriteLine("Patient Street Address = {0}", message.GetHL7Item("PID-11.1")[0]); Console.WriteLine("Patient City = {0}", message.GetHL7Item("PID-11.3")[0]); Console.WriteLine("Patient State = {0}", message.GetHL7Item("PID-11.4")[0]); Console.WriteLine("Patient Zip = {0}", message.GetHL7Item("PID-11.5")[0]); Console.WriteLine("Patient Country = {0}", message.GetHL7Item("PID-11.6")[0]); Console.WriteLine("Patient Class = {0}", message.GetHL7Item("PV1-2")[0]); Console.WriteLine("============================================"); }
/// <summary> /// Receive data from a client connection, look for MLLP HL7 message. /// </summary> /// <param name="client"></param> private void ReceiveData(object client) { // generate a random sequence number to use for the file names Random random = new Random(Guid.NewGuid().GetHashCode()); int filenameSequenceStart = random.Next(0, 1000000); TcpClient tcpClient = (TcpClient)client; NetworkStream clientStream = tcpClient.GetStream(); //mb stop conn dropping //clientStream.ReadTimeout = TCP_TIMEOUT; //clientStream.WriteTimeout = TCP_TIMEOUT; byte[] messageBuffer = new byte[4096]; int bytesRead; String messageData = ""; String sHL7 = ""; int messageCount = 0; Console.WriteLine("looking for HL7..."); while (true) { bytesRead = 0; try { // Wait until a client application submits a message bytesRead = clientStream.Read(messageBuffer, 0, 4096); } catch (Exception e) { // A network error has occurred LogInformation("Connection from " + tcpClient.Client.RemoteEndPoint + " has ended"); break; } if (bytesRead == 0) { // The client has disconected LogInformation("The client " + tcpClient.Client.RemoteEndPoint + " has disconnected"); break; } // Message buffer received successfully messageData += Encoding.UTF8.GetString(messageBuffer, 0, bytesRead); // Find a VT character, this is the beginning of the MLLP frame Console.WriteLine("read HL7 first line into buffer..."); int start = messageData.IndexOf((char)0x0B); if (start >= 0) { Console.WriteLine("found index"); // Search for the end of the MLLP frame (a FS character) int end = messageData.IndexOf((char)0x1C); if (end > start) { Console.WriteLine("found end of message"); messageCount++; try { // queue the message to sent to the passthru host if the -PassThru option has been set if (passthruHost != null) { messageQueue.Enqueue(messageData.Substring(start + 1, end - (start + 1))); } Console.WriteLine("made it past pass-through"); // create a HL7message object from the message recieved. Use this to access elements needed to populate the ACK message and file name of the archived message HL7Message message = new HL7Message(messageData.Substring(start + 1, end - (start + 1))); // get the HL7 message as a text string that can be processed with nHAPI (Mike B) sHL7 = messageData.Substring(start + 1, end - (start + 1)); //Console.WriteLine("Patient Name = {0}", message.GetHL7Item("PID-5.1")[0]); // !Begin Marc's Help // create HL7Processor to update SQL DB with HL7! <ala: Mike B> //HL7Processor hl7Processor = new HL7Processor(); // !End Marc's Help //hl7Processor.ProcessData(message); hl7Processor.ProcessHL7Data(sHL7); //Mike B: Return something if this cannot be processed, key off that for NAK Response //Console.WriteLine(message.ToString()); // if true: if (hl7Processor.sendNAK == false) //send ACK { messageData = ""; // reset the message data string for the next message string messageTrigger = message.GetMessageTrigger(); string messageControlID = message.GetHL7Item("MSH-10")[0]; string acceptAckType = "AL"; //message.GetHL7Item("MSH-15")[0]; string dateStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString(); string filename = dateStamp + "_" + (filenameSequenceStart + messageCount).ToString("D6") + "_" + messageTrigger + ".hl7"; // increment sequence number for each filename // Write the HL7 message to file. //WriteMessagetoFile(message.ToString(), this.archivePath + filename); // send ACK message is MSH-15 is set to AL and ACKs not disbaled by -NOACK command line switch if ((this.sendACK) && (acceptAckType.ToUpper() == "AL")) { LogInformation("Sending ACK (Message Control ID: " + messageControlID + ")"); // generate ACK Message and send in response to the message received string response = GenerateACK(message.ToString()); // TO DO: send ACKS if set in message header, or specified on command line //Console.WriteLine("ACK = {0}", response); byte[] encodedResponse = Encoding.UTF8.GetBytes(response); // Send response try { //MB this is where you could also send a NAK clientStream.Write(encodedResponse, 0, encodedResponse.Length); clientStream.Flush(); } catch (Exception e) { // A network error has occurred LogInformation("An error has occurred while sending an ACK to the client " + tcpClient.Client.RemoteEndPoint); LogInformation(e.Message); break; } } } else //send NAK { messageData = ""; // reset the message data string for the next message string messageTrigger = message.GetMessageTrigger(); string messageControlID = message.GetHL7Item("MSH-10")[0]; string acceptAckType = "AL"; //message.GetHL7Item("MSH-15")[0]; string dateStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString(); string filename = dateStamp + "_" + (filenameSequenceStart + messageCount).ToString("D6") + "_" + messageTrigger + ".hl7"; // increment sequence number for each filename // Write the HL7 message to file. //WriteMessagetoFile(message.ToString(), this.archivePath + filename); // send ACK message is MSH-15 is set to AL and ACKs not disbaled by -NOACK command line switch if ((this.sendACK) && (acceptAckType.ToUpper() == "AL")) { LogInformation("Sending NAK (Message Control ID: " + messageControlID + ")"); // generate ACK Message and send in response to the message received string response = GenerateNAK(message.ToString()); // TO DO: send ACKS if set in message header, or specified on command line //Console.WriteLine("ACK = {0}", response); byte[] encodedResponse = Encoding.UTF8.GetBytes(response); // Send response try { //MB this is where you could also send a NAK clientStream.Write(encodedResponse, 0, encodedResponse.Length); clientStream.Flush(); } catch (Exception e) { // A network error has occurred LogInformation("An error has occurred while sending an NAK to the client " + tcpClient.Client.RemoteEndPoint); LogInformation(e.Message); break; } } //MB To do: Disconnect Listener after NAK this.RequestStop(); } /* * messageData = ""; // reset the message data string for the next message * string messageTrigger = message.GetMessageTrigger(); * string messageControlID = message.GetHL7Item("MSH-10")[0]; * string acceptAckType = "AL"; //message.GetHL7Item("MSH-15")[0]; * string dateStamp = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString(); * string filename = dateStamp + "_" + (filenameSequenceStart + messageCount).ToString("D6") + "_" + messageTrigger + ".hl7"; // increment sequence number for each filename * // Write the HL7 message to file. * //WriteMessagetoFile(message.ToString(), this.archivePath + filename); * // send ACK message is MSH-15 is set to AL and ACKs not disbaled by -NOACK command line switch * if ((this.sendACK) && (acceptAckType.ToUpper() == "AL")) * { * LogInformation("Sending ACK (Message Control ID: " + messageControlID + ")"); * // generate ACK Message and send in response to the message received * string response = GenerateACK(message.ToString()); // TO DO: send ACKS if set in message header, or specified on command line * * //Console.WriteLine("ACK = {0}", response); * * byte[] encodedResponse = Encoding.UTF8.GetBytes(response); * // Send response * try * { * * //MB this is where you could also send a NAK * * clientStream.Write(encodedResponse, 0, encodedResponse.Length); * clientStream.Flush(); * } * catch (Exception e) * { * // A network error has occurred * LogInformation("An error has occurred while sending an ACK to the client " + tcpClient.Client.RemoteEndPoint); * LogInformation(e.Message); * break; * } * } */ } // if false NAK catch (Exception e) { messageData = ""; // reset the message data string for the next message LogWarning("An exception occurred while parsing the HL7 message"); LogWarning(e.Message); break; } } } } LogInformation("Total messages received:" + messageCount); clientStream.Close(); clientStream.Dispose(); tcpClient.Close(); }