#include "dialog_inputserialnum.h" #include "ui_dialog_inputserialnum.h" #include "base/HGBase.h" #include "HGString.h" #include "HGUIGlobal.h" #include "app_cfg.h" #include "dialog_setlogpath.h" Dialog_InputSerialNum::Dialog_InputSerialNum(class MainWindow *mainWnd, Dialog_logIn::LogInType logInType, const QString &devName, const QString &profileName, const QString &accountName, SANE_Handle handle, const QString &serialNum, const QString &devType) : QDialog(mainWnd), ui(new Ui::Dialog_InputSerialNum), m_mainWnd(mainWnd), m_logInType(logInType), m_handle(handle), m_valid(true), m_profileName(profileName), m_accountName(accountName), m_serialNum(serialNum), m_devType(devType), m_devName(devName), m_hg(nullptr), m_curIndex(0), m_originVid(0), m_originPid(0) { ui->setupUi(this); setWindowFlags(Qt::Dialog | Qt::WindowCloseButtonHint); QFont ft; ft.setPointSize(15); this->setFont(ft); QFont ft2; ft2.setPointSize(20); if (m_logInType != Dialog_logIn::LogInType_InitTest) { ui->lineEdit_sn->setEnabled(false); ui->pbtn_setSn->setEnabled(false); ui->pbtn_setConfig->setEnabled(false); } ui->pbtn_slectLogPath->setVisible(false); if (m_accountName == "admin") { ui->pbtn_slectLogPath->setVisible(true); } updatePassStatus(false); ui->pushButton_status->setFont(ft2); ui->pbtn_enterTest->setEnabled(false); ui->label_scanner->setText(tr("Device name: %1").arg(m_devName)); ui->lineEdit_sn->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9]+$"))); ui->lineEdit_sn->setMaxLength(14); if (handle != nullptr) { ui->lineEdit_sn->setText(serialNum); ui->lineEdit_sn->selectAll(); } else { ui->lineEdit_sn->setText(""); ui->pbtn_setSn->setEnabled(false); } if (m_handle != nullptr) m_hg = new hgscanner(nullptr, nullptr, this, m_handle); connect(this, SIGNAL(testResult()), this, SLOT(on_testResult())); m_originVid = getDevVid(); m_originPid = getDevPid(); setSleepTime(); updateTargetCfg(); updateCurDeviceCfg(); } Dialog_InputSerialNum::~Dialog_InputSerialNum() { delete m_hg; m_hg = nullptr; delete ui; } QString Dialog_InputSerialNum::GetSn() { return ui->lineEdit_sn->text(); } QString Dialog_InputSerialNum::GetDevType() { return m_devType; } void Dialog_InputSerialNum::Invalid(const QString &devName) { if (devName == m_devName) { sane_close(m_handle); m_handle = nullptr; m_valid = false; reject(); } } void Dialog_InputSerialNum::setSpeedMode() { AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); LPWRITECFG lp = new WRITECFG; int speedMode = jsonGlobal.speed_mode; lp->cat = WRITE_CAT_SPEED; lp->val = (void*)&speedMode; func_test_go(WRITE_CFG_NAME, (const wchar_t*)lp, m_hg); delete lp; QString info = tr("Set") + tr("Speep mode: %1PPM").arg(QString::number(speedMode)); addInteractInfo(info, true); } void Dialog_InputSerialNum::setSleepTime() { AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); LPWRITECFG lp = new WRITECFG; int sleepTime = jsonGlobal.sleep_time; lp->cat = WRITE_CAT_SLEEP; lp->val = (void*)&sleepTime; func_test_go(WRITE_CFG_NAME, (const wchar_t*)lp, m_hg); delete lp; QString info = tr("Set") + (sleepTime > 0 ? tr("Sleep time: %1minute").arg(QString::number(sleepTime)) : tr("Sleep time: Not sleeping")); addInteractInfo(info, true); } void Dialog_InputSerialNum::setVidPid() { AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); LPWRITECFG lp = new WRITECFG; VIDPID vidpid; vidpid.VID = jsonGlobal.vid_to; vidpid.PID = jsonGlobal.pid_to; int vidpidValue = vidpid.Value; lp->cat = WRITE_CAT_VID_PID; lp->val = (void*)&vidpidValue; func_test_go(WRITE_CFG_NAME, (const wchar_t*)lp, m_hg); delete lp; char buf[10]; sprintf(buf, "%x", jsonGlobal.vid_to); char buf2[10]; sprintf(buf2, "%x", jsonGlobal.pid_to); QString info = tr("Set") + tr("Vid: %1 Pid: %2").arg(QString::fromStdString(buf)).arg(QString::fromStdString(buf2)); addInteractInfo(info, true); } void Dialog_InputSerialNum::setDevceModel() { AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); std::string deviceModel = jsonGlobal.device_model.toStdString(); unsigned int len = deviceModel.size(); SANE_Status ret = sane_io_control(m_handle, IO_CTRL_CODE_SET_DEVS_MODEL, &deviceModel[0], &len); QString info(""); if (ret == SANE_STATUS_GOOD) { info = tr("Set") + tr("Device model: %1").arg(QString::fromStdString(deviceModel)); } addInteractInfo(info, true); } void Dialog_InputSerialNum::rebootDevice() { const wchar_t *p = L"0"; func_test_go(HGPDTTOOLDB_NAME_REBOOT_DEVICE, p, m_hg); } int Dialog_InputSerialNum::getSpeedMode() { SANE_Int speedMode = 0; unsigned int len = sizeof(SANE_Int); sane_io_control(m_handle, IO_CTRL_CODE_GET_SPEED_MODE, &speedMode, &len); const wchar_t G100[6] = { 70,80,90,110 }; const wchar_t G200[6] = { 100,110,120,130 }; const wchar_t G300[6] = { 40,50,60,70,80,90 }; const wchar_t G306[6] = { 30,40,50,60,70 }; const wchar_t G400[6] = { 40,50,60,70,80,90 }; int speed = 0; QString devType = m_devType; if (getDeviceFwVersion().mid(3, 1) == "Z") //306 { devType = "G306"; } if ((int)speedMode >= 30) speed = (int)speedMode; else { if (devType == "G100") speed = G100[(int)speedMode - 1]; else if (devType == "G200") speed = G200[(int)speedMode - 1]; else if (devType == "G300") speed = G300[(int)speedMode - 1]; else if (devType == "G306") speed = G306[(int)speedMode - 1]; else if (devType == "G400") speed = G400[(int)speedMode - 1]; } return speed; } int Dialog_InputSerialNum::getSleepTime() { int sleepTime = SANE_POWER_FIRST; unsigned int len2 = sizeof(int); sane_io_control(m_handle, IO_CTRL_CODE_GET_POWER_LEVEL, &sleepTime, &len2); if (sleepTime > 99999 || sleepTime == -1) return 0; return (sleepTime / 60); } int Dialog_InputSerialNum::getDevVid() { VIDPID vidpid; int vidpid_val; unsigned int len = sizeof(int); sane_io_control(m_handle, IO_CTRL_CODE_GET_VIDPID, &vidpid_val, &len); vidpid.Value = vidpid_val; int vid = vidpid.VID; return vid; } int Dialog_InputSerialNum::getDevPid() { VIDPID vidpid; int vidpid_val; unsigned int len = sizeof(int); sane_io_control(m_handle, IO_CTRL_CODE_GET_VIDPID, &vidpid_val, &len); vidpid.Value = vidpid_val; int pid = vidpid.PID; return pid; } QString Dialog_InputSerialNum::getDeviceModel() { std::string devs_model; unsigned int devs_len = 0; SANE_Status ret = sane_io_control(m_handle, IO_CTRL_CODE_GET_DEVS_MODEL, &devs_model[0], &devs_len); if (ret == SANE_STATUS_NO_MEM) { devs_model.resize(devs_len); ret = sane_io_control(m_handle, IO_CTRL_CODE_GET_DEVS_MODEL, &devs_model[0], &devs_len); } return QString::fromStdString(devs_model); } QString Dialog_InputSerialNum::getDeviceFwVersion() { std::string firmwareVersion; unsigned int devs_len = 0; SANE_Status ret = sane_io_control(m_handle, IO_CTRL_CODE_GET_HARDWARE_VERSION, &firmwareVersion[0], &devs_len); if (ret == SANE_STATUS_NO_MEM) { firmwareVersion.resize(devs_len); ret = sane_io_control(m_handle, IO_CTRL_CODE_GET_HARDWARE_VERSION, &firmwareVersion[0], &devs_len); } return QString::fromStdString(firmwareVersion).left(10); } void Dialog_InputSerialNum::updateTargetCfg() { ui->textBrowser_target->clear(); AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); int speedMode = jsonGlobal.speed_mode; int sleepTime = jsonGlobal.sleep_time; int vid_to = jsonGlobal.vid_to; int pid_to = jsonGlobal.pid_to; QString devideModel = jsonGlobal.device_model; QString fwVersion = jsonGlobal.firmware_version; char buf[10]; sprintf(buf, "%x", vid_to); char buf2[10]; sprintf(buf2, "%x", pid_to); QString content; content.append(tr("Target Configration:") + "\n\n"); if (!devideModel.isEmpty()) content.append(tr("Device model: %1").arg(devideModel) + "\n"); if (!fwVersion.isEmpty()) content.append(tr("Firmware version: %1").arg(fwVersion) + "\n"); content.append(tr("Speep mode: %1PPM").arg(QString::number(speedMode)) + "\n"); content.append((sleepTime > 0 ? tr("Sleep time: %1minute").arg(QString::number(sleepTime)) : tr("Sleep time: Not sleeping")) + "\n"); content.append(tr("Vid: %1\nPid: %2").arg(QString::fromStdString(buf)).arg(QString::fromStdString(buf2)) + "\n"); ui->textBrowser_target->setText(content); } void Dialog_InputSerialNum::updateCurDeviceCfg() { ui->textBrowser_curConfig->clear(); if (m_handle == nullptr) return; int speedMode = getSpeedMode(); int sleepTime = getSleepTime(); int vid_to = getDevVid(); int pid_to = getDevPid(); AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); QString devideModel(""); if (!jsonGlobal.device_model.isEmpty()) { devideModel = getDeviceModel(); } QString fwVersion(""); if (!jsonGlobal.firmware_version.isEmpty()) { fwVersion = getDeviceFwVersion(); } char buf[10]; sprintf(buf, "%x", vid_to); char buf2[10]; sprintf(buf2, "%x", pid_to); QString content; content.append(tr("Current Configration of device:") + "\n\n"); if (!devideModel.isEmpty() && !jsonGlobal.device_model.isEmpty()) content.append(tr("Device model: %1").arg(devideModel) + "\n"); if (!fwVersion.isEmpty()) content.append(tr("Firmware version: %1").arg(fwVersion) + "\n"); content.append(tr("Speep mode: %1PPM").arg(QString::number(speedMode)) + "\n"); content.append(sleepTime > 0 ? tr("Sleep time: %1minute").arg(QString::number(sleepTime)) + "\n" : tr("Sleep time: Not sleeping") + "\n"); content.append(tr("Vid: %1\nPid: %2").arg(QString::fromStdString(buf)).arg(QString::fromStdString(buf2)) + "\n"); ui->textBrowser_curConfig->setText(content); } QString Dialog_InputSerialNum::getLogPath() { HGChar logPath[512]; HGBase_GetDocumentsPath(logPath, 512); HGChar procName[512]; HGBase_GetProcessName(procName, 512); strcat(logPath, procName); strcat(logPath, "/Test_Log/"); strcat(logPath, m_devType.toStdString().c_str()); strcat(logPath, "/"); return getCfgValue(m_devType.toStdString().c_str(), "logPath", getStdFileName(StdStringToUtf8(logPath).c_str())); } void Dialog_InputSerialNum::writeTestLog(QString logContent) { QString logPath = getLogPath(); HGBase_CreateDir(getStdString(logPath).c_str()); QString fileName = logPath + m_serialNum + ".log"; QFile file(fileName); if(!file.open(QIODevice::ReadWrite | QIODevice::Append)) { return; } QDateTime dateTime= QDateTime::currentDateTime(); QString curTime = dateTime .toString("yyyy-MM-dd hh:mm:ss"); QString content = "[" + curTime + "] "; content += logContent; QTextStream txtOutput(&file); txtOutput << content << endl; file.close(); } void Dialog_InputSerialNum::addInteractInfo(QString info, bool isNormal) { QDateTime dateTime= QDateTime::currentDateTime(); QString curTime = dateTime .toString("yyyy-MM-dd hh:mm:ss"); QString content = "[" + curTime + "] "; content.append(info + "\n"); if (isNormal) ui->textBrowser_info->insertPlainText(content); else { ui->textBrowser_info->setTextColor(Qt::red); ui->textBrowser_info->insertPlainText(content); } ui->textBrowser_info->moveCursor(QTextCursor::End); ui->textBrowser_info->setTextColor(Qt::black); } void Dialog_InputSerialNum::updatePassStatus(bool pass) { if (pass) { ui->pushButton_status->setText(tr("Pass")); ui->pushButton_status->setStyleSheet("background-color: green"); } else { ui->pushButton_status->setText(tr("Fail")); ui->pushButton_status->setStyleSheet("background-color: red"); } } void Dialog_InputSerialNum::on_testResult() { //m_mainWnd->releaseTesting(); } void Dialog_InputSerialNum::on_pbtn_setSn_clicked() { QString sn = ui->lineEdit_sn->text(); std::string serialNum = sn.toStdString(); QString testLog = tr("Tester: %1, Test item: %2").arg(m_accountName).arg(QString(tr("Set serial number: %1 ").arg(sn))); if(SANE_STATUS_GOOD != sane_io_control(m_handle, IO_CTRL_CODE_SET_SERIAL, &serialNum[0], nullptr)) { //QMessageBox::information(this, tr("tips"), tr("set sn failed")); QString info = QString(tr("Set serial number: %1 ").arg(sn)) + tr("Failed!"); writeTestLog(testLog + tr("set sn failed")); addInteractInfo(info, false); } else { //QMessageBox::information(this, tr("tips"), tr("set sn succeed")); QString info = QString(tr("Set serial number: %1 ").arg(sn)) + tr("Succeed!"); writeTestLog(testLog + tr("set sn succeed")); addInteractInfo(info, true); } } void Dialog_InputSerialNum::on_pbtn_checkConfig_clicked() { AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); int speedMode_to = jsonGlobal.speed_mode; int sleepTime_to = jsonGlobal.sleep_time; int vid_to = jsonGlobal.vid_to; int pid_to = jsonGlobal.pid_to; QString deviceModel_to = jsonGlobal.device_model; QString firmwareVersion_to = jsonGlobal.firmware_version; int speedMode = getSpeedMode(); int sleepTime = getSleepTime(); int vid = getDevVid(); int pid = getDevPid(); QString item; bool right = true; LPWRITECFG lp = new WRITECFG; if (!deviceModel_to.isEmpty()) { QString deviceModel = getDeviceModel(); if (deviceModel_to != deviceModel) { item.append(tr("Device model")); item.append(" "); right = false; QString info = tr("Inconsistent configuration %1detected, please reconfigure, and check again").arg(item); addInteractInfo(info, false); } } if (!firmwareVersion_to.isEmpty()) { QString fwVersion = getDeviceFwVersion(); if (firmwareVersion_to != fwVersion) { right = false; QString info = tr("Inconsistent firmware detected, please burn the correct firmware, and check again"); addInteractInfo(info, false); } } if (speedMode_to != speedMode) { item.append(tr("Speed mode")); item.append(" "); right = false; QString info = tr("Inconsistent configuration %1detected, please reconfigure, and check again").arg(item); addInteractInfo(info, false); } if (sleepTime_to != sleepTime) { item.append(tr("Sleep time")); item.append(" "); right = false; QString info = tr("Inconsistent configuration %1detected, please reconfigure, and check again").arg(item); addInteractInfo(info, false); } if (vid != vid_to || pid != pid_to) { item.append(tr("Vid and Pid")); item.append(" "); right = false; QString info = tr("Inconsistent configuration %1detected, please reboot and reconfigure, and check again").arg(item); addInteractInfo(info, false); } if (m_originVid != vid_to || m_originPid != pid_to) { right = false; QString info = tr("Detected that the current device's vid-pid and device object are not consistent. "); addInteractInfo(info + tr("Please reboot device to update. "), false); } updatePassStatus(right); ui->pbtn_enterTest->setEnabled(right); if (right) { QString info = tr("Consistent configuration"); addInteractInfo(info, true); } delete lp; updateTargetCfg(); updateCurDeviceCfg(); } void Dialog_InputSerialNum::on_pbtn_enterTest_clicked() { if (!m_valid) { QMessageBox::information(this, tr("tips"), tr("device is lost")); reject(); return; } if (ui->lineEdit_sn->text().isEmpty()) { QMessageBox::information(this, tr("tips"), tr("Serial num is empty")); return; } QString str = ui->lineEdit_sn->text(); for (int i = 0; i < str.size(); ++i) { if ((str.at(i) < '0' && str.at(i) > '9') || (str.at(i) < 'A' && str.at(i) > 'Z')) { QMessageBox::information(this, tr("tips"), tr("sn is illegal")); return; } } if (m_mainWnd->FindInterface(ui->lineEdit_sn->text()) != nullptr) { QMessageBox::information(this, tr("tips"), tr("The device is testing")); return; } accept(); } void Dialog_InputSerialNum::on_pbtn_setConfig_clicked() { AnalysisJson analysisJson(m_profileName); AnalysisJson::json_global jsonGlobal = analysisJson.GetGlobal(); bool supportDevModel = false; QString deviceModel = jsonGlobal.device_model; if (!deviceModel.isEmpty()) { supportDevModel = true; setDevceModel(); } setSpeedMode(); setSleepTime(); setVidPid(); int speedMode = jsonGlobal.speed_mode; int sleepTime = jsonGlobal.sleep_time; int vid_to = jsonGlobal.vid_to; int pid_to = jsonGlobal.pid_to; char buf[10]; sprintf(buf, "%x", vid_to); char buf2[10]; sprintf(buf2, "%x", pid_to); QString content; if (supportDevModel) content.append(tr("Device model: %1").arg(deviceModel) + " "); content.append(tr("Speep mode: %1PPM").arg(QString::number(speedMode)) + " "); content.append((sleepTime > 0 ? tr("Sleep time: %1minute").arg(QString::number(sleepTime)) : tr("Sleep time: Not sleeping")) + " "); content.append(tr("Vid: %1 Pid: %2").arg(QString::fromStdString(buf)).arg(QString::fromStdString(buf2))); writeTestLog(tr("Tester: %1, Test item: %2").arg(m_accountName).arg(tr("Set configuration to device ") + tr("Test content: ") + content)); updateTargetCfg(); updateCurDeviceCfg(); if (m_originVid != vid_to || m_originPid != pid_to) { QString info = tr("Detected that the current device's vid-pid and device object are not consistent. "); addInteractInfo(info + tr("Please reboot device to update. "), false); // QMessageBox msg(QMessageBox::Question, tr("tips"), // info + tr("Do you want to restart now?"), // QMessageBox::Yes | QMessageBox::No, this); // msg.exec(); // if (msg.clickedButton() == msg.button(QMessageBox::Yes)) // { // rebootDevice(); // } } //QMessageBox::information(this, tr("Prompt"), (tr("Success, please check the configuration"))); } void Dialog_InputSerialNum::on_pbtn_reboot_clicked() { rebootDevice(); } void Dialog_InputSerialNum::on_pbtn_slectLogPath_clicked() { Dialog_SetLogPath dlg(m_devType, this); dlg.exec(); }