Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/mooltipass/moolticute
Browse files Browse the repository at this point in the history
  • Loading branch information
limpkin committed Aug 20, 2023
2 parents b408e29 + f2deb60 commit 62cf83d
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 105 deletions.
21 changes: 15 additions & 6 deletions src/MPDeviceBleImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,10 +1229,10 @@ void MPDeviceBleImpl::getBattery()
}
}

void MPDeviceBleImpl::nihmReconditioning()
void MPDeviceBleImpl::nihmReconditioning(bool enableRestart)
{
auto *jobs = new AsyncJobs("NiMH Reconditioning", mpDev);
m_nimhResponse = "";
m_nimhResultSec = 0;

jobs->append(new MPCommandJob(mpDev, MPCmd::NIMH_RECONDITION, [this](const QByteArray &data, bool &)
{
Expand All @@ -1246,7 +1246,7 @@ void MPDeviceBleImpl::nihmReconditioning()
const auto dischargeTimeUpper = bleProt->toIntFromLittleEndian(static_cast<quint8>(payload[2]), static_cast<quint8>(payload[3]));
quint32 dischargeTime = dischargeTimeLower;
dischargeTime |= static_cast<quint32>((dischargeTimeUpper<<16));
m_nimhResponse = QString::number(dischargeTime/1000.0);
m_nimhResultSec = dischargeTime/1000.0;
if (std::numeric_limits<quint32>::max() == dischargeTime)
{
qCritical() << "NiMH Recondition failed";
Expand All @@ -1255,19 +1255,28 @@ void MPDeviceBleImpl::nihmReconditioning()
return true;
}));

connect(jobs, &AsyncJobs::finished, [this](const QByteArray &data)
connect(jobs, &AsyncJobs::finished, [this, enableRestart](const QByteArray &data)
{
Q_UNUSED(data)
qDebug() << "NiMH Reconditioning finished";
bool restarted = false;
if (enableRestart && m_nimhResultSec < RECONDITION_RESTART_UNDER_SECS)
{
restarted = true;
}
createAndAddCustomJob("NiMH Reconditioning finished job",
[this](){emit nimhReconditionFinished(true, m_nimhResponse);});
[this, restarted, result = m_nimhResultSec](){emit nimhReconditionFinished(true, QString::number(result), restarted);});
if (restarted)
{
nihmReconditioning(true);
}
});

connect(jobs, &AsyncJobs::failed, [this](AsyncJob *)
{
qDebug() << "NiMH Reconditioning failed";
createAndAddCustomJob("NiMH Reconditioning failed job",
[this](){emit nimhReconditionFinished(false, m_nimhResponse);});
[this](){emit nimhReconditionFinished(false, QString::number(m_nimhResultSec));});
});

mpDev->enqueueAndRunJob(jobs);
Expand Down
7 changes: 4 additions & 3 deletions src/MPDeviceBleImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MPDeviceBleImpl: public QObject
void readBatteryPercent(const QByteArray& statusData);
void getBattery();

void nihmReconditioning();
void nihmReconditioning(bool enableRestart);
void getSecurityChallenge(const QString& key, const MessageHandlerCb &cb);

void processDebugMsg(const QByteArray& data, bool& isDebugMsg);
Expand Down Expand Up @@ -194,7 +194,7 @@ class MPDeviceBleImpl: public QObject
void batteryPercentChanged(int batteryPct);
void userCategoriesFetched(QJsonObject categories);
void notesFetched();
void nimhReconditionFinished(bool success, QString response);
void nimhReconditionFinished(bool success, QString response, bool restarted = false);
void changeBleName(const QString& name);

private slots:
Expand Down Expand Up @@ -251,7 +251,7 @@ public slots:
std::atomic_bool m_fetchingNotes = {false};

bool m_enforceLayout = false;
QString m_nimhResponse = "";
quint32 m_nimhResultSec = 0;

int m_miniFilePartCounter = 0;

Expand Down Expand Up @@ -291,6 +291,7 @@ public slots:
static constexpr int WAKEUP_DEVICE_BUNDLE_VERSION = 10;
static constexpr int POINTED_TO_ADDR_SIZE = 2;
static constexpr int PLATFORM_SERIAL_NUM_FIRST_BYTE = 16;
static constexpr int RECONDITION_RESTART_UNDER_SECS = 2500;
const QByteArray DEFAULT_BUNDLE_PASSWORD = "\x63\x44\x31\x91\x3a\xfd\x23\xff\xb3\xac\x93\x69\x22\x5b\xf3\xc0";
const QString DEFAULT_BLE_NAME = "Mooltipass Mini BLE";
};
Expand Down
31 changes: 22 additions & 9 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1993,6 +1993,17 @@ bool MainWindow::validateSerialString(const QString &serialStr, uint &serialNum)
return true;
}

void MainWindow::displayReconditionWaitScreen(double lastElapsedTime)
{
QString lastElapsedText = lastElapsedTime > 0 ? QString("<p>Recondition restarted. Last elapsed time: %1 seconds</p>").arg(lastElapsedTime) : "";
ui->labelWait->show();
ui->labelWait->setText(tr("<html><!--nimh_recondition--><head/><body><p><span style=\"font-size:12pt; font-weight:600;\">NiMH Recondition is in progress.</span></p>%1<p>Please wait.</p></body></html>").arg(lastElapsedText));
ui->stackedWidget->setCurrentWidget(ui->pageWaiting);
ui->progressBarWait->hide();
ui->labelProgressMessage->hide();
updateTabButtons();
}

void MainWindow::on_toolButton_clearBackupFilePath_released()
{
ui->lineEdit_dbBackupFilePath->clear();
Expand Down Expand Up @@ -2310,13 +2321,8 @@ void MainWindow::on_pushButtonNiMHRecondition_clicked()

if (btn == QMessageBox::Ok)
{
wsClient->sendNiMHReconditioning();
ui->labelWait->show();
ui->labelWait->setText(tr("<html><!--nimh_recondition--><head/><body><p><span style=\"font-size:12pt; font-weight:600;\">NiMH Recondition is in progress.</span></p><p>Please wait.</p></body></html>"));
ui->stackedWidget->setCurrentWidget(ui->pageWaiting);
ui->progressBarWait->hide();
ui->labelProgressMessage->hide();
updateTabButtons();
wsClient->sendNiMHReconditioning(ui->checkBoxContinueRecondition->isChecked());
displayReconditionWaitScreen();
}
}

Expand All @@ -2337,15 +2343,22 @@ void MainWindow::on_pushButtonSecurityValidate_clicked()
wsClient->sendSecurityChallenge(challengeString);
}

void MainWindow::onReconditionFinished(bool success, double dischargeTime)
void MainWindow::onReconditionFinished(bool success, double dischargeTime, bool restarted)
{
ui->stackedWidget->setCurrentWidget(ui->pageAdvanced);
updatePage();
updateTabButtons();
if (success)
{
QMessageBox::information(this, tr("NiMH Recondition Finished"),
if (!restarted)
{
QMessageBox::information(this, tr("NiMH Recondition Finished"),
tr("Recondition finished in %1 seconds").arg(dischargeTime));
}
else
{
displayReconditionWaitScreen(dischargeTime);
}
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private slots:

void on_pushButtonSecurityValidate_clicked();

void onReconditionFinished(bool success, double dischargeTime);
void onReconditionFinished(bool success, double dischargeTime, bool restarted);

void on_checkBoxEnforceBTLayout_stateChanged(int arg1);

Expand Down Expand Up @@ -263,6 +263,8 @@ private slots:

bool validateSerialString(const QString& serialStr, uint& serialNum);

void displayReconditionWaitScreen(double lastElapsedTime = 0);

Ui::MainWindow *ui = nullptr;
QtAwesome* awesome;

Expand Down
Loading

0 comments on commit 62cf83d

Please sign in to comment.