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