[76] | 1 | // %pacpus:license{
|
---|
[73] | 2 | // This file is part of the PACPUS framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[76] | 4 | // %pacpus:license}
|
---|
[91] | 5 | /// @author Gerald Dherbomez <firstname.surname@utc.fr>
|
---|
[73] | 6 | /// @version $Id: DbtPlyEngine.cpp 91 2013-05-19 10:32:48Z gdherbom $
|
---|
| 7 |
|
---|
[31] | 8 | #include <Pacpus/DbitePlayer/DbtPlyEngine.h>
|
---|
[73] | 9 |
|
---|
| 10 | #include <Pacpus/kernel/ComponentManager.h>
|
---|
| 11 | #include <Pacpus/kernel/Log.h>
|
---|
| 12 |
|
---|
| 13 | #include <cassert>
|
---|
| 14 | #include <limits>
|
---|
| 15 | #include <QDir>
|
---|
| 16 |
|
---|
| 17 | using namespace std;
|
---|
| 18 |
|
---|
| 19 | // Construction de la fabrique de composant DbtPlyEngine
|
---|
| 20 | //static ComponentFactory<DbtPlyEngine> factory("DbtPlyEngine");
|
---|
| 21 |
|
---|
| 22 | namespace pacpus {
|
---|
| 23 |
|
---|
| 24 | DECLARE_STATIC_LOGGER("pacpus.core.DbtPlyEngine");
|
---|
| 25 |
|
---|
| 26 | static const string kPropertyDataDirectory = "datadir";
|
---|
| 27 |
|
---|
| 28 | static const string kPropertyLoggerConfiguration = "log-config-file";
|
---|
| 29 |
|
---|
| 30 | static const string kPropertyReplayMode = "replay_mode";
|
---|
| 31 | static const string kPropertyReplayModeLastData = "1";
|
---|
| 32 | static const string kPropertyReplayModeAllData = "2";
|
---|
| 33 |
|
---|
| 34 | typedef float SpeedType;
|
---|
| 35 | static const SpeedType kInitialSpeed = 1.0;
|
---|
| 36 | static const SpeedType kMinSpeed = 1.0 / 32;
|
---|
| 37 | static const SpeedType kMaxSpeed = 32;
|
---|
| 38 |
|
---|
| 39 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 40 | /// Constructor
|
---|
| 41 | DbtPlyEngine::DbtPlyEngine(QString name)
|
---|
| 42 | : ComponentBase(name)
|
---|
| 43 | , mCurrentState(StoppedState::getInstance())
|
---|
| 44 | , mSpeed(kInitialSpeed)
|
---|
| 45 | , mIsReverse(false)
|
---|
| 46 | {
|
---|
| 47 | LOG_TRACE("constructor");
|
---|
| 48 |
|
---|
| 49 | sync_ = new QSemaphore(1);
|
---|
| 50 | sync_->acquire();
|
---|
| 51 | direction_ = 1;
|
---|
| 52 | tMinMaxSem_ = new QSemaphore(1);
|
---|
| 53 | tMinMaxSem_->release();
|
---|
| 54 | tDbtMin_ = numeric_limits<road_time_t>::max();
|
---|
| 55 | tDbtMax_ = 0;
|
---|
| 56 | dataDir_ = QString::null;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 60 | /// Destructor.
|
---|
| 61 | DbtPlyEngine::~DbtPlyEngine()
|
---|
| 62 | {
|
---|
| 63 | LOG_TRACE("destructor");
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 67 | /// Returns the directory where the data are stored.
|
---|
| 68 | /// @return QString::null if directory is invalid.
|
---|
| 69 | QString DbtPlyEngine::getDataDir()
|
---|
| 70 | {
|
---|
| 71 | return dataDir_;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 75 | /// Slot.
|
---|
| 76 | /// called when trigger reaches a new timer event
|
---|
| 77 | void DbtPlyEngine::engReceiver()
|
---|
| 78 | {
|
---|
| 79 | sync_->release();
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 83 | /// @returns @b true if the engine is in the PLAY_STATE, @b false otherwise
|
---|
| 84 | bool DbtPlyEngine::isPlaying()
|
---|
| 85 | {
|
---|
| 86 | return mCurrentState->isPlaying();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 90 | /// main loop of the thread
|
---|
| 91 | /// entry point for a new thread after the invocation of QThread::start()
|
---|
| 92 | void DbtPlyEngine::run()
|
---|
| 93 | {
|
---|
| 94 | LOG_DEBUG("run");
|
---|
| 95 |
|
---|
| 96 | // We are waiting the first trigger signal to init some values
|
---|
| 97 | // before entering in the while loop
|
---|
| 98 | sync_->acquire();
|
---|
| 99 | lastTDbt_ = tDbtMin_;
|
---|
| 100 |
|
---|
| 101 | // For statistics purpose
|
---|
| 102 | static const size_t kDeltaArraySize = 1000;
|
---|
| 103 | int deltaTDbtTab[kDeltaArraySize];
|
---|
| 104 | int i = 0;
|
---|
| 105 |
|
---|
| 106 | while (THREAD_ALIVE) {
|
---|
| 107 | tNow_ = road_time();
|
---|
| 108 | float elapsedTime = tNow_ - lastTNow_;
|
---|
| 109 | int deltaTDbt = direction_ * elapsedTime * mSpeed;
|
---|
| 110 | tDbt_ = lastTDbt_ + deltaTDbt;
|
---|
| 111 | deltaTDbtTab[(i++) % kDeltaArraySize] = elapsedTime;
|
---|
| 112 |
|
---|
| 113 | if ((tDbt_ <= tDbtMax_) && (tDbt_ >= tDbtMin_)) {
|
---|
| 114 | Q_EMIT play(tDbt_, tNow_, mIsReverse);
|
---|
| 115 | Q_EMIT curReplayTime(tDbt_);
|
---|
| 116 | lastTNow_ = tNow_ ;
|
---|
| 117 | lastTDbt_ = tDbt_ ;
|
---|
| 118 | } else {
|
---|
| 119 | lastTNow_ = tNow_ ;
|
---|
| 120 | lastTDbt_ = tDbt_ ;
|
---|
| 121 | LOG_INFO("Reading is finished.");
|
---|
| 122 |
|
---|
| 123 | mCurrentState->stop(*this);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | // The waiting is placed at the end due to the init before the while loop
|
---|
| 127 | sync_->acquire();
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 132 | /// Slot.
|
---|
| 133 | /// change the direction of replaying : normal or reverse
|
---|
| 134 | void DbtPlyEngine::changeDirection(bool reverse)
|
---|
| 135 | {
|
---|
| 136 | if (reverse) {
|
---|
| 137 | direction_ = -1;
|
---|
| 138 | mIsReverse = true;
|
---|
| 139 | } else {
|
---|
| 140 | direction_ = +1;
|
---|
| 141 | mIsReverse = false;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 146 | /// Configuration method of the engine
|
---|
| 147 | /// called automatically by the component manager
|
---|
| 148 | ComponentBase::COMPONENT_CONFIGURATION DbtPlyEngine::configureComponent(XmlComponentConfig /*config*/)
|
---|
| 149 | {
|
---|
| 150 | // datadir
|
---|
| 151 | dataDir_ = param.getProperty(kPropertyDataDirectory.c_str());
|
---|
| 152 | LOG_INFO("property " << kPropertyDataDirectory.c_str() << "=\""
|
---|
| 153 | << dataDir_ << "\"");
|
---|
| 154 | if (dataDir_.isNull()) {
|
---|
| 155 | LOG_FATAL("The data directory '" << componentName << "' is invalid or unavailable!");
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | dataDir_ = QDir::toNativeSeparators(dataDir_);
|
---|
| 159 | if (!dataDir_.endsWith(QDir::separator())) {
|
---|
| 160 | dataDir_ += QDir::separator();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 164 | // logger configuration
|
---|
| 165 | QString loggerConfig = param.getProperty(kPropertyLoggerConfiguration.c_str());
|
---|
| 166 | LOG_INFO("property " << kPropertyLoggerConfiguration.c_str() << "=\""
|
---|
| 167 | << loggerConfig << "\"");
|
---|
| 168 | if (!loggerConfig.isNull()) {
|
---|
| 169 | LOG_INFO("configuring logger with file '" << loggerConfig << "'");
|
---|
| 170 | LogConfigurator::configureLoggerWithFile(loggerConfig.toStdString().c_str());
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 174 | // Replay Mode
|
---|
| 175 | QString replayModeValue = param.getProperty(kPropertyReplayMode.c_str());
|
---|
| 176 | LOG_INFO("property " << kPropertyReplayMode.c_str() << "=\""
|
---|
| 177 | << replayModeValue << "\"");
|
---|
| 178 | if (replayModeValue.isNull()) {
|
---|
| 179 | LOG_INFO("property " << kPropertyReplayMode.c_str() << " unset."
|
---|
| 180 | << " Set to default = 1.");
|
---|
| 181 | replayMode_ = PlayModeLastData;
|
---|
| 182 | } else {
|
---|
| 183 | if (kPropertyReplayModeLastData == replayModeValue.toStdString()) {
|
---|
| 184 | replayMode_ = PlayModeLastData;
|
---|
| 185 | } else if (kPropertyReplayModeAllData == replayModeValue.toStdString()) {
|
---|
| 186 | replayMode_ = PlayModeAllData;
|
---|
| 187 | } else {
|
---|
| 188 | LOG_WARN("unknown " << kPropertyReplayMode.c_str() << " '" << replayModeValue << "'."
|
---|
| 189 | << " Set to default = 1.");
|
---|
| 190 | replayMode_ = PlayModeLastData;
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | return ComponentBase::CONFIGURED_OK;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 198 | /// The start function of the engine
|
---|
| 199 | void DbtPlyEngine::startActivity()
|
---|
| 200 | {
|
---|
| 201 | LOG_INFO("Starting...");
|
---|
| 202 |
|
---|
| 203 | THREAD_ALIVE = true;
|
---|
| 204 | start();
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 208 | /// The stop function of the engine
|
---|
| 209 | void DbtPlyEngine::stopActivity()
|
---|
| 210 | {
|
---|
| 211 | LOG_TRACE("stopping activity...");
|
---|
| 212 |
|
---|
| 213 | THREAD_ALIVE = false;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 217 | /// Slot.
|
---|
| 218 | /// called by each file manager to provide min and max time of their data
|
---|
| 219 | void DbtPlyEngine::tMinMax(road_time_t tMin, road_time_t tMax)
|
---|
| 220 | {
|
---|
| 221 | tMinMaxSem_->acquire();
|
---|
| 222 | if (tDbtMin_>tMin) {
|
---|
| 223 | tDbtMin_= tMin;
|
---|
| 224 | }
|
---|
| 225 | if (tDbtMax_<tMax) {
|
---|
| 226 | tDbtMax_ = tMax;
|
---|
| 227 | }
|
---|
| 228 | Q_EMIT timeMinMax(tDbtMin_, tDbtMax_);
|
---|
| 229 | tMinMaxSem_->release();
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 233 | /// Slot.
|
---|
| 234 | /// called when the time slider is pressed
|
---|
| 235 | void DbtPlyEngine::pauseEvent()
|
---|
| 236 | {
|
---|
| 237 | LOG_DEBUG("Clicked: pause");
|
---|
| 238 | mCurrentState->pause(*this);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 242 | /// Slot.
|
---|
| 243 | /// called when the time slider is released
|
---|
| 244 | void DbtPlyEngine::playEvent()
|
---|
| 245 | {
|
---|
| 246 | LOG_DEBUG("Clicked: play");
|
---|
| 247 |
|
---|
| 248 | // get user interface
|
---|
| 249 | ComponentManager * mgr = ComponentManager::getInstance();
|
---|
| 250 | DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
|
---|
| 251 | assert(ui);
|
---|
| 252 | // get time value from slider
|
---|
| 253 | lastTDbt_ = ui->getTime() + tDbtMin_;
|
---|
| 254 |
|
---|
| 255 | mCurrentState->play(*this);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 259 | void DbtPlyEngine::stopEvent()
|
---|
| 260 | {
|
---|
| 261 | LOG_DEBUG("Clicked: stop");
|
---|
| 262 | mCurrentState->stop(*this);
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 266 | void DbtPlyEngine::speedUpEvent()
|
---|
| 267 | {
|
---|
| 268 | LOG_DEBUG("Clicked: speed Up");
|
---|
| 269 | mCurrentState->speedUp(*this);
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 273 | void DbtPlyEngine::speedDownEvent()
|
---|
| 274 | {
|
---|
| 275 | LOG_DEBUG("Clicked: speed down");
|
---|
| 276 | mCurrentState->speedDown(*this);
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 280 | const DbtPlyEngineState * DbtPlyEngine::getState()
|
---|
| 281 | {
|
---|
| 282 | return mCurrentState;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 286 | void DbtPlyEngine::setState(DbtPlyEngineState * newState)
|
---|
| 287 | {
|
---|
| 288 | assert(newState);
|
---|
| 289 | LOG_DEBUG(mCurrentState->toString() << " => " << newState->toString());
|
---|
| 290 | mCurrentState = newState;
|
---|
| 291 |
|
---|
| 292 | Q_EMIT displayStateSig(mCurrentState, mSpeed);
|
---|
| 293 |
|
---|
| 294 | LOG_DEBUG("tDbt = " << tDbt_ << "\tlastTDbt = " << lastTDbt_);
|
---|
| 295 | LOG_DEBUG("tNow = " << tNow_ << "\tlastTNow = " << lastTNow_);
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | void DbtPlyEngine::speedUp()
|
---|
| 299 | {
|
---|
| 300 | mSpeed *= 2;
|
---|
| 301 | if (mSpeed > kMaxSpeed) {
|
---|
| 302 | mSpeed = kMaxSpeed;
|
---|
| 303 | }
|
---|
| 304 | assert(kMinSpeed <= mSpeed);
|
---|
| 305 | assert(mSpeed <= kMaxSpeed);
|
---|
| 306 | LOG_INFO("event: Speed Up, speed = " << mSpeed);
|
---|
| 307 | Q_EMIT displayStateSig(mCurrentState, mSpeed);
|
---|
| 308 | }
|
---|
| 309 |
|
---|
| 310 | void DbtPlyEngine::speedDown()
|
---|
| 311 | {
|
---|
| 312 | mSpeed /= 2;
|
---|
| 313 | if (mSpeed < kMinSpeed) {
|
---|
| 314 | mSpeed = kMinSpeed;
|
---|
| 315 | }
|
---|
| 316 | assert(kMinSpeed <= mSpeed);
|
---|
| 317 | assert(mSpeed <= kMaxSpeed);
|
---|
| 318 | LOG_INFO("event: Speed Up, speed = " << mSpeed);
|
---|
| 319 | Q_EMIT displayStateSig(mCurrentState, mSpeed);
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | void DbtPlyEngine::reset()
|
---|
| 323 | {
|
---|
| 324 | lastTNow_ = road_time();
|
---|
| 325 | lastTDbt_ = tDbtMin_;
|
---|
| 326 | mSpeed = kInitialSpeed;
|
---|
| 327 | Q_EMIT stopfile();
|
---|
| 328 |
|
---|
| 329 | // get user interface
|
---|
| 330 | ComponentManager * mgr = ComponentManager::getInstance();
|
---|
| 331 | DbtPlyUserInterface * ui = dynamic_cast<DbtPlyUserInterface *>(mgr->getComponent("dbiteUserInterface"));
|
---|
| 332 | assert(ui);
|
---|
| 333 | // reset time value of the slider
|
---|
| 334 | ui->resetTime();
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 | } // namespace pacpus
|
---|