-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
141 lines (116 loc) · 4.69 KB
/
Copy pathserver.py
File metadata and controls
141 lines (116 loc) · 4.69 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
"""PACS and MWL DICOM Servers
Provides:
- C-STORE SCP (Service Class Provider) for receiving DICOM images (PACS)
- C-FIND SCP for modality worklist management (MWL)
"""
import logging
import pydicom.uid as dicom_uid
from pynetdicom import AE, evt
from pynetdicom.sop_class import (
DigitalMammographyXRayImageStorageForPresentation, # type: ignore[attr-defined]
DigitalMammographyXRayImageStorageForProcessing, # type: ignore[attr-defined]
ModalityPerformedProcedureStep, # type: ignore[attr-defined]
ModalityWorklistInformationFind, # type: ignore[attr-defined]
)
from services.dicom.c_echo import CEcho
from services.dicom.c_store import CStore
from services.mwl.c_find import CFind
from services.mwl.n_create import NCreate
from services.mwl.n_set import NSet
from services.storage import MWLStorage, PACSStorage
logger = logging.getLogger(__name__)
class PACSServer:
"""DICOM PACS Server with C-STORE support."""
def __init__(
self,
ae_title: str = "SCREENING_PACS",
port: int = 4244,
storage_path: str = "/var/lib/pacs/storage",
db_path: str = "/var/lib/pacs/pacs.db",
block: bool = True,
mwl_db_path: str = "/var/lib/pacs/worklist.db",
):
"""
Initialize PACS server.
Args:
ae_title: Application Entity title
port: Port to listen on
storage_path: Directory for DICOM file storage
db_path: Path to SQLite database
mwl_db_path: Path to the MWL SQLite database (for failure notification lookups)
"""
self.ae_title = ae_title
self.port = port
self.storage = PACSStorage(db_path, storage_path)
self.mwl_storage = MWLStorage(mwl_db_path)
self.ae = None
self.block = block
def start(self):
"""Start the PACS server and listen for incoming connections."""
logger.info(f"Starting PACS server: {self.ae_title} on port {self.port}")
transfer_syntaxes = [
dicom_uid.JPEGLosslessSV1, # Hologic preferred
dicom_uid.ExplicitVRLittleEndian,
dicom_uid.ImplicitVRLittleEndian,
dicom_uid.ExplicitVRBigEndian,
dicom_uid.JPEGLSLossless,
dicom_uid.JPEG2000Lossless,
]
self.ae = AE(ae_title=self.ae_title)
self.ae.add_supported_context(DigitalMammographyXRayImageStorageForPresentation, transfer_syntaxes)
self.ae.add_supported_context(DigitalMammographyXRayImageStorageForProcessing, transfer_syntaxes)
handlers = [
(evt.EVT_C_ECHO, CEcho().call),
(evt.EVT_C_STORE, CStore(self.storage, mwl_storage=self.mwl_storage).call),
]
logger.info(f"PACS server listening on 0.0.0.0:{self.port}")
logger.info(f"Storage: {self.storage.storage_root}")
logger.info(f"Database: {self.storage.db_path}")
self.ae.start_server(("0.0.0.0", self.port), block=self.block, evt_handlers=handlers) # type: ignore
def stop(self):
"""Stop the PACS server."""
if self.ae:
logger.info("Stopping PACS server")
self.ae.shutdown()
self.storage.close()
class MWLServer:
"""DICOM Modality Worklist server with C-FIND support."""
def __init__(
self,
ae_title: str = "MWL_SCP",
port: int = 4243,
db_path: str = "/var/lib/pacs/worklist.db",
block: bool = True,
):
"""
Initialize MWL server.
Args:
ae_title: Application Entity Title
port: Port to listen on
db_path: Path to SQLite worklist database
block: Whether to block when starting server
"""
self.ae_title = ae_title
self.port = port
self.storage = MWLStorage(db_path)
self.ae = None
self.block = block
def start(self):
"""Start the MWL server."""
logger.info(f"Starting MWL server: {self.ae_title} on port {self.port}")
self.ae = AE(ae_title=self.ae_title)
self.ae.add_supported_context(ModalityWorklistInformationFind)
self.ae.add_supported_context(ModalityPerformedProcedureStep)
handlers = [
(evt.EVT_C_FIND, CFind(self.storage).call),
(evt.EVT_N_CREATE, NCreate(self.storage).call),
(evt.EVT_N_SET, NSet(self.storage).call),
]
logger.info(f"MWL server listening on 0.0.0.0:{self.port}")
logger.info(f"Database: {self.storage.db_path}")
self.ae.start_server(("0.0.0.0", self.port), block=self.block, evt_handlers=handlers) # type: ignore
def stop(self):
"""Stop the MWL server."""
if self.ae:
logger.info("Stopping MWL server")
self.ae.shutdown()