void CProtocolDemo::GetCPUInfo(CPointDemoBase *pPoint_) { // (1) 处理器使用率 // 这里要从/proc/stat中提取四个数据:用户模式(user)、低优先级的用户模式(nice)、内核模式(system)以及空闲的处理器时间(idle)。它们均位于/proc/stat文件的第一行。CPU的利用率使用如下公式来计算。 // CPU利用率 = 100 *(user + nice + system)/(user + nice + system + idle) CTagI *pTempTagI = pPoint_->m_pTag; if (pTempTagI) { double fValue=0.0; FILE *fd; // int n; char buff[256]; CPU_OCCUPY cpu_occupy; // cpu_occupy=cpust; fd = fopen (PROC_STAT, "r"); fgets (buff, sizeof(buff), fd); sscanf (buff, "%s %u %u %u %u", cpu_occupy.name, &cpu_occupy.user, &cpu_occupy.nice,&cpu_occupy.system, &cpu_occupy.idle); fclose(fd); qDebug()<<"======================"<<cpu_occupy.user<<cpu_occupy.nice<<cpu_occupy.system<<cpu_occupy.idle; fValue = 100 * (cpu_occupy.user+cpu_occupy.nice+cpu_occupy.system)/(cpu_occupy.user+cpu_occupy.nice+cpu_occupy.system+cpu_occupy.idle); QVariant Value(fValue); pTempTagI->SetValue(pPoint_,Value,Value); } }
void CJavaScript_Protocol::slot_TagChange(CPointI *pPoint_, QVariant /*VarSet_*/) { CJavaScript_DataChange_Point *pPoint = (CJavaScript_DataChange_Point *)pPoint_; qDebug()<<"222222222222222"<<__func__<<__LINE__<<pPoint->m_strLink; for (int nJavaScriptCount = 0; nJavaScriptCount < pPoint->m_JavaScriptList.count(); ++nJavaScriptCount) { JavaScript *pJavaScript = pPoint->m_JavaScriptList.at(nJavaScriptCount); qDebug()<<__func__<<__LINE__<<pPoint->m_strLink<<pJavaScript->m_strScript; QScriptEngine engine; QMap<QString,PointStruct*>::iterator iterator; for (iterator = pJavaScript->m_PointStructMap.begin(); iterator != pJavaScript->m_PointStructMap.end(); ++iterator) { QString strName = iterator.value()->m_strName; qDebug()<<__func__<<__LINE__<<pPoint->m_strLink<<pJavaScript->m_strScript<<strName; CTagI *pTag = iterator.value()->m_pPoint->m_pTag; if (pTag) { iterator.value()->m_pPoint->m_SrcValue = pTag->GetProjectValue()->GetVarValue(); engine.globalObject().setProperty(strName,pTag->GetProjectValue()->GetVarValue().toFloat()); qDebug()<<__func__<<__LINE__<<pPoint->m_strLink<<pJavaScript->m_strScript<<strName<<pTag->GetProjectValue()->GetVarValue().toFloat(); }else { qDebug()<<__func__<<__LINE__<<pPoint->m_strLink<<pJavaScript->m_strScript<<strName<<"return;"; // return; } } engine.evaluate(pJavaScript->m_strScript); for (iterator = pJavaScript->m_PointStructMap.begin(); iterator != pJavaScript->m_PointStructMap.end(); ++iterator) { QString strName = iterator.value()->m_strName; qDebug()<<__func__<<__LINE__<<pPoint->m_strLink<<pJavaScript->m_strScript<<strName; CTagI *pTag = iterator.value()->m_pPoint->m_pTag; if (pTag) { qDebug()<<__func__<<__LINE__<<pTag->GetName(); QVariant value = engine.globalObject().property(strName).toVariant(); if (value != iterator.value()->m_pPoint->m_SrcValue) { engine.globalObject().setProperty(strName,pTag->GetProjectValue()->GetVarValue().toFloat()); pTag->SetValue(iterator.value()->m_pPoint,value,value); qDebug()<<__func__<<__LINE__<<pTag->GetName()<<"setvalue1"<<value; }else { qDebug()<<__func__<<__LINE__<<pTag->GetName()<<"setvalue2"<<value; } }else { // return; } } } }
void CProtocolDLT_645_07::ProcessRespond(CDLT_645_07Response &response) { if (response.GetUIntValue(8,1) & 0x40) { return; } else { QByteArray address((char *)(response.GetBuffer(0)+1),6);/// 设备地址 unsigned int unDeviceAddress = response.GetUIntValue(10,4); qDebug()<<"response.GetUIntValue(10,4);"<<unDeviceAddress; m_PointNode.FindPointMap(address,unDeviceAddress); DLT_645_07PointMap *pPointMap = m_PointNode.FindPointMap(address,unDeviceAddress); if (pPointMap) { qDebug()<<"if (pPointMap)"<<pPointMap->count()<<address.toHex()<<unDeviceAddress; DLT_645_07PointMap::iterator iterator; for (iterator = pPointMap->begin(); iterator != pPointMap->end(); ++iterator) { CPointDLT_645_07 *pPoint = iterator.value(); if (pPoint) { QString strLink = pPoint->m_strRTDBName; int nByteCount = pPoint->m_nByteCount; int nStartByte = pPoint->m_nStartByte; qDebug()<<"if (pPoint)"<<pPoint->m_strRTDBName<<pPoint->m_nByteCount<<pPoint->m_nStartByte; BYTE nLength = response.GetUIntValue(9,1); if ((nByteCount+4) <= nLength) { qDebug()<<"if ((nByteCount+4) <= nLength)"; BYTE Value[nByteCount]; for (int i = 0; i < nByteCount; ++i) { Value[i] = response.GetUIntValue(i+14+nStartByte,1); Value[i]-= 0x33; } BYTE Temp[nByteCount]; for (int i = 0; i < nByteCount; ++i) { Temp[i] = Value[nByteCount-1-i]; } QByteArray baValue((char *)Temp,nByteCount); baValue = baValue.toHex(); ShowMessage(2,baValue); int nDecimalPlaces = pPoint->m_nDecimalPlaces;/// @note 小数点位数 QString Project_(baValue); if (nDecimalPlaces) { if (Project_.count()<nDecimalPlaces) { Project_ = "00000000000000000000"+Project_; Project_.insert(Project_.count()-nDecimalPlaces,'.'); Project_ = Project_.right(nDecimalPlaces+2); }else { Project_.insert(Project_.count()-nDecimalPlaces,'.'); } } while(Project_.count()) { if (Project_.at(0)!='0') { break; }else { Project_.remove(0,1); } } if (Project_.count()) { if (Project_.at(0)=='.') Project_.insert(0,'0'); } QVariant VarProject_; QVariant VarOriginal_; if (Project_.contains('.')) { VarProject_ = QVariant(Project_.toFloat()); VarOriginal_ = QVariant(Project_.toFloat()); }else { VarProject_ = QVariant(Project_.toInt()); VarOriginal_ = QVariant(Project_.toInt()); } qDebug()<<"Project_:="<< Project_<<Project_.toFloat()<<Project_.toInt()<<VarProject_; ShowMessage(2,VarProject_.toString()+"---"+VarOriginal_.toString()); CTagI *pTag = pPoint->GetTag();; if (pTag) { pTag->SetValue(pPoint,VarProject_,VarOriginal_); } } } } }else { qDebug()<<"if (pPointMap) NULL"; } } #if 0 else {
void CProtocolDemo::GetRand(CPointDemoBase *pPoint_) { // qDebug()<<"xxxxxxxxxxxxxxxxxxxxxxx"<<pPoint_->m_nFunction<<pPoint_->m_strRTDBName; QVariant Value; int nType = pPoint_->m_nFunction; int nMax = pPoint_->m_nPara1; int nMin = pPoint_->m_nPara2; int nStep = pPoint_->m_nPara3; CTagI *pTempTagI = pPoint_->m_pTag; // qDebug()<<__func__<<"pTempTagI ="<<pTempTagI; // qDebug()<<"pPoint_->m_pTag"<<pPoint_->m_pTag->GetName(); if (pTempTagI) { QString strValue; if (pTempTagI->GetPointCollection()) { strValue = pTempTagI->GetProjectValue()->GetVarValue().toString(); } int m_nLastValue = strValue.toInt(); //功能:生成各种随机数据 //参数:nType生成数据的方式 //返回值: double fValue=0.0; // srand(time(NULL)); switch(nType) { case 70://常量 参数为:70 常量值 fValue = nMax; break; case 72://一定范围内的随机量 参数为:72 最大值 最小值 fValue= rand()%(nMax-nMin)+nMin; break; case 74://单调增加 参数为:74 初值 终值 步长(<0 为单调递减) { int nStartValue = nMax; int nEndValue = nMin; Q_ASSERT(nStartValue <= nEndValue); m_nLastValue += nStep; if(m_nLastValue>nEndValue) //不在有效值范围内) { m_nLastValue = nStartValue; } else if(m_nLastValue<nStartValue) { m_nLastValue = nEndValue; } } fValue= m_nLastValue; break; case 76://正弦变量 参数为:76 基值 幅值 步长(角度) { int nBaseValue = nMax; int nK = nMin; fValue = nBaseValue + nK * sin(m_nLastValue*3.14/180.0); m_nLastValue += nStep; if(m_nLastValue>360) { m_nLastValue -= 360; } } break; case 78://方波 参数为:78 高值,低值(扫描周期就是方波长度) if(m_nLastValue>0) { fValue = nMax; } else { fValue = nMin; } m_nLastValue = 1 - m_nLastValue; break; case 80://遥信量 参数为:80 (扫描周期就是变位周期) // qDebug()<<"AAAAAAAAAAAAAAAAAAAAAAAAAAAAA"<<pPoint_->m_nFunction<<pPoint_->m_strRTDBName; m_nLastValue = 1 - m_nLastValue; fValue=m_nLastValue; break; case 82://遥信量 参数为:82 每次变位的可能性(0-100) { int nRand= rand()%101;//0--100 // qDebug()<<"ZZZZZZZZz"<<m_nLastValue<<nRand; if(nRand<nMax) { m_nLastValue = 1 - m_nLastValue; } fValue=m_nLastValue; } break; default: Q_ASSERT(false);//("IO变量定义无效:未定义的随机变量取值方式 funcno=100 param1=%d\n",nType); break; } Value.setValue(fValue); // qDebug()<<"Value_.setValue(fValue);"<<Value; pTempTagI->SetValue(pPoint_,Value,Value); } // return true; }
void CProtocolDemo::GetMemoryInfo(CPointDemoBase *pPoint_) { CTagI *pTempTagI = pPoint_->m_pTag; if (pTempTagI) { double fValue=0.0; switch(pPoint_->m_nFunction) { case 20://物理内存总数 { QString strTitle_("MemTotal"); QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); fValue = m.total; break; } } file.close(); } } break; case 22://物理内存空闲 { QString strTitle_("MemFree"); QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); fValue = m.total; break; } } file.close(); } } break; case 24://物理内存使用 { QString strTitle_1("MemTotal"); QString strTitle_2("MemFree"); double value1 = 0.0; double value2 = 0.0; QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_1)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); value1 = m.total; break; }else if (lineList.at(i).contains(strTitle_2)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); value2 = m.total; break; } } file.close(); fValue = value1 - value2; } } break; case 26://虚内存总数 { QString strTitle_("VmallocTotal"); QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); fValue = m.total; break; } } file.close(); } } break; case 28://虚内存空闲 { QString strTitle_1("VmallocTotal"); QString strTitle_2("VmallocUsed"); double value1 = 0.0; double value2 = 0.0; QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_1)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); value1 = m.total; break; }else if (lineList.at(i).contains(strTitle_2)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); value2 = m.total; break; } } file.close(); fValue = value1 - value2; } } break; case 30://虚内存占用 { QString strTitle_("VmallocUsed"); QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); fValue = m.total; break; } } file.close(); } } break; case 32://文件页总数 break; case 34://文件页空闲 break; case 36://文件页占用 break; case 38://内存占用率 break; case 40://当前程序占用空间 { uid_t current_pid = getpid(); //获取进程ID QString strTitle_("VmallocUsed"); QString filename = QString("/proc/%1/stat").arg(QString::number(current_pid)); qDebug()<<"current pid="<<current_pid; QFile file(filename); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); fValue = m.total; break; } } file.close(); } } break; case 42://内存使用峰值 { QString strValue; if (pTempTagI->GetPointCollection()) { strValue = pTempTagI->GetProjectValue()->GetVarValue().toString(); } int m_nLastValue = strValue.toInt(); QString strTitle_1("MemTotal"); QString strTitle_2("MemFree"); double value1 = 0.0; double value2 = 0.0; QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_1)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); value1 = m.total; break; }else if (lineList.at(i).contains(strTitle_2)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); value2 = m.total; break; } } file.close(); if (m_nLastValue < value1-value2) { fValue = value1 - value2; } } } break; case 44://占用的虚拟内存大小 { QString strTitle_("VmallocUsed"); QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); fValue = m.total; break; } } file.close(); } } break; case 46://占用的虚拟内存峰值 { QString strValue; if (pTempTagI->GetPointCollection()) { strValue = pTempTagI->GetProjectValue()->GetVarValue().toString(); } int m_nLastValue = strValue.toInt(); QString strTitle_("VmallocUsed"); QFile file(PROC_MEMINFO); MEM_OCCUPY m; if (file.open(QIODevice::ReadOnly| QIODevice::Text)) { QTextStream in(&file); QString data = in.readAll(); QStringList lineList = data.split('\n'); for (int i = 0; i < lineList.count(); ++i) { if (lineList.at(i).contains(strTitle_)) { sscanf (lineList.at(i).toStdString().data(), "%s %lu %s", m.name, &m.total, m.name2); if (m_nLastValue < (int)m.total) { fValue = m.total; } break; } } file.close(); } } break; default: Q_ASSERT(false);//("IO变量定义无效:未定义的随机变量取值方式 funcno=100 param1=%d\n",nType); break; } QVariant Value(fValue); // qDebug()<<"Value_.setValue(fValue);"<<Value; pTempTagI->SetValue(pPoint_,Value,Value); } }
void CProtocolDemo::GetTimeInfo(CPointDemoBase *pPoint_) { // CPU_OCCUPY cpu_stat1; // CPU_OCCUPY cpu_stat2; // MEM_OCCUPY mem_stat; // int cpu; //获取内存 // CLinuxInfo::get_memoccupy ((MEM_OCCUPY *)&mem_stat); //第一次获取cpu使用情况 // CLinuxInfo::get_cpuoccupy((CPU_OCCUPY *)&cpu_stat1); // sleep(10); //第二次获取cpu使用情况 // CLinuxInfo::get_cpuoccupy((CPU_OCCUPY *)&cpu_stat2); //计算cpu使用率 // cpu = CLinuxInfo::cal_cpuoccupy ((CPU_OCCUPY *)&cpu_stat1, (CPU_OCCUPY *)&cpu_stat2); CTagI *pTempTagI = pPoint_->m_pTag; if (pTempTagI) { double fValue=0.0; switch(pPoint_->m_nFunction) { case 50://windows系统已经运行时间 // fValue= GetTickCount()/1000.0f; { struct sysinfo info; time_t cur_time = 0; // time_t boot_time = 0; // struct tm *ptm = NULL; if (sysinfo(&info)) { // PrintRunInfo("get systime from boottime to now"); return ; } qDebug()<<info.uptime; time(&cur_time); // if (cur_time > info.uptime) { // boot_time = cur_time - info.uptime; // } // else { // boot_time = info.uptime - cur_time; // } fValue = info.uptime; } break; case 52://程序启动时间 秒 // FileTimeToSystemTime(&timeCreate,&systemTime); // fValue= CTime(systemTime).GetTime(); { fValue = pPoint_->m_nStartRunTime; } break; case 54://程序运行时间 秒 // fValue= clock()/(float)CLOCKS_PER_SEC; fValue = GetSysAbsTime() - pPoint_->m_nStartRunTime; break; case 56://内核运行时间 // FileTimeToSystemTime(&timeKernel,&systemTime); // fValue= systemTime.wHour*3600+systemTime.wMinute*60+systemTime.wSecond+systemTime.wMilliseconds/1000.0; { } break; case 58://用户运行时间 // FileTimeToSystemTime(&timeUser,&systemTime); // fValue= systemTime.wHour*3600+systemTime.wMinute*60+systemTime.wSecond+systemTime.wMilliseconds/1000.0; fValue = clock(); break; default: Q_ASSERT(false);//"IO变量定义无效:未定义的时间变量取值方式 funcno=4 param1=%d\n",nType); break; } QVariant value(fValue); pTempTagI->SetValue(pPoint_,value,value); } // return fValue; }