diff --git a/src/ATBAPP/ATBDeviceControllerPlugin.cpp b/src/ATBAPP/ATBDeviceControllerPlugin.cpp index 46eeba9..d17d84b 100644 --- a/src/ATBAPP/ATBDeviceControllerPlugin.cpp +++ b/src/ATBAPP/ATBDeviceControllerPlugin.cpp @@ -1,5 +1,6 @@ #include "src/ATBAPP/ATBDeviceControllerPlugin.h" #include "src/ATBAPP/ATBHealthEvent.h" +#include "src/ATBAPP/Utils.h" #include #include @@ -9,6 +10,9 @@ #include #include +#include + + ATBDeviceControllerPlugin::ATBDeviceControllerPlugin(QObject *parent) : QObject(parent), pluginState(PLUGIN_STATE::NOT_INITIALIZED) @@ -135,7 +139,65 @@ void ATBDeviceControllerPlugin::cashAbort() // for an external account request, e.g. by an ui-button: void ATBDeviceControllerPlugin::requestAccount() { - qCritical() << "TODO: implement ATBDeviceControllerPlugin::requestAccount()"; + qCritical() << "ATBDeviceControllerPlugin::requestAccount()"; + + this->private_startAccount(); +} + +void ATBDeviceControllerPlugin::private_startAccount() +{ + uint16_t backupedAccNumbers[8]; // array of account numbers + uint8_t nrOfVals; // number of saved accounts + + // it is not defined which one is the latest account + hw->log_getHoldAccountNumbers(&nrOfVals, backupedAccNumbers); + + qsort( backupedAccNumbers, nrOfVals, sizeof (uint16_t), Utils::compare ); + + uint16_t latestAccountNumber = backupedAccNumbers[nrOfVals-1]; + + hw->log_selectVaultRecord(latestAccountNumber); + + this->accountCheckCounter = 0; + QTimer::singleShot(500, this, SLOT(private_checkAccountData())); +} + +void ATBDeviceControllerPlugin::private_checkAccountData() +{ + if (hw->log_chkIfVaultRecordAvailable()) { + this->private_getAccountData(); + } + else { + if (this->accountCheckCounter < 10) { + this->accountCheckCounter++; + QTimer::singleShot(500, this, SLOT(private_checkAccountData())); + } + else { + // cannot get accountData within ~10*500ms + qCritical() << "checkAccountData() failed"; + + // TODO: create and send an HealthEvent... + } + } +} + + +void ATBDeviceControllerPlugin::private_getAccountData() +{ + struct T_vaultRecord retVR; + + hw->log_getVaultRecord(&retVR); + + QHash accountData; + + accountData.insert("AccountingNumber", QString::number(retVR.AccountingNumber)); + + for (uint i = 0; i < sizeof(retVR.coinsInVault); ++i) { + accountData.insert("COIN_" + QString::number(i) + "_Quantity", retVR.coinsInVault[i]); + accountData.insert("COIN_" + QString::number(i) + "_Value", retVR.coinDenomination[i]); + } + + emit requestAccountResponse(accountData); } @@ -161,20 +223,9 @@ void ATBDeviceControllerPlugin::onVaultDoorOpened() void ATBDeviceControllerPlugin::onCoinBoxRemoved() { - hw->log_selectVaultRecord(0); - - // TODO: - // - create and send account (-> requestAccountResponse()) - - // account: - // collect account data - QHash accountData; - - // send account - // -> currently skipped: emit this->requestAccountResponse(accountData); - - qCritical() << "ATBDeviceControllerPlugin::onCoinBoxRemoved()"; + + this->private_startAccount(); } void ATBDeviceControllerPlugin::onCoinBoxInserted() @@ -603,8 +654,6 @@ const QString ATBDeviceControllerPlugin::getString(nsDeviceControllerInterface:: } - - /************************************************************************************************ * ... end */ diff --git a/src/ATBAPP/ATBDeviceControllerPlugin.h b/src/ATBAPP/ATBDeviceControllerPlugin.h index c48b6cb..7ffc9bd 100644 --- a/src/ATBAPP/ATBDeviceControllerPlugin.h +++ b/src/ATBAPP/ATBDeviceControllerPlugin.h @@ -129,6 +129,10 @@ private: nsDeviceControllerInterface::CASH_STATE currentCashState; + // counts failed hw->log_chkIfVaultRecordAvailable() + int accountCheckCounter; + + private slots: // printer @@ -152,6 +156,11 @@ private slots: void onCoinBoxRemoved(); void onCoinBoxInserted(); void onAllDoorsClosed(); + + // account handling + void private_startAccount(); + void private_checkAccountData(); + void private_getAccountData(); }; #endif // ATBDEVICECONTROLLERPLUGIN_H