PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusSendTextCommand.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"
9 
10 #include "vtkPlusDataCollector.h"
11 
13 
14 namespace
15 {
16  static const std::string SEND_TEXT_CMD = "SendText";
17 }
18 
19 //----------------------------------------------------------------------------
21  : ResponseExpected(true)
22 {
23  // It handles only one command, set its name by default
24  this->SetName(SEND_TEXT_CMD);
25 }
26 
27 //----------------------------------------------------------------------------
29 {
30 }
31 
32 //----------------------------------------------------------------------------
34 {
35  this->SetName(SEND_TEXT_CMD);
36 }
37 
38 //----------------------------------------------------------------------------
39 void vtkPlusSendTextCommand::GetCommandNames(std::list<std::string>& cmdNames)
40 {
41  cmdNames.clear();
42  cmdNames.push_back(SEND_TEXT_CMD);
43 }
44 
45 //----------------------------------------------------------------------------
46 std::string vtkPlusSendTextCommand::GetDescription(const std::string& commandName)
47 {
48  std::string desc;
49  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, SEND_TEXT_CMD))
50  {
51  desc += SEND_TEXT_CMD;
52  desc += ": Send text data to the device.";
53  }
54  return desc;
55 }
56 
57 //----------------------------------------------------------------------------
59 {
60  return this->DeviceId;
61 }
62 
63 //----------------------------------------------------------------------------
64 void vtkPlusSendTextCommand::SetDeviceId(const std::string& deviceId)
65 {
66  this->DeviceId = deviceId;
67 }
68 
69 //----------------------------------------------------------------------------
71 {
72  return this->Text;
73 }
74 
75 //----------------------------------------------------------------------------
76 void vtkPlusSendTextCommand::SetText(const std::string& text)
77 {
78  this->Text = text;
79 }
80 
81 //----------------------------------------------------------------------------
83 {
84  return this->ResponseText;
85 }
86 
87 //----------------------------------------------------------------------------
88 void vtkPlusSendTextCommand::SetResponseText(const std::string& responseText)
89 {
90  this->ResponseText = responseText;
91 }
92 
93 //----------------------------------------------------------------------------
94 void vtkPlusSendTextCommand::PrintSelf(ostream& os, vtkIndent indent)
95 {
96  this->Superclass::PrintSelf(os, indent);
97  os << indent << "Text: " << this->Text;
98 }
99 
100 //----------------------------------------------------------------------------
102 {
104  {
105  return PLUS_FAIL;
106  }
107  XML_READ_STRING_ATTRIBUTE_OPTIONAL(DeviceId, aConfig);
108  XML_READ_STRING_ATTRIBUTE_OPTIONAL(Text, aConfig);
109  return PLUS_SUCCESS;
110 }
111 
112 //----------------------------------------------------------------------------
114 {
116  {
117  return PLUS_FAIL;
118  }
119  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(DeviceId, aConfig);
120  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(Text, aConfig);
121  return PLUS_SUCCESS;
122 }
123 
124 //----------------------------------------------------------------------------
126 {
127  LOG_DEBUG("vtkPlusSendTextCommand::Execute: " << (!this->Name.empty() ? this->Name : "(undefined)")
128  << ", device: " << (this->DeviceId.empty() ? "(undefined)" : this->DeviceId)
129  << ", text: " << (this->Text.empty() ? "(undefined)" : this->Text));
130 
131  vtkPlusDataCollector* dataCollector = GetDataCollector();
132  if (dataCollector == NULL)
133  {
134  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "Invalid data collector.");
135  return PLUS_FAIL;
136  }
137 
138  // Get device pointer
139  if (this->DeviceId.empty())
140  {
141  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "No DeviceId specified.");
142  return PLUS_FAIL;
143  }
144  vtkPlusDevice* device = NULL;
145  if (dataCollector->GetDevice(device, this->DeviceId) != PLUS_SUCCESS)
146  {
147  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Device ")
148  + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId) + std::string(" is not found."));
149  return PLUS_FAIL;
150  }
151 
152  // Send text (and receive response)
153  std::string textToSend;
154  std::string response;
155  if (!this->GetText().empty())
156  {
157  textToSend = GetText();
158  }
159  PlusStatus status = device->SendText(GetText(), GetResponseExpected() ? &response : NULL);
160  SetResponseText(response.c_str());
161  if (status != PLUS_SUCCESS)
162  {
163  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Failed to send text '") + GetText() + "'"
164  + " to device " + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId));
165  return PLUS_FAIL;
166  }
167  this->QueueCommandResponse(PLUS_SUCCESS, response);
168  return PLUS_SUCCESS;
169 }
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 void PrintSelf(ostream &os, vtkIndent indent)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
virtual std::string GetDescription(const std::string &commandName)
virtual void SetText(const std::string &text)
virtual std::string GetText() const
#define PLUS_FAIL
Definition: PlusCommon.h:43
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
This command send text to a device and optionally return with a response textThis command is used for...
PhidgetLCD_Font int int const char * text
Definition: phidget22.h:4287
Manages devices that record image or positional data.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
void QueueCommandResponse(PlusStatus status, const std::string &message, const std::string &error="", const igtl::MessageBase::MetaDataMap *metaData=nullptr)
virtual std::string GetDeviceId() const
virtual vtkPlusDataCollector * GetDataCollector()
std::string DeviceId
virtual std::string GetResponseText() const
vtkStandardNewMacro(vtkPlusSendTextCommand)
virtual void GetCommandNames(std::list< std::string > &cmdNames)
virtual bool GetResponseExpected()
virtual void SetDeviceId(const std::string &deviceId)
virtual PlusStatus SendText(const std::string &textToSend, std::string *textReceived=NULL)
virtual void SetResponseText(const std::string &responseText)