-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·1540 lines (1379 loc) · 63.3 KB
/
server.py
File metadata and controls
executable file
·1540 lines (1379 loc) · 63.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
ASTM LIS2-A2 Mock Server for OpenELIS Analyzer Testing
This server simulates an ASTM-compatible laboratory analyzer for testing
the OpenELIS analyzer field mapping feature.
Reference Documents:
- specs/004-astm-analyzer-mapping/research.md Section 1 (ASTM Protocol)
- specs/004-astm-analyzer-mapping/spec.md FR-001, FR-002
ASTM LIS2-A2 Protocol Overview:
1. Client sends ENQ (0x05) to initiate communication
2. Server responds with ACK (0x06) if ready
3. Client sends data frames: <STX><FN><data><ETX/ETB><checksum><CR><LF>
4. Server ACKs each frame
5. Client sends EOT (0x04) to end transmission
6. Roles can reverse for bidirectional communication
Usage:
python server.py [--port PORT] [--analyzer-type TYPE]
Environment Variables:
ASTM_PORT: Server port (default: 5000)
ANALYZER_TYPE: Analyzer type from fields.json (default: HEMATOLOGY)
RESPONSE_DELAY_MS: Simulated response delay in milliseconds (default: 100)
"""
import select
import re
import socket
import threading
import uuid
import json
import os
import sys
import time
import logging
import argparse
from typing import Optional, Dict, List, Any
from protocols.astm_handler import generate_astm_message, ASTMHandler
from protocols.hl7_handler import HL7Handler, generate_oru_r01
from protocols.mllp_listener import MLLPProtocolHandler
from protocols.serial_handler import SerialHandler, send_astm_over_serial
from protocols.file_handler import FileHandler
from push import push_hl7_http, push_astm_to_destination
from api import start_api_server
# Optional: template loader for HL7 --hl7 push and /simulate/hl7 API (Abbott, etc.)
try:
from template_loader import TemplateLoader
HAS_HL7_SIM = True
except ImportError:
TemplateLoader = None
HAS_HL7_SIM = False
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# ASTM LIS2-A2 Control Characters
ENQ = b'\x05' # Enquiry - Start transmission
ACK = b'\x06' # Acknowledge - Positive response
NAK = b'\x15' # Negative Acknowledge
EOT = b'\x04' # End of Transmission
STX = b'\x02' # Start of Text (frame start)
ETX = b'\x03' # End of Text (frame end with checksum)
ETB = b'\x17' # End of Text Block (intermediate frame)
CR = b'\x0D' # Carriage Return
LF = b'\x0A' # Line Feed
VT = b'\x0B' # MLLP Start Block
FS = b'\x1C' # MLLP End Block
# Server Configuration
DEFAULT_PORT = 5000
DEFAULT_ANALYZER_TYPE = 'HEMATOLOGY'
DEFAULT_RESPONSE_DELAY_MS = 100
# CLSI LIS1-A Timeout Requirements
ESTABLISHMENT_TIMEOUT = 15 # seconds - ENQ response timeout
FRAME_ACK_TIMEOUT = 15 # seconds - Frame ACK timeout
RECEIVER_TIMEOUT = 30 # seconds - Receiver waiting for frame/EOT
SOCKET_TIMEOUT = 60 # seconds - Overall socket timeout (keep for safety)
MAX_CONNECTIONS = 10
# Restricted characters per CLSI LIS1-A 8.6
RESTRICTED_CHARS = [
b'\x01', # SOH
b'\x02', # STX
b'\x03', # ETX
b'\x04', # EOT
b'\x05', # ENQ
b'\x06', # ACK
b'\x10', # DLE
b'\x15', # NAK
b'\x16', # SYN
b'\x17', # ETB
b'\x0A', # LF (only allowed as last char of frame)
b'\x11', # DC1
b'\x12', # DC2
b'\x13', # DC3
b'\x14', # DC4
]
class ASTMProtocolHandler:
"""Handles ASTM LIS2-A2 protocol communication for a single client.
Supports both legacy fields.json and template-driven ASTM generation.
When astm_template is provided (or resolved from local_port + port_to_template),
field query responses and data generation use ASTMHandler.generate() for
spec-compliant messages (GeneXpert, etc.).
"""
def __init__(self, conn: socket.socket, addr: tuple, fields_config: Dict,
response_delay_ms: int = DEFAULT_RESPONSE_DELAY_MS,
astm_template: Optional[Dict] = None,
local_port: Optional[int] = None,
port_to_template: Optional[Dict[int, str]] = None):
self.conn = conn
self.addr = addr
self.fields_config = fields_config
self.response_delay_ms = response_delay_ms
if port_to_template and local_port is not None:
template_name = port_to_template.get(local_port) or os.environ.get("ASTM_TEMPLATE")
self.astm_template = _load_template(template_name) if template_name else None
if self.astm_template and template_name:
name = self.astm_template.get("analyzer", {}).get("name", template_name)
logger.info("Connection on port %s: using template %s (%s)", local_port, template_name, name)
else:
self.astm_template = astm_template
self.frame_number = 0
self.last_accepted_frame = 0 # Track last accepted frame number per CLSI LIS1-A
self.retransmit_count = 0 # Track retransmissions per CLSI LIS1-A
self.received_data: List[bytes] = []
self.received_orders: List[Dict[str, str]] = [] # inbound LIS orders awaiting result push
self.running = True
def handle(self):
"""Main handler loop for client connection."""
logger.info(f"Client connected: {self.addr}")
self.conn.settimeout(SOCKET_TIMEOUT)
# If template has proactive_enq, send ENQ immediately (like real
# GeneXpert which has queued results). This creates contention if the
# client also sends ENQ — matching real instrument behavior per
# CLSI LIS1-A §8.2.7.1.
if self.astm_template and self.astm_template.get('astm_config', {}).get('proactive_enq'):
logger.info(f"[PROACTIVE_ENQ] Sending ENQ to {self.addr} (instrument has data)")
self._send(ENQ)
try:
response = self._receive_byte()
if response == ACK:
# Client accepted — send our data
self._send_frames_from_template()
elif response == ENQ:
# CONTENTION: both sides sent ENQ. Yield to client — when the
# LIS has active work (orders), it takes priority over our
# queued results. ACK the client's ENQ so it proceeds to send
# its frames; the normal receive loop processes them and
# send_order_response pushes results back. The template's
# proactive push is forfeit for this connection.
logger.info(f"[PROACTIVE_ENQ] Contention with {self.addr}, yielding to client")
self._send(ACK)
# Fall through to normal receive loop regardless
except socket.timeout:
logger.debug(f"[PROACTIVE_ENQ] No response, entering receive mode")
try:
while self.running:
data = self._receive_byte()
if not data:
break
if data == ENQ:
self._handle_enq()
elif data == EOT:
self._handle_eot()
elif data == STX:
self._handle_frame()
else:
logger.warning(f"Unexpected byte received: {data.hex()}")
except socket.timeout:
logger.info(f"Client timeout: {self.addr}")
except ConnectionResetError:
logger.info(f"Client disconnected: {self.addr}")
except Exception as e:
logger.error(f"Error handling client {self.addr}: {e}")
finally:
self._cleanup()
def _receive_byte(self) -> Optional[bytes]:
"""Receive a single byte from client."""
try:
return self.conn.recv(1)
except socket.timeout:
return None
def _receive_until(self, terminator: bytes, max_length: int = 4096) -> bytes:
"""Receive data until terminator sequence."""
data = b''
while len(data) < max_length:
byte = self._receive_byte()
if not byte:
break
data += byte
if data.endswith(terminator):
break
return data
def _send(self, data: bytes):
"""Send data to client with optional delay."""
if self.response_delay_ms > 0:
time.sleep(self.response_delay_ms / 1000.0)
self.conn.send(data)
def _handle_enq(self):
"""Handle ENQ (enquiry) - client wants to send data."""
logger.debug(f"Received ENQ from {self.addr}")
# Per CLSI LIS1-A: Must respond within establishment timeout
self._send(ACK)
logger.debug(f"Sent ACK to {self.addr}")
def _handle_eot(self):
"""Handle EOT (end of transmission)."""
logger.debug(f"Received EOT from {self.addr}")
# Process any received data
if self.received_data:
self._process_received_data()
# Connection remains open for potential next transmission
def _handle_frame(self):
"""Handle incoming ASTM data frame."""
# Read frame content until ETX/ETB + checksum + CR + LF
frame_data = self._receive_until(CR + LF)
if not frame_data:
logger.warning("Empty frame received")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
# Parse frame: <FN><data><ETX/ETB><checksum>
if len(frame_data) < 4: # Minimum: FN + ETX + checksum(2) + CRLF
logger.warning(f"Frame too short: {len(frame_data)}")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
# Extract frame content (between STX and checksum)
# Frame number is first byte after STX
frame_num_bytes = frame_data[0:1]
# Extract frame number (ASCII digit '1'-'7')
try:
frame_num_char = frame_num_bytes.decode('ascii', errors='strict')
if not frame_num_char.isdigit():
logger.warning(f"Invalid frame number: {frame_num_char}")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
frame_num = int(frame_num_char)
except (UnicodeDecodeError, ValueError) as e:
logger.warning(f"Invalid frame number encoding: {e}")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
# Validate frame number per CLSI LIS01-A2 §6.3.2.1:
# Frame numbers 0-7, begin at 1, increment by 1, wrap 7→0.
if self.last_accepted_frame == 0:
# First frame — accept any valid frame number (0-7)
if frame_num < 0 or frame_num > 7:
logger.warning(f"Invalid frame number range: {frame_num} (must be 0-7)")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
else:
# Subsequent frames: must be same as last accepted OR one higher (mod 8)
expected_frame = (self.last_accepted_frame + 1) % 8
if frame_num != self.last_accepted_frame and frame_num != expected_frame:
logger.warning(f"Frame number mismatch: expected {expected_frame} or {self.last_accepted_frame}, got {frame_num}")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
# Find ETX or ETB position
etx_pos = frame_data.find(ETX)
etb_pos = frame_data.find(ETB)
if etx_pos == -1 and etb_pos == -1:
logger.warning("No frame terminator found")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
term_pos = etx_pos if etx_pos != -1 else etb_pos
content = frame_data[1:term_pos]
# Validate message characters per CLSI LIS1-A 8.6
if not self._validate_message_chars(content):
logger.warning("Restricted characters found in message text")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
# Extract checksum (2 hex digits after ETX/ETB)
checksum_start = term_pos + 1
if len(frame_data) < checksum_start + 2:
logger.warning("Incomplete checksum in frame")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
checksum_str = frame_data[checksum_start:checksum_start+2].decode('ascii', errors='ignore')
# Calculate expected checksum: sum of bytes from frame number to ETX, mod 256
checksum_data = frame_data[0:term_pos+1] # FN + content + ETX
calculated_checksum = sum(checksum_data) % 256
expected_checksum_str = f'{calculated_checksum:02X}'
if checksum_str.upper() != expected_checksum_str:
logger.warning(f"Checksum mismatch: expected {expected_checksum_str}, got {checksum_str}")
self._send(NAK)
self.retransmit_count += 1
if self.retransmit_count >= 6:
logger.error("Aborting: Frame retransmitted 6 times without success")
self._send(EOT)
self.running = False
return
# Frame is valid - reset retransmit counter and update last accepted frame
self.retransmit_count = 0
self.last_accepted_frame = frame_num
logger.debug(f"Received frame {frame_num}: {content[:50]}...")
self.received_data.append(content)
self._send(ACK)
def _process_received_data(self):
"""Process accumulated received data."""
logger.info(f"[MESSAGE] Processing {len(self.received_data)} frames from {self.addr}")
# Detect query type before processing/clearing data.
# - Field query: header-only capability discovery (H + L)
# - Results query: explicit Q-record request (H + Q + L)
is_field_query = self._is_field_query()
is_results_query = self._is_results_query()
logger.debug(
f"[MESSAGE] Query detection: is_field_query={is_field_query}, "
f"is_results_query={is_results_query}"
)
for frame_idx, frame in enumerate(self.received_data, 1):
try:
decoded = frame.decode('utf-8', errors='replace')
logger.debug(f"[MESSAGE] Frame {frame_idx}/{len(self.received_data)}: {decoded[:100]}")
# Parse ASTM records (optional X|1|template_name| overrides port-based template)
if decoded.startswith('X|'):
self._process_template_hint(decoded)
elif decoded.startswith('H|'):
self._process_header(decoded)
elif decoded.startswith('P|'):
self._process_patient(decoded)
elif decoded.startswith('O|'):
self._process_order(decoded)
elif decoded.startswith('R|'):
self._process_result(decoded)
elif decoded.startswith('Q|'):
self._process_query_record(decoded)
elif decoded.startswith('L|'):
self._process_terminator(decoded)
except Exception as e:
logger.error(f"[MESSAGE] Error processing frame {frame_idx}: {e}", exc_info=True)
# Snapshot orders before clearing for response dispatch
orders_snapshot = list(self.received_orders)
self.received_orders = []
# Clear received data
self.received_data = []
# Respond to query / order if detected
if is_results_query:
logger.info(f"[MESSAGE] Results query detected from {self.addr}, sending template results")
self.send_results_query_response()
elif is_field_query:
logger.info(f"[MESSAGE] Field query detected from {self.addr}, sending field list")
self.send_field_query_response()
elif orders_snapshot:
logger.info(
f"[ORDER_IN] {len(orders_snapshot)} order record(s) from {self.addr}, pushing results"
)
self.send_order_response(orders_snapshot)
else:
logger.debug(f"[MESSAGE] No query/order detected, no response needed")
def _process_template_hint(self, record: str):
"""Process X (template hint) record: X|1|template_name| overrides port-based template."""
parts = record.strip().split('|')
if len(parts) >= 3 and parts[2].strip():
name = parts[2].strip()
t = _load_template(name)
if t and t.get('protocol', {}).get('type') == 'ASTM':
self.astm_template = t
logger.info("Template hint: using %s", name)
else:
logger.warning("Template hint unknown or not ASTM: %s", name)
def _process_header(self, record: str):
"""Process H (Header) record."""
logger.info(f"Header record received: {record[:60]}...")
def _process_patient(self, record: str):
"""Process P (Patient) record."""
parts = record.split('|')
patient_id = parts[3] if len(parts) > 3 else 'Unknown'
logger.info(f"Patient record: ID={patient_id}")
def _process_order(self, record: str):
"""Process O (Order) record. Tracks accession + requested test code for
an inbound LIS order so send_order_response can echo a matching result.
ASTM universal-test-id is field 5 (parts[4]); format `^^^TESTCODE` per
LIS2-A2 §7.4.4."""
parts = record.split('|')
sample_id = parts[2] if len(parts) > 2 else ''
test_universal_id = parts[4] if len(parts) > 4 else ''
test_components = test_universal_id.split('^')
test_code = test_components[3] if len(test_components) > 3 else ''
logger.info(f"Order record: Sample={sample_id} Test={test_code}")
if sample_id and test_code:
self.received_orders.append({
'sample_id': sample_id,
'test_code': test_code,
})
def _process_result(self, record: str):
"""Process R (Result) record."""
parts = record.split('|')
if len(parts) >= 4:
test_code = parts[2] if len(parts) > 2 else 'Unknown'
value = parts[3] if len(parts) > 3 else ''
unit = parts[4] if len(parts) > 4 else ''
logger.info(f"Result record: {test_code} = {value} {unit}")
def _process_query_record(self, record: str):
"""Process Q (ASTM query) record."""
parts = record.split('|')
sample_id = parts[2] if len(parts) > 2 else ""
requested_tests = parts[4] if len(parts) > 4 else "ALL"
logger.info(
f"Query record: sample={sample_id or 'unknown'} tests={requested_tests or 'ALL'}"
)
def _process_terminator(self, record: str):
"""Process L (Terminator) record."""
logger.debug("Message terminator received")
def _is_field_query(self) -> bool:
"""Detect if received message is a field query request.
Query is detected when:
- Header (H) record is present
- No explicit Q record
- No Patient (P) or Order (O) records follow
- Only header + terminator received
"""
if not self.received_data:
return False
has_header = False
has_patient_or_order = False
has_query_record = False
for frame in self.received_data:
try:
decoded = frame.decode('utf-8', errors='replace')
if decoded.startswith('H|'):
has_header = True
elif decoded.startswith('P|') or decoded.startswith('O|'):
has_patient_or_order = True
elif decoded.startswith('Q|'):
has_query_record = True
except:
pass
return has_header and not has_patient_or_order and not has_query_record
def _is_results_query(self) -> bool:
"""Detect if received message is an ASTM results query (H + Q + L)."""
if not self.received_data:
return False
has_header = False
has_query_record = False
has_patient_or_order = False
for frame in self.received_data:
try:
decoded = frame.decode('utf-8', errors='replace')
if decoded.startswith('H|'):
has_header = True
elif decoded.startswith('Q|'):
has_query_record = True
elif decoded.startswith('P|') or decoded.startswith('O|'):
has_patient_or_order = True
except:
pass
return has_header and has_query_record and not has_patient_or_order
def _validate_message_chars(self, content: bytes) -> bool:
"""Validate message text doesn't contain restricted characters per CLSI LIS1-A 8.6."""
# LF is allowed as last character of frame, so check content without trailing LF
content_to_check = content.rstrip(LF)
for restricted in RESTRICTED_CHARS:
if restricted in content_to_check:
logger.warning(f"Restricted character found in message: {restricted.hex()}")
return False
return True
def send_field_query_response(self):
"""Send available fields or a full template-based ASTM message in response to a query.
When astm_template is set (via ASTM_TEMPLATE env var), generates a full
spec-compliant ASTM message using ASTMHandler. This is the pull-based flow:
bridge connects as client, mock responds with template-generated data.
When no template is set, falls back to legacy field list response.
"""
logger.info(f"[FIELD_QUERY] Sending response to {self.addr}")
# Initiate transmission: send ENQ and wait for ACK
logger.debug(f"[FIELD_QUERY] Sending ENQ to initiate response")
self._send(ENQ)
try:
response = self._receive_byte()
if response != ACK:
logger.warning(f"[FIELD_QUERY] Did not receive ACK, got: {response.hex() if response else 'none'}")
return
logger.debug(f"[FIELD_QUERY] Received ACK, proceeding")
except socket.timeout:
logger.warning("[FIELD_QUERY] Timeout waiting for ACK")
return
if self.astm_template:
# Template mode: generate full ASTM message and send as framed records
logger.info(f"[FIELD_QUERY] Using template: {self.astm_template.get('analyzer', {}).get('name', 'unknown')}")
try:
message = ASTMHandler().generate(self.astm_template, use_seed=True)
records = [r for r in message.strip().split('\n') if r.strip()]
for i, record in enumerate(records):
if not self._send_frame(record.strip()):
logger.warning(f"[FIELD_QUERY] Send failed at record {i+1}/{len(records)}")
break
logger.info(f"[FIELD_QUERY] Sent {len(records)} template records to {self.addr}")
except Exception as e:
logger.error(f"[FIELD_QUERY] Template generation failed: {e}", exc_info=True)
# Fall through to EOT
else:
# Legacy mode: send field list from fields.json
header_record = f"H|\\^&|||MockAnalyzer^ASTM-Mock^1.0|||||||LIS2-A2"
self._send_frame(header_record)
frame_seq = 1
analyzer_type = os.getenv('ANALYZER_TYPE', DEFAULT_ANALYZER_TYPE)
fields = self.fields_config.get(analyzer_type, [])
if not fields and self.fields_config:
analyzer_type = list(self.fields_config.keys())[0]
fields = self.fields_config[analyzer_type]
logger.info(f"[FIELD_QUERY] Sending {len(fields)} fields for {analyzer_type}")
for field in fields:
field_name = field.get('name', 'Unknown')
display_name = field.get('displayName', field_name)
field_type = field.get('type', 'NUMERIC')
unit = field.get('unit', '')
astm_ref = field.get('astmRef', f'^^^{field_name}')
if display_name != field_name and '^' not in astm_ref:
test_id = f"{astm_ref}^{display_name}"
else:
test_id = astm_ref
record = f"R|{frame_seq}|{test_id}||{unit}|||{field_type}"
if not self._send_frame(record):
logger.warning(f"[FIELD_QUERY] Send failed at frame {frame_seq}")
break
frame_seq += 1
if not self._send_frame("L|1|N"):
logger.warning("[FIELD_QUERY] Terminator frame send failed")
# End transmission
self._send(EOT)
logger.info(f"[FIELD_QUERY] Response complete for {self.addr}")
def send_results_query_response(self):
"""Send a template-backed P/O/R response for H+Q results-query flows."""
logger.info(f"[RESULTS_QUERY] Sending response to {self.addr}")
self._send(ENQ)
try:
response = self._receive_byte()
if response != ACK:
logger.warning(
f"[RESULTS_QUERY] Did not receive ACK, got: {response.hex() if response else 'none'}"
)
return
except socket.timeout:
logger.warning("[RESULTS_QUERY] Timeout waiting for ACK")
return
try:
if self.astm_template:
message = ASTMHandler().generate(self.astm_template, use_seed=True)
records = [r for r in message.strip().split('\n') if r.strip()]
else:
records = [
"H|\\^&|||MockAnalyzer^ASTM-Mock^1.0|||||||LIS2-A2",
"P|1|PAT-RESULTS||DOE^JANE",
"O|1|ACC-RESULTS",
"R|1|^^^GLUCOSE|102.5|mg/dL",
"L|1|N",
]
for i, record in enumerate(records):
if not self._send_frame(record.strip()):
logger.warning(
f"[RESULTS_QUERY] Send failed at record {i + 1}/{len(records)}"
)
break
logger.info(f"[RESULTS_QUERY] Sent {len(records)} records to {self.addr}")
finally:
self._send(EOT)
logger.info(f"[RESULTS_QUERY] Response complete for {self.addr}")
def send_order_response(self, orders: List[Dict[str, str]]):
"""After receiving an inbound LIS order, push matching result records
back via a FRESH TCP connection to the bridge's ASTM listener. The
originating sample_id is echoed verbatim in the response O-record
(ASTM field 3) — that is the protocol-native correlation OpenELIS's
inbound result import keys on.
Same-connection push doesn't work end-to-end: the bridge's outbound
forwarder closes the TCP socket after sending EOT, so any response
sent on `self.conn` is dropped. Mirrors the HL7 path which already
opens a fresh MLLP connection back to bridge:2575 (see
_push_order_result in protocols/mllp_listener.py).
Destination defaults to the bridge's inbound ASTM listener:
ORDER_RESULT_PUSH_HOST=openelis-analyzer-bridge
ORDER_RESULT_PUSH_ASTM_PORT=12001
Empty host disables the push (useful for standalone dev runs).
Result values come from the loaded ASTM template's
`fields[].seedValue` (NUMERIC) or `seedQualitative` (other). A test code
not in this analyzer's profile gets an error-flagged result record (ASTM
result status X = cannot obtain result) rather than being silently
omitted or failing the whole response — so the mismatch stays visible.
"""
if not self.astm_template:
logger.warning("[ORDER_IN] No template loaded; cannot generate result records")
return
host = os.environ.get("ORDER_RESULT_PUSH_HOST", "openelis-analyzer-bridge")
port_raw = os.environ.get("ORDER_RESULT_PUSH_ASTM_PORT", "12001")
if not host:
logger.info("[ORDER_IN] ORDER_RESULT_PUSH_HOST empty; ASTM result push disabled")
return
try:
port = int(port_raw)
except (TypeError, ValueError):
logger.warning(
"[ORDER_IN] ORDER_RESULT_PUSH_ASTM_PORT=%r invalid; skipping push", port_raw
)
return
template_fields = {f.get('code'): f for f in self.astm_template.get('fields', [])}
test_patient = self.astm_template.get('testPatient', {})
patient_id = test_patient.get('id', 'PAT-ORDER')
patient_name = test_patient.get('name', 'DOE^JOHN')
# Group orders by sample so a multi-test order shares one P/O block.
# dict preserves insertion order in Python 3.7+.
samples: Dict[str, List[str]] = {}
for order in orders:
samples.setdefault(order['sample_id'], []).append(order['test_code'])
# Stamp the analyzer's own identity in the H-record sender field so the
# bridge can corroborate it against the connection source IP (mirrors
# ASTMHandler.generate). A hardcoded sender would defeat content-based
# identity on the order-response path.
ident = self.astm_template.get("identification", {})
astm_header = ident.get("astm_header")
if not astm_header:
anal = self.astm_template.get("analyzer", {})
astm_header = (
f"{anal.get('manufacturer', '')}^{anal.get('model', '')}^{anal.get('name', '')}".strip("^")
or anal.get("name", "MockAnalyzer")
)
records: List[str] = [
f"H|\\^&|||{astm_header}|||||||LIS2-A2",
]
patient_seq = 1
order_seq = 1
result_seq = 1
for sample_id, test_codes in samples.items():
records.append(f"P|{patient_seq}|{patient_id}||{patient_name}")
records.append(f"O|{order_seq}|{sample_id}|||^^^|R")
patient_seq += 1
order_seq += 1
for test_code in test_codes:
field = template_fields.get(test_code)
if not field:
# Ordered a test this analyzer can't run. A faithful analyzer
# reports that (result status X = cannot obtain result) rather
# than silently omitting it, so the mismatch is visible.
logger.warning(
f"[ORDER_IN] Ordered test {test_code} not in analyzer profile; "
f"emitting error result (status X)"
)
records.append(f"R|{result_seq}|^^^{test_code}|TEST NOT PERFORMED|||||X")
result_seq += 1
continue
if field.get('type') == 'NUMERIC':
value = field.get('seedValue', '')
unit = field.get('unit', '')
else:
value = field.get('seedQualitative', '')
unit = ''
records.append(f"R|{result_seq}|^^^{test_code}|{value}|{unit}||||F")
result_seq += 1
records.append("L|1|N")
# push_astm_tcp opens a new TCP connection, performs ENQ/ACK/frames/EOT,
# and returns True on success. Send each record as a single line; the
# session helper handles framing.
from push import push_astm_tcp
message = "\n".join(records)
# Source the push from the interface the order arrived on (this analyzer's
# IP = the inbound connection's LOCAL address). The mock is attached to one
# network per analyzer; the bridge identifies the source analyzer by the
# push's source IP, so an unbound push from an arbitrary interface gets
# mis-attributed. NOTE: self.conn.getsockname() (local), not self.addr (the
# bridge's remote address).
try:
source_ip = self.conn.getsockname()[0]
except OSError:
source_ip = None
logger.info(
f"[ORDER_IN] Pushing {len(records)} ASTM result records for {len(orders)} order(s) "
f"to {host}:{port} (source={source_ip})"
)
try:
ok, push_err = push_astm_tcp(host, port, message, timeout=10, source_ip=source_ip)
if ok:
logger.info(
f"[ORDER_IN] Pushed ASTM result message to {host}:{port}"
)
else:
logger.warning(
f"[ORDER_IN] push_astm_tcp returned false for {host}:{port}: {push_err}"
)
except Exception as e:
logger.error(
f"[ORDER_IN] Exception pushing ASTM result to {host}:{port}: {e}",
exc_info=True,
)
def _send_frames_from_template(self):
"""Send template data as ASTM frames (ENQ/ACK already established).
Used by proactive ENQ flow where the handshake is handled by the caller.
"""
if self.astm_template:
message = ASTMHandler().generate(self.astm_template, use_seed=True)
records = [r for r in message.strip().split('\n') if r.strip()]
for i, record in enumerate(records):
if not self._send_frame(record.strip()):
logger.warning(f"[PROACTIVE_ENQ] Send failed at record {i+1}/{len(records)}")
break
logger.info(f"[PROACTIVE_ENQ] Sent {len(records)} records to {self.addr}")
self._send(EOT)
def _send_frame(self, content: str):
"""Send an ASTM frame with proper framing."""
# CLSI LIS01-A2 §6.3.2.1: Frame numbers 0-7, begin at 1, wrap 7→0.
# Sequence: 1,2,3,4,5,6,7,0,1,2,... Formula: (index + 1) % 8
self.frame_number = (self.frame_number + 1) % 8
# Build frame: <STX><FN><content><ETX><checksum><CR><LF>
frame_num = str(self.frame_number).encode()
content_bytes = content.encode('utf-8')
# Calculate checksum (sum of bytes from frame num to ETX, mod 256)
checksum_data = frame_num + content_bytes + ETX
checksum = sum(checksum_data) % 256
checksum_str = f'{checksum:02X}'.encode()
frame = STX + frame_num + content_bytes + ETX + checksum_str + CR + LF
self.conn.send(frame)
# Wait for ACK with proper timeout per CLSI LIS1-A
self.conn.settimeout(FRAME_ACK_TIMEOUT)
try:
response = self.conn.recv(1)
if response == EOT:
# Receiver interrupt request per CLSI LIS1-A 8.3.5
logger.info("Receiver interrupt requested (EOT received)")
return False # Signal to stop sending
elif response != ACK:
logger.warning(f"Frame not ACKed: {response.hex() if response else 'none'}")
# Per CLSI LIS1-A: Should retransmit on NAK, abort after 6 failures
return False
except socket.timeout:
logger.warning("Timeout waiting for frame ACK (15s limit per CLSI LIS1-A)")
return False
return True
def _cleanup(self):
"""Clean up connection resources."""
try:
self.conn.close()
except:
pass
logger.info(f"Client disconnected: {self.addr}")
class ASTMMockServer:
"""ASTM LIS2-A2 Mock Server for analyzer testing.
Supports template-driven mode via ASTM_TEMPLATE env var (single port) or
port-to-template mapping (multi-port). When a connection is accepted, the
template is selected by the port (request-based); the handler loads the
template and processes the message accordingly.
"""
def __init__(self, port: int = DEFAULT_PORT,
analyzer_type: str = DEFAULT_ANALYZER_TYPE,
response_delay_ms: int = DEFAULT_RESPONSE_DELAY_MS,
port_to_template: Optional[Dict[int, str]] = None):
self.port = port
self.analyzer_type = analyzer_type
self.response_delay_ms = response_delay_ms
self.fields_config = self._load_fields_config()
if port_to_template is None:
port_to_template = _load_port_templates(port)
self.port_to_template = port_to_template
if port_to_template:
self.ports = sorted(port_to_template.keys())
self.astm_template = None
else:
self.ports = [port]
self.astm_template = self._load_astm_template()
self.running = False
self.server_sockets: List[socket.socket] = []
self.client_threads: List[threading.Thread] = []
def _load_astm_template(self) -> Optional[Dict]:
"""Load ASTM template from ASTM_TEMPLATE env var if set."""
template_name = os.environ.get('ASTM_TEMPLATE')
if not template_name:
return None
template = _load_template(template_name)
if template:
proto = template.get('protocol', {}).get('type', '')
name = template.get('analyzer', {}).get('name', template_name)
if proto != 'ASTM':
logger.warning(f"ASTM_TEMPLATE={template_name} is not ASTM protocol ({proto}), ignoring")
return None
logger.info(f"Loaded ASTM template: {name} ({template_name})")
else:
logger.warning(f"ASTM_TEMPLATE={template_name} not found in templates/")
return template
def _load_fields_config(self) -> Dict:
"""Load analyzer field configuration from JSON file."""
config_path = os.path.join(os.path.dirname(__file__), 'fields.json')
if os.path.exists(config_path):
try:
with open(config_path, 'r') as f:
return json.load(f)
except Exception as e:
logger.error(f"Error loading fields.json: {e}")
# Return default configuration if file not found
return self._get_default_fields()
def _get_default_fields(self) -> Dict:
"""Return default field configuration."""
return {
"HEMATOLOGY": [
{"name": "WBC", "astmRef": "R|1|^^^WBC", "type": "NUMERIC", "unit": "10^3/μL"},
{"name": "RBC", "astmRef": "R|1|^^^RBC", "type": "NUMERIC", "unit": "10^6/μL"},
{"name": "HGB", "astmRef": "R|1|^^^HGB", "type": "NUMERIC", "unit": "g/dL"},
{"name": "HCT", "astmRef": "R|1|^^^HCT", "type": "NUMERIC", "unit": "%"},
{"name": "PLT", "astmRef": "R|1|^^^PLT", "type": "NUMERIC", "unit": "10^3/μL"}
],
"CHEMISTRY": [
{"name": "Glucose", "astmRef": "R|1|^^^GLUCOSE", "type": "NUMERIC", "unit": "mg/dL"},
{"name": "Creatinine", "astmRef": "R|1|^^^CREATININE", "type": "NUMERIC", "unit": "mg/dL"}
]
}
def start(self):
"""Start the mock server (single or multi-port)."""
self.running = True
try:
if self.port_to_template:
self._start_multi_port()
else:
self._start_single_port()
except KeyboardInterrupt:
logger.info("Server shutdown requested")
except Exception as e:
logger.error(f"Server error: {e}")
finally:
self.stop()
def _start_single_port(self):
"""Single-port mode: one socket, one template (legacy)."""
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', self.port))
sock.listen(MAX_CONNECTIONS)
self.server_sockets.append(sock)
logger.info("ASTM Mock Server started on port %s", self.port)
logger.info("Analyzer type: %s", self.analyzer_type)
logger.info("Response delay: %sms", self.response_delay_ms)
while self.running:
try:
sock.settimeout(1.0)
conn, addr = sock.accept()
handler = ASTMProtocolHandler(
conn, addr, self.fields_config, self.response_delay_ms,
astm_template=self.astm_template
)
thread = threading.Thread(target=handler.handle, daemon=True)
thread.start()
self.client_threads.append(thread)
except socket.timeout:
continue
def _resolve_protocol_for_port(self, port: int) -> str:
"""Determine protocol type for a port by checking its template.
Returns 'HL7' or 'ASTM' (default). Both are equal citizens —
the template's protocol.type field determines the handler.
"""
template_name = self.port_to_template.get(port)