Mercurial > hg > orthanc
comparison OrthancServer/Resources/Samples/Python/ChangesLoop.py @ 4044:d25f4c0fa160 framework
splitting code into OrthancFramework and OrthancServer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jun 2020 20:30:34 +0200 |
parents | Resources/Samples/Python/ChangesLoop.py@94f4a18a79cc |
children | d9473bd5ed43 |
comparison
equal
deleted
inserted
replaced
4043:6c6239aec462 | 4044:d25f4c0fa160 |
---|---|
1 #!/usr/bin/python | |
2 | |
3 # Orthanc - A Lightweight, RESTful DICOM Store | |
4 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
5 # Department, University Hospital of Liege, Belgium | |
6 # Copyright (C) 2017-2020 Osimis S.A., Belgium | |
7 # | |
8 # This program is free software: you can redistribute it and/or | |
9 # modify it under the terms of the GNU General Public License as | |
10 # published by the Free Software Foundation, either version 3 of the | |
11 # License, or (at your option) any later version. | |
12 # | |
13 # This program is distributed in the hope that it will be useful, but | |
14 # WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 # General Public License for more details. | |
17 # | |
18 # You should have received a copy of the GNU General Public License | |
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | |
21 | |
22 | |
23 import time | |
24 import sys | |
25 import RestToolbox | |
26 | |
27 | |
28 ## | |
29 ## Print help message | |
30 ## | |
31 | |
32 if len(sys.argv) != 3: | |
33 print(""" | |
34 Sample script that continuously monitors the arrival of new DICOM | |
35 images into Orthanc (through the Changes API). | |
36 | |
37 Usage: %s [hostname] [HTTP port] | |
38 For instance: %s 127.0.0.1 8042 | |
39 """ % (sys.argv[0], sys.argv[0])) | |
40 exit(-1) | |
41 | |
42 URL = 'http://%s:%d' % (sys.argv[1], int(sys.argv[2])) | |
43 | |
44 | |
45 | |
46 ## | |
47 ## The following function is called each time a new instance is | |
48 ## received. | |
49 ## | |
50 | |
51 def NewInstanceReceived(path): | |
52 global URL | |
53 patientName = RestToolbox.DoGet(URL + path + '/content/PatientName') | |
54 | |
55 # Remove the possible trailing characters due to DICOM padding | |
56 patientName = patientName.strip() | |
57 | |
58 print('New instance received for patient "%s": "%s"' % (patientName, path)) | |
59 | |
60 | |
61 | |
62 ## | |
63 ## Main loop that listens to the changes API. | |
64 ## | |
65 | |
66 current = 0 | |
67 while True: | |
68 r = RestToolbox.DoGet(URL + '/changes', { | |
69 'since' : current, | |
70 'limit' : 4 # Retrieve at most 4 changes at once | |
71 }) | |
72 | |
73 for change in r['Changes']: | |
74 # We are only interested in the arrival of new instances | |
75 if change['ChangeType'] == 'NewInstance': | |
76 # Call the callback function | |
77 path = change['Path'] | |
78 NewInstanceReceived(path) | |
79 | |
80 # Delete the instance once it has been discovered | |
81 RestToolbox.DoDelete(URL + path) | |
82 | |
83 current = r['Last'] | |
84 | |
85 if r['Done']: | |
86 print('Everything has been processed: Waiting...') | |
87 time.sleep(1) |