Hello,
Actually, you can get metadata in the “OnStoredInstance” callback of Python. For instance:
import orthanc
def OnStoredInstance(dicom, instanceId):
print(dicom.GetInstanceMetadata(‘RemoteAET’))
orthanc.RegisterOnStoredInstanceCallback(OnStoredInstance)
However, as written in the documentation of the Python plugin:“Warning - Your callback function will be called synchronously with the core of Orthanc. This implies that deadlocks might emerge if you call other core primitives of Orthanc in your callback (such deadlocks are particular visible in the presence of other plugins or Lua scripts). It is thus strongly advised to avoid any call to the REST API of Orthanc in the callback. If you have to call other primitives of Orthanc, you should make these calls in a separate thread, passing the pending events to be processed through a message queue.” [Python plugin for Orthanc — Orthanc Book documentation]
As a consequence, here is a preferred way to implement auto-routing of instances using Python:
import json
import orthanc
ROUTING_AET = ‘HELLO’
TARGET = ‘sample’
def OnChange(changeType, level, resource):
if changeType == orthanc.ChangeType.NEW_INSTANCE:
On new instance whose source AET matches the AET of
interest, send it to another modality
metadata = json.loads(orthanc.RestApiGet(‘/instances/%s/metadata?expand’ % resource))
if metadata.get(‘RemoteAET’) == ROUTING_AET:
orthanc.RestApiPost(‘/modalities/%s/store’ % TARGET, resource)
elif changeType == orthanc.ChangeType.JOB_SUCCESS:
Delete the routed instance once it has properly been
received by the target modality
job = json.loads(orthanc.RestApiGet(‘/jobs/%s’ % resource))
instances = job[‘Content’][‘ParentResources’]
for instance in instances:
orthanc.RestApiDelete(‘/instances/%s’ % instance)
orthanc.RegisterOnChangeCallback(OnChange)
Also, check out the following sample in the Orthanc Book for a more efficient way of auto-routing, by sending studies as a whole:
HTH,
Sébastien-