def hl7ACK(request, ack_type, err_description='', message_type=None, extra_segments=None): """ Generate a HL7 ACK message for a given request message This is used early on in the communications code and should not be used by the application. The default ack type is ACK^req type. This can be replaced by passing in a message type, e.g. "MFK^M01" for a master file notification ack message. """ serial = next_serial() if extra_segments is None: extra_segments = [] ack_type = ack_type.upper() iMSH = request['MSH'][0] local_fac, local_app = iMSH[5], iMSH[4] remote_fac, remote_app = iMSH[3], iMSH[2] req_message_type = iMSH[8] version_id = iMSH[11] control_id = iMSH[9] if not message_type: message_type = hl7.Field(SEP[1], ['ACK', req_message_type[1]]) elif type(message_type) not in [hl7.Field, str, unicode]: message_type = hl7.Field(SEP[1], message_type) # assume list MSH = hl7.Segment(SEP[0], ['MSH', SEP[1:], local_app , local_fac, remote_app, remote_fac, timestamp(), '', message_type, str(serial), 'P', version_id, '']) MSA = hl7.Segment(SEP[0], ['MSA', ack_type, control_id, err_description]) response = hl7.Message(CR_SEP, [MSH, MSA] + extra_segments) return response
def create_msh_response(msg, response_type='AA'): ''' create the msh msa response ''' SEP = '|^~\&' CR_SEP = '\r' msh = msg.segment('MSH') control_id = unicode(msg.segment('MSH')[9]) msh_response = hl7.Segment(SEP[0], [ hl7.Field(SEP[1], ['MSH']), hl7.Field('', SEP[1:]), hl7.Field('', msh[4]), hl7.Field('', msh[5]), hl7.Field('', msh[2]), hl7.Field('', msh[3]), hl7.Field('', datetime.now().strftime('%Y%m%d%H%M%S')), hl7.Field('', 'ACK'), hl7.Field('', control_id), hl7.Field('', msh[10]), hl7.Field('', msh[11]) ]) msa_response = hl7.Segment(SEP[0], [ hl7.Field(SEP[1], ['MSA']), hl7.Field('', response_type), hl7.Field('', control_id) ]) response = hl7.Message(CR_SEP, [msh_response, msa_response]) return response
def make_hl7_message(self, req: "CamcopsRequest") -> None: """ Makes an HL7 message and stores it in ``self._hl7_msg``. May also store it in ``self.message`` (which is saved to the database), if we're saving HL7 messages. See - http://python-hl7.readthedocs.org/en/latest/index.html """ task = self.exported_task.task recipient = self.exported_task.recipient # --------------------------------------------------------------------- # Parts # --------------------------------------------------------------------- msh_segment = make_msh_segment(message_datetime=req.now, message_control_id=str(self.id)) pid_segment = task.get_patient_hl7_pid_segment(req, recipient) other_segments = task.get_hl7_data_segments(req, recipient) # --------------------------------------------------------------------- # Whole message # --------------------------------------------------------------------- segments = [msh_segment, pid_segment] + other_segments self._hl7_msg = hl7.Message(SEGMENT_SEPARATOR, segments) if recipient.hl7_keep_message: self.message = str(self._hl7_msg)
def make_hl7_ack(success): message_datetime = datetime.datetime.now(dateutil.tz.tzlocal()) segments = [ make_msh_segment(message_datetime), make_msa_segment(message_datetime, success), ] msg = hl7.Message(SEGMENT_SEPARATOR, segments) return msg
def test_create_msg(self): # Create a message MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ["MSH"])]) MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ["MSA"])]) response = hl7.Message(CR_SEP, [MSH, MSA]) response["MSH.F1.R1"] = SEP[0] response["MSH.F2.R1"] = SEP[1:] self.assertEqual(str(response), "MSH|^~\\&|\rMSA\r")
def test_create_msg(self): # Create a message MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ['MSH'])]) MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ['MSA'])]) response = hl7.Message(CR_SEP, [MSH, MSA]) response['MSH.F1.R1'] = SEP[0] response['MSH.F2.R1'] = SEP[1:] self.assertEqual(six.text_type(response), 'MSH|^~\\&|\rMSA')
def test_append(self): # Append a segment to a message MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ["MSH"])]) response = hl7.Message(CR_SEP, [MSH]) response["MSH.F1.R1"] = SEP[0] response["MSH.F2.R1"] = SEP[1:] MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ["MSA"])]) response.append(MSA) self.assertEqual(str(response), "MSH|^~\\&|\rMSA\r")
def test_append(self): # Append a segment to a message MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ['MSH'])]) response = hl7.Message(CR_SEP, [MSH]) response['MSH.F1.R1'] = SEP[0] response['MSH.F2.R1'] = SEP[1:] MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ['MSA'])]) response.append(MSA) self.assertEqual(six.text_type(response), 'MSH|^~\\&|\rMSA')
def hl7NAK(ack_type, err_description, version_id='2.4'): """ Generate a HL7 NAK message. The request cannot be used as it may have invalid structure. This is used early on in the communications code and should not be used by the application. """ serial = next_serial() MSH = hl7.Segment(SEP[0], ['MSH', SEP[1:],'','','','',timestamp(), '', 'ACK', str(serial), 'P', version_id, '']) MSA = hl7.Segment(SEP[0], ['MSA', ack_type, '', err_description, '']) response = hl7.Message(CR_SEP, [MSH, MSA]) return response
def hl7Response(request, message_type, version_id='2.4', extra_segments=None): """ Generate a HL7 RESPONSE message for a given request message. Just a MSG HDR message_type can be a string 'MFN^M02' or a list ['MFN', 'M02'] or a hl7.Field('^', ['MFN', 'M02']) """ serial = next_serial() if extra_segments is None: extra_segments = [] iMSH = request['MSH'][0] local_fac, local_app = iMSH[5], iMSH[4] remote_fac, remote_app = iMSH[3], iMSH[2] # version_id = iMSH[11] if type(message_type) not in [hl7.Field, str, unicode]: message_type = hl7.Field(SEP[1], message_type) # assume list MSH = hl7.Segment(SEP[0], [ 'MSH', SEP[1:], # 2.16.9.2 MSH-2: Encoding Characters local_app, # 2.16.9.3 MSH-3: Sending Application local_fac, # 2.16.9.4 MSH-4: Sending Facility remote_app, # 2.16.9.5 MSH-5: Receiving Application remote_fac, # 2.16.9.6 MSH-6: Receiving Facility timestamp(), # 2.16.9.7 MSH-7: Date/Time Of Message '', # 2.16.9.8 MSH-8: Security message_type, # 2.16.9.9 MSH-9: Message Type str(serial), # 2.16.9.10 MSH-10: Message Control ID 'P', # 2.16.9.11 MSH-11: Processing ID version_id, # 2.16.9.12 MSH-12: Version ID '', # Sequence Number # Continuation Pointer # Accept Acknowledgment Type # Application Acknowledgment Type # Country Code # Character Set # Principal Language Of Message # Alternate Character Set Handling Scheme # Conformance Statement ID ]) response = hl7.Message(CR_SEP, [MSH] + extra_segments) return response
def test_append_from_source(self): # Copy a segment between messages MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ["MSH"])]) MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ["MSA"])]) response = hl7.Message(CR_SEP, [MSH, MSA]) response["MSH.F1.R1"] = SEP[0] response["MSH.F2.R1"] = SEP[1:] self.assertEqual(str(response), "MSH|^~\\&|\rMSA\r") src_msg = hl7.parse(rep_sample_hl7) PID = src_msg["PID"][0] self.assertEqual( str(PID), "PID|Field1|Component1^Component2|Component1^Sub-Component1&Sub-Component2^Component3|Repeat1~Repeat2", ) response.append(PID) self.assertEqual( str(response), "MSH|^~\\&|\rMSA\rPID|Field1|Component1^Component2|Component1^Sub-Component1&Sub-Component2^Component3|Repeat1~Repeat2\r", )
def test_append_from_source(self): # Copy a segment between messages MSH = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ['MSH'])]) MSA = hl7.Segment(SEP[0], [hl7.Field(SEP[1], ['MSA'])]) response = hl7.Message(CR_SEP, [MSH, MSA]) response['MSH.F1.R1'] = SEP[0] response['MSH.F2.R1'] = SEP[1:] self.assertEqual(six.text_type(response), 'MSH|^~\\&|\rMSA') src_msg = hl7.parse(rep_sample_hl7) PID = src_msg['PID'][0] self.assertEqual( six.text_type(PID), 'PID|Field1|Component1^Component2|Component1^Sub-Component1&Sub-Component2^Component3|Repeat1~Repeat2' ) response.append(PID) self.assertEqual( six.text_type(response), 'MSH|^~\\&|\rMSA\rPID|Field1|Component1^Component2|Component1^Sub-Component1&Sub-Component2^Component3|Repeat1~Repeat2' )