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) {
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);
if (!targetWifiList.isEmpty()) {
if (!isWifiReconnecting) {
@ -1768,8 +1779,9 @@ void MainWindow::getConnListDone(QStringList slist)
//进行wifi列表优化选择
void MainWindow::wifiListOptimize(QStringList& slist)
{
//这个函数可能会把已经连接的那个wifi给筛选出去
QString headLine = slist.at(0);
int indexSignal,indexSecu, indexFreq, indexBSsid, indexName;
int indexSignal,indexSecu, indexFreq, indexBSsid, indexName,indexPath;
headLine = headLine.trimmed();
bool isChineseExist = headLine.contains(QRegExp("[\\x4e00-\\x9fa5]+"));
@ -1779,14 +1791,15 @@ void MainWindow::wifiListOptimize(QStringList& slist)
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");
}
int indexPath = headLine.indexOf("DBUS-PATH");
QStringList targetList; //slist优化同名同频AP中只留信号最强
targetList<<slist.at(0); //把第一行加进去
@ -1823,6 +1836,71 @@ void MainWindow::wifiListOptimize(QStringList& slist)
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进行连接
QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){
QStringList target;
@ -1847,18 +1925,18 @@ QStringList MainWindow::connectableWifiPriorityList(const QStringList slist){
}
int indexPath = headLine.indexOf("DBUS-PATH");
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 name = line.mid(indexName,indexPath - indexName).trimmed();
QString wifiname = line.mid(indexName,indexPath - indexName).trimmed();
QString wifibssid = line.mid(indexBSsid, indexName-indexBSsid).trimmed();
QString wifiObjectPath = line.mid(indexPath).trimmed();
int freq = line.mid(indexFreq,4).trimmed().toInt();
int signal = line.mid(indexSignal,3).trimmed().toInt();
if(freq >= 5000 && ocf->isWifiConfExist(name) && signal > 55 && name != DisconnectedWifiSsidManualy){ //两格以上有配置的5Gwifi中选择信号最佳的
if (ocf->isWifiConfExist(wifiname) && wifiname != DisconnectedWifiSsidManualy) { //两格以上有配置的5Gwifi中选择信号最佳的
target << wifiObjectPath <<wifibssid;
tmp.removeAt(i);
}
}
ocf->deleteLater();
return target;
}
@ -1985,7 +2063,8 @@ void MainWindow::loadWifiListDone(QStringList slist)
if (actWifiBssidList.contains(wbssid)) {
actWifiName = wname;
}
if ("*" == line.mid(0,indexSignal).trimmed()){
if ("*" == line.mid(0,indexSignal).trimmed()) {
//在华为的电脑中因为前面的优选工作及时有已经连接的wifi也可能会被筛选出去
actWifiBssid = wbssid;
}
}
@ -2002,6 +2081,8 @@ void MainWindow::loadWifiListDone(QStringList slist)
QString wname = line.mid(indexName, indexPath - indexName).trimmed();
QString wfreq = line.mid(indexFreq, 4).trimmed();
if (!isHuaWeiPC) {
//如果不是华为的电脑选择wifi在这里执行
if (actWifiName != "--" && actWifiName == wname) {
if (!actWifiBssidList.contains(wbssid)) {
continue; //若当前热点ssid名称和已经连接的wifi的ssid名称相同但bssid不同则跳过
@ -2010,6 +2091,14 @@ void MainWindow::loadWifiListDone(QStringList slist)
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);
}
}
int max_freq = wfreq.toInt();
int min_freq = wfreq.toInt();
@ -3315,7 +3404,10 @@ void MainWindow::connWifiDone(int connFlag)
WifiAuthThread *wifi_auth_thread=new WifiAuthThread();
wifi_auth_thread->start();
syslog(LOG_DEBUG, "Wi-Fi already connected by clicking button");
if (!isHuaWeiPC) {
//如果是华为电脑,使用获取连接信号的方式更新列表
this->ksnm->execGetWifiList(this->wcardname);
}
QString txt(tr("Conn Wifi Success"));
objKyDBus->showDesktopNotify(txt);

View File

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

View File

@ -32,6 +32,7 @@
#include <QtDBus/QDBusObjectPath>
#include <QDBusReply>
#include <QDBusObjectPath>
#include <QtConcurrent>
extern int currentActWifiSignalLv;
@ -886,6 +887,17 @@ void OneConnForm::slotConnWifiResult(int connFlag)
connType = "";
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)));
}