comparison Plugins/Samples/ModalityWorklists/Plugin.cpp @ 2958:bb7a66efbeb1

OrthancPlugins::SetGlobalContext() in OrthancPluginCppWrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 04 Dec 2018 16:31:29 +0100
parents d4fd4614f275
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2957:ccf61f6e22ef 2958:bb7a66efbeb1
26 #include <json/reader.h> 26 #include <json/reader.h>
27 #include <string.h> 27 #include <string.h>
28 #include <iostream> 28 #include <iostream>
29 #include <algorithm> 29 #include <algorithm>
30 30
31 static OrthancPluginContext* context_ = NULL;
32 static std::string folder_; 31 static std::string folder_;
33 static bool filterIssuerAet_ = false; 32 static bool filterIssuerAet_ = false;
34 33
35 /** 34 /**
36 * This is the main function for matching a DICOM worklist against a query. 35 * This is the main function for matching a DICOM worklist against a query.
38 static bool MatchWorklist(OrthancPluginWorklistAnswers* answers, 37 static bool MatchWorklist(OrthancPluginWorklistAnswers* answers,
39 const OrthancPluginWorklistQuery* query, 38 const OrthancPluginWorklistQuery* query,
40 const OrthancPlugins::FindMatcher& matcher, 39 const OrthancPlugins::FindMatcher& matcher,
41 const std::string& path) 40 const std::string& path)
42 { 41 {
43 OrthancPlugins::MemoryBuffer dicom(context_); 42 OrthancPlugins::MemoryBuffer dicom;
44 dicom.ReadFile(path); 43 dicom.ReadFile(path);
45 44
46 if (matcher.IsMatch(dicom)) 45 if (matcher.IsMatch(dicom))
47 { 46 {
48 // This DICOM file matches the worklist query, add it to the answers 47 // This DICOM file matches the worklist query, add it to the answers
49 OrthancPluginErrorCode code = OrthancPluginWorklistAddAnswer 48 OrthancPluginErrorCode code = OrthancPluginWorklistAddAnswer
50 (context_, answers, query, dicom.GetData(), dicom.GetSize()); 49 (OrthancPlugins::GetGlobalContext(), answers, query, dicom.GetData(), dicom.GetSize());
51 50
52 if (code != OrthancPluginErrorCode_Success) 51 if (code != OrthancPluginErrorCode_Success)
53 { 52 {
54 OrthancPlugins::LogError(context_, "Error while adding an answer to a worklist request"); 53 OrthancPlugins::LogError("Error while adding an answer to a worklist request");
55 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code); 54 ORTHANC_PLUGINS_THROW_PLUGIN_ERROR_CODE(code);
56 } 55 }
57 56
58 return true; 57 return true;
59 } 58 }
64 63
65 static OrthancPlugins::FindMatcher* CreateMatcher(const OrthancPluginWorklistQuery* query, 64 static OrthancPlugins::FindMatcher* CreateMatcher(const OrthancPluginWorklistQuery* query,
66 const char* issuerAet) 65 const char* issuerAet)
67 { 66 {
68 // Extract the DICOM instance underlying the C-Find query 67 // Extract the DICOM instance underlying the C-Find query
69 OrthancPlugins::MemoryBuffer dicom(context_); 68 OrthancPlugins::MemoryBuffer dicom;
70 dicom.GetDicomQuery(query); 69 dicom.GetDicomQuery(query);
71 70
72 // Convert the DICOM as JSON, and dump it to the user in "--verbose" mode 71 // Convert the DICOM as JSON, and dump it to the user in "--verbose" mode
73 Json::Value json; 72 Json::Value json;
74 dicom.DicomToJson(json, OrthancPluginDicomToJsonFormat_Short, 73 dicom.DicomToJson(json, OrthancPluginDicomToJsonFormat_Short,
75 static_cast<OrthancPluginDicomToJsonFlags>(0), 0); 74 static_cast<OrthancPluginDicomToJsonFlags>(0), 0);
76 75
77 OrthancPlugins::LogInfo(context_, "Received worklist query from remote modality " + 76 OrthancPlugins::LogInfo("Received worklist query from remote modality " +
78 std::string(issuerAet) + ":\n" + json.toStyledString()); 77 std::string(issuerAet) + ":\n" + json.toStyledString());
79 78
80 if (!filterIssuerAet_) 79 if (!filterIssuerAet_)
81 { 80 {
82 return new OrthancPlugins::FindMatcher(context_, query); 81 return new OrthancPlugins::FindMatcher(query);
83 } 82 }
84 else 83 else
85 { 84 {
86 // Alternative sample showing how to fine-tune an incoming C-Find 85 // Alternative sample showing how to fine-tune an incoming C-Find
87 // request, before matching it against the worklist database. The 86 // request, before matching it against the worklist database. The
124 { 123 {
125 json.removeMember(PREGNANCY_STATUS); 124 json.removeMember(PREGNANCY_STATUS);
126 } 125 }
127 126
128 // Encode the modified JSON as a DICOM instance, then convert it to a C-Find matcher 127 // Encode the modified JSON as a DICOM instance, then convert it to a C-Find matcher
129 OrthancPlugins::MemoryBuffer modified(context_); 128 OrthancPlugins::MemoryBuffer modified;
130 modified.CreateDicom(json, OrthancPluginCreateDicomFlags_None); 129 modified.CreateDicom(json, OrthancPluginCreateDicomFlags_None);
131 return new OrthancPlugins::FindMatcher(context_, modified); 130
131 return new OrthancPlugins::FindMatcher(modified);
132 } 132 }
133 } 133 }
134 134
135 135
136 136
168 { 168 {
169 parsedFilesCount++; 169 parsedFilesCount++;
170 // We found a worklist (i.e. a DICOM find with extension ".wl"), match it against the query 170 // We found a worklist (i.e. a DICOM find with extension ".wl"), match it against the query
171 if (MatchWorklist(answers, query, *matcher, it->path().string())) 171 if (MatchWorklist(answers, query, *matcher, it->path().string()))
172 { 172 {
173 OrthancPlugins::LogInfo(context_, "Worklist matched: " + it->path().string()); 173 OrthancPlugins::LogInfo("Worklist matched: " + it->path().string());
174 matchedWorklistCount++; 174 matchedWorklistCount++;
175 } 175 }
176 } 176 }
177 } 177 }
178 } 178 }
179 179
180 std::ostringstream message; 180 std::ostringstream message;
181 message << "Worklist C-Find: parsed " << parsedFilesCount << " files, found " << matchedWorklistCount << " match(es)"; 181 message << "Worklist C-Find: parsed " << parsedFilesCount << " files, found " << matchedWorklistCount << " match(es)";
182 OrthancPlugins::LogInfo(context_, message.str()); 182 OrthancPlugins::LogInfo(message.str());
183 183
184 } 184 }
185 catch (fs::filesystem_error&) 185 catch (fs::filesystem_error&)
186 { 186 {
187 OrthancPlugins::LogError(context_, "Inexistent folder while scanning for worklists: " + source.string()); 187 OrthancPlugins::LogError("Inexistent folder while scanning for worklists: " + source.string());
188 return OrthancPluginErrorCode_DirectoryExpected; 188 return OrthancPluginErrorCode_DirectoryExpected;
189 } 189 }
190 190
191 // Uncomment the following line if too many answers are to be returned 191 // Uncomment the following line if too many answers are to be returned
192 // OrthancPluginMarkWorklistAnswersIncomplete(context_, answers); 192 // OrthancPluginMarkWorklistAnswersIncomplete(OrthancPlugins::GetGlobalContext(), answers);
193 193
194 return OrthancPluginErrorCode_Success; 194 return OrthancPluginErrorCode_Success;
195 } 195 }
196 catch (OrthancPlugins::PluginException& e) 196 catch (OrthancPlugins::PluginException& e)
197 { 197 {
202 202
203 extern "C" 203 extern "C"
204 { 204 {
205 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c) 205 ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
206 { 206 {
207 context_ = c; 207 OrthancPlugins::SetGlobalContext(c);
208 208
209 /* Check the version of the Orthanc core */ 209 /* Check the version of the Orthanc core */
210 if (OrthancPluginCheckVersion(c) == 0) 210 if (OrthancPluginCheckVersion(c) == 0)
211 { 211 {
212 OrthancPlugins::ReportMinimalOrthancVersion(context_, 212 OrthancPlugins::ReportMinimalOrthancVersion(ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
213 ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER,
214 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER, 213 ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER,
215 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER); 214 ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER);
216 return -1; 215 return -1;
217 } 216 }
218 217
219 OrthancPlugins::LogWarning(context_, "Sample worklist plugin is initializing"); 218 OrthancPlugins::LogWarning("Sample worklist plugin is initializing");
220 OrthancPluginSetDescription(context_, "Serve DICOM modality worklists from a folder with Orthanc."); 219 OrthancPluginSetDescription(c, "Serve DICOM modality worklists from a folder with Orthanc.");
221 220
222 OrthancPlugins::OrthancConfiguration configuration(context_); 221 OrthancPlugins::OrthancConfiguration configuration;
223 222
224 OrthancPlugins::OrthancConfiguration worklists; 223 OrthancPlugins::OrthancConfiguration worklists;
225 configuration.GetSection(worklists, "Worklists"); 224 configuration.GetSection(worklists, "Worklists");
226 225
227 bool enabled = worklists.GetBooleanValue("Enable", false); 226 bool enabled = worklists.GetBooleanValue("Enable", false);
228 if (enabled) 227 if (enabled)
229 { 228 {
230 if (worklists.LookupStringValue(folder_, "Database")) 229 if (worklists.LookupStringValue(folder_, "Database"))
231 { 230 {
232 OrthancPlugins::LogWarning(context_, "The database of worklists will be read from folder: " + folder_); 231 OrthancPlugins::LogWarning("The database of worklists will be read from folder: " + folder_);
233 OrthancPluginRegisterWorklistCallback(context_, Callback); 232 OrthancPluginRegisterWorklistCallback(OrthancPlugins::GetGlobalContext(), Callback);
234 } 233 }
235 else 234 else
236 { 235 {
237 OrthancPlugins::LogError(context_, "The configuration option \"Worklists.Database\" must contain a path"); 236 OrthancPlugins::LogError("The configuration option \"Worklists.Database\" must contain a path");
238 return -1; 237 return -1;
239 } 238 }
240 239
241 filterIssuerAet_ = worklists.GetBooleanValue("FilterIssuerAet", false); 240 filterIssuerAet_ = worklists.GetBooleanValue("FilterIssuerAet", false);
242 } 241 }
243 else 242 else
244 { 243 {
245 OrthancPlugins::LogWarning(context_, "Worklist server is disabled by the configuration file"); 244 OrthancPlugins::LogWarning("Worklist server is disabled by the configuration file");
246 } 245 }
247 246
248 return 0; 247 return 0;
249 } 248 }
250 249
251 250
252 ORTHANC_PLUGINS_API void OrthancPluginFinalize() 251 ORTHANC_PLUGINS_API void OrthancPluginFinalize()
253 { 252 {
254 OrthancPluginLogWarning(context_, "Sample worklist plugin is finalizing"); 253 OrthancPlugins::LogWarning("Sample worklist plugin is finalizing");
255 } 254 }
256 255
257 256
258 ORTHANC_PLUGINS_API const char* OrthancPluginGetName() 257 ORTHANC_PLUGINS_API const char* OrthancPluginGetName()
259 { 258 {