优化WiFi的扫描机制,节省内存开销

This commit is contained in:
you name 2021-03-31 19:57:50 +08:00
parent 5ebdc6c3cb
commit c6b26cdaeb
2 changed files with 96 additions and 30 deletions

View File

@ -128,14 +128,7 @@ MainWindow::MainWindow(QWidget *parent) :
KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon())); KWindowEffects::enableBlurBehind(this->winId(), true, QRegion(path.toFillPolygon().toPolygon()));
hasWifiConnected = false; hasWifiConnected = false;
// QDBusConnection systemBus = QDBusConnection::systemBus(); numberForWifiScan = 0;
// if (!systemBus.registerService("com.kylin.NetworkManager.qt.systemdbus")){
// qCritical() << "QDbus register service failed reason:" << systemBus.lastError();
// }
// if (!systemBus.registerObject("/", new SysdbusRegister(), QDBusConnection::ExportAllSlots | QDBusConnection::ExportAllSignals)){
// qCritical() << "QDbus register object failed reason:" << systemBus.lastError();
// }
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
@ -559,7 +552,7 @@ void MainWindow::initTimer()
checkWifiListChanged = new QTimer(this); checkWifiListChanged = new QTimer(this);
checkWifiListChanged->setTimerType(Qt::PreciseTimer); checkWifiListChanged->setTimerType(Qt::PreciseTimer);
QObject::connect(checkWifiListChanged, SIGNAL(timeout()), this, SLOT(onRequestScanAccesspoint())); QObject::connect(checkWifiListChanged, SIGNAL(timeout()), this, SLOT(onRequestScanAccesspoint()));
checkWifiListChanged->start(11*1000); checkWifiListChanged->start(10*1000);
//网线插入时定时执行 //网线插入时定时执行
wiredCableUpTimer = new QTimer(this); wiredCableUpTimer = new QTimer(this);
@ -698,10 +691,11 @@ void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
} }
} else { } else {
this->hide(); this->hide();
numberForWifiScan = 0;
} }
break; break;
case QSystemTrayIcon::DoubleClick: case QSystemTrayIcon::DoubleClick:
this->hide(); //this->hide();
break; break;
case QSystemTrayIcon::Context: case QSystemTrayIcon::Context:
//右键点击托盘图标弹出菜单 //右键点击托盘图标弹出菜单
@ -2955,8 +2949,6 @@ void MainWindow::activeWifiDisconn()
} }
void MainWindow::activeStartLoading() void MainWindow::activeStartLoading()
{ {
QTimer::singleShot(13*1000, objKyDBus, SLOT(requestScanWifi() ));
syslog(LOG_DEBUG, "Wi-Fi is disconnected"); syslog(LOG_DEBUG, "Wi-Fi is disconnected");
QString txt(tr("Wi-Fi is disconnected")); QString txt(tr("Wi-Fi is disconnected"));
objKyDBus->showDesktopNotify(txt); objKyDBus->showDesktopNotify(txt);
@ -3293,25 +3285,96 @@ void MainWindow::onExternalWifiSwitchChange(bool wifiEnabled)
void MainWindow::onRequestScanAccesspoint() void MainWindow::onRequestScanAccesspoint()
{ {
if (is_init_wifi_list || is_connect_hide_wifi) { if (isHuaWeiPC) {
return; //遇到启动软件第一次加载wifi列表的时候或正在连接隐藏wifi停止更新 numberForWifiScan += 1;
} int numScanPerSecond0 = 0;
int numScanPerSecond1 = 0;
if (numberForWifiScan > 12) {
numScanPerSecond0 = (numberForWifiScan-12)/6; //获取除数
numScanPerSecond1 = (numberForWifiScan-12)%6; //获取余数
}
if (is_stop_check_net_state==0 && this->is_btnWifiList_clicked==1 && this->isVisible()) { if (is_init_wifi_list || is_connect_hide_wifi) {
BackThread *loop_bt = new BackThread(); return; //遇到启动软件第一次加载wifi列表的时候或正在连接隐藏wifi停止更新
IFace *loop_iface = loop_bt->execGetIface(); }
if (loop_iface->wstate != 2) { if (is_stop_check_net_state==0 && this->is_btnWifiList_clicked==1) {
if (loop_iface->wstate == 0) { if (this->isVisible()) {
objKyDBus->requestScanWifi(); this->toScanWifi(1);
} else {
switch (numberForWifiScan) {
case 1 :
this->toScanWifi(0);
break;
case 2:
this->toScanWifi(0);
break;
case 3:
this->toScanWifi(0);
break;
case 6:
this->toScanWifi(0);
break;
case 9:
this->toScanWifi(0);
break;
case 12:
this->toScanWifi(0);
break;
}
if ((numScanPerSecond0 != 0) && (numScanPerSecond1 == 0)) {
this->toScanWifi(0);
}
} // end if(this->isVisible())
}
if (numberForWifiScan > 500) numberForWifiScan = 0;
} else {
if (is_init_wifi_list || is_connect_hide_wifi) {
return; //遇到启动软件第一次加载wifi列表的时候或正在连接隐藏wifi停止更新
}
if (is_stop_check_net_state==0 && this->is_btnWifiList_clicked==1 && this->isVisible()) {
BackThread *loop_bt = new BackThread();
IFace *loop_iface = loop_bt->execGetIface();
if (loop_iface->wstate != 2) {
current_wifi_list_state = UPDATE_WIFI_LIST;
this->ksnm->execGetWifiList(this->wcardname); //更新wifi列表
} }
delete loop_iface;
loop_bt->deleteLater();
}
}
}
void MainWindow::toScanWifi(bool isShow)
{
BackThread *loop_bt = new BackThread();
IFace *loop_iface = loop_bt->execGetIface();
if (loop_iface->wstate != 2) {
if (loop_iface->wstate == 0) {
if (isShow) {
objKyDBus->requestScanWifi(); //要求后台扫描AP
}
}
if (loop_iface->wstate == 1) {
objKyDBus->requestScanWifi(); //要求后台扫描AP
}
if (isShow) {
//只有在显示窗口时更新wifi列表
current_wifi_list_state = UPDATE_WIFI_LIST; current_wifi_list_state = UPDATE_WIFI_LIST;
this->ksnm->execGetWifiList(this->wcardname); //更新wifi列表 this->ksnm->execGetWifiList(this->wcardname); //更新wifi列表
} }
delete loop_iface;
loop_bt->deleteLater();
} }
delete loop_iface;
loop_bt->deleteLater();
} }
void MainWindow::on_setNetSpeed() void MainWindow::on_setNetSpeed()
@ -3521,6 +3584,7 @@ bool MainWindow::event(QEvent *event)
if (event->type() == QEvent::ActivationChange) { if (event->type() == QEvent::ActivationChange) {
if (QApplication::activeWindow() != this) { if (QApplication::activeWindow() != this) {
this->hide(); this->hide();
numberForWifiScan = 0;
} }
} }

View File

@ -174,10 +174,6 @@ public:
int is_fly_mode_on = 0; //是否已经打开飞行模式 int is_fly_mode_on = 0; //是否已经打开飞行模式
int is_hot_sopt_on = 0; //是否已经打开热点 int is_hot_sopt_on = 0; //是否已经打开热点
int is_connect_hide_wifi = 0; //是否正在连接隐藏wifi int is_connect_hide_wifi = 0; //是否正在连接隐藏wifi
QString currSelNetName = ""; //当前ScrollArea中选中的网络名称
QStringList canReconnectWifiList; //当前可以会连的wifi列表
QString currConnIfname = ""; //当前连接的有线网对应网卡名称,只有一个有线网连接的情况
QString oldWifiIpv4Method = ""; //原来的wifi的ipv4地址获取方式,自动还是手动
int currSelNetNum = 0; //当前选中的item序号 int currSelNetNum = 0; //当前选中的item序号
bool isLanBeConnUp = false; //lan是否连接上 bool isLanBeConnUp = false; //lan是否连接上
bool isWifiBeConnUp = false; //wifi是否是连接上 bool isWifiBeConnUp = false; //wifi是否是连接上
@ -185,14 +181,19 @@ public:
bool isToSetWifiValue = true; //本次执行是否进行赋值 bool isToSetWifiValue = true; //本次执行是否进行赋值
bool isWifiReconnecting = false; //是否正在执行wifi的回连 bool isWifiReconnecting = false; //是否正在执行wifi的回连
int addNumberForWifi = 0; //短时间内收到关于wifi连接信号的次数 int addNumberForWifi = 0; //短时间内收到关于wifi连接信号的次数
bool isHuaWeiPC;
QString currSelNetName = ""; //当前ScrollArea中选中的网络名称
QStringList canReconnectWifiList; //当前可以会连的wifi列表
QString currConnIfname = ""; //当前连接的有线网对应网卡名称,只有一个有线网连接的情况
QString oldWifiIpv4Method = ""; //原来的wifi的ipv4地址获取方式,自动还是手动
int numberForWifiScan; //该值控制wifi的扫描
int m_priX; int m_priX;
int m_priY; int m_priY;
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();
@ -362,6 +363,7 @@ private slots:
void activeStartLoading(); void activeStartLoading();
void activeGetWifiList(); void activeGetWifiList();
void onRequestScanAccesspoint(); void onRequestScanAccesspoint();
void toScanWifi(bool isShow);
void on_setNetSpeed(); void on_setNetSpeed();
void on_checkOverTime(); void on_checkOverTime();