From fed72bef49ab8c7c13949ca9a8e24ebc3a9e8bcb Mon Sep 17 00:00:00 2001 From: ur23 <ur23@ic.ac.uk> Date: Wed, 7 Feb 2024 20:36:35 +0000 Subject: [PATCH] parsing_msg_types --- main.py | 10 ++++++---- utils.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index a0fd2d0..d0adabf 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,6 @@ import socket from joblib import load -from utils import process_mllp_message, parse_hl7_message, create_acknowledgement +from utils import process_mllp_message, parse_hl7_message, create_acknowledgement, parse_system_message from constants import DT_MODEL_PATH, REVERSE_LABELS_MAP @@ -28,9 +28,11 @@ def start_server(host="0.0.0.0", port=8440): if hl7_data: message = parse_hl7_message(hl7_data) print("Parsed HL7 Message:") - print(message) - print(type(message)) - + # print(message) + # print(type(message)) + category,mrn,data = parse_system_message(message) #category is type of system message and data consists of age sex if PAS admit or date of blood test and creatanine result + + print(category,mrn,data,'\n') # Create and send ACK message ack_message = create_acknowledgement(message) sock.sendall(ack_message) diff --git a/utils.py b/utils.py index 8dd74c2..06ad06c 100644 --- a/utils.py +++ b/utils.py @@ -84,3 +84,46 @@ def populate_test_results_table(db, path): date = row[j] result = float(row[j+1]) db.insert_test_result(mrn, date, result) + + +def parse_system_message(message): + """ + Parses the HL7 message and returns the parsed message object. + """ + mrn = 0 + category = '' + data = ['']*2 + segments = str(message).split('\n') + if len(segments) < 4: + parsed_seg = segments[1].split('|') + if len(parsed_seg) > 4: + mrn = parsed_seg[3] + category = 'PAS-admit' + date_of_birth = parsed_seg[7] + data[0] = calculate_age(date_of_birth) + data[1] = parsed_seg[8][0] + else: + mrn = parsed_seg[3].replace('\r','') + category = 'PAS-discharge' + else: + mrn = segments[1].split('|')[3] + category = 'LIMS' + data[0] = segments[2].split('|')[7] #date of blood test + data[1] = float(segments[3].split('|')[5]) + + return category,mrn,data + +def calculate_age(date_of_birth): + """ + Calculate age based on the date of birth provided in the format YYYYMMDD. + """ + # Parse the date of birth string into a datetime object + dob = datetime.datetime.strptime(date_of_birth, "%Y%m%d") + + # Get the current date + current_date = datetime.datetime.now() + + # Calculate the difference between the current date and the date of birth + age = current_date.year - dob.year - ((current_date.month, current_date.day) < (dob.month, dob.day)) + + return age \ No newline at end of file -- GitLab