Add function to reconnect the best wifi after disconnect a wifi

This commit is contained in:
andrew-wuhan-huawei-990-pc 2021-03-25 21:49:36 +08:00
parent 051a165ddc
commit de8e3d4ca9
3 changed files with 128 additions and 22 deletions

View File

@ -1699,9 +1699,20 @@ void MainWindow::getWifiListDone(QStringList slist)
} }
} }
if (current_wifi_list_state == RECONNECT_WIFI) { if (isHuaWeiPC) {
wifiListOptimize(slist); if (slist.size() >= 2) {
wifiListOptimize(slist);
getFinalWifiList(slist);
}
}
qDebug()<<"------------";
foreach (QString str, slist) {
qDebug()<<str;
}
qDebug()<<"------------";
if (current_wifi_list_state == RECONNECT_WIFI) {
QStringList targetWifiList = connectableWifiPriorityList(slist); QStringList targetWifiList = connectableWifiPriorityList(slist);
if (!targetWifiList.isEmpty()) { if (!targetWifiList.isEmpty()) {
if (!isWifiReconnecting) { if (!isWifiReconnecting) {
@ -1768,8 +1779,9 @@ void MainWindow::getConnListDone(QStringList slist)
//进行wifi列表优化选择 //进行wifi列表优化选择
void MainWindow::wifiListOptimize(QStringList& slist) void MainWindow::wifiListOptimize(QStringList& slist)
{ {
//这个函数可能会把已经连接的那个wifi给筛选出去
QString headLine = slist.at(0); QString headLine = slist.at(0);
int indexSignal,indexSecu, indexFreq, indexBSsid, indexName; int indexSignal,indexSecu, indexFreq, indexBSsid, indexName,indexPath;
headLine = headLine.trimmed(); headLine = headLine.trimmed();
bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+")); bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
@ -1779,14 +1791,15 @@ void MainWindow::wifiListOptimize(QStringList& slist)
indexFreq = headLine.indexOf("频率") + 4; indexFreq = headLine.indexOf("频率") + 4;
indexBSsid = headLine.indexOf("BSSID") + 6; indexBSsid = headLine.indexOf("BSSID") + 6;
indexName = indexBSsid + 19; indexName = indexBSsid + 19;
indexPath = headLine.indexOf("DBUS-PATH");
} else { } else {
indexSignal = headLine.indexOf("SIGNAL"); indexSignal = headLine.indexOf("SIGNAL");
indexSecu = headLine.indexOf("SECURITY"); indexSecu = headLine.indexOf("SECURITY");
indexFreq = headLine.indexOf("FREQ"); indexFreq = headLine.indexOf("FREQ");
indexBSsid = headLine.indexOf("BSSID"); indexBSsid = headLine.indexOf("BSSID");
indexName = indexBSsid + 19; indexName = indexBSsid + 19;
indexPath = headLine.indexOf("DBUS-PATH");
} }
int indexPath = headLine.indexOf("DBUS-PATH");
QStringList targetList; //slist优化同名同频AP中只留信号最强 QStringList targetList; //slist优化同名同频AP中只留信号最强
targetList<<slist.at(0); //把第一行加进去 targetList<<slist.at(0); //把第一行加进去
@ -1823,6 +1836,71 @@ void MainWindow::wifiListOptimize(QStringList& slist)
return ; return ;
} }
void MainWindow::getFinalWifiList(QStringList &slist)
{
if(slist.size() < 2) return ;
QString headLine = slist.at(0);
int indexSignal,indexSecu, indexFreq, indexBSsid, indexName,indexPath;
headLine = headLine.trimmed();
bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
if (isChineseExist) {
indexSignal = headLine.indexOf("SIGNAL");
indexSecu = headLine.indexOf("安全性");
indexFreq = headLine.indexOf("频率") + 4;
indexBSsid = headLine.indexOf("BSSID") + 6;
indexName = indexBSsid + 19;
indexPath = headLine.indexOf("DBUS-PATH");
} else {
indexSignal = headLine.indexOf("SIGNAL");
indexSecu = headLine.indexOf("SECURITY");
indexFreq = headLine.indexOf("FREQ");
indexBSsid = headLine.indexOf("BSSID");
indexName = indexBSsid + 19;
indexPath = headLine.indexOf("DBUS-PATH");
}
QStringList deleteWifiStr;
for(int ii = 1;ii < slist.size();ii++){
if ((ii+1) == slist.size()) {
break;
}
QString wifiInfo = slist.at(ii);
QString conName = wifiInfo.mid(indexName, indexPath - indexName).trimmed();
int conSignal = wifiInfo.mid(indexSignal,3).trimmed().toInt();
int conFreq = wifiInfo.mid(indexFreq,4).trimmed().toInt();
for(int jj=ii+1;jj<slist.size();jj++){
QString wifiStr = slist.at(jj);
QString name = wifiStr.mid(indexName, indexPath - indexName).trimmed();
int signal = wifiStr.mid(indexSignal,3).trimmed().toInt();
int freq = wifiStr.mid(indexFreq,4).trimmed().toInt();
if(conName == name){
if (conFreq > freq) {
if ((signal-conSignal) > 50) { //低频的信号格数比高频大于两格,选低频
deleteWifiStr.append(wifiInfo);
} else {
deleteWifiStr.append(wifiStr);
}
} else {
if ((conSignal-signal) > 50) { //低频的信号格数比高频大于两格,选低频
deleteWifiStr.append(wifiStr);
} else {
deleteWifiStr.append(wifiInfo);
}
}
break;
}
}
}
foreach (QString deleteStr, deleteWifiStr) {
slist.removeOne(deleteStr);
}
return;
}
//从有配置文件的wifi选出最优wifi进行连接 //从有配置文件的wifi选出最优wifi进行连接
QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){ QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){
QStringList target; QStringList target;
@ -1847,18 +1925,18 @@ QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){
} }
int indexPath = headLine.indexOf("DBUS-PATH"); int indexPath = headLine.indexOf("DBUS-PATH");
QStringList tmp = slist; QStringList tmp = slist;
for(int i=1;i<tmp.size();i++){ for (int i=1;i<tmp.size();i++) {
QString line = tmp.at(i); QString line = tmp.at(i);
QString name = line.mid(indexName,indexPath - indexName).trimmed(); QString wifiname = line.mid(indexName,indexPath - indexName).trimmed();
QString wifibssid = line.mid(indexBSsid, indexName-indexBSsid).trimmed(); QString wifibssid = line.mid(indexBSsid, indexName-indexBSsid).trimmed();
QString wifiObjectPath = line.mid(indexPath).trimmed(); QString wifiObjectPath = line.mid(indexPath).trimmed();
int freq = line.mid(indexFreq,4).trimmed().toInt(); if (ocf->isWifiConfExist(wifiname) && wifiname != DisconnectedWifiSsidManualy) { //两格以上有配置的5Gwifi中选择信号最佳的
int signal = line.mid(indexSignal,3).trimmed().toInt();
if(freq >= 5000 && ocf->isWifiConfExist(name) && signal > 55 && name != DisconnectedWifiSsidManualy){ //两格以上有配置的5Gwifi中选择信号最佳的
target << wifiObjectPath <<wifibssid; target << wifiObjectPath <<wifibssid;
tmp.removeAt(i); tmp.removeAt(i);
} }
} }
ocf->deleteLater();
return target; return target;
} }
@ -1985,7 +2063,8 @@ void MainWindow::loadWifiListDone(QStringList slist)
if (actWifiBssidList.contains(wbssid)) { if (actWifiBssidList.contains(wbssid)) {
actWifiName = wname; actWifiName = wname;
} }
if ("*" == line.mid(0,indexSignal).trimmed()){ if ("*" == line.mid(0,indexSignal).trimmed()) {
//在华为的电脑中因为前面的优选工作及时有已经连接的wifi也可能会被筛选出去
actWifiBssid = wbssid; actWifiBssid = wbssid;
} }
} }
@ -2002,13 +2081,23 @@ void MainWindow::loadWifiListDone(QStringList slist)
QString wname = line.mid(indexName, indexPath - indexName).trimmed(); QString wname = line.mid(indexName, indexPath - indexName).trimmed();
QString wfreq = line.mid(indexFreq, 4).trimmed(); QString wfreq = line.mid(indexFreq, 4).trimmed();
if (actWifiName != "--" && actWifiName == wname) { if (!isHuaWeiPC) {
if (!actWifiBssidList.contains(wbssid)) { //如果不是华为的电脑选择wifi在这里执行
continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同但bssid不同则跳过 if (actWifiName != "--" && actWifiName == wname) {
if (!actWifiBssidList.contains(wbssid)) {
continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同但bssid不同则跳过
}
}
if ((wnames.contains(wname) && wbssid != actWifiBssid)) {
continue; //过滤相同名称的wifi
}
} else {
//qDebug() << "--------------------> actWifiName= " << actWifiName << " wname= " << wname;
if ((actWifiName == wname) && actWifiBssidList.size()>=1) {
//防止列表中没有已经连接的那个wifi
wbssid = actWifiBssidList.at(0);
actWifiBssid = actWifiBssidList.at(0);
} }
}
if ((wnames.contains(wname) && wbssid != actWifiBssid)) {
continue; //过滤相同名称的wifi
} }
int max_freq = wfreq.toInt(); int max_freq = wfreq.toInt();
@ -3312,10 +3401,13 @@ void MainWindow::connWifiDone(int connFlag)
{ {
// Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接 // Wifi连接结果0点击连接成功 1失败 2没有配置文件 3开机启动网络工具时已经连接
if (connFlag == 0) { if (connFlag == 0) {
WifiAuthThread *wifi_auth_thread=new WifiAuthThread(); WifiAuthThread *wifi_auth_thread=new WifiAuthThread();
wifi_auth_thread->start(); wifi_auth_thread->start();
syslog(LOG_DEBUG, "Wi-Fi already connected by clicking button"); syslog(LOG_DEBUG, "Wi-Fi already connected by clicking button");
this->ksnm->execGetWifiList(this->wcardname); if (!isHuaWeiPC) {
//如果是华为电脑,使用获取连接信号的方式更新列表
this->ksnm->execGetWifiList(this->wcardname);
}
QString txt(tr("Conn Wifi Success")); QString txt(tr("Conn Wifi Success"));
objKyDBus->showDesktopNotify(txt); objKyDBus->showDesktopNotify(txt);

View File

@ -184,7 +184,7 @@ public:
bool isWifiBeConnUp = false; //wifi是否是连接上 bool isWifiBeConnUp = false; //wifi是否是连接上
bool isToSetLanValue = true; //本次执行是否进行赋值 bool isToSetLanValue = true; //本次执行是否进行赋值
bool isToSetWifiValue = true; //本次执行是否进行赋值 bool isToSetWifiValue = true; //本次执行是否进行赋值
bool isWifiReconnecting; bool isWifiReconnecting = false; //是否正在执行wifi的回连
int addNumberForWifi = 0; int addNumberForWifi = 0;
int m_priX; int m_priX;
@ -192,6 +192,8 @@ public:
int m_priWid; int m_priWid;
int m_priHei; int m_priHei;
int isHuaWeiPC;
public slots: public slots:
void onPhysicalCarrierChanged(bool flag); void onPhysicalCarrierChanged(bool flag);
void onCarrierUpHandle(); void onCarrierUpHandle();
@ -301,7 +303,8 @@ private:
QString lastAddedConn = ""; QString lastAddedConn = "";
QString oldActLanName = ""; //上一次获取的已连接有线网名称 QString oldActLanName = ""; //上一次获取的已连接有线网名称
int oldDbusActLanDNS = 0; //上一次获取的已连接有线网的DNS代号 int oldDbusActLanDNS = 0; //上一次获取的已连接有线网的DNS代号
void wifiListOptimize(QStringList& slist); //只保留同名同频信号最强AP void wifiListOptimize(QStringList& slist); //只保留同名同频信号最强AP
void getFinalWifiList(QStringList& slist); //获取应该显示在wifi列表中的最优列表参数
QStringList connectableWifiPriorityList(const QStringList slist); //可连接wifi优先级列表 QStringList connectableWifiPriorityList(const QStringList slist); //可连接wifi优先级列表
//循环检测网络连接状态 //循环检测网络连接状态
QTimer *iconTimer = nullptr; QTimer *iconTimer = nullptr;
@ -329,7 +332,6 @@ private:
bool hasWifiConnected;//当前是否有wifi连接 bool hasWifiConnected;//当前是否有wifi连接
QDBusInterface *mDbusXrandInter; QDBusInterface *mDbusXrandInter;
int isHuaWeiPC;
private slots: private slots:
void iconActivated(QSystemTrayIcon::ActivationReason reason); void iconActivated(QSystemTrayIcon::ActivationReason reason);

View File

@ -32,6 +32,7 @@
#include <QtDBus/QDBusObjectPath> #include <QtDBus/QDBusObjectPath>
#include <QDBusReply> #include <QDBusReply>
#include <QDBusObjectPath> #include <QDBusObjectPath>
#include <QtConcurrent>
extern int currentActWifiSignalLv; extern int currentActWifiSignalLv;
@ -886,6 +887,17 @@ void OneConnForm::slotConnWifiResult(int connFlag)
connType = ""; connType = "";
if (connFlag == 0) { if (connFlag == 0) {
if (mw->isHuaWeiPC) {
//network-manager可能回连接到其他bssid对应的网络改成我们想要连接的那个网络
QFuture < void > future1 = QtConcurrent::run([=]() {
qDebug() << "实际连接的wifi的bssid是 " << wifiBSsid;
QString modityCmd = "nmcli connection modify \""+ wifiName + "\" " + "802-11-wireless.bssid " + wifiBSsid;
system(modityCmd.toUtf8().data());
QString reconnectWifiCmd = "nmcli connection up \"" + wifiName + "\"";
system(reconnectWifiCmd.toUtf8().data());
});
}
disconnect(this, SIGNAL(selectedOneWifiForm(QString,int)), mw, SLOT(oneWifiFormSelected(QString,int))); disconnect(this, SIGNAL(selectedOneWifiForm(QString,int)), mw, SLOT(oneWifiFormSelected(QString,int)));
} }