PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusUSDigitalEncoderCommand.cxx
Go to the documentation of this file.
1 /*=Plus=header=begin======================================================
2 Program: Plus
3 Copyright (c) Laboratory for Percutaneous Surgery. All rights reserved.
4 See License.txt for details.
5 =========================================================Plus=header=end*/
6 
7 #include "PlusConfigure.h"
10 
11 #include "vtkPlusDataCollector.h"
12 
14 
15 namespace
16 {
17  static const std::string ENCODER_CMD = "USDigitalEncoder";
18 }
19 
20 //----------------------------------------------------------------------------
22  : ResponseExpected(true)
23 {
24  this->SetName(ENCODER_CMD);
25 }
26 
27 //----------------------------------------------------------------------------
29 {
30 }
31 
32 //----------------------------------------------------------------------------
34 {
35  this->SetName(ENCODER_CMD);
36 }
37 
38 //----------------------------------------------------------------------------
39 void vtkPlusUSDigitalEncoderCommand::GetCommandNames(std::list<std::string>& cmdNames)
40 {
41  cmdNames.clear();
42  cmdNames.push_back(ENCODER_CMD);
43 }
44 
45 //----------------------------------------------------------------------------
46 std::string vtkPlusUSDigitalEncoderCommand::GetDescription(const std::string& commandName)
47 {
48  std::string desc;
49  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, ENCODER_CMD))
50  {
51  desc += ENCODER_CMD;
52  desc += ": Send commands to a USDigital Encoder device.";
53  }
54  return desc;
55 }
56 
57 //----------------------------------------------------------------------------
59 {
60  return this->DeviceId;
61 }
62 
63 //----------------------------------------------------------------------------
64 void vtkPlusUSDigitalEncoderCommand::SetDeviceId(const std::string& deviceId)
65 {
66  this->DeviceId = deviceId;
67 }
68 
69 //----------------------------------------------------------------------------
70 void vtkPlusUSDigitalEncoderCommand::PrintSelf(ostream& os, vtkIndent indent)
71 {
72  this->Superclass::PrintSelf(os, indent);
73  os << indent << "Device Id: " << this->DeviceId;
74 }
75 
76 //----------------------------------------------------------------------------
78 {
80  {
81  return PLUS_FAIL;
82  }
83  this->CommandList.clear();
84  XML_READ_STRING_ATTRIBUTE_OPTIONAL(DeviceId, aConfig);
85 
86  // Parse nested elements and store requested parameter changes
87  for (int elemIndex = 0; elemIndex < aConfig->GetNumberOfNestedElements(); ++elemIndex)
88  {
89  vtkXMLDataElement* currentElem = aConfig->GetNestedElement(elemIndex);
90  if (igsioCommon::IsEqualInsensitive(currentElem->GetName(), vtkPlusUSDigitalEncodersTracker::USDIGITALENCODER_COMMAND_SET_ORIGIN))
91  {
92  const char* encoderId = currentElem->GetAttribute("EncoderId");
93  if (!encoderId)
94  {
95  LOG_ERROR("Unable to find required EncoderId in " << (currentElem->GetName() ? currentElem->GetName() : "(undefined)") << " element in USDigitalEncoder command");
96  continue;
97  }
99  }
100  else if (igsioCommon::IsEqualInsensitive(currentElem->GetName(), vtkPlusUSDigitalEncodersTracker::USDIGITALENCODER_COMMAND_SET_ALL_ORIGIN))
101  {
103  }
104  else if (igsioCommon::IsEqualInsensitive(currentElem->GetName(), vtkPlusUSDigitalEncodersTracker::USDIGITALENCODER_COMMAND_GET_POSITION))
105  {
106  const char* encoderId = currentElem->GetAttribute("EncoderId");
107  if (!encoderId)
108  {
109  LOG_ERROR("Unable to find required EncoderId in " << (currentElem->GetName() ? currentElem->GetName() : "(undefined)") << " element in USDigitalEncoder command");
110  continue;
111  }
113  }
114  else {
115  LOG_ERROR("Invalid command name (" << currentElem->GetName() << ") provided to USDigitalEncoderCommand.");
116  }
117  }
118  return PLUS_SUCCESS;
119 }
120 
121 //----------------------------------------------------------------------------
123 {
125  {
126  return PLUS_FAIL;
127  }
128  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(DeviceId, aConfig);
129  return PLUS_SUCCESS;
130 }
131 
132 //----------------------------------------------------------------------------
134 {
135  LOG_DEBUG("vtkPlusUSDigitalEncoderCommand::Execute: " << (!this->Name.empty() ? this->Name : "(undefined)")
136  << ", device: " << (this->DeviceId.empty() ? "(undefined)" : this->DeviceId));
137 
138  vtkPlusDataCollector* dataCollector = GetDataCollector();
139  if (dataCollector == NULL)
140  {
141  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "Invalid data collector.");
142  return PLUS_FAIL;
143  }
144 
145  // Get device pointer
146  if (this->DeviceId.empty())
147  {
148  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "No DeviceId specified.");
149  return PLUS_FAIL;
150  }
151  vtkPlusDevice* aDevice = NULL;
152  if (dataCollector->GetDevice(aDevice, this->DeviceId) != PLUS_SUCCESS)
153  {
154  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Device ")
155  + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId) + std::string(" is not found."));
156  return PLUS_FAIL;
157  }
158 
159  vtkPlusUSDigitalEncodersTracker* device = dynamic_cast<vtkPlusUSDigitalEncodersTracker*>(aDevice);
160  std::string resultString = "<CommandReply>";
161  std::string error = "";
162  std::map < std::string, std::pair<IANA_ENCODING_TYPE, std::string> > metaData;
163  PlusStatus status = PLUS_SUCCESS;
164 
165  std::list<std::pair<std::string, std::string>>::iterator commandIt;
166  for (commandIt = this->CommandList.begin(); commandIt != this->CommandList.end(); ++commandIt)
167  {
168  std::string commandName = commandIt->first;
169  std::string encoderId = commandIt->second;
170 
171  resultString += "<Command Name=\"" + commandName + "\" ";
172 
174  {
175  int encoderId_int = std::stoi(encoderId);
176  if (device->SetUSDigitalA2EncoderOriginWithID(encoderId_int) == PLUS_SUCCESS)
177  {
178  resultString += "Success=\"true\"/>";
179  }
180  else
181  {
182  resultString += "Success=\"false\"/>";
183  status = PLUS_FAIL;
184  }
185  }
187  {
189  {
190  resultString += "Success=\"true\"/>";
191  }
192  else
193  {
194  resultString += "Success=\"false\"/>";
195  status = PLUS_FAIL;
196  }
197  }
199  {
200  int encoderId_int = std::stoi(encoderId);
201  long encoderPos;
202  if (device->GetUSDigitalA2EncoderPositionWithID(encoderId_int, &encoderPos) == PLUS_SUCCESS)
203  {
204  std::stringstream ss;
205  ss << encoderPos;
206  resultString += "Success=\"true\" EncoderId=\"" + encoderId + "\" Position=\"" + ss.str() + "\"/>";
207  }
208  else
209  {
210  resultString += "Success=\"false\" EncoderId=\"" + encoderId + "\"/>";
211  status = PLUS_FAIL;
212  }
213  }
214  }
215  resultString += "</CommandReply>";
216 
217  if (status != PLUS_SUCCESS)
218  {
219  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Failed to execute command '")
220  + " for device " + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId));
221  return PLUS_FAIL;
222  }
223 
224  vtkSmartPointer<vtkPlusCommandRTSCommandResponse> commandResponse = vtkSmartPointer<vtkPlusCommandRTSCommandResponse>::New();
225  commandResponse->UseDefaultFormatOff();
226  commandResponse->SetClientId(this->ClientId);
227  commandResponse->SetOriginalId(this->Id);
228  commandResponse->SetDeviceName(this->DeviceName);
229  commandResponse->SetCommandName(this->GetName());
230  commandResponse->SetStatus(status);
231  commandResponse->SetRespondWithCommandMessage(this->RespondWithCommandMessage);
232  commandResponse->SetErrorString(error);
233  commandResponse->SetResultString(resultString);
234  commandResponse->SetParameters(metaData);
235  this->CommandResponseQueue.push_back(commandResponse);
236  return PLUS_SUCCESS;
237 }
virtual void PrintSelf(ostream &os, vtkIndent indent)
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
PlusStatus GetDevice(vtkPlusDevice *&aDevice, const std::string &aDeviceId) const
std::string Name
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
#define PLUS_FAIL
Definition: PlusCommon.h:43
This command controls USDigitalEncodersTracker devices.This command is used for controlling a USDigit...
PlusStatus GetUSDigitalA2EncoderPositionWithID(long id, long *pos)
Manages devices that record image or positional data.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
Interface for multiple US Digital A2, A2T, A4, HBA2, HBA4 or HD25A encoders to generate pose informat...
bool RespondWithCommandMessage
void QueueCommandResponse(PlusStatus status, const std::string &message, const std::string &error="", const igtl::MessageBase::MetaDataMap *metaData=nullptr)
virtual vtkPlusDataCollector * GetDataCollector()
std::string DeviceId
std::string DeviceName
virtual void GetCommandNames(std::list< std::string > &cmdNames)
vtkStandardNewMacro(vtkPlusUSDigitalEncoderCommand)
virtual void SetDeviceId(const std::string &deviceId)
std::list< std::pair< std::string, std::string > > CommandList
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
virtual void PrintSelf(ostream &os, vtkIndent indent)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
PlusCommandResponseList CommandResponseQueue
virtual std::string GetDescription(const std::string &commandName)