127 lines
4.5 KiB
C++
127 lines
4.5 KiB
C++
#include "GetProfileInfoScript.h"
|
||
|
||
#include <QJsonDocument>
|
||
#include <QJsonObject>
|
||
#include <QRegularExpression>
|
||
#include <QThread>
|
||
|
||
#include "adb/AdbUtils.h"
|
||
#include "dao/MaterialDAO.h"
|
||
#include "dao/BankProfileDAO.h"
|
||
#include "dao/DeviceDAO.h"
|
||
#include "net/NetworkService.h"
|
||
|
||
namespace Dushanbe {
|
||
|
||
GetProfileInfoScript::GetProfileInfoScript(
|
||
QString deviceId,
|
||
QString appCode,
|
||
QObject *parent
|
||
) : CommonScript(parent),
|
||
m_deviceId(std::move(deviceId)),
|
||
m_appCode(std::move(appCode)) {
|
||
}
|
||
|
||
GetProfileInfoScript::~GetProfileInfoScript() = default;
|
||
|
||
void GetProfileInfoScript::doStart() {
|
||
DeviceInfo device = DeviceDAO::getDeviceById(m_deviceId); if (device.id.isEmpty()) device = DeviceDAO::findByAdbSerial(m_deviceId); if (!device.adbSerial.isEmpty()) m_deviceId = device.adbSerial;
|
||
const BankProfileInfo app = BankProfileDAO::getApplication(device.id, m_appCode);
|
||
|
||
if (device.id.isEmpty() || app.id == -1) {
|
||
m_error = "Device or app not found: " + m_deviceId + " " + m_appCode;
|
||
qCritical() << m_error;
|
||
emit finishedWithResult(m_error);
|
||
return;
|
||
}
|
||
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_deviceId, ++m_counter));
|
||
|
||
if (!xmlScreenParser.isHomeScreen()) {
|
||
qDebug() << "[Dushanbe::GetProfileInfo] Not on home screen, restarting app...";
|
||
if (!runAppAndGoToHomeScreen(m_deviceId, app.package, app.pinCode,
|
||
device.screenWidth, device.screenHeight)) {
|
||
m_error = "Cannot reach home screen: " + device.name + " (" + m_deviceId + ") " + m_appCode;
|
||
qWarning() << m_error;
|
||
AdbUtils::tryToKillApplication(m_deviceId, app.package);
|
||
emit finishedWithResult(m_error);
|
||
return;
|
||
}
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_deviceId, ++m_counter));
|
||
}
|
||
|
||
// Парсим баланс с домашнего экрана
|
||
const double parsedBalance = xmlScreenParser.parseBalanceFromHomeScreen();
|
||
qDebug() << "[Dushanbe::GetProfileInfo] Parsed balance:" << parsedBalance;
|
||
if (parsedBalance > 0.0) {
|
||
BankProfileDAO::updateAmount(app.id, parsedBalance);
|
||
}
|
||
|
||
// Тапаем по кнопке меню (гамбургер, левый верхний угол)
|
||
// Это первый clickable ImageView в верхней панели
|
||
Node menuButton;
|
||
for (const Node &node : xmlScreenParser.findAllNodes()) {
|
||
if (node.className == "android.widget.ImageView" && node.clickable && node.y() < 300) {
|
||
menuButton = node;
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (menuButton.x() == 0 && menuButton.y() == 0) {
|
||
m_error = "Cannot find menu button: " + m_deviceId;
|
||
qWarning() << m_error;
|
||
AdbUtils::tryToKillApplication(m_deviceId, app.package);
|
||
emit finishedWithResult(m_error);
|
||
return;
|
||
}
|
||
|
||
AdbUtils::makeTap(m_deviceId, menuButton.x(), menuButton.y());
|
||
QThread::msleep(2000);
|
||
|
||
// Ждём боковое меню
|
||
bool sideMenuFound = false;
|
||
for (int attempt = 0; attempt < 5; ++attempt) {
|
||
xmlScreenParser.parseAndSaveXml(AdbUtils::getScreenDumpAsXml(m_deviceId, ++m_counter));
|
||
if (xmlScreenParser.isSideMenu()) {
|
||
sideMenuFound = true;
|
||
break;
|
||
}
|
||
QThread::msleep(1000);
|
||
}
|
||
|
||
if (!sideMenuFound) {
|
||
m_error = "Side menu not detected: " + m_deviceId;
|
||
qWarning() << m_error;
|
||
AdbUtils::tryToKillApplication(m_deviceId, app.package);
|
||
emit finishedWithResult(m_error);
|
||
return;
|
||
}
|
||
|
||
// Парсим ФИО и телефон из бокового меню
|
||
const QString fullName = xmlScreenParser.parseProfileFullName();
|
||
QString phone = xmlScreenParser.parseProfilePhone();
|
||
phone.remove(QRegularExpression("[^0-9+]"));
|
||
qDebug() << "[Dushanbe::GetProfileInfo] fullName:" << fullName << "phone:" << phone;
|
||
|
||
if (!fullName.isEmpty()) {
|
||
BankProfileDAO::updateProfileInfo(app.id, fullName, phone, "");
|
||
}
|
||
|
||
createBankProfileIfNeeded(device.id, m_appCode, parsedBalance);
|
||
|
||
const QList<MaterialInfo> allAccounts = MaterialDAO::getAccounts(device.id, m_appCode);
|
||
QList<QPair<MaterialInfo, Node>> accountsToPost;
|
||
for (const auto &acc : allAccounts) {
|
||
if (acc.materialId.isEmpty()) {
|
||
accountsToPost.append({acc, Node()});
|
||
}
|
||
}
|
||
if (!accountsToPost.isEmpty()) {
|
||
postAccountsData(accountsToPost);
|
||
}
|
||
|
||
emit finishedWithResult(m_error);
|
||
}
|
||
|
||
} // namespace Dushanbe
|