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