comparison OrthancServer/ServerContext.cpp @ 1007:871c49c9b11d lua-scripting

lua routing is working
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Jul 2014 17:35:00 +0200
parents 649d47854314
children 187ed107a59f
comparison
equal deleted inserted replaced
1006:649d47854314 1007:871c49c9b11d
44 #include <dcmtk/dcmdata/dcfilefo.h> 44 #include <dcmtk/dcmdata/dcfilefo.h>
45 45
46 46
47 #include "Scheduler/DeleteInstanceCommand.h" 47 #include "Scheduler/DeleteInstanceCommand.h"
48 #include "Scheduler/StoreScuCommand.h" 48 #include "Scheduler/StoreScuCommand.h"
49 #include "Scheduler/StorePeerCommand.h"
49 50
50 51
51 52
52 #define ENABLE_DICOM_CACHE 1 53 #define ENABLE_DICOM_CACHE 1
53 54
118 119
119 return true; 120 return true;
120 } 121 }
121 122
122 123
124 static IServerCommand* ParseOperation(ServerContext& context,
125 const std::string& operation,
126 const Json::Value& parameters)
127 {
128 if (operation == "delete")
129 {
130 LOG(INFO) << "Lua script to delete instance " << parameters["instance"].asString();
131 return new DeleteInstanceCommand(context);
132 }
133
134 if (operation == "store-scu")
135 {
136 std::string modality = parameters["modality"].asString();
137 LOG(INFO) << "Lua script to send instance " << parameters["instance"].asString()
138 << " to modality " << modality << " using Store-SCU";
139 return new StoreScuCommand(context, Configuration::GetModalityUsingSymbolicName(modality));
140 }
141
142 if (operation == "store-peer")
143 {
144 std::string peer = parameters["peer"].asString();
145 LOG(INFO) << "Lua script to send instance " << parameters["instance"].asString()
146 << " to peer " << peer << " using HTTP";
147
148 OrthancPeerParameters parameters;
149 Configuration::GetOrthancPeer(parameters, peer);
150 return new StorePeerCommand(context, parameters);
151 }
152
153 throw OrthancException(ErrorCode_ParameterOutOfRange);
154 }
155
156
123 void ServerContext::ApplyOnStoredInstance(const std::string& instanceId, 157 void ServerContext::ApplyOnStoredInstance(const std::string& instanceId,
124 const Json::Value& simplifiedDicom, 158 const Json::Value& simplifiedDicom,
125 const Json::Value& metadata) 159 const Json::Value& metadata)
126 { 160 {
127 LuaContextLocker locker(*this); 161 LuaContextLocker locker(*this);
128 162
129 if (locker.GetLua().IsExistingFunction(ON_STORED_INSTANCE)) 163 if (locker.GetLua().IsExistingFunction(ON_STORED_INSTANCE))
130 { 164 {
165 locker.GetLua().Execute("_InitializeJob()");
166
131 LuaFunctionCall call(locker.GetLua(), ON_STORED_INSTANCE); 167 LuaFunctionCall call(locker.GetLua(), ON_STORED_INSTANCE);
132 call.PushString(instanceId); 168 call.PushString(instanceId);
133 call.PushJson(simplifiedDicom); 169 call.PushJson(simplifiedDicom);
134 call.PushJson(metadata); 170 call.PushJson(metadata);
135 171 call.Execute();
136 Json::Value result; 172
137 call.ExecuteToJson(result); 173 Json::Value operations;
138 174 LuaFunctionCall call2(locker.GetLua(), "_AccessJob");
139 printf("TODO\n"); 175 call2.ExecuteToJson(operations);
140 std::cout << result; 176
141 } 177 if (operations.type() != Json::arrayValue)
142 178 {
143 #if 0 179 throw OrthancException(ErrorCode_InternalError);
144 { 180 }
145 // Autorouting test
146 RemoteModalityParameters p = Configuration::GetModalityUsingSymbolicName("sample");
147 181
148 ServerJob job; 182 ServerJob job;
149 ServerCommandInstance& a = job.AddCommand(new StoreScuCommand(*this, p)); 183 ServerCommandInstance* previousCommand = NULL;
150 a.AddInput(instanceId); 184
151 185 for (Json::Value::ArrayIndex i = 0; i < operations.size(); ++i)
152 /*ServerCommandInstance& b = job.AddCommand(new DeleteInstanceCommand(*this)); 186 {
153 a.ConnectNext(b);*/ 187 if (operations[i].type() != Json::objectValue ||
154 188 !operations[i].isMember("operation"))
155 job.SetDescription("Autorouting test"); 189 {
190 throw OrthancException(ErrorCode_InternalError);
191 }
192
193 const Json::Value& parameters = operations[i];
194 std::string operation = parameters["operation"].asString();
195
196 ServerCommandInstance& command = job.AddCommand(ParseOperation(*this, operation, operations[i]));
197
198 if (parameters.isMember("instance") &&
199 parameters["instance"].asString() != "")
200 {
201 command.AddInput(parameters["instance"].asString());
202 }
203 else if (previousCommand != NULL)
204 {
205 previousCommand->ConnectNext(command);
206 }
207 else
208 {
209 throw OrthancException(ErrorCode_InternalError);
210 }
211
212 previousCommand = &command;
213 }
214
215 job.SetDescription(std::string("Lua script: ") + ON_STORED_INSTANCE);
156 scheduler_.Submit(job); 216 scheduler_.Submit(job);
157 } 217 }
158 #endif
159 } 218 }
160 219
161 220
162 StoreStatus ServerContext::Store(std::string& resultPublicId, 221 StoreStatus ServerContext::Store(std::string& resultPublicId,
163 DicomInstanceToStore& dicom) 222 DicomInstanceToStore& dicom)
223 } 282 }
224 283
225 if (status == StoreStatus_Success || 284 if (status == StoreStatus_Success ||
226 status == StoreStatus_AlreadyStored) 285 status == StoreStatus_AlreadyStored)
227 { 286 {
287 Json::Value metadata = Json::objectValue;
288 for (std::map<MetadataType, std::string>::const_iterator
289 it = instanceMetadata.begin();
290 it != instanceMetadata.end(); ++it)
291 {
292 metadata[EnumerationToString(it->first)] = it->second;
293 }
294
228 try 295 try
229 { 296 {
230 #if 1
231 Json::Value metadata = Json::objectValue;
232 for (std::map<MetadataType, std::string>::const_iterator
233 it = instanceMetadata.begin();
234 it != instanceMetadata.end(); ++it)
235 {
236 metadata[EnumerationToString(it->first)] = it->second;
237 }
238 #else
239 Json::Value metadata;
240 index_.GetMetadata(metadata, resultPublicId);
241 #endif
242
243 std::cout << metadata;
244
245 ApplyOnStoredInstance(resultPublicId, simplified, metadata); 297 ApplyOnStoredInstance(resultPublicId, simplified, metadata);
246 } 298 }
247 catch (OrthancException&) 299 catch (OrthancException& e)
248 { 300 {
249 LOG(ERROR) << "Error when dealing with OnStoredInstance"; 301 LOG(ERROR) << "Lua error in OnStoredInstance: " << e.What();
250 } 302 }
251 } 303 }
252 304
253 return status; 305 return status;
254 } 306 }