code_production/app/HGProductionTool/dialog_inputserialnum.cpp

549 lines
16 KiB
C++

#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::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 G400[6] = { 40,50,60,70,80,90 };
int speed = 0;
if ((int)speedMode >= 40)
speed = (int)speedMode;
else
{
if (m_devType == "G100")
speed = G100[(int)speedMode - 1];
else if (m_devType == "G200")
speed = G200[(int)speedMode - 1];
else if (m_devType == "G300")
speed = G300[(int)speedMode - 1];
else if (m_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;
}
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;
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");
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 Pid: %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();
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");
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 Pid: %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;
int speedMode = getSpeedMode();
int sleepTime = getSleepTime();
int vid = getDevVid();
int pid = getDevPid();
QString item;
bool right = true;
LPWRITECFG lp = new WRITECFG;
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()
{
setSpeedMode();
setSleepTime();
setVidPid();
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;
char buf[10];
sprintf(buf, "%x", vid_to);
char buf2[10];
sprintf(buf2, "%x", pid_to);
QString content;
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();
}