diff --git a/mainwindow.cpp b/mainwindow.cpp index 7ba4cd2..26f6df3 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -211,6 +211,86 @@ void MainWindow::customEvent(QEvent *event) { } else { qCritical() << "!!! UNKNOWN SENDER !!!"; } + } else + if (event->type() == UpdateDcEvent::type()) { + UpdateDcEvent *pevent = (UpdateDcEvent *)event; + UpdateDcEvent::UpdateStep const updateStep = pevent->updateStep(); + QObject const *sender = pevent->sender(); + if (sender == m_worker) { + QDateTime const &recv = QDateTime::currentDateTime(); + QDateTime const &send = pevent->sendDateTime(); + qint64 const delay = recv.toMSecsSinceEpoch() - send.toMSecsSinceEpoch(); + switch(updateStep) { + case UpdateDcEvent::UpdateStep::NONE: + break; + case UpdateDcEvent::UpdateStep::DC_REBOOT: { + m_hw->bl_rebootDC(); + QString msg = QDateTime::currentDateTime().toString(Qt::ISODateWithMs) + + QString(": reset device controller (delay=%1ms").arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + m_updateStep = UpdateDcEvent::UpdateStep::DC_REBOOT; + } break; + case UpdateDcEvent::UpdateStep::BL_START: { + QString const &msg = recv.toString(Qt::ISODateWithMs) + + QString(": start bootloader (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + m_hw->bl_startBL(); + if (pevent->count() == BL_START_COUNT) { + m_updateStep = UpdateDcEvent::UpdateStep::BL_START; + } + } break; + case UpdateDcEvent::UpdateStep::BL_CHECK: { + if (m_updateStep != UpdateDcEvent::UpdateStep::BL_IS_UP) { + QString const &msg = recv.toString(Qt::ISODateWithMs) + + QString(": request bootloader version (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + m_hw->bl_checkBL(); + //m_updateStep = UpdateDcEvent::UpdateStep::BL_CHECK; + } + } break; + case UpdateDcEvent::UpdateStep::BL_IS_UP: { + QString msg = recv.toString(Qt::ISODateWithMs) + + QString(": check running bootloader (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + if (m_updateStep != UpdateDcEvent::UpdateStep::BL_IS_UP) { + if (m_hw->bl_isUp()) { + msg = recv.toString(Qt::ISODateWithMs) + + QString(": bootloader running (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + m_updateStep = UpdateDcEvent::UpdateStep::BL_IS_UP; + } else { + msg = recv.toString(Qt::ISODateWithMs) + + QString(": bootloader stop requested (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + if (m_updateStep == UpdateDcEvent::UpdateStep::BL_STOP) { + msg = QDateTime::currentDateTime().toString(Qt::ISODateWithMs) + + QString(": bootloader down (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + m_updateStep = UpdateDcEvent::UpdateStep::BL_IS_DOWN; + } + } + } + } break; + case UpdateDcEvent::UpdateStep::BL_STOP: { + QString const &msg = QDateTime::currentDateTime().toString(Qt::ISODateWithMs) + + QString(": stop bootloader (%1, delay=%2ms)").arg(pevent->count()).arg(delay); + emit m_worker->showStatusMessage("dc update", msg); + Utils::printInfoMsg(msg.toUpper()); + //if (m_bootLoaderIsUp) { + m_hw->bl_stopBL(); + m_updateStep = UpdateDcEvent::UpdateStep::BL_STOP; + //} + } break; + default: ; + } + } } QThread::yieldCurrentThread();