From ecef963f718dd5c83486583b92e7c28b9f61e727 Mon Sep 17 00:00:00 2001 From: www-git-cn <102035872+www-git-cn@users.noreply.github.com> Date: Tue, 5 Mar 2024 18:01:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BA=86=E9=83=A8=E5=88=86?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- datamanager.cpp | 57 +++++++++++++++++++++++++++++++++++++------------ datamanager.h | 4 +++- line.cpp | 15 +++++++++++++ line.h | 4 ++++ protocol.cpp | 2 +- 5 files changed, 66 insertions(+), 16 deletions(-) diff --git a/datamanager.cpp b/datamanager.cpp index 9889546..356a822 100644 --- a/datamanager.cpp +++ b/datamanager.cpp @@ -41,7 +41,11 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock) QJsonDocument json_data = QJsonDocument::fromJson(list[i]); if (json_data["type"] == "message") { - + if (json_data["action"] == "count") + { + p_this->count = p_this->linedb->getCount(); + p_this->update_count(sock,p_this->count); + } } else if (json_data["type"] == "code") { @@ -58,25 +62,41 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock) qDebug() << data_build; data_encrypt = p_this->protocol->data_encrypt(data_build); p_this->server->sendToclient(sock, data_encrypt); + p_this->count = p_this->linedb->getCount(); + p_this->update_count(sock,p_this->count); } else if (json_data["action"] == "park") //存车消息 { QString name = json_data["user"].toString(); - QString number = p_this->linedb->choose(); //获取一个可用的车位编号 - qDebug() << "获取到的可用车位编号是" << number; - //随机生成取车码 - QString code = p_this->getRandomString(8); - //更新取车码 - qDebug() << name << code << number; - p_this->database->insertCode(name,code,number); - p_this->linedb->setState(number); + QString find_code = p_this->database->findCode(name); + if(find_code == "000") + { + QString number = p_this->linedb->choose(); //获取一个可用的车位编号 + qDebug() << "获取到的可用车位编号是" << number; + //随机生成取车码 + QString code = p_this->getRandomString(8); + //更新取车码 + qDebug() << name << code << number; + p_this->database->insertCode(name,code,number); + p_this->linedb->setState(number); - QByteArray data_build, data_encrypt; - data_build = p_this->protocol->data_maker(Protocol::CODE, QString("park"), QString("server"),code); - qDebug() << data_build; - data_encrypt = p_this->protocol->data_encrypt(data_build); - p_this->server->sendToclient(sock, data_encrypt); + QByteArray data_build, data_encrypt; + data_build = p_this->protocol->data_maker(Protocol::CODE, QString("park"), QString("server"),code); + qDebug() << data_build; + data_encrypt = p_this->protocol->data_encrypt(data_build); + p_this->server->sendToclient(sock, data_encrypt); + p_this->count -= 1; + p_this->update_count(sock,p_this->count); + } + else + { + QByteArray data_build, data_encrypt; + data_build = p_this->protocol->data_maker(Protocol::CODE, QString("park"), QString("server"), find_code); + qDebug() << data_build; + data_encrypt = p_this->protocol->data_encrypt(data_build); + p_this->server->sendToclient(sock, data_encrypt); + } } else if(json_data["action"] == "update") @@ -201,6 +221,15 @@ void DataManager::update_namelist(QTcpSocket *sock) } } +void DataManager::update_count(QTcpSocket *sock, int count) +{ + QByteArray data_build, data_encrypt; + data_build = p_this->protocol->data_maker(Protocol::MESSAGE, QString("count"), QString::number(count)); + qDebug() << data_build << "发送count信息"; + data_encrypt = p_this->protocol->data_encrypt(data_build); + p_this->server->sendToclient(sock, data_encrypt); +} + QString DataManager::getRandomString(int nLen) { srand(QDateTime::currentMSecsSinceEpoch()); diff --git a/datamanager.h b/datamanager.h index fab79b0..432f726 100644 --- a/datamanager.h +++ b/datamanager.h @@ -6,7 +6,7 @@ #include #include #include -#include > +#include #include "database.h" #include "tcpserver.h" @@ -32,10 +32,12 @@ private: Line *linedb; Protocol *protocol; TCPServer *server; + int count; QList online_socket_list; QList online_name_list; static DataManager *p_this; void update_namelist(QTcpSocket *sock); + void update_count(QTcpSocket *sock, int count); }; #endif // DATAMANAGER_H diff --git a/line.cpp b/line.cpp index 780a156..58726cf 100644 --- a/line.cpp +++ b/line.cpp @@ -50,6 +50,21 @@ Line::Line() db_.close(); } +int Line::getCount() +{ + db_.open(); + QString sql = QString("select * from table_line where state = '000'"); + QSqlQuery query(db_); + query.exec(sql); + QSqlQueryModel *queryModel = new QSqlQueryModel(); + queryModel->setQuery(query); + int nRecordCount = queryModel->rowCount(); + qDebug() << "表的条目数:" << nRecordCount; + return nRecordCount; + +} + + QString Line::findCode(QString number) { QString sql = QString("select * from table_line where number = :number"); diff --git a/line.h b/line.h index 0487c47..ad6c1d8 100644 --- a/line.h +++ b/line.h @@ -6,6 +6,8 @@ #include #include #include +#include +#include class Line { @@ -23,6 +25,8 @@ public: void resetState(QString number); + int getCount(); + private: QSqlDatabase db_; diff --git a/protocol.cpp b/protocol.cpp index 6e621d4..86da575 100644 --- a/protocol.cpp +++ b/protocol.cpp @@ -27,7 +27,7 @@ QByteArray Protocol::data_builder(QList &args) case MESSAGE: { data.insert("type","message"); - data.insert("sendname",args[1].get_string()); + data.insert("action",args[1].get_string()); data.insert("data",args[2].get_string()); break; }