Skip to content
Snippets Groups Projects
Commit 1b8aba5b authored by RohitMidha23's avatar RohitMidha23
Browse files

added code to read from simulator socket

parent 51dae3e9
No related branches found
No related tags found
1 merge request!1Server interaction with Simulator
main.py 0 → 100644
import socket
from utils import process_mllp_message, parse_hl7_message, create_acknowledgement
def start_server(host="0.0.0.0", port=8440):
"""
Starts the TCP server to listen for incoming MLLP messages on the specified port.
"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.connect((host, port))
print(f"Connected to simulator on {host}:{port}")
while True:
data = sock.recv(1024)
if not data:
print("No data received. Closing connection.")
break
hl7_data = process_mllp_message(data)
if hl7_data:
message = parse_hl7_message(hl7_data)
print("Parsed HL7 Message:")
print(message)
# Create and send ACK message
ack_message = create_acknowledgement(message)
sock.sendall(ack_message)
else:
print("No valid MLLP message received.")
def main():
start_server()
if __name__ == "__main__":
main()
utils.py 0 → 100644
import socket
import hl7
import datetime
from constants import MLLP_START_CHAR, MLLP_END_CHAR
def process_mllp_message(data):
"""
Extracts the HL7 message from the MLLP data.
"""
start_index = data.find(MLLP_START_CHAR)
end_index = data.find(MLLP_END_CHAR)
if start_index != -1 and end_index != -1:
return data[start_index + 1 : end_index]
return None
def parse_hl7_message(hl7_data):
"""
Parses the HL7 message and returns the parsed message object.
"""
message = hl7.parse(hl7_data.decode("utf-8"))
return message
def create_acknowledgement(hl7_msg):
"""
Creates an HL7 ACK message for the received message.
"""
# Construct the ACK message based on the simulator's expectations
ack_msg = f"MSH|^~\\&|||||{datetime.datetime.now().strftime('%Y%m%d%H%M%S')}||ACK||P|2.5\rMSA|AA|\r"
framed_ack = MLLP_START_CHAR + ack_msg.encode() + MLLP_END_CHAR
return framed_ack
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment