emsApplication/applications/emsConfigurer/mainwindow.cpp

123 lines
3.4 KiB
C++
Raw Normal View History

2024-09-10 16:12:38 +08:00
#include "mainwindow.h"
2024-09-10 14:38:23 +08:00
#include "ui_mainwindow.h"
2024-09-10 16:12:38 +08:00
#include <QPixmap>
#include <QMessageBox>
#include <QRegularExpression>
#include <QRegularExpressionValidator>
2024-09-10 14:38:23 +08:00
2024-10-11 14:20:12 +08:00
#include <hv/hlog.h>
2024-09-10 16:55:41 +08:00
#include "MainDialog.h"
2024-10-11 14:20:12 +08:00
#include "mysqlutils.h"
2024-09-10 16:55:41 +08:00
2024-09-10 14:38:23 +08:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
2024-09-10 16:55:41 +08:00
, m_pMainDialog(nullptr)
2024-09-10 14:38:23 +08:00
{
ui->setupUi(this);
2024-09-10 16:12:38 +08:00
this->setWindowIcon(QIcon(":/images/icon.png"));
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->setStyleSheet(qsLineEditStyle);
QRegularExpression rx("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
QRegularExpressionValidator* ipValidator = new QRegularExpressionValidator(rx, this);
ui->serverIp->setValidator(ipValidator);
setIp("127.0.0.1");
//setIp("192.168.0.155");
2024-09-10 14:38:23 +08:00
}
MainWindow::~MainWindow()
{
delete ui;
2024-09-10 16:55:41 +08:00
if (m_pMainDialog)
{
delete m_pMainDialog;
m_pMainDialog = nullptr;
}
2024-09-10 14:38:23 +08:00
}
2024-09-10 16:12:38 +08:00
void MainWindow::setIp(const QString &ip)
{
ui->serverIp->setText(ip);
}
2024-09-10 16:55:41 +08:00
2024-10-12 14:00:37 +08:00
bool MainWindow::testDatabase()
2024-10-11 14:20:12 +08:00
{
2024-10-17 20:25:43 +08:00
CWaitorCursor wait;
2024-10-11 14:20:12 +08:00
QString svr = ui->serverIp->text();
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";
2024-10-11 14:20:12 +08:00
std::string dbname = "hjems";
bool dbok = MysqlUtils::getInstance()->OpenDatabase(dbserver,dbport,dbuser,dbpasswd,dbname);
if (!dbok)
{
hloge("failed to open database, exit now...");
2024-10-12 14:00:37 +08:00
return false;
2024-10-11 14:20:12 +08:00
}
hlogi("database connection test successfully!");
2024-10-12 14:00:37 +08:00
return true;
}
bool MainWindow::testServerEcho()
{
using namespace hv;
QString svr = ui->serverIp->text();
int connfd = AppTcpClient::getInstance()->Initialize("127.0.0.1",DEVICE_SERVER_PORT);
if (connfd <= 0)
{
hloge("failed to connect to device data service ...");
return false;
}
hlogi("connect to device data service port %d, connfd=%d ...", DEVICE_SERVER_PORT, connfd);
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, connfd=%d ...", cnt, connfd);
return cnt > 0 ? true : false;
}
2024-10-12 14:00:37 +08:00
void MainWindow::on_pb_Logon_clicked()
{
if (testDatabase() && testServerEcho())
2024-10-12 14:00:37 +08:00
{
m_pMainDialog = new MainDialog();
m_pMainDialog->show();
this->close();
}
else
{
QMessageBox::critical(this, tr("Critical Message"),tr("Failed to retrieve device data!"));
}
}
2024-10-11 14:20:12 +08:00
2024-10-12 14:00:37 +08:00
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!"));
2024-10-12 14:00:37 +08:00
else
QMessageBox::critical(this, tr("Critical Message"),tr("Failed to connect to device data service!"));
2024-10-11 14:20:12 +08:00
}