📄 ysdataprocess.cpp
字号:
//处理结构 if(!pAct||size==0) return false;int count=size/sizeof(st_faultact);if(count<=0) return false; st_faultact pst[count];memset(&pst,0x00,sizeof(st_faultact)*count);memcpy(&pst,pAct,size); CDataBase db;bool bNew=0;int ret=0;QString SqlStr="";QString tmpStr="";QString serr=""; bool bc=isConnectDatabase(hdbc,bNew); if(!bc) { //数据库连接失败,将结构存入文件 PushEventToFile((void*)&pst,DATA_FAULTACT,sizeof(st_faultact)*count);return 0; } if(PRecordIndex==""||PRecordIndex.isEmpty()) return false; for(int i=0;i<count;i++) { tmpStr.sprintf("Update EVENT_RPT set RETTIME=%d,FUN=%d,INF=%d,DPI=%d,NOF=%d,FAN=%d,\ PFUNCODE=%d,PINFCODE=%d,\"EGROUP\"=%d,ENTRY=%d \ Where RecordIndex='%s';", pst[i].m_ActTime,pst[i].m_FunCode,pst[i].m_InfCode,pst[i].m_Dpi,pst[i].m_FaultNum,0, pst[i].m_PFunCode,pst[i].m_PInfCode,pst[i].m_Group,pst[i].m_Entry,PRecordIndex.data()); SqlStr+=tmpStr; } ret=db.SqlExec(hdbc,SqlStr); if(ret<0) { WriteSysLog(hdbc,2,"写故障报告元件出错!"); } if(bNew) db.DisConnectDataBase(hdbc); return !(ret<0);}/////////////////////////////////////插入一条波形记录到波形记录表/////////////////////////bool BCDataProcess::InsertFaultWave(long &hdbc,st_fault &st,QString &RecordIndex){ CDataBase db;bool bNew=0;int ret=0;QString SqlStr="";QString serr=""; bool bc=isConnectDatabase(hdbc,bNew); if(!bc) { return false; } QString CurrentDate=getTimeStr(); RecordIndex=g_PublicClass.GetNewRecordIndex(QString("%1").arg(st.m_MachineNo)); SqlStr.sprintf("Insert Into WAVE_RPT(\ RECORDINDEX,\ STATIONNO,DEVICENO,CPUNO,\ ACTTIME,NOTE,\ FUN,INF,DPI,NOF,FAN,PathName,Name,FileName) \ Values('%s', %d,%d,%d ,'%s','%s', %d,%d,%d,%d,%d,'%s','%s','%s');", RecordIndex.data(), st.m_StationNo,st.m_MachineNo,st.m_CpuNo, st.m_ActionTime,"", 0,0,0,0,0, GetCimPathName(hdbc,st.m_StationNo,st.m_MachineNo).data(), GetCimName(hdbc,st.m_StationNo,st.m_MachineNo).data(),_GetFileName(st.m_WaveFile).data()); ret=db.SqlExec(hdbc,SqlStr); if(ret<0) { WriteSysLog(hdbc,0,"写故障波形时出错!"); } if(bNew) db.DisConnectDataBase(hdbc); return !(ret<0);}/////////////////////////////////////插入一个波形文件/////////////////////////bool BCDataProcess::InsertFaultWaveFiles(long &hdbc,QString WRecordIndex,int MachineNo,QString FileName,QString FileType){ QString SqlStr=""; QString tmpFile="",RecordIndex=""; tmpFile=FileName+"."+FileType; CDataBase db;bool bNew=0; bool bc=isConnectDatabase(hdbc,bNew); if(!bc) { printf(" bc=isConnectDatabase() ========== false, Leave\n"); return false; } if(_isFileExists(tmpFile)) { RecordIndex=g_PublicClass.GetNewRecordIndex(QString("%1").arg(MachineNo)); SqlStr.sprintf("Insert Into WAVE_DETAIL_RPT(\ RECORDINDEX,PRECORDINDEX,ESIZE,\"type\",FLAG,\ SAME,EFILE) \ Values('%s','%s',%u,'%s',%d,%d,?);", RecordIndex.data(),WRecordIndex.data(),(unsigned int)_getFileLength(tmpFile),FileType.upper().data(),0, 0); db.InsertWithLargeObject(hdbc,SqlStr,tmpFile); if(bNew) db.DisConnectDataBase(hdbc); return true; } else { printf("_isFileExists(%s)--------------------------leave\n", tmpFile.data()); } if(bNew) db.DisConnectDataBase(hdbc); return false;}/////////////////////////////////////插入一条波形记录到波形记录表/////////////////////////bool BCDataProcess::InsertFaultDetail(long &hdbc,st_detail &st,QString &RecordIndex){ CDataBase db;bool bNew=0;int ret=0;QString SqlStr="";QString serr=""; bool bc=isConnectDatabase(hdbc,bNew); if(!bc) {return 0;} QString CurrentDate=getTimeStr(); RecordIndex=g_PublicClass.GetNewRecordIndex(QString("%1").arg(st.m_MachineNo)); SqlStr.sprintf("Insert Into DETAIL_RPT(\ RECORDINDEX,\ STATIONNO,DEVICENO,CPUNO,\ ACTTIME,TABLENAME,PRECORDINDEX,\ FUN,INF,NOF,FAN,FLAG,EVALUE) \ Values('%s', %d,%d,%d ,'%s','%s','%s', %d,%d,%d,%d,'%s','%s');", RecordIndex.data(), st.m_StationNo,st.m_MachineNo,st.m_CpuNo, st.m_ActionTime,st.m_TableName,st.m_PRecordIndex, st.m_Fun,st.m_Inf,st.m_Nof,st.m_Fan,st.m_Flag,st.m_EValue); ret=db.SqlExec(hdbc,SqlStr); if(ret<0) { WriteSysLog(hdbc,0,"写故障波形时出错!"); } if(bNew) db.DisConnectDataBase(hdbc); return !(ret<0);}/////////////////////////////////////////输出波形文件///////////////////////////////////////////////////bool BCDataProcess::ExportWaveFile(long &hdbc,QString RecordIndex,QString FileName){ if(RecordIndex==""||RecordIndex.isEmpty()) return false; if(FileName==""||FileName.isEmpty()) return false; CDataBase db;bool bNew=0;int ret=0;QString SqlStr="";long result=0;QString serr=""; bool bc=isConnectDatabase(hdbc,bNew);if(!bc) return 0; SqlStr.sprintf("Select TYPE,EFile from WAVE_DETAIL_RPT where PRECORDINDEX='%s';",RecordIndex.data()); //Liang 2006-11-01 //printf("Database:%u\n",hdbc); ret=db.SqlSelect(hdbc,SqlStr,result); if(ret<0) { db.ClearResult(result);if(bNew) db.DisConnectDataBase(hdbc);return false; } int rowcount=db.GetRecordCount(result); QString tmpFile; for(int i=0;i<rowcount;i++) { QString FileType=db.GetFieldValue(result,i,0); Oid oid=db.GetFieldValue(result,i,1).toInt(); tmpFile=_getFileNameNoExtend(FileName)+"."+FileType.lower(); db.exportfile(hdbc,oid,(char*)tmpFile.data()); serr.sprintf("输出文件成功:%s!",tmpFile.data()); serr.sprintf("输出文件成功:%s!",tmpFile.data()); WriteSysLog(hdbc,0,serr); } db.ClearResult(result); if(bNew) db.DisConnectDataBase(hdbc); return true;}bool BCDataProcess::AddChildDevices(CDeviceNode* pNode,DeviceInfoListType &devicelist,unsigned int threadid){ st_deviceinfo dst; DeviceInfoListType::iterator it; pNode->m_ChildDevList.clear(); for(int i=0;i<(int)devicelist.count();i++) { it=devicelist.at(i); memset(&dst,0x00,sizeof(st_deviceinfo));memcpy(&dst,&(*it),sizeof(st_deviceinfo)); dst.m_ThreadId=threadid; pNode->m_ChildDevList.append(dst); } return true;}//Liang 2206/6/27void BCDataProcess::SetDeviceRun(int number , bool run) //true=run , false=stop{ bool bNew = 0 ; long hdbc = -1 ; bool bc = isConnectDatabase(hdbc , bNew) ; if (!bc) { return ; }//if QString SqlStr ; SqlStr.sprintf("update OPERATEEQUIPMENT set COMMSTATE=%d where CODE=%d ;" , run?0:1 , number) ; CDataBase db ; db.SqlExec(hdbc , SqlStr) ; if (!run) { SqlStr.sprintf("delete from DEVICESTARTINFO where MACHINENO=%d ;" , number) ; db.SqlExec(hdbc , SqlStr) ; }//if if (bNew) { db.DisConnectDataBase(hdbc) ; }//if}//SetDeviceRun//Liang 2206/6/28void BCDataProcess::SendCommStateToMain(int number , bool run){ st_event Rpt_Event ; memset(&Rpt_Event , 0x00 , sizeof Rpt_Event) ; Rpt_Event.m_StationNo = g_PublicClass.m_StationNo ; Rpt_Event.m_MachineNo = number ; Rpt_Event.m_CpuNo = 0 ; Rpt_Event.m_Entry = 255 ; static int iFaultNo = 0 ; Rpt_Event.m_FaultNum = ++iFaultNo ; QString temp = getTimeStr(); strcpy(Rpt_Event.m_ActionTime , temp.data()) ; QString RecordIndex ; long hdbc = -1 ; if (!InsertSelfChk(hdbc , Rpt_Event , RecordIndex)) { return ; }//if st_eventact EventAct ; memset(&EventAct , 0x00 , sizeof EventAct) ; EventAct.m_MachineNo = number ; EventAct.m_CpuNo = 0 ; EventAct.m_FaultNum = iFaultNo ; EventAct.m_ActNo = 0 ; EventAct.m_ActTime = 0 ; EventAct.m_Dpi = int(run) ; temp.sprintf("与设备号为%d的设备" , number) ; if (run) { temp += "通信恢复正常" ; }//if else { temp += "通信发生异常" ; }//else ++EventAct.m_Dpi ; strcpy(EventAct.m_ActElement , temp.data()) ; EventAct.m_FunCode = 255 ; EventAct.m_InfCode = 255 ; EventAct.m_PFunCode = 255 ; EventAct.m_PInfCode = 255 ; EventAct.m_Group = 255 ; EventAct.m_Entry = 255 ; if (!InsertSelfChkAct(hdbc , &EventAct , sizeof EventAct , RecordIndex)) { return ; }//if UpdateCOMMSTATE(number , !run) ; NotifyMsg(number , DATA_EVENT , RecordIndex , "ALERT_RPT") ;}//SendCommStateToMainvoid BCDataProcess::NotifyMsg(int number , uint dataype , const QString &RecordIndex , const QString &TableName){ CDataNode *pNode = new CDataNode() ; memset(&pNode->m_nodeinfo , 0X00 , sizeof pNode->m_nodeinfo) ; pNode->m_nodeinfo.m_MachineNo = number ; pNode->m_nodeinfo.requestid = g_PublicClass.m_SelfPid ; pNode->m_nodeinfo.m_iResult = 1 ; pNode->m_nodeinfo.m_DataType = dataype ; strcpy(pNode->m_nodeinfo.m_RecordFlag , RecordIndex.data()) ; strcpy(pNode->m_nodeinfo.m_TableName , TableName.data()) ; long hdbc = -1 ; /*int pid = GetModulePid(hdbc , "98") ; if (pid > 0) { if (!CheckModuleRunning(hdbc , pid)) { pid = 0 ; }//if }//if if (pid > 0) //上送主模块 { pNode->m_nodeinfo.mtype = pid ; //Liang 2006-03-12 g_PublicClass.m_NodeManage.AddSendDataNode(pNode) ; }//if else *///上送操作模块 { //PreMakeNoteList(hdbc , pNode->m_nodeinfo) ; //Liang 2006-03-23 by yys NoticOperModule(hdbc , pNode->m_nodeinfo) ; MakeToSendState(hdbc , pNode->m_nodeinfo) ; //Liang 2006-03-12 delete pNode ; }//else}//NotifyMsvoid BCDataProcess::UpdateCOMMSTATE(int number , bool exception){ bool bNew = 0 ; long hdbc = -1 ; bool bc = isConnectDatabase(hdbc , bNew) ; if (!bc){return ;}//if QString SqlStr ; SqlStr.sprintf("update OPERATEEQUIPMENT set COMMSTATE=%d where CODE=%d ;" , exception?1:0 , number) ; CDataBase db ; db.SqlExec(hdbc , SqlStr) ; if (bNew){db.DisConnectDataBase(hdbc) ;}//if}//UpdateCOMMSTATE/////////////////////////////////从故障序号得到记录号//////////////////////////////Tablename=表名,MachineNo=设备号,FaultNum故障序号QString BCDataProcess::GetRecordIndexByEventNum(long &hdbc,QString Tablename,int MachineNo,int FaultNum){ //将相同序号的事件按时间倒序排列,最新的就是需要找的记录 QString SqlStr="";int ret=0;long result=0;QString RecordIndex="";bool bNew=0;CDataBase db; bool bc=isConnectDatabase(hdbc,bNew);if(!bc) return ""; SqlStr.sprintf("Select RecordIndex from %s where MachineNo=%d and FaultNum=%d Order by ActionTime desc;",\ Tablename.data(),MachineNo,FaultNum); ret=db.SqlSelect(hdbc,SqlStr,result); if(ret<0) { db.ClearResult(result);if(bNew) db.DisConnectDataBase(hdbc);return ""; } int rowcount=db.GetRecordCount(result); if(rowcount>0) //存在记录 { RecordIndex=db.GetFieldValue(result,0,0); db.ClearResult(result);if(bNew) db.DisConnectDataBase(hdbc); return RecordIndex; } db.ClearResult(result);if(bNew) db.DisConnectDataBase(hdbc); return "";}//////////////////////////////当数据库暂时连接失败时,将事件放入文件中//////////////////////////数据类型,大小,内容bool BCDataProcess::PushEventToFile(void* event,int DataType,int size){ CLMutexLocker lock(&m_DataLocker); ofstream wdata(g_PublicClass.m_PushFile.data(), ios::app | ios::binary | ios::out) ; char* dt=new char[sizeof(int)];memset(dt,0x00,sizeof(int));memcpy(dt,&DataType,sizeof(int)); wdata.write(dt,sizeof(int)); //写入数据类型 memset(dt,0x00,sizeof(int));memcpy(dt,&size,sizeof(int)); wdata.write(dt,sizeof(int)); //写入大小 char* save=new char[size];memset(save,0x00,size);memcpy(save,event,size); wdata.write((char*)save,size); //结构数据 wdata.close(); delete [] dt;delete [] save; return true;}/////////////////////////////读出事件类型,大小及内容///////////////////////////////////bool BCDataProcess::PopEventToFile(char* &event,int &DataType,int &size){ CLMutexLocker lock(&m_DataLocker); ifstream rdata(g_PublicClass.m_PushFile.data(), ios::binary | ios::in) ; char* dt=new char[sizeof(int)];memset(dt,0x00,sizeof(int)); rdata.read(dt,sizeof(int)); //读出数据类型 memcpy(&DataType,dt,sizeof(int)); memset(dt,0x00,sizeof(int)); rdata.read(dt,sizeof(int)); //读出结构大小 memcpy(&size,dt,sizeof(int)); if(size>0) { event=new char[size];memset(event,0x00,size);rdata.read(event,size); } delete [] dt; long currPos=rdata.tellg(); rdata.close(); CopyFile(currPos); return true;}////////////////////////从起始点fromsize复制文件//////////////////////////bool BCDataProcess::CopyFile(long fromsize){fromsize=fromsize; QString tmpfile=g_PublicClass.m_PushFile+"1"; /* QDir d; d.remove(tmpfile);d.rename(g_PublicClass.m_PushFile,tmpfile);d.remove(g_PublicClass.m_PushFile); ifstream rdata(tmpfile.data(), ios::binary | ios::in) ; ofstream wdata(g_PublicClass.m_PushFile.data(), ios::app | ios::binary | ios::out) ; char buff[1024];memset(buff,0x00,1024);int len=1024; rdata.seekg(0,ios::end);long sylen=rdata.tellg();rdata.seekg(fromsize);sylen=sylen - fromsize; while(sylen>0) { memset(buff,0x00,1024); if(sylen>len) { rdata.read(buff,len);sylen-=len;wdata.write(buff,len); } else { rdata.read(buff,sylen);wdata.write(buff,sylen); break; } } wdata.close();rdata.close();d.remove(tmpfile);*/ return true;}///////////////////////////////外壳应用函数//////////////////////////////////////////////////////////////////////////////得到全局消息队列号/////////////////////////////////bool BCDataProcess::GetPublicInfo(long &hdbc){ CDataBase db;bool bNew=false;int ret=0;long result=0;QString SqlStr=""; bool bc=isConnectDatabase(hdbc,bNew);if(!bc) return 0; SqlStr.sprintf("select PID,SENDQUEUEID,RECVQUEUEID,SENDQUEUEIDB,RECVQUEUEIDB from PublicInfo;"); ret=db.SqlSelect(hdbc,SqlStr,result);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -