emsApplication/applications/emsConfigurer/mainwindow.cpp

144 lines
4.0 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>
#include <QMessageBox>
#include <hv/hlog.h>
#include "MainDialog.h"
#include "mysqlutils.h"
#include "kutilities.h"
#include "ipaddress.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_pMainDialog(nullptr)
{
ui->setupUi(this);
this->setWindowIcon(QIcon(":/images/icon.png"));
this->setWindowTitle(tr("EMU Configurer Kit"));
QPixmap pixmap(":/images/hj-net.png");
pixmap = pixmap.scaled(250, 75, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放
ui->label_logo->setPixmap(pixmap);
QString qsLineEditStyle("QLineEdit { min-height: 20px; min-width: 120px; }");
ui->userToken->setStyleSheet(qsLineEditStyle);
ui->serverIP->setIP("127.0.0.1");
ui->pb_Logon->setFixedSize(100, 30);
ui->pb_Close->setFixedSize(100, 30);
ui->pb_Test->setFixedSize(45, 25);
QFont font("Arial", 24, QFont::Bold);
ui->productName1->setText(tr("Configurer Kit"));
ui->productName1->setFont(font);
QFont font2("Arial", 12, QFont::Bold);
ui->productName2->setText(tr("for EMU Host"));
ui->productName2->setFont(font2);
}
MainWindow::~MainWindow()
{
delete ui;
if (m_pMainDialog)
{
delete m_pMainDialog;
m_pMainDialog = nullptr;
}
}
void MainWindow::setIp(const QString &ip)
{
ui->serverIP->setIP(ip);
}
bool MainWindow::testDatabase()
{
CWaitorCursor wait;
QString svr = ui->serverIP->getIP();
std::string dbserver = svr.toStdString(); //"tcp://192.168.4.254";
int dbport = 3306;
std::string dbuser = "root";
std::string dbpasswd = "Hj57471000";
//std::string dbpasswd = "L2ysc1s1kr";
std::string dbname = "hjems";
bool dbok = MysqlUtils::getInstance()->OpenDatabase(dbserver,dbport,dbuser,dbpasswd,dbname);
if (!dbok)
{
hloge("failed to open database, exit now...");
return false;
}
hlogi("database connection test successfully!");
return true;
}
void MainWindow::onMessage(const hv::SocketChannelPtr& channel, hv::Buffer* buf)
{
hlogd("MainWindow<==\n%s", Kutilities::printHex((void*)buf->data(),(int)buf->size()).c_str());
}
bool MainWindow::testServerEcho()
{
using namespace hv;
QString svr = ui->serverIP->getIP();
int connfd = AppTcpClient::getInstance()->Initialize(svr,DEVICE_SERVER_PORT);
if (connfd <= 0)
{
hloge("failed to create socket for connecting to device data service ...");
return false;
}
hlogi("create socket for connecting to device data service port %d, connfd=%d ...", DEVICE_SERVER_PORT, connfd);
CWaitorCursor wait1;
hv_msleep(500);
//设置返回消息回调
onMessageCallbackFunc f = std::bind(&MainWindow::onMessage, this, std::placeholders::_1, std::placeholders::_2);
AppTcpClient::getInstance()->resetOnMessageCallbackFunction(f);
unsigned char frame[10] = { 0x02, 0x01,0x00,0x00,0x00, 0x00,0xEE,0xFF,0xEE,0xFF};
int cnt = AppTcpClient::getInstance()->m_tcpclient->send((void*)frame,10);
hlogd("send %d bytes test echo signal, connfd=%d ...", cnt, connfd);
return cnt > 0 ? true : false;
}
void MainWindow::on_pb_Logon_clicked()
{
if (testDatabase() && testServerEcho())
{
m_pMainDialog = new MainDialog();
m_pMainDialog->show();
this->close();
}
else
{
QMessageBox::critical(this, tr("Critical Message"),tr("Failed to retrieve device data!"));
}
}
void MainWindow::on_pb_Test_clicked()
{
if (testDatabase())
QMessageBox::information(this, tr("Successfully"),tr("Connect to device database successfully!"));
else
QMessageBox::critical(this, tr("Critical Message"),tr("Failed to connect to device database!"));
//测试与设备端接收服务的连接
if (testServerEcho())
QMessageBox::information(this, tr("Successfully"),tr("Connect to device data service successfully!"));
else
QMessageBox::critical(this, tr("Critical Message"),tr("Failed to connect to device data service!"));
}