Mercurial > hg > orthanc-book
annotate Sphinx/source/users/lua.rst @ 358:011b01ccf52d
fixing external hyperlinks
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 30 Mar 2020 18:47:35 +0200 |
parents | 6b432ce3a56e |
children | d217af5e6cb3 |
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 | |
357 | 30 ``LuaScripts`` variable. A comma-separated list of paths can be |
31 specified to install multiple scripts. | |
0 | 32 |
33 To upload a script stored in the file "``script.lua``" through the | |
34 **REST API**, use the following command:: | |
35 | |
36 $ curl -X POST http://localhost:8042/tools/execute-script --data-binary @script.lua | |
37 | |
38 Pay attention to the fact that, contrarily to the scripts installed | |
39 from the configuration file, the scripts installed through the REST | |
40 API are non-persistent: They are discarded after a restart of Orthanc, | |
41 which makes them useful for script prototyping. You can also interpret | |
42 a single Lua command through the REST API:: | |
43 | |
44 $ curl -X POST http://localhost:8042/tools/execute-script --data-binary "print(42)" | |
45 | |
46 *Note:* The ``--data-binary`` cURL option is used instead of | |
47 ``--data`` to prevent the interpretation of newlines by cURL, which is | |
48 `mandatory for the proper evaluation | |
358
011b01ccf52d
fixing external hyperlinks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
357
diff
changeset
|
49 <https://stackoverflow.com/questions/3872427/how-to-send-line-break-with-curl>`__ |
011b01ccf52d
fixing external hyperlinks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
357
diff
changeset
|
50 of the possible comments inside the Lua script. |
0 | 51 |
52 Lua API | |
53 ------- | |
54 | |
55 | |
56 .. _lua-callbacks: | |
57 | |
58 Callbacks to react to events | |
59 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
60 | |
355
e4e9cca89c64
clarified mutual exclusion of Lua scripts callbacks.
David Wikler <david.wikler@ulb.ac.be>
parents:
319
diff
changeset
|
61 The Lua engine of Orthanc invokes the following callbacks that |
0 | 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 | |
319
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
113 Some other **resource-related events** are available: |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
114 |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
115 * ``function OnDeletedPatient(patientId)``: Invoked when a patient has |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
116 been removed from the Orthanc database (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
117 * ``function OnDeletedStudy(studyId)``: Invoked when a study has been |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
118 removed from the Orthanc database (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
119 * ``function OnDeletedSeries(seriesId)``: Invoked when a series has |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
120 been removed from the Orthanc database (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
121 * ``function OnDeletedInstance(instanceId)``: Invoked when a instance |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
122 has been removed from the Orthanc database (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
123 * ``function OnUpdatedPatient(patientId)``: Invoked when some metadata |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
124 or some attachment associated with the given patient has been |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
125 updated (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
126 * ``function OnUpdatedStudy(studyId)``: Invoked when some metadata or |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
127 some attachment associated with the given study has been updated |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
128 (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
129 * ``function OnUpdatedSeries(seriesId)``: Invoked when some metadata |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
130 or some attachment associated with the given series has been updated |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
131 (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
132 * ``function OnUpdatedInstance(instanceId)``: Invoked when some |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
133 metadata or some attachment associated with the given instance has |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
134 been updated (new in Orthanc 1.6.0). |
be69f8c86f56
Lua events for deleted/updated resources
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
283
diff
changeset
|
135 |
64
a3df3c2b68cf
import the documentation of the Worklists sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
136 Furthermore, whenever a DICOM association is negotiated for C-Store |
0 | 137 SCP, several callbacks are successively invoked to specify which |
138 **transfer syntaxes** are accepted for the association. These | |
139 callbacks are listed in `this sample script | |
140 <https://bitbucket.org/sjodogne/orthanc/src/default/Resources/Samples/Lua/TransferSyntaxEnable.lua>`__. | |
141 | |
142 *Note:* All of these callbacks are guaranteed to be **invoked in | |
143 mutual exclusion**. This implies that Lua scripting in Orthanc does | |
357 | 144 not support any kind of concurrency. |
145 | |
146 If a callback is specified multiple times in separate scripts, the | |
147 event handler of the latest loaded script is used. | |
0 | 148 |
149 | |
150 .. _lua-rest: | |
151 | |
152 Calling the REST API of Orthanc | |
153 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
154 | |
155 Lua scripts have :ref:`full access to the REST API <rest>` of Orthanc | |
156 through the following functions: | |
157 | |
252
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
158 * ``RestApiGet(uri, builtin, headers)`` |
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
159 * ``RestApiPost(uri, body, builtin, headers)`` |
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
160 * ``RestApiPut(uri, body, builtin, headers)`` |
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
161 * ``RestApiDelete(uri, builtin, headers)`` |
0 | 162 |
237 | 163 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
|
164 |
237 | 165 * ``uri`` specifies the resource being accessed |
166 (e.g. ``/instances``). It must not include the URL schema | |
167 (protocol), hostname or port. | |
0 | 168 |
237 | 169 * In the context of a POST or PUT request, ``body`` is a string |
170 containing the body of the request | |
171 (e.g. ``{"Keep":"StudyDate"}``). This string will often correspond | |
172 to a JSON-formatted version of a `Lua table | |
173 <http://lua-users.org/wiki/TablesTutorial>`__. The ``DumpJson()`` | |
174 function (see below) is very useful to achieve this conversion from | |
175 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
|
176 |
237 | 177 * ``builtin`` is an optional Boolean that specifies whether the |
178 request targets only the built-in REST API of Orthanc (if set to | |
179 ``true``), or the full the REST API after being tainted by plugins | |
180 (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
|
181 |
252
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
182 * ``headers`` is an optional argument and was added in release |
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
183 1.5.7. It allows to provide the REST API endpoint with HTTP headers. |
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
184 |
237 | 185 .. highlight:: bash |
186 | |
187 For instance:: | |
188 | |
189 RestApiPost('/instances/5af318ac-78fb-47ff-b0b0-0df18b0588e0/anonymize', '{}') | |
190 | |
0 | 191 |
192 General-purpose functions | |
193 ^^^^^^^^^^^^^^^^^^^^^^^^^ | |
194 | |
195 The Lua engine of Orthanc contain several general-purpose ancillary | |
196 functions: | |
197 | |
198 * ``PrintRecursive(v)`` recursively prints the content of a `Lua table | |
199 <http://www.lua.org/pil/2.5.html>`__ to the log file of Orthanc. | |
200 * ``ParseJson(s)`` converts a string encoded in the `JSON format | |
201 <https://en.wikipedia.org/wiki/JSON>`__ to a Lua table. | |
202 * ``DumpJson(v, keepStrings)`` encodes a Lua table as a JSON string. | |
203 Setting the optional argument ``keepStrings`` (available from | |
204 release 0.9.5) to ``true`` prevents the automatic conversion of | |
205 strings to integers. | |
206 * ``GetOrthancConfiguration()`` returns a Lua table containing the | |
207 content of the :ref:`configuration files <configuration>` of | |
208 Orthanc. | |
209 | |
210 | |
211 Similarly to the functions to :ref:`call the REST API of Orthanc | |
212 <lua-rest>`, several functions are available to make generic HTTP | |
213 requests to Web services: | |
214 | |
90 | 215 * ``HttpGet(url, headers)`` |
216 * ``HttpPost(url, body, headers)`` | |
217 * ``HttpPut(url, body, headers)`` | |
218 * ``HttpDelete(url, headers)`` | |
0 | 219 * ``SetHttpCredentials(username, password)`` can be used to setup the |
220 HTTP credentials. | |
221 | |
252
eaad8233e474
HTTP headers available for the RestApiXXX Lua function
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
237
diff
changeset
|
222 The ``headers`` argument is optional and was added in release |
91 | 223 1.2.1. It allows to set the HTTP headers for the HTTP client request. |
0 | 224 |
283 | 225 Example:: |
226 | |
227 local preview = RestApiGet('/instances/' .. instanceId .. '/preview') | |
228 local headers = { | |
229 ["content-type"] = "image/png", | |
230 } | |
231 HttpPost("http://localhost/my-web-service/instance-preview", preview, headers) | |
232 | |
0 | 233 .. _lua-origin: |
234 | |
235 Origin of the instances | |
236 ^^^^^^^^^^^^^^^^^^^^^^^ | |
237 | |
238 Whenever Orthanc decides whether it should should store a new instance | |
239 (cf. the ``ReceivedInstanceFilter()`` callback), or whenever it has | |
240 actually stored a new instance (cf. the ``OnStoredInstance`` | |
241 callback), an ``origin`` parameter is provided. This parameter is a | |
242 `Lua table <http://www.lua.org/pil/2.5.html>`__ that describes from | |
243 which Orthanc subsystem the new instance comes from. | |
244 | |
245 There are 4 possible subsystems, that can be distinguished according | |
246 to the value of ``origin["RequestOrigin"]``: | |
247 | |
248 * ``RestApi``: The instance originates from some HTTP request to the REST | |
249 API. In this case, the ``RemoteIp`` and ``Username`` fields are | |
250 available in ``origin``. They respectively describe the IP address | |
251 of the HTTP client, and the username that was used for HTTP | |
252 authentication (as defined in the ``RegisteredUsers`` | |
253 :ref:`configuration variable <configuration>`). | |
254 * ``DicomProtocol``: The instance originates from a DICOM C-Store. | |
255 The fields ``RemoteIp``, ``RemoteAet`` and ``CalledAet`` | |
256 respectively provide the IP address of the DICOM SCU (client), the | |
257 application entity title of the DICOM SCU client, and the | |
258 application entity title of the Orthanc SCP server. The | |
259 ``CalledAet`` can be used for :ref:`advanced auto-routing scenarios | |
260 <lua-auto-routing>`, when a single instance of Orthanc acts as a | |
261 proxy for several DICOM SCU clients. | |
262 * ``Lua``: The instance originates from a Lua script. | |
263 * ``Plugins``: The instance originates from a plugin. | |
264 | |
265 | |
266 .. _lua-filter-dicom: | |
267 | |
75 | 268 Filtering incoming DICOM instances |
0 | 269 ---------------------------------- |
270 | |
271 .. highlight:: lua | |
272 | |
273 Each time a DICOM instance is received by Orthanc (either through the | |
274 DICOM protocol or through the REST API), the | |
275 ``ReceivedInstanceFilter()`` Lua function is invoked. If this callback | |
276 returns ``true``, the instance is accepted for storage. If it returns | |
277 ``false``, the instance is discarded. This mechanism can be used to | |
278 filter the incoming DICOM instances. Here is an example of a Lua | |
279 filter that only allows incoming instances of MR modality:: | |
280 | |
281 function ReceivedInstanceFilter(dicom, origin) | |
282 -- Only allow incoming MR images | |
283 if dicom.Modality == 'MR' then | |
284 return true | |
285 else | |
286 return false | |
287 end | |
288 end | |
289 | |
290 The argument dicom corresponds to a `Lua table | |
291 <http://www.lua.org/pil/2.5.html>`__ (i.e. an associative array) that | |
292 contains the DICOM tags of the incoming instance. For debugging | |
293 purpose, you can print this structure as follows:: | |
294 | |
295 function ReceivedInstanceFilter(dicom, origin) | |
296 PrintRecursive(dicom) | |
297 -- Accept all incoming instances (default behavior) | |
298 return true | |
299 end | |
300 | |
301 The argument ``origin`` is :ref:`documented separately <lua-origin>`. | |
302 | |
303 | |
304 .. _lua-filter-rest: | |
305 | |
75 | 306 Filtering incoming REST requests |
0 | 307 -------------------------------- |
308 | |
309 .. highlight:: lua | |
310 | |
311 Lua scripting can be used to control the access to the various URI of | |
312 the REST API. Each time an incoming HTTP request is received, the | |
313 ``IncomingHttpRequestFilter()`` Lua function is called. The access to | |
314 the resource is granted if and only if this callback script returns | |
315 ``true``. | |
316 | |
317 This mechanism can be used to implement fine-grained `access control | |
25 | 318 lists <https://en.wikipedia.org/wiki/Access_control_list>`__. Here is |
0 | 319 an example of a Lua script that limits POST, PUT and DELETE requests |
320 to an user that is called "admin":: | |
321 | |
322 function IncomingHttpRequestFilter(method, uri, ip, username, httpHeaders) | |
323 -- Only allow GET requests for non-admin users | |
324 | |
325 if method == 'GET' then | |
326 return true | |
327 elseif username == 'admin' then | |
328 return true | |
329 else | |
330 return false | |
331 end | |
332 end | |
333 | |
334 Here is a description of the arguments of this Lua callback: | |
335 | |
336 * ``method``: The HTTP method (GET, POST, PUT or DELETE). | |
337 * ``uri``: The path to the resource (e.g. ``/tools/generate-uid``). | |
338 * ``ip``: The IP address of the host that has issued the HTTP request (e.g. ``127.0.0.1``). | |
339 * ``username``: If HTTP Basic Authentication is enabled in the | |
340 :ref:`configuration file <configuration>`, the name of the user that | |
341 has issued the HTTP request (as defined in the ``RegisteredUsers`` | |
342 configuration variable). If the authentication is disabled, this | |
343 argument is set to the empty string. | |
344 * ``httpHeaders``: The HTTP headers of the incoming request. This | |
345 argument is available since Orthanc 1.0.1. It is useful if the | |
346 authentication should be achieved through tokens, for instance | |
347 against a `LDAP | |
348 <https://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol>`__ | |
349 or `OAuth2 <https://en.wikipedia.org/wiki/OAuth>`__ server. | |
350 | |
351 | |
352 .. _lua-auto-routing: | |
353 | |
75 | 354 Auto-routing of DICOM images |
0 | 355 ---------------------------- |
356 | |
357 .. highlight:: lua | |
358 | |
359 Since release 0.8.0, the routing of DICOM flows can be very easily | |
360 automated with Orthanc. All you have to do is to declare your | |
361 destination modality in the :ref:`configuration file <configuration>` | |
362 (section ``DicomModalities``), then to create and install a Lua | |
363 script. For instance, here is a sample script:: | |
364 | |
365 function OnStoredInstance(instanceId, tags, metadata) | |
366 Delete(SendToModality(instanceId, 'sample')) | |
367 end | |
368 | |
369 If this script is loaded into Orthanc, whenever a new DICOM instance | |
370 is received by Orthanc, it will be routed to the modality whose | |
371 symbolic name is ``sample`` (through a Store-SCU command), then it | |
372 will be removed from Orthanc. In other words, this is a **one-liner | |
373 script to implement DICOM auto-routing**. | |
374 | |
375 Very importantly, thanks to this feature, you do not have to use the | |
376 REST API or to create external scripts in order to automate simple | |
377 imaging flows. The scripting engine is entirely contained inside the | |
378 Orthanc core system. | |
379 | |
380 Thanks to Lua expressiveness, you can also implement conditional | |
381 auto-routing. For instance, if you wish to route only patients whose | |
382 name contains "David", you would simply write:: | |
383 | |
384 function OnStoredInstance(instanceId, tags, metadata) | |
385 -- Extract the value of the "PatientName" DICOM tag | |
386 local patientName = string.lower(tags['PatientName']) | |
387 | |
388 if string.find(patientName, 'david') ~= nil then | |
389 -- Only route patients whose name contains "David" | |
390 Delete(SendToModality(instanceId, 'sample')) | |
391 | |
392 else | |
393 -- Delete the patients that are not called "David" | |
394 Delete(instanceId) | |
395 end | |
396 end | |
397 | |
398 Besides ``SendToModality()``, a mostly identical function with the | |
399 same arguments called ``SendToPeer()`` can be used to route instances | |
400 to :ref:`Orthanc peers <peers>`. It is also possible to modify the | |
401 received instances before routing them. For instance, here is how you | |
402 would replace the ``StationName`` DICOM tag:: | |
403 | |
404 function OnStoredInstance(instanceId, tags, metadata) | |
405 -- Ignore the instances that result from a modification to avoid | |
406 -- infinite loops | |
407 if (metadata['ModifiedFrom'] == nil and | |
408 metadata['AnonymizedFrom'] == nil) then | |
409 | |
410 -- The tags to be replaced | |
411 local replace = {} | |
412 replace['StationName'] = 'My Medical Device' | |
413 | |
414 -- The tags to be removed | |
415 local remove = { 'MilitaryRank' } | |
416 | |
417 -- Modify the instance, send it, then delete the modified instance | |
418 Delete(SendToModality(ModifyInstance(instanceId, replace, remove, true), 'sample')) | |
419 | |
420 -- Delete the original instance | |
421 Delete(instanceId) | |
422 end | |
423 end | |
424 | |
425 | |
75 | 426 Important remarks about auto-routing |
427 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | |
19
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
428 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
429 The ``SendToModality()``, ``SendToPeer()``, ``ModifyInstance()`` and |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
430 ``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
|
431 (implying a single DICOM instance, and possibly a basic modification |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
432 of this instance). The ``ModifyInstance()`` function `could also lead |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
433 to problems |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
434 <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
|
435 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
|
436 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
437 For more evolved auto-routing scenarios, remember that Lua scripts |
191 | 438 :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
|
439 illustrated by the ``AutoroutingModification.lua`` sample available in |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
440 the source distribution of Orthanc:: |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
441 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
442 function OnStoredInstance(instanceId, tags, metadata, origin) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
443 -- 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
|
444 -- avoid infinite loops |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
445 if origin['RequestOrigin'] ~= 'Lua' then |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
446 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
447 -- The tags to be replaced |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
448 local replace = {} |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
449 replace['StationName'] = 'My Medical Device' |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
450 replace['0031-1020'] = 'Some private tag' |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
451 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
452 -- The tags to be removed |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
453 local remove = { 'MilitaryRank' } |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
454 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
455 -- Modify the instance |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
456 local command = {} |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
457 command['Replace'] = replace |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
458 command['Remove'] = remove |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
459 local modifiedFile = RestApiPost('/instances/' .. instanceId .. '/modify', DumpJson(command, true)) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
460 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
461 -- Upload the modified instance to the Orthanc database so that |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
462 -- it can be sent by Orthanc to other modalities |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
463 local modifiedId = ParseJson(RestApiPost('/instances/', modifiedFile)) ['ID'] |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
464 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
465 -- Send the modified instance to another modality |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
466 RestApiPost('/modalities/sample/store', modifiedId) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
467 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
468 -- Delete the original and the modified instances |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
469 RestApiDelete('/instances/' .. instanceId) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
470 RestApiDelete('/instances/' .. modifiedId) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
471 end |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
472 end |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
473 |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
474 Also note that :ref:`other callbacks are available <lua-callbacks>` |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
475 (``OnStablePatient()``, ``OnStableStudy()`` and ``OnStableSeries()``) |
c98317fedf87
note about autorouting
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
476 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
|
477 with ``OnStoredInstance()``. |
75 | 478 |
99
e83da2f99e45
added 'troubleshooting C-Find queries' in the worklist section
amazy
parents:
91
diff
changeset
|
479 .. _lua-fix-cfind: |
75 | 480 |
77
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
481 Fixing C-Find requests |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
482 ---------------------- |
75 | 483 |
484 :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
|
485 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
|
486 `reported by users |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
487 <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
|
488 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
|
489 (cf. `this real-world example |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
490 <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
|
491 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
|
492 outgoing C-Find queries using a Lua script. |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
493 |
165
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
494 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
|
495 ``IncomingFindRequestFilter(query, origin)`` callback that is called |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
496 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
|
497 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
|
498 by some manufacturer:: |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
499 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
500 function IncomingFindRequestFilter(query, origin) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
501 -- First display the content of the C-Find query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
502 PrintRecursive(query) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
503 PrintRecursive(origin) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
504 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
505 -- Remove the "PrivateCreator" tag from the query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
506 local v = query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
507 v['5555,0010'] = nil |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
508 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
509 return v |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
510 end |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
511 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
512 The ``origin`` argument contains information about which modality has |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
513 issued the request. |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
514 |
165
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
515 Note that the ``IncomingFindRequestFilter`` callback is not applied to |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
516 C-Find requests targeting :ref:`modality worklists |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
517 <worklists-plugin>`. Since Orthanc 1.4.2, the corresponding |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
518 ``IncomingWorklistRequestFilter`` callback can be used to sanitize |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
519 C-FIND requests against worklists:: |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
520 |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
521 function IncomingWorklistRequestFilter(query, origin) |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
522 PrintRecursive(query) |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
523 PrintRecursive(origin) |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
524 |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
525 -- Implements the same behavior as the "FilterIssuerAet" |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
526 -- option of the sample worklist plugin |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
527 query['0040,0100'][1]['0040,0001'] = origin['RemoteAet'] |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
528 |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
529 return query |
b879a6274065
IncomingWorklistRequestFilter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
105
diff
changeset
|
530 end |
105
268ec482f051
IncomingFindRequestFilter not applied to worklist plugin
amazy
parents:
99
diff
changeset
|
531 |
77
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
532 Similarly, the callback ``OutgoingFindRequestFilter(query, modality)`` |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
533 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
|
534 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
|
535 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
|
536 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
|
537 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
|
538 from Orthanc Explorer):: |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
539 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
540 function OutgoingFindRequestFilter(query, modality) |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
541 for key, value in pairs(query) do |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
542 if value == '*' then |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
543 query[key] = '' |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
544 end |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
545 end |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
546 |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
547 return query |
48c7d2eb98da
Fixing C-Find requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
75
diff
changeset
|
548 end |