changeset 852:450f0efebe4f

FilterIncomingCStoreInstance python sample
author Alain Mazy <am@osimis.io>
date Mon, 30 May 2022 17:16:15 +0200
parents f282da89c1c1
children 2d9024137da0
files Sphinx/source/plugins/python.rst Sphinx/source/plugins/python/incoming-cstore-filter.py
diffstat 2 files changed, 36 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst	Mon May 30 11:07:58 2022 +0200
+++ b/Sphinx/source/plugins/python.rst	Mon May 30 17:16:15 2022 +0200
@@ -784,6 +784,19 @@
                     :language: python
 
 
+Filtering incoming C-Store instances (new in 4.0)
+.................................................
+
+Starting with release 4.0 of the Python plugin, it is possible to
+filter instances received from C-Store and return a specific error
+code to the sending modality.
+
+This can be used, e.g, to implement a quota per modality or return 
+an ``out-of-resources`` status if the Orthanc storage is almost full.
+
+.. literalinclude:: python/incoming-cstore-filter.py
+                    :language: python
+
 
 Performance and concurrency
 ---------------------------
--- a/Sphinx/source/plugins/python/incoming-cstore-filter.py	Mon May 30 11:07:58 2022 +0200
+++ b/Sphinx/source/plugins/python/incoming-cstore-filter.py	Mon May 30 17:16:15 2022 +0200
@@ -1,15 +1,30 @@
 import json
 import orthanc
 
-def Filter(receivedDicom):
+
+# this script accepts 3 instances from STORESCU and then, rejects the next ones
+
+storeScuInstanceCounter = 0
+
+def FilterIncomingCStoreInstance(receivedDicom):
     # The list ofvalid status codes for DIMSE C-STORE can be found:
     # https://dicom.nema.org/medical/Dicom/2021e/output/chtml/part04/sect_B.2.3.html
 
-    tags = json.loads(receivedDicom.GetInstanceSimplifiedJson())
-    if tags['PatientID'].startswith('001-'):
-        # Non-zero return value: The DICOM instance is discarded
-        return 0xA900  # DIMSE Failure: Data Set does not match SOP Class
-    else:
-        return 0  # Success: Accept the DICOM instance
+    global storeScuInstanceCounter
+
+    origin = receivedDicom.GetInstanceOrigin()
+
+    if origin == orthanc.InstanceOrigin.DICOM_PROTOCOL:  # should always be true in the CStore callback !
 
-orthanc.RegisterIncomingCStoreInstanceFilter(Filter)
+        remoteAet = receivedDicom.GetInstanceRemoteAet()
+        
+        if remoteAet == "STORESCU":
+            storeScuInstanceCounter += 1
+
+        if storeScuInstanceCounter > 3:
+            # Non-zero return value: The DICOM instance is discarded
+            return 0xA700
+    
+    return 0  # Success: Accept the DICOM instance
+
+orthanc.RegisterIncomingCStoreInstanceFilter(FilterIncomingCStoreInstance)