Mercurial > hg > orthanc-book
annotate Sphinx/source/users/lua.rst @ 247:325dd3901547
ohif
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sun, 19 May 2019 10:08:52 +0200 |
parents | 1a701dfdfcad |
children | eaad8233e474 |
rev | line source |
---|---|
0 | 1 .. _lua: |
2 | |
3 Server-side scripting with Lua | |
4 ============================== | |
5 | |
6 .. contents:: | |
7 | |
8 Since release 0.5.2, Orthanc supports server-side scripting through | |
25 | 9 the `Lua <https://en.wikipedia.org/wiki/Lua_(programming_language)>`__ |
0 | 10 scripting language. Thanks to this major feature, Orthanc can be tuned |
11 to specific medical workflows without being driven by an external | |
12 script. This page summarizes the possibilities of Orthanc server-side | |
13 scripting. | |
14 | |
15 Many other examples are `available in the source distribution | |
16 <https://bitbucket.org/sjodogne/orthanc/src/default/Resources/Samples/Lua/>`__. | |
17 | |
18 | |
75 | 19 Installing a Lua script |
0 | 20 ----------------------- |
21 | |
22 .. highlight:: bash | |
23 | |
24 A custom Lua script can be installed either by the :ref:`configuration | |
25 file <configuration>`, or by uploading it | |
26 through the :ref:`REST API <rest-samples>`. | |
27 | |
28 To install it by the **configuration file** method, you just have to | |
29 specify the path to the file containing the Lua script in the | |
30 ``LuaScripts`` variable. | |
31 | |
32 To upload a script stored in the file "``script.lua``" through the | |
33 **REST API**, use the following command:: | |
34 | |
35 $ curl -X POST http://localhost:8042/tools/execute-script --data-binary @script.lua | |
36 | |
37 Pay attention to the fact that, contrarily to the scripts installed | |
38 from the configuration file, the scripts installed through the REST | |
39 API are non-persistent: They are discarded after a restart of Orthanc, | |
40 which makes them useful for script prototyping. You can also interpret | |
41 a single Lua command through the REST API:: | |
42 | |
43 $ curl -X POST http://localhost:8042/tools/execute-script --data-binary "print(42)" | |
44 | |
45 *Note:* The ``--data-binary`` cURL option is used instead of | |
46 ``--data`` to prevent the interpretation of newlines by cURL, which is | |
47 `mandatory for the proper evaluation | |
25 | 48 <http://stackoverflow.com/questions/3872427/how-to-send-line-break-with-curl>`__ of the possible |
0 | 49 comments inside the Lua script. |
50 | |
51 | |
52 Lua API | |
53 ------- | |
54 | |
55 | |
56 .. _lua-callbacks: | |
57 | |
58 Callbacks to react to events | |
59 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
60 | |
61 The Lua engine of Orthanc comes invokes the following callbacks that | |
62 are triggered on various events. Here are the **generic events**: | |
63 | |
64 * ``function Initialize()``: Invoked as soon as the Orthanc server is started. | |
65 * ``function Finalize()``: Invoked just before the Orthanc server is stopped. | |
66 | |
67 Some **permission-related events** allow to filter incoming requests: | |
68 | |
69 * ``function ReceivedInstanceFilter(dicom, origin)``: | |
70 Invoked to known whether an incoming DICOM instance should be | |
71 accepted. :ref:`See this section <lua-filter-dicom>`. The ``origin`` | |
72 parameter is :ref:`documented separately <lua-origin>`. | |
73 * ``function IncomingHttpRequestFilter(method, uri, ip, username, | |
74 httpHeaders)``: Invoked to known whether a REST request should be | |
75 accepted. :ref:`See this section <lua-filter-rest>`. | |
76 | |
232 | 77 Some **job-related events** allow to react to :ref:`job <jobs>` completion/failure: |
78 | |
79 * ``function OnJobSubmitted(jobId)``: | |
80 Invoked when a new job has been submitted. Note that this does not | |
81 mean the the job execution has started. | |
82 * ``function OnJobFailure(jobId)``: | |
83 Invoked when a job has failed. | |
84 * ``function OnJobSuccess(jobId)``: | |
85 Invoked when a job has completed successfully. | |
86 | |
0 | 87 Some **DICOM-related events** allow to react to the reception of |
88 new medical images: | |
89 | |
90 * ``function OnStoredInstance(instanceId, tags, metadata, origin)``: | |
91 Invoked whenever a new instance has been stored into Orthanc. | |
92 This is especially useful for :ref:`lua-auto-routing`. The ``origin`` | |
93 parameter is :ref:`documented separately <lua-origin>`. | |
94 * ``function OnStablePatient(patientId, tags, metadata)``: Invoked | |
95 whenever a patient has not received any new instance for a certain | |
96 amount of time (cf. the option ``StableAge`` in the | |
97 :ref:`configuration file <configuration>`). The :ref:`identifier | |
98 <orthanc-ids>` of the patient is provided, together with her DICOM | |
78 | 99 tags and her :ref:`metadata <metadata>`. |
0 | 100 * ``function OnStableSeries(seriesId, tags, metadata)``: Invoked |
101 whenever a series has not received any new instance for a certain | |
102 amount of time. | |
103 * ``function OnStableStudy(studyId, tags, metadata)``: Invoked | |
104 whenever a study has not received any new instance for a certain | |
105 amount of time. | |
106 * ``function IncomingFindRequestFilter(source, origin)``: Invoked | |
107 whenever Orthanc receives an incoming C-Find query through the DICOM | |
108 protocol. This allows to inspect the content of the C-Find query, | |
109 and possibly modify it if a patch is needed for some manufacturer. A | |
110 `sample script is available | |
111 <https://bitbucket.org/sjodogne/orthanc/src/default/Resources/Samples/Lua/IncomingFindRequestFilter.lua>`__. | |
112 | |
64
a3df3c2b68cf
import the documentation of the Worklists sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
113 Furthermore, whenever a DICOM association is negotiated for C-Store |
0 | 114 SCP, several callbacks are successively invoked to specify which |
115 **transfer syntaxes** are accepted for the association. These | |
116 callbacks are listed in `this sample script | |
117 <https://bitbucket.org/sjodogne/orthanc/src/default/Resources/Samples/Lua/TransferSyntaxEnable.lua>`__. | |
118 | |
119 *Note:* All of these callbacks are guaranteed to be **invoked in | |
120 mutual exclusion**. This implies that Lua scripting in Orthanc does | |
121 not support any kind of concurrency. | |
122 | |
123 | |
124 .. _lua-rest: | |
125 | |
126 Calling the REST API of Orthanc | |
127 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
128 | |
129 Lua scripts have :ref:`full access to the REST API <rest>` of Orthanc | |
130 through the following functions: | |
131 | |
132 * ``RestApiGet(uri, builtin)`` | |
133 * ``RestApiPost(uri, body, builtin)`` | |
134 * ``RestApiPut(uri, body, builtin)`` | |
135 * ``RestApiDelete(uri, builtin)`` | |
136 | |
237 | 137 Here is a description of the parameters: |
235
17f8eda5fccb
Documented some parameter types, expected string formatting and added samples.
Tomas Zubiri <me@tomaszubiri.com>
parents:
191
diff
changeset
|
138 |
237 | 139 * ``uri`` specifies the resource being accessed |
140 (e.g. ``/instances``). It must not include the URL schema | |
141 (protocol), hostname or port. | |
0 | 142 |
237 | 143 * In the context of a POST or PUT request, ``body`` is a string |
144 containing the body of the request | |
145 (e.g. ``{"Keep":"StudyDate"}``). This string will often correspond | |
146 to a JSON-formatted version of a `Lua table | |
147 <http://lua-users.org/wiki/TablesTutorial>`__. The ``DumpJson()`` | |
148 function (see below) is very useful to achieve this conversion from | |
149 a Lua table to a plain string. | |
235
17f8eda5fccb
Documented some parameter types, expected string formatting and added samples.
Tomas Zubiri <me@tomaszubiri.com>
parents:
191
diff
changeset
|
150 |
237 | 151 * ``builtin`` is an optional Boolean that specifies whether the |
152 request targets only the built-in REST API of Orthanc (if set to | |
153 ``true``), or the full the REST API after being tainted by plugins | |
154 (if set to ``false``). | |
235
17f8eda5fccb
Documented some parameter types, expected string formatting and added samples.
Tomas Zubiri <me@tomaszubiri.com>
parents:
191
diff
changeset
|
155 |
237 | 156 .. highlight:: bash |
157 | |
158 For instance:: | |
159 | |
160 RestApiPost('/instances/5af318ac-78fb-47ff-b0b0-0df18b0588e0/anonymize', '{}') | |
161 | |
0 | 162 |
163 General-purpose functions | |
164 ^^^^^^^^^^^^^^^^^^^^^^^^^ | |
165 | |
166 The Lua engine of Orthanc contain several general-purpose ancillary | |
167 functions: | |
168 | |
169 * ``PrintRecursive(v)`` recursively prints the content of a `Lua table | |
170 <http://www.lua.org/pil/2.5.html>`__ to the log file of Orthanc. | |
171 * ``ParseJson(s)`` converts a string encoded in the `JSON format | |
172 <https://en.wikipedia.org/wiki/JSON>`__ to a Lua table. | |
173 * ``DumpJson(v, keepStrings)`` encodes a Lua table as a JSON string. | |
174 Setting the optional argument ``keepStrings`` (available from | |
175 release 0.9.5) to ``true`` prevents the automatic conversion of | |
176 strings to integers. | |
177 * ``GetOrthancConfiguration()`` returns a Lua table containing the | |
178 content of the :ref:`configuration files <configuration>` of | |
179 Orthanc. | |
180 | |
181 | |
182 Similarly to the functions to :ref:`call the REST API of Orthanc | |
183 <lua-rest>`, several functions are available to make generic HTTP | |
184 requests to Web services: | |
185 | |
90 | 186 * ``HttpGet(url, headers)`` |
187 * ``HttpPost(url, body, headers)`` | |
188 * ``HttpPut(url, body, headers)`` | |
189 * ``HttpDelete(url, headers)`` | |
0 | 190 * ``SetHttpCredentials(username, password)`` can be used to setup the |
191 HTTP credentials. | |
192 | |
91 | 193 The ``headers`` argument is optional and has been added in release |
194 1.2.1. It allows to set the HTTP headers for the HTTP client request. | |
0 | 195 |
196 .. _lua-origin: | |
197 | |
198 Origin of the instances | |
199 ^^^^^^^^^^^^^^^^^^^^^^^ | |
200 | |
201 Whenever Orthanc decides whether it should should store a new instance | |
202 (cf. the ``ReceivedInstanceFilter()`` callback), or whenever it has | |
203 actually stored a new instance (cf. the ``OnStoredInstance`` | |
204 callback), an ``origin`` parameter is provided. This parameter is a | |
205 `Lua table <http://www.lua.org/pil/2.5.html>`__ that describes from | |
206 which Orthanc subsystem the new instance comes from. | |
207 | |
208 There are 4 possible subsystems, that can be distinguished according | |
209 to the value of ``origin["RequestOrigin"]``: | |
210 | |
211 * ``RestApi``: The instance originates from some HTTP request to the REST | |
212 API. In this case, the ``RemoteIp`` and ``Username`` fields are | |
213 available in ``origin``. They respectively describe the IP address | |
214 of the HTTP client, and the username that was used for HTTP | |
215 authentication (as defined in the ``RegisteredUsers`` | |
216 :ref:`configuration variable <configuration>`). | |
217 * ``DicomProtocol``: The instance originates from a DICOM C-Store. | |
218 The fields ``RemoteIp``, ``RemoteAet`` and ``CalledAet`` | |
219 respectively provide the IP address of the DICOM SCU (client), the | |
220 application entity title of the DICOM SCU client, and the | |
221 application entity title of the Orthanc SCP server. The | |
222 ``CalledAet`` can be used for :ref:`advanced auto-routing scenarios | |
223 <lua-auto-routing>`, when a single instance of Orthanc acts as a | |
224 proxy for several DICOM SCU clients. | |
225 * ``Lua``: The instance originates from a Lua script. | |
226 * ``Plugins``: The instance originates from a plugin. | |
227 | |
228 | |
229 .. _lua-filter-dicom: | |
230 | |
75 | 231 Filtering incoming DICOM instances |
0 | 232 ---------------------------------- |
233 | |
234 .. highlight:: lua | |
235 | |
236 Each time a DICOM instance is received by Orthanc (either through the | |
237 DICOM protocol or through the REST API), the | |
238 ``ReceivedInstanceFilter()`` Lua function is invoked. If this callback | |
239 returns ``true``, the instance is accepted for storage. If it returns | |
240 ``false``, the instance is discarded. This mechanism can be used to | |
241 filter the incoming DICOM instances. Here is an example of a Lua | |
242 filter that only allows incoming instances of MR modality:: | |
243 | |
244 function ReceivedInstanceFilter(dicom, origin) | |
245 -- Only allow incoming MR images | |
246 if dicom.Modality == 'MR' then | |
247 return true | |
248 else | |
249 return false | |
250 end | |
251 end | |
252 | |
253 The argument dicom corresponds to a `Lua table | |
254 <http://www.lua.org/pil/2.5.html>`__ (i.e. an associative array) that | |
255 contains the DICOM tags of the incoming instance. For debugging | |
256 purpose, you can print this structure as follows:: | |
257 | |
258 function ReceivedInstanceFilter(dicom, origin) | |
259 PrintRecursive(dicom) | |
260 -- Accept all incoming instances (default behavior) | |
261 return true | |
262 end | |
263 | |
264 The argument ``origin`` is :ref:`documented separately <lua-origin>`. | |
265 | |
266 | |
267 .. _lua-filter-rest: | |
268 | |
75 | 269 Filtering incoming REST requests |
0 | 270 -------------------------------- |
271 | |
272 .. highlight:: lua | |
273 | |
274 Lua scripting can be used to control the access to the various URI of | |
275 the REST API. Each time an incoming HTTP request is received, the | |
276 ``IncomingHttpRequestFilter()`` Lua function is called. The access to | |
277 the resource is granted if and only if this callback script returns | |
278 ``true``. | |
279 | |
280 This mechanism can be used to implement fine-grained `access control | |
25 | 281 lists <https://en.wikipedia.org/wiki/Access_control_list>`__. Here is |
0 | 282 an example of a Lua script that limits POST, PUT and DELETE requests |
283 to an user that is called "admin":: | |
284 | |
285 function IncomingHttpRequestFilter(method, uri, ip, username, httpHeaders) | |
286 -- Only allow GET requests for non-admin users | |
287 | |
288 if method == 'GET' then | |
289 return true | |
290 elseif username == 'admin' then | |
291 return true | |
292 else | |
293 return false | |
294 end | |
295 end | |
296 | |
297 Here is a description of the arguments of this Lua callback: | |
298 | |
299 * ``method``: The HTTP method (GET, POST, PUT or DELETE). | |
300 * ``uri``: The path to the resource (e.g. ``/tools/generate-uid``). | |
301 * ``ip``: The IP address of the host that has issued the HTTP request (e.g. ``127.0.0.1``). | |
302 * ``username``: If HTTP Basic Authentication is enabled in the | |
303 :ref:`configuration file <configuration>`, the name of the user that | |
304 has issued the HTTP request (as defined in the ``RegisteredUsers`` | |
305 configuration variable). If the authentication is disabled, this | |
306 argument is set to the empty string. | |
307 * ``httpHeaders``: The HTTP headers of the incoming request. This | |
308 argument is available since Orthanc 1.0.1. It is useful if the | |
309 authentication should be achieved through tokens, for instance | |
310 against a `LDAP | |
311 <https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol>`__ | |
312 or `OAuth2 <https://en.wikipedia.org/wiki/OAuth>`__ server. | |
313 | |
314 | |
315 .. _lua-auto-routing: | |
316 | |
75 | 317 Auto-routing of DICOM images |
0 | 318 ---------------------------- |
319 | |
320 .. highlight:: lua | |
321 | |
322 Since release 0.8.0, the routing of DICOM flows can be very easily | |
323 automated with Orthanc. All you have to do is to declare your | |
324 destination modality in the :ref:`configuration file <configuration>` | |
325 (section ``DicomModalities``), then to create and install a Lua | |
326 script. For instance, here is a sample script:: | |
327 | |
328 function OnStoredInstance(instanceId, tags, metadata) | |
329 Delete(SendToModality(instanceId, 'sample')) | |
330 end | |
331 | |
332 If this script is loaded into Orthanc, whenever a new DICOM instance | |
333 is received by Orthanc, it will be routed to the modality whose | |
334 symbolic name is ``sample`` (through a Store-SCU command), then it | |
335 will be removed from Orthanc. In other words, this is a **one-liner | |
336 script to implement DICOM auto-routing**. | |
337 | |
338 Very importantly, thanks to this feature, you do not have to use the | |
339 REST API or to create external scripts in order to automate simple | |
340 imaging flows. The scripting engine is entirely contained inside the | |
341 Orthanc core system. | |
342 | |
343 Thanks to Lua expressiveness, you can also implement conditional | |
344 auto-routing. For instance, if you wish to route only patients whose | |
345 name contains "David", you would simply write:: | |
346 | |
347 function OnStoredInstance(instanceId, tags, metadata) | |
348 -- Extract the value of the "PatientName" DICOM tag | |
349 local patientName = string.lower(tags['PatientName']) | |
350 | |
351 if string.find(patientName, 'david') ~= nil then | |
352 -- Only route patients whose name contains "David" | |
353 Delete(SendToModality(instanceId, 'sample')) | |
354 | |
355 else | |
356 -- Delete the patients that are not called "David" | |
357 Delete(instanceId) | |
358 end | |
359 end | |
360 | |
361 Besides ``SendToModality()``, a mostly identical function with the | |
362 same arguments called ``SendToPeer()`` can be used to route instances | |
363 to :ref:`Orthanc peers <peers>`. It is also possible to modify the | |
364 received instances before routing them. For instance, here is how you | |
365 would replace the ``StationName`` DICOM tag:: | |
366 | |
367 function OnStoredInstance(instanceId, tags, metadata) | |
368 -- Ignore the instances that result from a modification to avoid | |
369 -- infinite loops | |
370 if (metadata['ModifiedFrom'] == nil and | |
371 metadata['AnonymizedFrom'] == nil) then | |
372 | |
373 -- The tags to be replaced | |
374 local replace = {} | |
375 replace['StationName'] = 'My Medical Device' | |
376 | |
377 -- The tags to be removed | |
378 local remove = { 'MilitaryRank' } | |
379 | |
380 -- Modify the instance, send it, then delete the modified instance | |
381 Delete(SendToModality(ModifyInstance(instanceId, replace, remove, true), 'sample')) | |
382 | |
383 -- Delete the original instance | |
384 Delete(instanceId) | |
385 end | |
386 end | |
387 | |
388 | |
75 | 389 Important remarks about auto-routing |
390 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
19
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
391 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
392 The ``SendToModality()``, ``SendToPeer()``, ``ModifyInstance()`` and |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
393 ``Delete()`` functions are for the most basic cases of auto-routing |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
394 (implying a single DICOM instance, and possibly a basic modification |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
395 of this instance). The ``ModifyInstance()`` function `could also lead |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
396 to problems |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
397 <https://groups.google.com/d/msg/orthanc-users/hmv2y-LgKm8/oMAuGJWMBgAJ>`__ |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
398 if it deals with tags wrongly interpreted as numbers by Lua. |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
399 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
400 For more evolved auto-routing scenarios, remember that Lua scripts |
191 | 401 :ref:`have full access to the REST API of Orthanc <lua-rest>`. This is |
19
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
402 illustrated by the ``AutoroutingModification.lua`` sample available in |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
403 the source distribution of Orthanc:: |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
404 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
405 function OnStoredInstance(instanceId, tags, metadata, origin) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
406 -- Ignore the instances that result from the present Lua script to |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
407 -- avoid infinite loops |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
408 if origin['RequestOrigin'] ~= 'Lua' then |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
409 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
410 -- The tags to be replaced |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
411 local replace = {} |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
412 replace['StationName'] = 'My Medical Device' |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
413 replace['0031-1020'] = 'Some private tag' |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
414 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
415 -- The tags to be removed |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
416 local remove = { 'MilitaryRank' } |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
417 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
418 -- Modify the instance |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
419 local command = {} |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
420 command['Replace'] = replace |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
421 command['Remove'] = remove |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
422 local modifiedFile = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(command, true)) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
423 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
424 -- Upload the modified instance to the Orthanc database so that |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
425 -- it can be sent by Orthanc to other modalities |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
426 local modifiedId = ParseJson(RestApiPost('/instances/', modifiedFile)) ['ID'] |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
427 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
428 -- Send the modified instance to another modality |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
429 RestApiPost('/modalities/sample/store', modifiedId) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
430 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
431 -- Delete the original and the modified instances |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
432 RestApiDelete('/instances/' .. instanceId) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
433 RestApiDelete('/instances/' .. modifiedId) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
434 end |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
435 end |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
436 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
437 Also note that :ref:`other callbacks are available <lua-callbacks>` |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
438 (``OnStablePatient()``, ``OnStableStudy()`` and ``OnStableSeries()``) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
439 to react to other events than the reception of a single instance |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
440 with ``OnStoredInstance()``. |
75 | 441 |
99
e83da2f99e45
added 'troubleshooting C-Find queries' in the worklist section
amazy
parents:
91
diff
changeset
|
442 .. _lua-fix-cfind: |
75 | 443 |
77
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
444 Fixing C-Find requests |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
445 ---------------------- |
75 | 446 |
447 :ref:`C-Find requests <dicom-find>` are sometimes interpreted | |
77
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
448 differently by different DICOM servers (e.g. the ``*`` wildcard, as |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
449 `reported by users |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
450 <https://groups.google.com/d/msg/orthanc-users/3g7V7kqr3g0/IREL88RWAwAJ>`__), |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
451 and sometimes a querying modality might set unexpected DICOM tags |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
452 (cf. `this real-world example |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
453 <https://groups.google.com/d/msg/orthanc-users/PLWKqVVaXLs/n_0x4vKhAgAJ>`__). In |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
454 such situations, it is possible to dynamically fix incoming or |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
455 outgoing C-Find queries using a Lua script. |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
456 |
165
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
457 Sanitizing incoming C-Find requests can be done by implementing the |
77
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
458 ``IncomingFindRequestFilter(query, origin)`` callback that is called |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
459 whenever the Orthanc C-Find SCP is queried by a remote modality. For |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
460 instance, here is Lua script to remove a private tag that is specified |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
461 by some manufacturer:: |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
462 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
463 function IncomingFindRequestFilter(query, origin) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
464 -- First display the content of the C-Find query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
465 PrintRecursive(query) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
466 PrintRecursive(origin) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
467 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
468 -- Remove the "PrivateCreator" tag from the query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
469 local v = query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
470 v['5555,0010'] = nil |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
471 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
472 return v |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
473 end |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
474 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
475 The ``origin`` argument contains information about which modality has |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
476 issued the request. |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
477 |
165
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
478 Note that the ``IncomingFindRequestFilter`` callback is not applied to |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
479 C-Find requests targeting :ref:`modality worklists |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
480 <worklists-plugin>`. Since Orthanc 1.4.2, the corresponding |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
481 ``IncomingWorklistRequestFilter`` callback can be used to sanitize |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
482 C-FIND requests against worklists:: |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
483 |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
484 function IncomingWorklistRequestFilter(query, origin) |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
485 PrintRecursive(query) |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
486 PrintRecursive(origin) |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
487 |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
488 -- Implements the same behavior as the "FilterIssuerAet" |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
489 -- option of the sample worklist plugin |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
490 query['0040,0100'][1]['0040,0001'] = origin['RemoteAet'] |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
491 |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
492 return query |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
493 end |
105
268ec482f051
IncomingFindRequestFilter not applied to worklist plugin
amazy
parents:
99
diff
changeset
|
494 |
77
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
495 Similarly, the callback ``OutgoingFindRequestFilter(query, modality)`` |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
496 is invoked whenever Orthanc acts as a C-Find SCU, which gives the |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
497 opportunity to dynamically fix outgoing C-Find requests before they |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
498 are actually sent to the queried modality. For instance, here is a |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
499 sample Lua callback that would replace asterisk wildcards (i.e. ``*``) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
500 by an empty string for any query/retrieve issued by Orthanc (including |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
501 from Orthanc Explorer):: |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
502 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
503 function OutgoingFindRequestFilter(query, modality) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
504 for key, value in pairs(query) do |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
505 if value == '*' then |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
506 query[key] = '' |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
507 end |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
508 end |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
509 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
510 return query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
511 end |