You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
6.8 KiB
C++

1 year ago
#include "module4g.h"
module4g::module4g(QObject *parent) : QObject(parent)
{
eth1_ip_kong = false;//网线ip状态判断
eth1_line_lan_flag = true;
times = 0;
QTimer::singleShot(1000, this, SLOT(onTimerTimeOutPing4g()));
}
void module4g::onTimerTimeOutPing4g()
{
qDebug("ping 4g: timeout 5s ping -I ppp0 202.108.22.5 > /home/data/4g_quailty");
std::system("timeout 5s ping -I ppp0 www.baidu.com -n > /home/data/4g_quailty");
QTimer::singleShot(5000, this, SLOT(onTimerTimeOutCheck4g()));
}
void module4g::onTimerTimeOutCheck4g()
{
QFile file_read_net("/home/data/4g_quailty");
QString net_check = "";
bool flag_4g = false;
if(file_read_net.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file_read_net);//创建一个对象让它去读文件
QString ping1=in.readLine();
QString ping2=in.readLine();
net_check=ping2.left(2);
}
file_read_net.close();
if (net_check=="64")
{
flag_4g = true;
}
else
{
flag_4g = false;
}
emit sig4gstate(flag_4g);
QString line_4G_ip = "";
if(flag_4g == false)
{
times = times +1;
if(times > 20)
{
times = 0;
std::system("/home/4G_reset.sh &");
std::system("ifconfig ppp0 | grep \"inet addr\" | awk '{ print $2}' | awk -F: '{print $2}' > /home/4G_IP");
QFile file_read_gip("/home/4G_IP");
if(file_read_gip.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file_read_gip);//创建一个对象让它去读文件
line_4G_ip=in.readLine();
}
file_read_gip.close();
if(line_4G_ip == "")
{
}else{
QString IP_4G_set="route add default gw "+line_4G_ip;
QString IP_4G_reset="route del default gw "+line_4G_ip;
std::system(IP_4G_reset.toLatin1());
std::system(IP_4G_set.toLatin1());
}
}
}
QFile file_write_Lan("/home/LAN_IP");
int count = 0;
bool flag_lan = false;
QString line_lan_ip = "";
QList<QNetworkInterface> netList = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface net,netList)//遍历每一个接口信息
{
if(net.name() == "eth1"){
count++;
QList<QNetworkAddressEntry> entryList = net.addressEntries();//获取ip地址和子网掩码和广播地址
if(entryList.isEmpty())
{
flag_lan = false;
if(!file_write_Lan.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
{
qDebug()<<"文件清空失败";
}
file_write_Lan.close();
}else{
flag_lan = true;
foreach(QNetworkAddressEntry entry,entryList)//遍历ip条目列表
{
if(entry.ip().protocol() == QAbstractSocket::IPv4Protocol){
if(file_write_Lan.open(QIODevice::WriteOnly))
{
QTextStream setWIFI_data(&file_write_Lan);
setWIFI_data<<entry.ip().toString()<<endl;
}
file_write_Lan.close();
line_lan_ip = entry.ip().toString();
}else{
}
}
}
}else{
}
}
if(count == 0)
{
flag_lan = false;
if(!file_write_Lan.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
{
qDebug()<<"文件清空失败";
}
file_write_Lan.close();
}
emit sigLanState(flag_lan);
QString wifi_ip = "";
if(flag_lan == false)
{
if(eth1_ip_kong == false)
{
eth1_line_lan_flag = false;
eth1_ip_kong = true;
QFile file_read_gip("/home/4G_IP");
if(file_read_gip.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file_read_gip);//创建一个对象让它去读文件
line_4G_ip=in.readLine();
}
file_read_gip.close();
QFile file_read_wifi_ip("/home/WIFI_IP");
if(file_read_wifi_ip.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file_read_wifi_ip);//创建一个对象让它去读文件
wifi_ip=in.readLine();
}
file_read_wifi_ip.close();
if(line_4G_ip == "")
{
if(wifi_ip == "")
{
//eth1_ip_kong = false;
}else{
QString wifi_ip_reset="route del default gw "+wifi_ip;
std::system(wifi_ip_reset.toLatin1());
QString wifi_ip_set="route add default gw "+wifi_ip;
std::system(wifi_ip_set.toLatin1());
}
}else{
QString IP_4G_set="route add default gw "+line_4G_ip;
QString IP_4G_reset="route del default gw "+line_4G_ip;
std::system(IP_4G_reset.toLatin1());
std::system(IP_4G_set.toLatin1());
if(wifi_ip == "")
{
}else{
QString wifi_ip_reset="route del default gw "+wifi_ip;
std::system(wifi_ip_reset.toLatin1());
QString wifi_ip_set="route add default dev mlan0 metric 20 gw "+wifi_ip;
std::system(wifi_ip_set.toLatin1());
}
}
}else{
}
}else{
eth1_ip_kong = false;
if(eth1_line_lan_flag == false)
{
int local = line_lan_ip.indexOf('.',9,Qt::CaseInsensitive);//意思是从第9位往后找找到的第一个.’所在的位置
line_lan_ip=line_lan_ip.left(local+1);//从左向右截取n个字符串
line_lan_ip=line_lan_ip+"1";
line_lan_ip="route add default gw "+line_lan_ip;
std::system(line_lan_ip.toLatin1());
eth1_line_lan_flag = true;
}else{
if(flag_4g == false)
{
int local = line_lan_ip.indexOf('.',9,Qt::CaseInsensitive);//意思是从第9位往后找找到的第一个.’所在的位置
line_lan_ip=line_lan_ip.left(local+1);//从左向右截取n个字符串
line_lan_ip=line_lan_ip+"1";
line_lan_ip="route add default gw "+line_lan_ip;
std::system(line_lan_ip.toLatin1());
}
}
}
QTimer::singleShot(1000, this, SLOT(onTimerTimeOutPing4g()));
}