#include "AbstractSpectrumDataMessage.h" #include AbstractSpectrumDataMessage::AbstractSpectrumDataMessage() { msg_items.append( "MSG_TYPE" ); // [0] msg_items.append( "MSG_ID" ); // [1] msg_items.append( "REF_ID" ); // [2] msg_items.append( "PROD_ID" ); // [3] msg_items.append( "DATA_TYPE" ); // [4] data_types.append( "SAMPLEPHD" ); // [0] data_types.append( "SPHDF" ); // [1] data_types.append( "SPHDP" ); // [2] data_types.append( "GASBKPHD" ); // [3] data_types.append( "BLANKPHD" ); // [4] data_types.append( "DETBKPHD" ); // [5] data_types.append( "QCPHD" ); // [6] data_types.append( "CALIBPHD" ); // [7] data_types.append( "MET" ); // [8] data_types.append( "RMSSOH" ); // [9] data_types.append( "ALERT_FLOW" ); // [10] data_types.append( "ALERT_SYSTEM" ); // [11] data_types.append( "ALERT_TEMP" ); // [12] data_types.append( "ALERT_UPS" ); // [13] msg.verify_srid = false; } AbstractSpectrumDataMessage::~AbstractSpectrumDataMessage() { } QString AbstractSpectrumDataMessage::GetSpectrumDataTypeFromFile(QString data_file) { QString ret_string; QFile file(data_file); if ( file.open(QIODevice::ReadOnly | QIODevice::Text) ) { QTextStream content(&file); if (!content.atEnd()) { if ( AnalyseMessgeInfo(content, true) ) { ret_string = msg.data_type; if ( QLatin1String("SPHDP")==ret_string || QLatin1String("SPHDF")==ret_string ) { ret_string = QLatin1String("SAMPLEPHD"); msg.data_type = ret_string; } } } } file.close(); return ret_string; } QString AbstractSpectrumDataMessage::GetSpectrumDataTypeFromMessage(QString data_msg) { QString ret_string; QTextStream content(&data_msg, QIODevice::ReadOnly); if (!content.atEnd()) { if ( AnalyseMessgeInfo(content, true) ) { ret_string = msg.msg_type; if ( QLatin1String("SPHDP")==ret_string && QLatin1String("SPHDF")==ret_string ) { ret_string = QLatin1String("SAMPLEPHD"); } } } return ret_string; } bool AbstractSpectrumDataMessage::AnalyseFile(QString file_name) { bool bRet = true; QFile file(file_name); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { return bRet &= false; } QTextStream content(&file); if (content.atEnd()) { file.close(); return bRet &= false; } bRet &= AnalyseMessgeInfo(content); /* if (bRet) { bRet &= AnalyseMessgeBody(content); } */ file.close(); /* if ( bRet && msg.verify_srid ) { ChangeOriginalFile(file_name); } */ return bRet; } bool AbstractSpectrumDataMessage::AnalyseMsg(QString &msg_string) { bool bRet = true; QTextStream content(&msg_string, QIODevice::ReadOnly); if (content.atEnd()) { return bRet &= false; } bRet &= AnalyseMessgeInfo(content); /* if (bRet) { bRet &= AnalyseMessgeBody(content); } */ return bRet; } const MessageInfo &AbstractSpectrumDataMessage::GetMessageInfo() { return msg; } void AbstractSpectrumDataMessage::ClearData() { msg.data_type.clear(); msg.msg_id.clear(); msg.msg_type.clear(); msg.verify_srid = false; } bool AbstractSpectrumDataMessage::AnalyseMessgeInfo(QTextStream &content, bool only_analyse_msg) { bool bRet = true; QString item_text = content.readLine(); if ( 0!=item_text.left(5).compare(QLatin1String("BEGIN")) && 0!=item_text.mid(6).compare(QLatin1String("IMS2.0")) ) { bRet &= false; //消息头的协议标示不对,消息无效 return bRet; } QString line = content.readLine(); while (bRet) { QTextStream line_stream(&line); line_stream >> item_text; if ( 0==item_text.compare(msg_items.at(0)) && bRet ) //MSG_TYPE { line_stream >> msg.msg_type; if ( msg.msg_type!=(QLatin1String("DATA")) ) { bRet &= false; //消息类型不是指定的“DATA”类型,消息过滤 break; } line = content.readLine(); } else if ( 0==item_text.compare(msg_items.at(1)) && bRet ) //MSG_ID { for (int i=0; !line_stream.atEnd(); ++i) { if ( 0==i ) { line_stream >> msg.msg_id; } else if (1==i) { line_stream >> msg.msg_src_code; } } line = content.readLine(); } else if ( 0==item_text.compare(msg_items.at(2)) && bRet ) //REF_ID { QString ref_id_after = line_stream.readLine(); QTextStream ref_id_after_stream(&ref_id_after); for (int i=0; !ref_id_after_stream.atEnd(); ++i) { QString temp; ref_id_after_stream >> temp; if (0==i) { msg.ref_id_str = temp; } else if (1==i) { msg.ref_src_code = temp; } else if (2==1) { msg.seq_num = temp; } else if (3==1) { msg.tot_num = temp; } } msg.verify_srid = true; line = content.readLine(); } else if ( 0==item_text.compare(msg_items.at(3)) && bRet ) //PROD_ID { line_stream >> msg.product_id >> msg.delivery_id; msg.verify_srid = true; line = content.readLine(); } else if ( 0==item_text.compare(msg_items.at(4)) && bRet ) //DATA_TYPE { line_stream >> msg.data_type; if ( data_types.contains(msg.data_type) && bRet ) { if ( !only_analyse_msg ) { bRet &= AnalyseMessgeBody(content); } return bRet; } else { line = content.readLine(); QTextStream data_line_stream(&line); data_line_stream >> item_text; while (!msg_items.contains(item_text) && !content.atEnd()) { line = content.readLine(); QTextStream line_stream(&line); line_stream >> item_text; } msg.verify_srid = true; } } else { //bRet = false; // bRet不置为false,表示针对消息头的有效检查持宽容态度,允许跟文件协议有能接受的出入 break; } if ( content.atEnd() || (content.status()!=QTextStream::Ok) ) { bRet = false; ClearData(); break; } } return bRet; } void AbstractSpectrumDataMessage::SetMsgDataType(QString data_type) { msg.data_type = data_type; } void AbstractSpectrumDataMessage::ChangeOriginalFile(QString file_name) { QFile file(file_name); if (file.open(QIODevice::ReadOnly | QIODevice::Text)) { QString temp_file_name = file_name + QLatin1String(".temp"); QFile temp_file(temp_file_name); if (!temp_file.open(QIODevice::WriteOnly | QIODevice::Text)) { QString error = file.errorString(); return; } QTextStream in(&file); QTextStream out(&temp_file); bool bInBody = true; QString line = in.readLine(); while ( !in.atEnd() && (in.status()==QTextStream::Ok) ) { QString item_text; QTextStream line_stream(&line); line_stream >> item_text; if ( 0==item_text.compare(msg_items.at(4)) && bInBody ) //DATA_TYPE { QString data_type; line_stream >> data_type; if ( data_types.contains(data_type) && bInBody ) { bInBody = false; } else { line = in.readLine(); QTextStream data_line_stream(&line); data_line_stream >> item_text; while (!msg_items.contains(item_text) && !in.atEnd()) { line = in.readLine(); QTextStream line_stream(&line); line_stream >> item_text; } } } else if ( 0==item_text.compare(msg_items.at(2)) && bInBody ) //REF_ID { line = in.readLine(); } else if ( 0==item_text.compare(msg_items.at(3)) && bInBody ) //PROD_ID { line = in.readLine(); } else { out << line << "\n"; line = in.readLine(); } } temp_file.close(); file.close(); QFile::remove(file_name); QFile::rename(temp_file_name, file_name); } }