优化了部分代码
This commit is contained in:
parent
af565e360a
commit
ecef963f71
|
@ -41,7 +41,11 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
|
||||||
QJsonDocument json_data = QJsonDocument::fromJson(list[i]);
|
QJsonDocument json_data = QJsonDocument::fromJson(list[i]);
|
||||||
if (json_data["type"] == "message")
|
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")
|
else if (json_data["type"] == "code")
|
||||||
{
|
{
|
||||||
|
@ -58,25 +62,41 @@ void DataManager::recv_from_cln(QByteArray &data, QTcpSocket *sock)
|
||||||
qDebug() << data_build;
|
qDebug() << data_build;
|
||||||
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
||||||
p_this->server->sendToclient(sock, data_encrypt);
|
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") //存车消息
|
else if (json_data["action"] == "park") //存车消息
|
||||||
{
|
{
|
||||||
QString name = json_data["user"].toString();
|
QString name = json_data["user"].toString();
|
||||||
QString number = p_this->linedb->choose(); //获取一个可用的车位编号
|
QString find_code = p_this->database->findCode(name);
|
||||||
qDebug() << "获取到的可用车位编号是" << number;
|
if(find_code == "000")
|
||||||
//随机生成取车码
|
{
|
||||||
QString code = p_this->getRandomString(8);
|
QString number = p_this->linedb->choose(); //获取一个可用的车位编号
|
||||||
//更新取车码
|
qDebug() << "获取到的可用车位编号是" << number;
|
||||||
qDebug() << name << code << number;
|
//随机生成取车码
|
||||||
p_this->database->insertCode(name,code,number);
|
QString code = p_this->getRandomString(8);
|
||||||
p_this->linedb->setState(number);
|
//更新取车码
|
||||||
|
qDebug() << name << code << number;
|
||||||
|
p_this->database->insertCode(name,code,number);
|
||||||
|
p_this->linedb->setState(number);
|
||||||
|
|
||||||
QByteArray data_build, data_encrypt;
|
QByteArray data_build, data_encrypt;
|
||||||
data_build = p_this->protocol->data_maker(Protocol::CODE, QString("park"), QString("server"),code);
|
data_build = p_this->protocol->data_maker(Protocol::CODE, QString("park"), QString("server"),code);
|
||||||
qDebug() << data_build;
|
qDebug() << data_build;
|
||||||
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
data_encrypt = p_this->protocol->data_encrypt(data_build);
|
||||||
p_this->server->sendToclient(sock, data_encrypt);
|
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")
|
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)
|
QString DataManager::getRandomString(int nLen)
|
||||||
{
|
{
|
||||||
srand(QDateTime::currentMSecsSinceEpoch());
|
srand(QDateTime::currentMSecsSinceEpoch());
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#include <QRandomGenerator>
|
#include <QRandomGenerator>
|
||||||
#include <QChar>
|
#include <QChar>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
#include <QtGlobal>>
|
#include <QtGlobal>
|
||||||
|
|
||||||
#include "database.h"
|
#include "database.h"
|
||||||
#include "tcpserver.h"
|
#include "tcpserver.h"
|
||||||
|
@ -32,10 +32,12 @@ private:
|
||||||
Line *linedb;
|
Line *linedb;
|
||||||
Protocol *protocol;
|
Protocol *protocol;
|
||||||
TCPServer *server;
|
TCPServer *server;
|
||||||
|
int count;
|
||||||
QList <QTcpSocket*> online_socket_list;
|
QList <QTcpSocket*> online_socket_list;
|
||||||
QList <QString> online_name_list;
|
QList <QString> online_name_list;
|
||||||
static DataManager *p_this;
|
static DataManager *p_this;
|
||||||
void update_namelist(QTcpSocket *sock);
|
void update_namelist(QTcpSocket *sock);
|
||||||
|
void update_count(QTcpSocket *sock, int count);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DATAMANAGER_H
|
#endif // DATAMANAGER_H
|
||||||
|
|
15
line.cpp
15
line.cpp
|
@ -50,6 +50,21 @@ Line::Line()
|
||||||
db_.close();
|
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 Line::findCode(QString number)
|
||||||
{
|
{
|
||||||
QString sql = QString("select * from table_line where number = :number");
|
QString sql = QString("select * from table_line where number = :number");
|
||||||
|
|
4
line.h
4
line.h
|
@ -6,6 +6,8 @@
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
#include <QSqlQueryModel>
|
||||||
|
#include <QSqlQuery>
|
||||||
|
|
||||||
class Line
|
class Line
|
||||||
{
|
{
|
||||||
|
@ -23,6 +25,8 @@ public:
|
||||||
|
|
||||||
void resetState(QString number);
|
void resetState(QString number);
|
||||||
|
|
||||||
|
int getCount();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
QSqlDatabase db_;
|
QSqlDatabase db_;
|
||||||
|
|
|
@ -27,7 +27,7 @@ QByteArray Protocol::data_builder(QList<any_types> &args)
|
||||||
case MESSAGE:
|
case MESSAGE:
|
||||||
{
|
{
|
||||||
data.insert("type","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());
|
data.insert("data",args[2].get_string());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue