213 lines
5.5 KiB
C++
213 lines
5.5 KiB
C++
#include "widget.h"
|
||
#include "ui_widget.h"
|
||
|
||
|
||
Widget::Widget(QWidget *parent)
|
||
: QWidget(parent)
|
||
, ui(new Ui::Widget)
|
||
{
|
||
ui->setupUi(this);
|
||
ui->log->setEnabled(false);
|
||
|
||
|
||
//连接mqtt服务器,端口1883
|
||
mqtt_client = new QMqttClient(this);
|
||
mqtt_client->setHostname(hostname);
|
||
mqtt_client->setPort(port);
|
||
mqtt_client->setClientId("mobile");
|
||
mqtt_client->setUsername("Phone");
|
||
mqtt_client->setPassword("Phone");
|
||
mqtt_client->connectToHost();
|
||
connect(mqtt_client, &QMqttClient::disconnected, this, &Widget::disconnected_handler);
|
||
connect(mqtt_client, &QMqttClient::messageReceived, this, &Widget::received_handler);
|
||
connect(mqtt_client, &QMqttClient::pingResponseReceived, this, &Widget::pingresp_handler);
|
||
connect(mqtt_client, &QMqttClient::connected, this, &Widget::connected_handler);
|
||
connect(mqtt_client, &QMqttClient::errorChanged, this, &Widget::error_handler);
|
||
connect(mqtt_client, &QMqttClient::stateChanged, this, &Widget::state_handler);
|
||
|
||
connect(ui->pushButton, &QPushButton::clicked, [=]() {
|
||
ui->stackedWidget->setCurrentIndex(0);
|
||
});
|
||
|
||
connect(ui->pushButton_2, &QPushButton::clicked, [=]() {
|
||
ui->stackedWidget->setCurrentIndex(1);
|
||
});
|
||
|
||
}
|
||
|
||
|
||
|
||
Widget::~Widget()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
|
||
//点击存车按钮
|
||
void Widget::on_pushButton_3_clicked()
|
||
{
|
||
emit parking();
|
||
}
|
||
|
||
//点击取车按钮
|
||
void Widget::on_pushButton_4_clicked()
|
||
{
|
||
if(ui->code->toPlainText().isEmpty())
|
||
{
|
||
QMessageBox::information(this,"取车错误","请先放置正确的取车码!");
|
||
}
|
||
emit pick_up(ui->code->toPlainText());
|
||
ui->code->clear();
|
||
ui->pushButton_3->setEnabled(true);
|
||
}
|
||
|
||
void Widget::connected_handler()
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+"\n"
|
||
+"MQTT服务器已连接上\n";
|
||
ui->log->insertPlainText(content);
|
||
sub = mqtt_client->subscribe(topic,1);
|
||
if (sub)
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+"\n"
|
||
+"Topic: "+ topic + QLatin1Char('\n')
|
||
+"Qos: "+QLatin1Char(sub->qos()+'0')+QLatin1Char('\n')+
|
||
"已订阅\n";
|
||
ui->log->insertPlainText(content);
|
||
}
|
||
else
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+"\n"
|
||
+"Topic: "+ topic + QLatin1Char('\n')
|
||
+"Qos: "+QLatin1Char(sub->qos()+'0')+QLatin1Char('\n')+
|
||
"订阅失败,请检查网络情况\n";
|
||
ui->log->insertPlainText(content);
|
||
}
|
||
|
||
}
|
||
|
||
void Widget::disconnected_handler()
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+"\n"
|
||
+"MQTT服务器已断开\n";
|
||
ui->log->insertPlainText(content);
|
||
|
||
}
|
||
|
||
void Widget::received_handler(const QByteArray &message, const QMqttTopicName &topic)
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+"\n"
|
||
+"Topic: "+topic.name()+ QLatin1Char('\n')
|
||
+"Message: "+message+ QLatin1Char('\n')+
|
||
+"Qos: "+QLatin1Char(sub->qos()+'0')+QLatin1Char('\n');
|
||
ui->log->insertPlainText(content);
|
||
}
|
||
|
||
void Widget::pingresp_handler()
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+ QLatin1Char('\n')
|
||
+ QLatin1String("MQTT Pong!")
|
||
+ QLatin1Char('\n');
|
||
ui->log->insertPlainText(content);
|
||
}
|
||
|
||
void Widget::state_handler()
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+ QLatin1Char('\n')
|
||
+ ":MQTT Client State Change"
|
||
+ QString::number(mqtt_client->state())
|
||
+ u'\n';
|
||
ui->log->insertPlainText(content);
|
||
}
|
||
|
||
void Widget::error_handler(const QMqttClient::ClientError error)
|
||
{
|
||
const QString content = QDateTime::currentDateTime().toString()
|
||
+"\n"
|
||
+"Error: "+ QString::number(static_cast<int>(error));
|
||
ui->log->insertPlainText(content);
|
||
}
|
||
|
||
|
||
void Widget::disconnect_form_server()
|
||
{
|
||
QMessageBox::critical(this,"连接超时","请检查您的网络连接!");
|
||
qApp->quit();
|
||
}
|
||
|
||
void Widget::update_line(QString data)
|
||
{
|
||
//向mqtt服务器发送路径消息
|
||
qDebug() << "向mqtt服务器发送的路径消息" << data;
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
void Widget::update_code(QString code)
|
||
{
|
||
if(code == "000")
|
||
{
|
||
QMessageBox::information(this,"消息","当前用户并无存车信息");
|
||
}
|
||
else
|
||
{
|
||
ui->code->setText(code);
|
||
ui->pushButton_3->setEnabled(false);
|
||
}
|
||
}
|
||
|
||
void Widget::handle_count(QString count)
|
||
{
|
||
QString data = QString("当前剩余空位'%1'").arg(count);
|
||
ui->label_4->setText(data);
|
||
if(count.toInt() == 0)
|
||
{
|
||
ui->pushButton_3->setEnabled(false);
|
||
}
|
||
else
|
||
{
|
||
ui->pushButton_3->setEnabled(true);
|
||
}
|
||
}
|
||
|
||
void Widget::on_pushButton_5_clicked()
|
||
{
|
||
emit find_code();
|
||
if(!ui->code->document()->isEmpty())
|
||
{
|
||
QMessageBox::information(this,"消息","已显示当前车位信息!");
|
||
}
|
||
}
|
||
|
||
|
||
void Widget::on_pushButton_6_clicked()
|
||
{
|
||
emit update_count();
|
||
}
|
||
|
||
|
||
void Widget::on_pushButton_7_clicked()
|
||
{
|
||
ui->stackedWidget->setCurrentIndex(0);
|
||
}
|
||
|