PlusLib  2.9.0
Software library for tracked ultrasound image acquisition, calibration, and processing.
vtkPlusGenericSerialCommand.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 SERIAL_CMD_NAME = "SerialCommand";
18 }
19 
20 //----------------------------------------------------------------------------
22  : ResponseExpected(true)
23 {
24  // It handles only one command, set its name by default
25  this->SetName(SERIAL_CMD_NAME);
26 }
27 
28 //----------------------------------------------------------------------------
30 {
31 }
32 
33 //----------------------------------------------------------------------------
35 {
36  this->SetName(SERIAL_CMD_NAME);
37 }
38 
39 //----------------------------------------------------------------------------
40 void vtkPlusGenericSerialCommand::GetCommandNames(std::list<std::string>& cmdNames)
41 {
42  cmdNames.clear();
43  cmdNames.push_back(SERIAL_CMD_NAME);
44 }
45 
46 //----------------------------------------------------------------------------
47 std::string vtkPlusGenericSerialCommand::GetDescription(const std::string& commandName)
48 {
49  std::string desc;
50  if (commandName.empty() || igsioCommon::IsEqualInsensitive(commandName, SERIAL_CMD_NAME))
51  {
52  desc += SERIAL_CMD_NAME;
53  desc += ": Send text data to the device.";
54  }
55  return desc;
56 }
57 
58 //----------------------------------------------------------------------------
60 {
61  return this->DeviceId;
62 }
63 
64 //----------------------------------------------------------------------------
65 void vtkPlusGenericSerialCommand::SetDeviceId(const std::string& deviceId)
66 {
67  this->DeviceId = deviceId;
68 }
69 
70 //----------------------------------------------------------------------------
72 {
73  return this->CommandName;
74 }
75 
76 //----------------------------------------------------------------------------
78 {
79  this->CommandName = text;
80 }
81 
82 //----------------------------------------------------------------------------
84 {
85  return this->CommandValue;
86 }
87 
88 //----------------------------------------------------------------------------
90 {
91  this->CommandValue = text;
92 }
93 
94 //----------------------------------------------------------------------------
95 void vtkPlusGenericSerialCommand::PrintSelf(ostream& os, vtkIndent indent)
96 {
97  this->Superclass::PrintSelf(os, indent);
98 }
99 
100 //----------------------------------------------------------------------------
102 {
104  {
105  return PLUS_FAIL;
106  }
107 
108  XML_READ_STRING_ATTRIBUTE_OPTIONAL(DeviceId, aConfig);
109  XML_READ_STRING_ATTRIBUTE_OPTIONAL(CommandName, aConfig);
110  XML_READ_STRING_ATTRIBUTE_OPTIONAL(CommandValue, aConfig);
111  return PLUS_SUCCESS;
112 
113 }
114 
115 //----------------------------------------------------------------------------
117 {
119  {
120  return PLUS_FAIL;
121  }
122  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(DeviceId, aConfig);
123  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(CommandName, aConfig);
124  XML_WRITE_STRING_ATTRIBUTE_IF_NOT_EMPTY(CommandValue, aConfig);
125  return PLUS_SUCCESS;
126 }
127 
128 //----------------------------------------------------------------------------
130 {
131  LOG_DEBUG("vtkPlusGenericSerialCommand::Execute: " << (!this->CommandName.empty() ? this->CommandName : "(undefined)")
132  << ", device: " << (this->DeviceId.empty() ? "(undefined)" : this->DeviceId)
133  << ", value: " << (this->CommandValue.empty() ? "(undefined)" : this->CommandValue));
134 
135  vtkPlusDataCollector* dataCollector = GetDataCollector();
136  if (dataCollector == NULL)
137  {
138  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "Invalid data collector.");
139  return PLUS_FAIL;
140  }
141 
142  // Get device pointer
143  if (this->DeviceId.empty())
144  {
145  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", "No DeviceId specified.");
146  return PLUS_FAIL;
147  }
148  vtkPlusDevice* aDevice = NULL;
149  if (dataCollector->GetDevice(aDevice, this->DeviceId) != PLUS_SUCCESS)
150  {
151  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Device ")
152  + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId) + std::string(" is not found."));
153  return PLUS_FAIL;
154  }
155  vtkPlusGenericSerialDevice* device = dynamic_cast<vtkPlusGenericSerialDevice*>(aDevice);
156 
157  // Send text (and receive response)
158  std::string textToSend;
159  std::string response;
160  if (!this->GetCommandValue().empty())
161  {
162  textToSend = GetCommandValue();
163  }
164  PlusStatus status;
165  if (igsioCommon::IsEqualInsensitive(this->GetCommandName(), vtkPlusGenericSerialDevice::SERIAL_COMMAND_GET_RTS))
166  {
167  response = device->GetRTS() ? "True" : "False";
168  status = PLUS_SUCCESS;
169  }
170  else if (igsioCommon::IsEqualInsensitive(this->GetCommandName(), vtkPlusGenericSerialDevice::SERIAL_COMMAND_SET_RTS))
171  {
172  bool onOff = textToSend == "True" ? true : false;
173  status = device->SetRTS(onOff);
174  }
175  else if (igsioCommon::IsEqualInsensitive(this->GetCommandName(), vtkPlusGenericSerialDevice::SERIAL_COMMAND_GET_CTS))
176  {
177  bool onOff;
178  status = device->GetCTS(onOff);
179  response = onOff ? "True" : "False";
180  }
181  else
182  {
183  status = PLUS_FAIL;
184  }
185  if (status != PLUS_SUCCESS)
186  {
187  this->QueueCommandResponse(PLUS_FAIL, "Command failed. See error message.", std::string("Failed to execute command '") + GetCommandName() + "'"
188  + " on device " + (this->DeviceId.empty() ? "(undefined)" : this->DeviceId));
189  return PLUS_FAIL;
190  }
191  this->QueueCommandResponse(PLUS_SUCCESS, response);
192  return PLUS_SUCCESS;
193 }
virtual void PrintSelf(ostream &os, vtkIndent indent)
virtual void PrintSelf(ostream &os, vtkIndent indent)
Abstract interface for tracker and video devices.
Definition: vtkPlusDevice.h:60
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
static const char * SERIAL_COMMAND_GET_CTS
PlusStatus GetDevice(vtkPlusDevice *&aDevice, const std::string &aDeviceId) const
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
virtual void SetDeviceId(const std::string &deviceId)
igsioStatus PlusStatus
Definition: PlusCommon.h:40
virtual std::string GetDescription(const std::string &commandName)
virtual PlusStatus ReadConfiguration(vtkXMLDataElement *aConfig)
#define PLUS_FAIL
Definition: PlusCommon.h:43
PhidgetLCD_Font int int const char * text
Definition: phidget22.h:4287
static const char * SERIAL_COMMAND_GET_RTS
Manages devices that record image or positional data.
#define PLUS_SUCCESS
Definition: PlusCommon.h:44
void SetCommandValue(const std::string &text)
virtual void GetCommandNames(std::list< std::string > &cmdNames)
void QueueCommandResponse(PlusStatus status, const std::string &message, const std::string &error="", const igtl::MessageBase::MetaDataMap *metaData=nullptr)
virtual vtkPlusDataCollector * GetDataCollector()
std::string DeviceId
vtkStandardNewMacro(vtkPlusGenericSerialCommand)
virtual PlusStatus WriteConfiguration(vtkXMLDataElement *aConfig)
virtual std::string GetDeviceId() const
static const char * SERIAL_COMMAND_SET_RTS
This command is for communicating with vtkPlusGenericSerialDevices.This command is used for communica...
Generic interface for communicating with a serial device.
void SetCommandName(const std::string &text)