Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
7
70102 Peace
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Midha, Rohit
70102 Peace
Commits
1b8aba5b
Commit
1b8aba5b
authored
1 year ago
by
RohitMidha23
Browse files
Options
Downloads
Patches
Plain Diff
added code to read from simulator socket
parent
51dae3e9
No related branches found
No related tags found
1 merge request
!1
Server interaction with Simulator
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.py
+38
-0
38 additions, 0 deletions
main.py
utils.py
+34
-0
34 additions, 0 deletions
utils.py
with
72 additions
and
0 deletions
main.py
0 → 100644
+
38
−
0
View file @
1b8aba5b
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
()
This diff is collapsed.
Click to expand it.
utils.py
0 → 100644
+
34
−
0
View file @
1b8aba5b
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
\r
MSA|AA|
\r
"
framed_ack
=
MLLP_START_CHAR
+
ack_msg
.
encode
()
+
MLLP_END_CHAR
return
framed_ack
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment