33 #define _USE_MATH_DEFINES
41 #include <vtkDoubleArray.h>
42 #include <QCoreApplication>
77 mConfigurationFilePath(
""),
81 connect(
settings(), SIGNAL(valueChangedFor(QString)),
this, SLOT(globalConfigurationFileChangedSlot(QString)));
83 this->setConfigurationFile(
profile()->getToolConfigFilePath());
88 this->destroyTrackerThread();
109 this->startTracking();
118 this->stopTracking();
120 this->uninitialize();
128 bool TrackingSystemIGSTKService::isConfigured()
const
133 bool TrackingSystemIGSTKService::isInitialized()
const
138 bool TrackingSystemIGSTKService::isTracking()
const
143 void TrackingSystemIGSTKService::configure()
145 if (mConfigurationFilePath.isEmpty() || !QFile::exists(mConfigurationFilePath))
147 reportWarning(QString(
"Configuration file [%1] is not valid, could not configure the toolmanager.").arg(mConfigurationFilePath));
152 ConfigurationFileParser configParser(mConfigurationFilePath, mLoggingFolder);
154 std::vector<ToolFileParser::TrackerInternalStructure> trackers = configParser.getTrackers();
156 if (trackers.empty())
162 ToolFileParser::TrackerInternalStructure trackerStructure = trackers[0];
164 ToolFileParser::ToolInternalStructure referenceToolStructure;
165 std::vector<ToolFileParser::ToolInternalStructure> toolStructures;
166 QString referenceToolFile = configParser.getAbsoluteReferenceFilePath();
167 std::vector<QString> toolfiles = configParser.getAbsoluteToolFilePaths();
168 for (std::vector<QString>::iterator it = toolfiles.begin(); it != toolfiles.end(); ++it)
170 ToolFileParser toolParser(*it, mLoggingFolder);
171 ToolFileParser::ToolInternalStructure internalTool = toolParser.getTool();
172 if ((*it) == referenceToolFile)
173 referenceToolStructure = internalTool;
175 toolStructures.push_back(internalTool);
179 mTrackerThread.reset(
new IgstkTrackerThread(trackerStructure, toolStructures, referenceToolStructure));
181 connect(mTrackerThread.get(), SIGNAL(
configured(
bool)),
this, SLOT(trackerConfiguredSlot(
bool)));
182 connect(mTrackerThread.get(), SIGNAL(
initialized(
bool)),
this, SLOT(initializedSlot(
bool)));
183 connect(mTrackerThread.get(), SIGNAL(tracking(
bool)),
this, SLOT(trackerTrackingSlot(
bool)));
184 connect(mTrackerThread.get(), SIGNAL(error()),
this, SLOT(uninitialize()));
188 mTrackerThread->start();
191 void TrackingSystemIGSTKService::trackerConfiguredSlot(
bool on)
201 reportDebug(
"Received a configured signal in ToolManager, but we don't have a mTrackerThread, this should never happen, contact programmer.");
207 std::map<QString, IgstkToolPtr> igstkTools = mTrackerThread->getTools();
208 IgstkToolPtr reference = mTrackerThread->getRefereceTool();
209 std::map<QString, IgstkToolPtr>::iterator it = igstkTools.begin();
210 for (; it != igstkTools.end(); ++it)
216 mTools.push_back(tool);
219 reportWarning(
"Creation of the cxTool " + it->second->getUid() +
" failed.");
234 void TrackingSystemIGSTKService::deconfigure()
236 if (!this->isConfigured())
239 if (this->isInitialized())
241 connect(
this, SIGNAL(
uninitialized()),
this, SLOT(deconfigureAfterUninitializedSlot()));
242 this->uninitialize();
247 this->destroyTrackerThread();
254 report(
"IGSTK Tracking Service is deconfigured.");
257 void TrackingSystemIGSTKService::initialize()
259 if (!this->isConfigured())
261 connect(
this, SIGNAL(
configured()),
this, SLOT(initializeAfterConfigSlot()));
266 if (!this->isConfigured())
268 reportWarning(
"Please configure before trying to initialize.");
273 if (!this->createSymlink())
281 mTrackerThread->initialize(
true);
283 reportError(
"Cannot initialize the tracking system because the tracking thread does not exist.");
286 void TrackingSystemIGSTKService::uninitialize()
288 if (this->isTracking())
290 connect(
this, SIGNAL(
trackingStopped()),
this, SLOT(uninitializeAfterTrackingStoppedSlot()));
291 this->stopTracking();
295 if (!this->isInitialized())
300 mTrackerThread->initialize(
false);
309 bool TrackingSystemIGSTKService::createSymlink()
312 QFileInfo symlink = this->getSymlink();
313 QDir linkDir(symlink.absolutePath());
314 QString linkfile = symlink.absoluteFilePath();
317 if (!linkDir.exists())
320 QString(
"Folder %1 does not exist. It is required to exist and be writable in order to connecto to IGSTK.").arg(linkDir.path()));
324 QDir devDir(
"/dev/");
328 filters <<
"cu.usbserial*" <<
"cu.KeySerial*" <<
"serial" <<
"ttyUSB*";
330 QStringList files = devDir.entryList(filters, QDir::System);
335 QString(
"No usb connections found in /dev using filters %1").arg(filters.join(
";")));
340 report(QString(
"Device files: %1").arg(files.join(
",")));
341 if (files.size() > 1)
343 QString(
"More than one tracker connected? Will only try to connect to: %1").arg(files[0]));
346 QString device = devDir.filePath(files[0]);
349 QFile(linkfile).remove();
350 QFile devFile(device);
351 QFileInfo devFileInfo(device);
352 if (!devFileInfo.isWritable())
354 reportError(QString(
"Device %1 is not writable. Connection will fail.").arg(device));
358 bool val = devFile.link(linkfile);
362 QString(
"Symlink %1 creation to device %2 failed with code %3").arg(linkfile).arg(device).arg(
368 report(QString(
"Created symlink %1 to device %2").arg(linkfile).arg(device));
371 devFile.setPermissions(
372 QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner | QFile::ReadGroup | QFile::WriteGroup
373 | QFile::ExeGroup | QFile::ReadOther | QFile::WriteOther | QFile::ExeOther);
377 QFileInfo TrackingSystemIGSTKService::getSymlink()
const
379 QString name(
"/Library/CustusX/igstk.links");
381 QDir::root().mkdir(name);
382 QString linkFile = linkDir.path() +
"/cu.CustusX.dev0";
383 return QFileInfo(linkDir, linkFile);
388 void TrackingSystemIGSTKService::cleanupSymlink()
390 report(
"Cleaning up symlinks.");
391 QFile(this->getSymlink().absoluteFilePath()).remove();
395 void TrackingSystemIGSTKService::startTracking()
397 if (!this->isInitialized())
399 connect(
this, SIGNAL(
initialized()),
this, SLOT(startTrackingAfterInitSlot()));
405 mTrackerThread->track(
true);
408 void TrackingSystemIGSTKService::stopTracking()
410 if (!this->isTracking())
415 mTrackerThread->track(
false);
418 void TrackingSystemIGSTKService::setConfigurationFile(QString configurationFile)
420 if (configurationFile == mConfigurationFilePath)
423 if (this->isConfigured())
425 connect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
429 mConfigurationFilePath = configurationFile;
434 if (mLoggingFolder == loggingFolder)
437 if (this->isConfigured())
439 connect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
443 mLoggingFolder = loggingFolder;
446 void TrackingSystemIGSTKService::initializedSlot(
bool value)
458 report(
"IGSTK Tracking Service is uninitialized.");
464 void TrackingSystemIGSTKService::trackerTrackingSlot(
bool value)
482 void TrackingSystemIGSTKService::startTrackingAfterInitSlot()
484 disconnect(
this, SIGNAL(
initialized()),
this, SLOT(startTrackingAfterInitSlot()));
485 this->startTracking();
488 void TrackingSystemIGSTKService::initializeAfterConfigSlot()
490 disconnect(
this, SIGNAL(
configured()),
this, SLOT(initializeAfterConfigSlot()));
494 void TrackingSystemIGSTKService::uninitializeAfterTrackingStoppedSlot()
496 disconnect(
this, SIGNAL(
trackingStopped()),
this, SLOT(uninitializeAfterTrackingStoppedSlot()));
497 this->uninitialize();
500 void TrackingSystemIGSTKService::deconfigureAfterUninitializedSlot()
502 disconnect(
this, SIGNAL(
uninitialized()),
this, SLOT(deconfigureAfterUninitializedSlot()));
506 void TrackingSystemIGSTKService::configureAfterDeconfigureSlot()
508 disconnect(
this, SIGNAL(
deconfigured()),
this, SLOT(configureAfterDeconfigureSlot()));
512 void TrackingSystemIGSTKService::globalConfigurationFileChangedSlot(QString key)
514 if (key ==
"toolConfigFile")
516 this->setConfigurationFile(
profile()->getToolConfigFilePath());
527 void TrackingSystemIGSTKService::destroyTrackerThread()
531 mTrackerThread->quit();
532 mTrackerThread->wait(2000);
533 if (mTrackerThread->isRunning())
535 mTrackerThread->terminate();
536 mTrackerThread->wait();
538 QObject::disconnect(mTrackerThread.get());
539 mTrackerThread.reset();
cxResource_EXPORT ProfilePtr profile()
void reportError(QString msg)
TrackingSystemIGSTKService()
void uninitialized()
system is uninitialized
virtual TrackerConfigurationPtr getConfiguration()
boost::shared_ptr< class IgstkTool > IgstkToolPtr
virtual void setLoggingFolder(QString loggingFolder)
boost::shared_ptr< class TrackerConfiguration > TrackerConfigurationPtr
void reportWarning(QString msg)
void trackingStarted()
system starts tracking
virtual Tool::State getState() const
Settings * settings()
Shortcut for accessing the settings instance.
void reportSuccess(QString msg)
void trackingStopped()
system stops tracking
void initialized()
system is initialized
void configured()
system is configured
boost::shared_ptr< ToolUsingIGSTK > ToolUsingIGSTKPtr
virtual ~TrackingSystemIGSTKService()
virtual std::vector< ToolPtr > getTools()
virtual void setState(const Tool::State val)
asynchronously request a state. Wait for signal stateChanged()
void reportDebug(QString msg)