16 #include "igtlioLogic.h" 17 #include "igtlioImageDevice.h" 18 #include "igtlioTransformDevice.h" 19 #include "igtlioCommandDevice.h" 20 #include "igtlioStatusDevice.h" 21 #include "igtlioStringDevice.h" 23 #include "igtlioConnector.h" 25 #include "igtlioImageConverter.h" 26 #include "igtlioTransformConverter.h" 27 #include "igtlioCommandConverter.h" 28 #include "igtlioStatusConverter.h" 29 #include "igtlioStringConverter.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()!=igtlio::Connector::STATE_OFF)
67 CX_LOG_DEBUG() <<
"NetworkHandler: Disconnecting from server" << mSession->GetConnector()->GetName();
68 igtlio::ConnectorPointer connector = mSession->GetConnector();
69 if(connector->GetState() != igtlio::Connector::STATE_WAIT_CONNECTION)
75 connector->SetServerStopFlag(
true);
77 mLogic->RemoveConnector(connector);
79 mProbeDefinitionFromStringMessages->reset();
82 void NetworkHandler::onDeviceReceived(vtkObject* caller_device,
void* unknown,
unsigned long event ,
void*)
86 vtkSmartPointer<igtlio::Device> receivedDevice(reinterpret_cast<igtlio::Device*>(caller_device));
88 igtlio::BaseConverter::HeaderData header = receivedDevice->GetHeader();
89 std::string device_type = receivedDevice->GetDeviceType();
96 QString deviceName(receivedDevice->GetDeviceName().c_str());
98 if(device_type == igtlio::ImageConverter::GetIGTLTypeName())
100 igtlio::ImageDevicePointer imageDevice = igtlio::ImageDevice::SafeDownCast(receivedDevice);
102 igtlio::ImageConverter::ContentData content = imageDevice->GetContent();
108 double timestampMS = header.timestamp * 1000;
109 cximage->setAcquisitionTime( QDateTime::fromMSecsSinceEpoch(qint64(timestampMS)));
112 mProbeDefinitionFromStringMessages->setImage(cximage);
114 if (mProbeDefinitionFromStringMessages->haveValidValues() && mProbeDefinitionFromStringMessages->haveChanged())
119 emit
probedefinition(deviceName, mProbeDefinitionFromStringMessages->createProbeDefintion(deviceName));
125 else if(device_type == igtlio::TransformConverter::GetIGTLTypeName())
127 igtlio::TransformDevicePointer transformDevice = igtlio::TransformDevice::SafeDownCast(receivedDevice);
128 igtlio::TransformConverter::ContentData content = transformDevice->GetContent();
134 Transform3D cxtransform = Transform3D::fromVtkMatrix(content.transform);
142 double timestamp = header.timestamp;
150 std::string openigtlinktransformid;
151 bool gotTransformId = receivedDevice->GetMetaDataElement(
"equipmentId", openigtlinktransformid);
156 emit
transform(deviceName, cxtransform, timestamp);
158 else if(device_type == igtlio::CommandConverter::GetIGTLTypeName())
161 igtlio::CommandDevicePointer command = igtlio::CommandDevice::SafeDownCast(receivedDevice);
163 igtlio::CommandConverter::ContentData content = command->GetContent();
165 <<
" name: " << content.name
166 <<
" content: " << content.content;
167 QString deviceName(content.name.c_str());
168 QString xml(content.content.c_str());
172 else if(device_type == igtlio::StatusConverter::GetIGTLTypeName())
174 igtlio::StatusDevicePointer status = igtlio::StatusDevice::SafeDownCast(receivedDevice);
176 igtlio::StatusConverter::ContentData content = status->GetContent();
178 CX_LOG_DEBUG() <<
"STATUS: " <<
" code: " << content.code
179 <<
" subcode: " << content.subcode
180 <<
" errorname: " << content.errorname
181 <<
" statusstring: " << content.statusstring;
184 else if(device_type == igtlio::StringConverter::GetIGTLTypeName())
186 igtlio::StringDevicePointer
string = igtlio::StringDevice::SafeDownCast(receivedDevice);
188 igtlio::StringConverter::ContentData content =
string->GetContent();
194 QString message(content.string_msg.c_str());
195 mProbeDefinitionFromStringMessages->parseStringMessage(header, message);
200 CX_LOG_WARNING() <<
"Found unhandled devicetype: " << device_type;
205 void NetworkHandler::onConnectionEvent(vtkObject* caller,
void* connector,
unsigned long event ,
void*)
209 if (event==igtlio::Logic::ConnectionAddedEvent)
213 if (event==igtlio::Logic::ConnectionAboutToBeRemovedEvent)
219 void NetworkHandler::onDeviceAddedOrRemoved(vtkObject* caller,
void* void_device,
unsigned long event,
void* callData)
223 if (event==igtlio::Logic::NewDeviceEvent)
225 igtlio::DevicePointer device(reinterpret_cast<igtlio::Device*>(void_device));
228 CX_LOG_DEBUG() <<
" NetworkHandler is listening to " << device->GetDeviceName();
229 qvtkReconnect(NULL, device, igtlio::Device::ReceiveEvent,
this, SLOT(onDeviceReceived(vtkObject*,
void*,
unsigned long,
void*)));
232 if (event==igtlio::Logic::RemovedDeviceEvent)
234 CX_LOG_WARNING() <<
"TODO: on remove device event, not implemented";
238 void NetworkHandler::periodicProcess()
240 mLogic->PeriodicProcess();
243 void NetworkHandler::connectToConnectionEvents()
245 foreach(
int eventId, QList<int>()
246 << igtlio::Logic::ConnectionAddedEvent
247 << igtlio::Logic::ConnectionAboutToBeRemovedEvent
250 qvtkReconnect(NULL, mLogic, eventId,
251 this, SLOT(onConnectionEvent(vtkObject*,
void*,
unsigned long,
void*)));
255 void NetworkHandler::connectToDeviceEvents()
257 foreach(
int eventId, QList<int>()
258 << igtlio::Logic::NewDeviceEvent
259 << igtlio::Logic::RemovedDeviceEvent
262 qvtkReconnect(NULL, mLogic, eventId,
263 this, SLOT(onDeviceAddedOrRemoved(vtkObject*,
void*,
unsigned long,
void*)));
QString qstring_cast(const T &val)
void commandRespons(QString devicename, QString xml)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
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(igtlio::LogicPointer logic)
igtlio::SessionPointer requestConnectToServer(std::string serverHost, int serverPort=-1, igtlio::SYNCHRONIZATION_TYPE sync=igtlio::BLOCKING, double timeout_s=5)
void image(ImagePtr image)
Namespace for all CustusX production code.