16 #include "igtlioLogic.h" 17 #include "igtlioImageDevice.h" 18 #include "igtlioTransformDevice.h" 19 #include "igtlioStatusDevice.h" 20 #include "igtlioStringDevice.h" 22 #include "igtlioConnector.h" 24 #include "igtlioImageConverter.h" 25 #include "igtlioTransformConverter.h" 26 #include "igtlioCommandConverter.h" 27 #include "igtlioStatusConverter.h" 28 #include "igtlioStringConverter.h" 29 #include "igtlioUsSectorDefinitions.h" 37 mTimer(new QTimer(this)),
40 qRegisterMetaType<Transform3D>(
"Transform3D");
41 qRegisterMetaType<ImagePtr>(
"ImagePtr");
45 this->connectToConnectionEvents();
46 this->connectToDeviceEvents();
48 connect(mTimer, SIGNAL(timeout()),
this, SLOT(periodicProcess()));
59 mSession = mLogic->ConnectToServer(serverHost, serverPort, sync, timeout_s);
65 if (mSession->GetConnector() && mSession->GetConnector()->GetState()!=igtlioConnector::STATE_OFF)
67 CX_LOG_DEBUG() <<
"NetworkHandler: Disconnecting from server" << mSession->GetConnector()->GetName();
68 igtlioConnectorPointer connector = mSession->GetConnector();
70 mLogic->RemoveConnector(connector);
72 mProbeDefinitionFromStringMessages->reset();
75 void NetworkHandler::onDeviceReceived(vtkObject* caller_device,
void* unknown,
unsigned long event ,
void*)
79 vtkSmartPointer<igtlioDevice> receivedDevice(reinterpret_cast<igtlioDevice*>(caller_device));
81 igtlioBaseConverter::HeaderData header = receivedDevice->GetHeader();
82 std::string device_type = receivedDevice->GetDeviceType();
89 QString deviceName(receivedDevice->GetDeviceName().c_str());
91 if(device_type == igtlioImageConverter::GetIGTLTypeName())
93 igtlioImageDevicePointer imageDevice = igtlioImageDevice::SafeDownCast(receivedDevice);
95 igtlioImageConverter::ContentData content = imageDevice->GetContent();
101 double timestampMS = header.timestamp * 1000;
102 cximage->setAcquisitionTime( QDateTime::fromMSecsSinceEpoch(qint64(timestampMS)));
107 std::string metaLabel;
108 std::string metaDataValue;
109 QStringList igtlioLabels;
111 igtlioLabels << IGTLIO_KEY_PROBE_TYPE;
112 igtlioLabels << IGTLIO_KEY_ORIGIN;
113 igtlioLabels << IGTLIO_KEY_ANGLES;
114 igtlioLabels << IGTLIO_KEY_BOUNDING_BOX;
115 igtlioLabels << IGTLIO_KEY_DEPTHS;
116 igtlioLabels << IGTLIO_KEY_LINEAR_WIDTH;
117 igtlioLabels << IGTLIO_KEY_SPACING_X;
118 igtlioLabels << IGTLIO_KEY_SPACING_Y;
122 for (
int i = 0; i < igtlioLabels.size(); ++i)
124 metaLabel = igtlioLabels[i].toStdString();
125 bool gotMetaData = receivedDevice->GetMetaDataElement(metaLabel, metaDataValue);
127 CX_LOG_WARNING() <<
"Cannot get needed igtlio meta information: " << metaLabel;
129 mProbeDefinitionFromStringMessages->parseValue(metaLabel.c_str(), metaDataValue.c_str());
133 mProbeDefinitionFromStringMessages->setImage(cximage);
135 if (mProbeDefinitionFromStringMessages->haveValidValues() && mProbeDefinitionFromStringMessages->haveChanged())
138 emit
probedefinition(deviceName, mProbeDefinitionFromStringMessages->createProbeDefintion(deviceName));
149 else if(device_type == igtlioTransformConverter::GetIGTLTypeName())
151 igtlioTransformDevicePointer transformDevice = igtlioTransformDevice::SafeDownCast(receivedDevice);
152 igtlioTransformConverter::ContentData content = transformDevice->GetContent();
158 Transform3D cxtransform = Transform3D::fromVtkMatrix(content.transform);
166 double timestamp = header.timestamp;
174 std::string openigtlinktransformid;
175 bool gotTransformId = receivedDevice->GetMetaDataElement(
"equipmentId", openigtlinktransformid);
180 emit
transform(deviceName, cxtransform, timestamp);
182 else if(device_type == igtlioStatusConverter::GetIGTLTypeName())
184 igtlioStatusDevicePointer status = igtlioStatusDevice::SafeDownCast(receivedDevice);
186 igtlioStatusConverter::ContentData content = status->GetContent();
188 CX_LOG_DEBUG() <<
"STATUS: " <<
" code: " << content.code
189 <<
" subcode: " << content.subcode
190 <<
" errorname: " << content.errorname
191 <<
" statusstring: " << content.statusstring;
194 else if(device_type == igtlioStringConverter::GetIGTLTypeName())
196 igtlioStringDevicePointer
string = igtlioStringDevice::SafeDownCast(receivedDevice);
198 igtlioStringConverter::ContentData content =
string->GetContent();
204 QString message(content.string_msg.c_str());
210 CX_LOG_WARNING() <<
"Found unhandled devicetype: " << device_type;
215 void NetworkHandler::onConnectionEvent(vtkObject* caller,
void* connector,
unsigned long event ,
void*)
219 if (event==igtlioLogic::ConnectionAddedEvent)
223 if (event==igtlioLogic::ConnectionAboutToBeRemovedEvent)
229 void NetworkHandler::onDeviceAddedOrRemoved(vtkObject* caller,
void* void_device,
unsigned long event,
void* callData)
233 if (event==igtlioLogic::NewDeviceEvent)
235 igtlioDevicePointer device(reinterpret_cast<igtlioDevice*>(void_device));
238 CX_LOG_DEBUG() <<
" NetworkHandler is listening to " << device->GetDeviceName();
239 qvtkReconnect(NULL, device, igtlioDevice::ReceiveEvent,
this, SLOT(onDeviceReceived(vtkObject*,
void*,
unsigned long,
void*)));
242 if (event==igtlioLogic::RemovedDeviceEvent)
244 CX_LOG_WARNING() <<
"TODO: on remove device event, not implemented";
248 void NetworkHandler::periodicProcess()
250 mLogic->PeriodicProcess();
253 void NetworkHandler::connectToConnectionEvents()
255 foreach(
int eventId, QList<int>()
256 << igtlioLogic::ConnectionAddedEvent
257 << igtlioLogic::ConnectionAboutToBeRemovedEvent
260 qvtkReconnect(NULL, mLogic, eventId,
261 this, SLOT(onConnectionEvent(vtkObject*,
void*,
unsigned long,
void*)));
265 void NetworkHandler::connectToDeviceEvents()
267 foreach(
int eventId, QList<int>()
268 << igtlioLogic::NewDeviceEvent
269 << igtlioLogic::RemovedDeviceEvent
272 qvtkReconnect(NULL, mLogic, eventId,
273 this, SLOT(onDeviceAddedOrRemoved(vtkObject*,
void*,
unsigned long,
void*)));
QString qstring_cast(const T &val)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
igtlioSessionPointer requestConnectToServer(std::string serverHost, int serverPort=-1, IGTLIO_SYNCHRONIZATION_TYPE sync=IGTLIO_BLOCKING, double timeout_s=5)
void string_message(QString message)
void transform(QString devicename, Transform3D transform, double timestamp)
void probedefinition(QString devicename, ProbeDefinitionPtr definition)
void disconnectFromServer()
boost::shared_ptr< class ProbeDefinitionFromStringMessages > ProbeDefinitionFromStringMessagesPtr
NetworkHandler(igtlioLogicPointer logic)
void image(ImagePtr image)
Namespace for all CustusX production code.