-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogconf.cpp
197 lines (176 loc) · 6.28 KB
/
dialogconf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#pragma execution_character_set("utf-8")
#include "dialogconf.h"
#include "ui_dialogconf.h"
#include<QPushButton>
#include<QMessageBox>
#include<QTime>
#include<QTimer>
#include<QHostAddress>
DialogConf::DialogConf(DeviceSystem *system,QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogConf)
{
this->device_system = system;
ui->setupUi(this);
ui->lineEdit->setEnabled(false);
ui->lineEdit_2->setEnabled(false);
ui->lineEdit_3->setEnabled(false);
ui->lineEdit->setText("mywifi5G");
ui->lineEdit_2->setText("1234567890");
ui->lineEdit_3->setText("114.55.104.50");
ui->comboBox->setEnabled(false);
mytcpclient = new MyTCPClient();
connect(mytcpclient, SIGNAL(connectionFailed()), this, SLOT(onTcpClientConnectFailed()));
connect(mytcpclient, SIGNAL(newMessage(QString, QByteArray)), this, SLOT(onTcpClientAppendMessage(QString, QByteArray)));
connect(mytcpclient, SIGNAL(myClientConnected(QString, quint16)), this, SLOT(onTcpClientConnected(QString, quint16)));
ui->lineEdit_4->setText(device_system->local_addr.toString());
ui->lineEdit_4->setEnabled(false);
ui->lineEdit_5->setText(QString::number(device_system->order_local_port));
ui->lineEdit_6->setText(QString::number(device_system->data_local_port));
ui->lineEdit_7->setText(device_system->tcp_target_addr.toString());
ui->lineEdit_8->setText(QString::number(device_system->tcp_target_port));
}
DialogConf::~DialogConf()
{
delete ui;
delete mytcpclient;
}
void DialogConf::onTcpClientConnectFailed()
{
ui->lineEdit->setEnabled(false);
ui->lineEdit_2->setEnabled(false);
ui->lineEdit_3->setEnabled(false);
ui->comboBox->setEnabled(false);
ui->pushButton_3->setEnabled(true);
QMessageBox::warning(this, QStringLiteral("警告"), "连接失败,请将本机WiFi连接至热点SORL_WIRELESS_NODE_X");
}
void DialogConf::onTcpClientAppendMessage(const QString &from, const QByteArray &message)
{
return_message = message;
}
bool DialogConf::SetCmdValue(QString cmd, QString value)
{
QString text;
if(value.isEmpty()) text = cmd;
else text = cmd + " "+ value;
QByteArray data = text.toUtf8();
data.append("\r\n");
return_message.clear();
mytcpclient->sendMessage(data);
qDebug()<<"data"<<data.data();
qDebug()<<"start------------------"<<QTime::currentTime();
mytcpclient->waitForReadyRead(4000);
this->sleep(2000);
if(return_message == data) return true;
return false;
}
void DialogConf::sleep(int msec)
{
QEventLoop loop;//定义一个新的事件循环
QTimer::singleShot(msec, &loop, SLOT(quit()));//非阻塞
loop.exec();//事件循环开始执行,程序会卡在这里,直到定时时间到,本循环被退出
}
void DialogConf::on_pushButton_3_clicked()
{
//连接采集终端
mytcpclient->closeClient();
QHostAddress tcpClientTargetAddr("192.168.100.1");
quint16 tcpClientTargetPort = 5001;
mytcpclient->connectTo(tcpClientTargetAddr, tcpClientTargetPort);
}
void DialogConf::onTcpClientConnected(const QString &from, const quint16 port)
{
ui->lineEdit->setEnabled(true);
ui->lineEdit_2->setEnabled(true);
ui->lineEdit_3->setEnabled(true);
if(ui->comboBox_2->currentIndex() == 0) ui->comboBox->setEnabled(true);
ui->pushButton_3->setEnabled(false);
qDebug()<<"onTcpClientConnected";
}
void DialogConf::on_buttonBox_2_accepted()
{
int port;
bool ok;
port = ui->lineEdit_5->text().toInt(&ok,10);
if(ok) device_system->order_local_port = quint16(port);
else {
ui->lineEdit_5->setText(QString::number(device_system->order_local_port));
QMessageBox::warning(this,"警告","port错误");
return;
}
port = ui->lineEdit_6->text().toInt(&ok,10);
if(ok) device_system->data_local_port = quint16(port);
else
{
ui->lineEdit_6->setText(QString::number(device_system->data_local_port));
QMessageBox::warning(this,"警告","port错误");
return;
}
QHostAddress tcp_target_addr;
ok = tcp_target_addr.setAddress(ui->lineEdit_7->text());
if(ok) device_system->tcp_target_addr = tcp_target_addr;
else
{
ui->lineEdit_7->setText(device_system->tcp_target_addr.toString());
QMessageBox::warning(this,"警告","地址错误");
return;
}
port = ui->lineEdit_8->text().toInt(&ok,10);
if(ok) device_system->tcp_target_port = quint16(port);
else
{
ui->lineEdit_8->setText(QString::number(device_system->tcp_target_port));
QMessageBox::warning(this,"警告","port错误");
return;
}
accept();
}
void DialogConf::on_buttonBox_accepted()
{
QString ssid_name = ui->lineEdit->text();
QString ssid_pwd = ui->lineEdit_2->text();
QString server_ip = ui->lineEdit_3->text();
QString device_id;
QString SET_MASTER_CLK_STATUS;
if(ui->comboBox_2->currentIndex() == 0)
{
device_id = ui->comboBox->currentText();
SET_MASTER_CLK_STATUS.append('N');
}
else
{
device_id="0";
SET_MASTER_CLK_STATUS.append('Y');
}
if(ssid_name.isEmpty() || ssid_pwd.isEmpty() || device_id.isEmpty()){
QMessageBox::warning(this,"警告","输入密码和名称");
return;
}
if(SetCmdValue("SET_NODE_ID",device_id)==true
&& SetCmdValue("SET_RSI_JOIN_SSID",ssid_name)==true
&& SetCmdValue("SET_RSI_PSK",ssid_pwd)==true
&& SetCmdValue("SET_MASTER_CLK",SET_MASTER_CLK_STATUS)==true
&& SetCmdValue("SET_SERVER_IP",server_ip)==true)
{
if(SetCmdValue("SAVE_ALL_PARA",""))
{
sleep(100);
if(SetCmdValue("RESET_SYSTEM",""))
{
QMessageBox::warning(this,"设置成功","系统即将重启");
accept();
return;
}
else QMessageBox::warning(this,"失败","重启失败");
}
else QMessageBox::warning(this,"失败","保存失败");
}
else QMessageBox::warning(this,"失败","设置失败");
mytcpclient->closeClient();
ui->pushButton_3->setEnabled(true);
ui->lineEdit->setEnabled(false);
ui->lineEdit_2->setEnabled(false);
ui->lineEdit_3->setEnabled(false);
ui->comboBox->setEnabled(false);
qDebug()<<"ssid_name"<<ssid_name<<"ssid_pwd"<<ssid_pwd<<"device_id"<<device_id;
}