Skip to content
Snippets Groups Projects
Commit dc231a3e authored by hra23's avatar hra23
Browse files

Merge branch 'server' into 'master'

Server interaction with Simulator

See merge request !1
parents 125b56e3 c8d27bf0
Branches
No related tags found
1 merge request!1Server interaction with Simulator
*.DS_Store
*__pycache__
\ No newline at end of file
MLLP_START_CHAR = b"\x0b"
MLLP_END_CHAR = b"\x1c\x0d"
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()
pandas==2.0.3
scikit-learn==1.3.2
matplotlib==3.5.3
numpy==1.24.4
joblib==1.3.2
hl7==0.4.5
\ No newline at end of file
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
import unittest
from utils import (
process_mllp_message,
parse_hl7_message,
create_acknowledgement,
)
import hl7
class TestUtilsClient(unittest.TestCase):
def test_process_mllp_message(self):
"""
Test processing of MLLP messages.
"""
mllp_message = b"\x0bMSH|^~\&|SIMULATION|SOUTH RIVERSIDE|||20240212131600||ADT^A01|||2.5\x1c\x0d"
expected_result = (
b"MSH|^~\&|SIMULATION|SOUTH RIVERSIDE|||20240212131600||ADT^A01|||2.5"
)
self.assertEqual(process_mllp_message(mllp_message), expected_result)
def test_parse_hl7_message(self):
"""
Test parsing of HL7 messages.
"""
hl7_message = (
"MSH|^~\&|SIMULATION|SOUTH RIVERSIDE|||20240212131600||ADT^A01|||2.5"
).encode()
parsed_message = parse_hl7_message(hl7_message)
self.assertIsInstance(parsed_message, hl7.Message)
self.assertTrue("MSH" in str(parsed_message))
def test_create_acknowledgement(self):
"""
Test creation of HL7 ACK messages.
"""
hl7_msg = hl7.parse(
"MSH|^~\&|SIMULATION|SOUTH RIVERSIDE|||20240212131600||ADT^A01|||2.5"
)
ack_message = create_acknowledgement(hl7_msg)
self.assertIn(b"MSH", ack_message)
self.assertIn(b"ACK", ack_message)
self.assertIn(b"MSA|AA|", ack_message)
if __name__ == "__main__":
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment