54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
		
		
			
		
	
	
			54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
|  | #include "mqtt_config.h"
 | ||
|  | #include "ui_mqtt_config.h"
 | ||
|  | #include <QRegExpValidator>
 | ||
|  | MqttConfig::MqttConfig(QWidget *parent) : | ||
|  |     QWidget(parent), | ||
|  |     ui(new Ui::MqttConfig) | ||
|  | { | ||
|  |     ui->setupUi(this); | ||
|  |     QString exp = "\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.)" | ||
|  |                     "{3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b"; | ||
|  |     QRegExp rege(exp); | ||
|  |     QValidator *Validator = new QRegExpValidator(rege); | ||
|  |     ui->lineEdit_ip->setValidator(Validator); | ||
|  |     m_tcpClient = MyTcpClient::instance(); | ||
|  |     connect(m_tcpClient, SIGNAL(dataReceived(const QByteArray &)), this, SLOT(readData(const QByteArray &))); | ||
|  | 
 | ||
|  | } | ||
|  | 
 | ||
|  | MqttConfig::~MqttConfig() | ||
|  | { | ||
|  |     delete ui; | ||
|  | } | ||
|  | void MqttConfig::readData(const QByteArray &data) { | ||
|  |     qDebug() << "Received from server:" << data; | ||
|  |     uint8_t cmd = data[3]; | ||
|  |     if (cmd == kConfigMQTTBrokerInfo) { | ||
|  |         MQTTBrokerConfigInfoRsp mqtt_config_rsp; | ||
|  |         memcpy(&mqtt_config_rsp, data.data(), sizeof(MQTTBrokerConfigInfoRsp)); | ||
|  |         qDebug() << "mqtt_config_rsp code" << mqtt_config_rsp.code  ; | ||
|  |     } | ||
|  | } | ||
|  | void MqttConfig::on_pushButton_confirm_clicked() | ||
|  | { | ||
|  |     MQTTBrokerConfigInfoReq mqtt_config = { {0xAA, 0x55, 0xAA}, kConfigMQTTBrokerInfo, 1,1,{} }; | ||
|  |     memcpy(mqtt_config.ip,ui->lineEdit_ip->text().toStdString().c_str(),sizeof(mqtt_config.ip)); | ||
|  |     mqtt_config.port = ui->lineEdit_port->text().toInt(); | ||
|  |     char send_buf[100] ={0}; | ||
|  |     memcpy(send_buf, (char*)&mqtt_config, sizeof(MQTTBrokerConfigInfoReq)); | ||
|  | 
 | ||
|  |     int length =  sizeof(MQTTBrokerConfigInfoReq); | ||
|  |     qint64 bytesWritten = m_tcpClient->sendData(send_buf, length); | ||
|  |     m_tcpClient->waitForRead(); | ||
|  |     qDebug() << "bytesWritten: " << bytesWritten; | ||
|  | 
 | ||
|  |     this->close(); | ||
|  | } | ||
|  | 
 | ||
|  | 
 | ||
|  | void MqttConfig::on_pushButton_cancel_clicked() | ||
|  | { | ||
|  |     this->close(); | ||
|  | } | ||
|  | 
 |