#include "networkDetection.h" #include networkDetection::networkDetection(QObject *parent, int id) : QObject(parent), m_id(id) { flagRun = true; CmdListChanged = false; m_process = new QProcess(this); } networkDetection::~networkDetection() { delete m_process; } void networkDetection::dowork(int id, QStringList cmdlist) { m_cmd = cmdlist; if (id != m_id) return; QString result; while (flagRun) { m_process->start("cmd", m_cmd); // if(id == 1) // { // QDateTime time = QDateTime::currentDateTime(); // QString str = time .toString("start yyyy-MM-dd // hh:mm:ss\n");//格式化时间 .toString("yyyy-MM-dd // hh:mm:ss");//格式化时间 qDebug()<< str; // } m_process->waitForFinished(); // 等待指令执行完毕 // if(id == 1) // { // QDateTime time = QDateTime::currentDateTime(); // QString str = time .toString("end yyyy-MM-dd // hh:mm:ss\n");//格式化时间 .toString("yyyy-MM-dd // hh:mm:ss");//格式化时间 qDebug()<< str; // } result = QString::fromLocal8Bit(m_process->readAll()); // 获取指令执行结果 // qDebug() << result; if (result.contains(QString("TTL="))) // 若包含TTL=字符串则认为网络在线 { // qDebug() << "在线"; QString delay = selectTTL(result); emit resultReady(m_id, "在线", delay); } else { // qDebug() << "离线"; emit resultReady(m_id, "离线", "通信异常"); } QThread::sleep(3); // 加sleep降低CPU占用率 } } void networkDetection::stopwork(int id) { if (id == m_id) { flagRun = false; } } void networkDetection::DyChangeCmdList(QStringList changeCmdList) { m_cmd.clear(); m_cmd = changeCmdList; CmdListChanged = true; } // 查找延迟 QString networkDetection::selectTTL(QString str) { str = str.remove(0, str.indexOf("平均 = ") + 5); str = str.left(str.indexOf("ms") + 2); return "通信正常\n" + str; }