#include "CatalogParser.h" #include #include #include "../Utils/logger.hpp" static const char* g_event_desc[] = { "ON", "OFF", "VLOST", "DEFECT", "ADD", "DEL", "UPDATE" }; bool CCatalogParser::Encode( std::string &message ) { std::ostringstream content; content<<"\r\n"; content<<"\r\n"; content<<"Catalog\r\n"; content<<""<< m_sn <<"\r\n"; content<<""<< m_deviceid <<"\r\n"; content<<"OK\r\n"; content<<""<< m_sum <<"\r\n"; content<<"\r\n"; for( auto it = m_devices.begin(); it != m_devices.end(); ++it ) { // 校验目录项的必选参数 const DeviceInfo &catalog = (*it); content<<"\r\n"; content<<""<\r\n"; content<<""<< g_event_desc[catalog.event] <<"\r\n"; if( catalog.event == EVENT_ADD || catalog.event == EVENT_UPDATE ) { content<<""<< catalog.name <<"\r\n"; content<<""<< catalog.manufacturer <<"\r\n"; content<<""<< catalog.model <<"\r\n"; content<<""<< catalog.owner <<"\r\n"; if( !catalog.civil.empty() ) { content<<""<\r\n"; } else { content<<""<\r\n"; } if( !catalog.block.empty() ) { content<<""<< catalog.block <<"\r\n"; } content<<"
"<< catalog.address <<"
\r\n"; content<<""<< catalog.parental <<"\r\n"; content<<""<< catalog.parentid <<"\r\n"; if( !catalog.safetyway.empty() ) { content<<""<< catalog.safetyway <<"\r\n"; } content<<""<< catalog.registerway <<"\r\n"; if( !catalog.certnum.empty() ) { content<<""<< catalog.certnum <<"\r\n"; } if( !catalog.certifiable.empty() ) { content<<""<< catalog.certifiable <<"\r\n"; } if( !catalog.errcode.empty() ) { content<<""<< catalog.errcode <<"\r\n"; } if( !catalog.endtime.empty() ) { content<<""<< catalog.endtime <<"\r\n"; } content<<""<< catalog.secrecy <<"\r\n"; if( !catalog.ip.empty() ) { content<<""<< catalog.ip <<"\r\n"; } if( !catalog.port.empty() ) { content<<""<< catalog.port <<"\r\n"; } if( !catalog.password.empty() ) { content<<""<< catalog.password <<"\r\n"; } content<<""<\r\n"; if( !catalog.longitude.empty() ) { content<<""<\r\n"; } if( !catalog.latitude.empty() ) { content<<""<\r\n"; } content<<"\r\n"; if( !catalog.ptz.empty() ) { content<<""<\r\n"; } if( !catalog.position.empty() ) { content<<""<\r\n"; } if( !catalog.room.empty() ) { content<<""<\r\n"; } if( !catalog.use.empty() ) { content<<""<\r\n"; } if( !catalog.supplylight.empty() ) { content<<""<\r\n"; } if( !catalog.direction.empty() ) { content<<""<\r\n"; } if( !catalog.resolution.empty() ) { content<<""<\r\n"; } if( !catalog.businessgroup.empty() ) { content<<""<\r\n"; } content<<"\r\n"; } content<<"
\r\n"; } content<<"
\r\n"; content<<"
\r\n\r\n"; message = content.str(); return true; } std::vector< DeviceInfo > CCatalogParser::Decode( const std::vector< tinyxml2::XMLNode* > &nodes) { std::vector< DeviceInfo > cat_list; // 必须参数校验 tinyxml2::XMLNode *pSumNum = NULL; size_t size = nodes.size(); for( size_t i = 0; i < size; ++i ) { tinyxml2::XMLNode *pNode = nodes[i]; if( pNode == NULL ) { LOG_DEBUG( "参数错误" ); continue; } const char* type = pNode->Value(); if( type == NULL ) { LOG_DEBUG( "参数名字为空" ); continue; } if( CGBMessage::CompareNoCase( type, "SumNum" ) ) { pSumNum = pNode; } } // 必选参数必须填 if( pSumNum == NULL ) { LOG_ERROR( "参数SumNum没有被设置" ); return cat_list; } // 参数解析 std::list< tinyxml2::XMLNode* > items; for( size_t i = 0; i < size; ++i ) { tinyxml2::XMLNode *pNode = nodes[i]; if( pNode == NULL ) { LOG_DEBUG( "参数错误" ); continue; } const char* type = pNode->Value(); if( type == NULL ) { LOG_DEBUG( "参数名字为空" ); continue; } if( CGBMessage::CompareNoCase( type, "Status" ) ) { tinyxml2::XMLNode *pChild = pNode->FirstChild(); if( pChild == NULL ) { LOG_DEBUG( "参数值没有设置" ); continue; } const char *value = pChild->Value(); if( value == NULL ) { LOG_DEBUG( "参数值为空" ); continue; } else { m_status = value; } } else if( CGBMessage::CompareNoCase( type, "SumNum" ) ) { tinyxml2::XMLNode *pChild = pNode->FirstChild(); if( pChild == NULL ) { LOG_DEBUG( "参数值没有设置" ); continue; } const char *value = pChild->Value(); if( value == NULL ) { LOG_DEBUG( "参数值为空" ); continue; } else { m_sum = atoi( value ); } } else if( CGBMessage::CompareNoCase( type, "DeviceList" ) ) { for(tinyxml2::XMLNode *pItem = pNode->FirstChild(); pItem != NULL; pItem = pItem->NextSibling() ) { const char *type = pItem->Value(); if( type == NULL ) { LOG_DEBUG( "参数名字为空" ); continue; } if( CGBMessage::CompareNoCase( type, "Item" ) ) { items.push_back( pItem ); } else { LOG_DEBUG( "国标未定义的参数:{}" , type ); continue; } } } else { LOG_DEBUG( "国标未定义的参数:{}" , type ); continue; } } // 目录项 std::list< tinyxml2::XMLNode* >::iterator it = items.begin(); std::list< tinyxml2::XMLNode* >::iterator end = items.end(); for( /*it*/; it != end; ++it ) { DeviceInfo catalog; for(tinyxml2::XMLNode *pItem = (*it)->FirstChild(); pItem != NULL; pItem = pItem->NextSibling() ) { const char *type = pItem->Value(); if( type == NULL ) { LOG_DEBUG( "参数名字为空" ); continue; } tinyxml2::XMLNode *pValue = pItem->FirstChild(); if( pValue == NULL ) { LOG_DEBUG( "参数值没有设置" ); continue; } const char *value = pValue->Value(); if( value == NULL ) { LOG_DEBUG( "参数值为空" ); continue; } if( CGBMessage::CompareNoCase( type, "DeviceID" ) ) { catalog.id = value; } else if( CGBMessage::CompareNoCase( type, "Event" ) ) { if( CGBMessage::CompareNoCase( value, "ON" ) ) { catalog.event = EVENT_ON; } else if( CGBMessage::CompareNoCase( value, "OFF" ) ) { catalog.event = EVENT_OFF; } else if( CGBMessage::CompareNoCase( value, "VLOST" ) ) { catalog.event = EVENT_VLOST; } else if( CGBMessage::CompareNoCase( value, "DEFECT" ) ) { catalog.event = EVENT_DEFECT; } else if( CGBMessage::CompareNoCase( value, "ADD" ) ) { catalog.event = EVENT_ADD; } else if( CGBMessage::CompareNoCase( value, "DEL" ) ) { catalog.event = EVENT_DEL; } else if( CGBMessage::CompareNoCase( value, "UPDATE" ) ) { catalog.event = EVENT_UPDATE; } else { LOG_ERROR( "Event参数值\'{}\'无效", value ); return cat_list; } } else if( CGBMessage::CompareNoCase( type, "Name" ) ) { catalog.name = value; } else if( CGBMessage::CompareNoCase( type, "Manufacturer" ) ) { catalog.manufacturer = value; } else if( CGBMessage::CompareNoCase( type, "Model" ) ) { catalog.model = value; } else if( CGBMessage::CompareNoCase( type, "Owner" ) ) { catalog.owner = value; } else if( CGBMessage::CompareNoCase( type, "CivilCode" ) ) { catalog.civil = value; } else if( CGBMessage::CompareNoCase( type, "Block" ) ) { catalog.block = value; } else if( CGBMessage::CompareNoCase( type, "Address" ) ) { catalog.address = value; } else if( CGBMessage::CompareNoCase( type, "Parental" ) ) { catalog.parental = value; } else if( CGBMessage::CompareNoCase( type, "ParentID" ) ) { catalog.parentid = value; } else if( CGBMessage::CompareNoCase( type, "SafetyWay" ) ) { catalog.safetyway = value; } else if( CGBMessage::CompareNoCase( type, "RegisterWay" ) ) { catalog.registerway = value; } else if( CGBMessage::CompareNoCase( type, "CertNum" ) ) { catalog.certnum = value; } else if( CGBMessage::CompareNoCase( type, "Certifiable" ) ) { catalog.certifiable = value; } else if( CGBMessage::CompareNoCase( type, "ErrCode" ) ) { catalog.errcode = value; } else if( CGBMessage::CompareNoCase( type, "EndTime" ) ) { catalog.endtime = value; } else if( CGBMessage::CompareNoCase( type, "Secrecy" ) ) { catalog.secrecy = value; } else if( CGBMessage::CompareNoCase( type, "IPAddress" ) ) { catalog.ip = value; } else if( CGBMessage::CompareNoCase( type, "Port" ) ) { catalog.port = value; } else if( CGBMessage::CompareNoCase( type, "Password" ) ) { catalog.password = value; } else if( CGBMessage::CompareNoCase( type, "Status" ) ) { catalog.status = value; } else if( CGBMessage::CompareNoCase( type, "Longitude" ) ) { catalog.longitude = value; } else if( CGBMessage::CompareNoCase( type, "Latitude" ) ) { catalog.latitude = value; } else if( CGBMessage::CompareNoCase( type, "PTZType" ) ) { catalog.ptz = value; } else if( CGBMessage::CompareNoCase( type, "PositionType" ) ) { catalog.position = value; } else if( CGBMessage::CompareNoCase( type, "RoomType" ) ) { catalog.room = value; } else if( CGBMessage::CompareNoCase( type, "UseType" ) ) { catalog.use = value; } else if( CGBMessage::CompareNoCase( type, "SupplyLightType" ) ) { catalog.supplylight = value; } else if( CGBMessage::CompareNoCase( type, "DirectionType" ) ) { catalog.direction = value; } else if( CGBMessage::CompareNoCase( type, "Resolution" ) ) { catalog.resolution = value; } else if( CGBMessage::CompareNoCase( type, "BusinessGroupID" ) ) { catalog.businessgroup = value; } else if( CGBMessage::CompareNoCase( type, "Info" ) ) { for(tinyxml2::XMLNode *pInfo = pItem->FirstChild(); pInfo != NULL; pInfo = pInfo->NextSibling() ) { pValue = pInfo->FirstChild(); if( pValue == NULL ) { LOG_DEBUG( "参数值没有设置" ); continue; } const char *type = pInfo->Value(); if( type == NULL ) { LOG_DEBUG( "参数名字为空" ); continue; } const char *value = pValue->Value(); if( value == NULL ) { LOG_DEBUG( "参数值为空" ); continue; } if( CGBMessage::CompareNoCase( type, "PTZType" ) ) { catalog.ptz = value; } else if( CGBMessage::CompareNoCase( type, "PositionType" ) ) { catalog.position = value; } else if( CGBMessage::CompareNoCase( type, "RoomType" ) ) { catalog.room = value; } else if( CGBMessage::CompareNoCase( type, "UseType" ) ) { catalog.use = value; } else if( CGBMessage::CompareNoCase( type, "SupplyLightType" ) ) { catalog.supplylight = value; } else if( CGBMessage::CompareNoCase( type, "DirectionType" ) ) { catalog.direction = value; } else if( CGBMessage::CompareNoCase( type, "Resolution" ) ) { catalog.resolution = value; } else if( CGBMessage::CompareNoCase( type, "BusinessGroupID" ) ) { catalog.businessgroup = value; } else { LOG_DEBUG( "国标未定义的参数:{}" , type ); } } } else { LOG_DEBUG( "国标未定义的参数:{}" , type ); } } // add to devices m_devices.push_back( catalog ); } return m_devices; } std::vector< DeviceInfo > CCatalogParser::DecodeCatlog(const char* body) { std::vector< DeviceInfo > cat_list; if (body == NULL) { return cat_list; } tinyxml2::XMLDocument doc; doc.Parse(body); tinyxml2::XMLElement* pRoot = doc.RootElement(); if (pRoot == NULL) { return cat_list; } tinyxml2::XMLNode* pCmd = 0; tinyxml2::XMLNode* pSN = 0; tinyxml2::XMLNode* pDeviceID = 0; std::vector< tinyxml2::XMLNode* > nodes; for (tinyxml2::XMLNode* pNode = pRoot->FirstChild(); pNode != 0; pNode = pNode->NextSibling()) { std::string value = pNode->Value(); if (value == "CmdType") { pCmd = pNode->FirstChild(); } else if (value == "SN") { pSN = pNode->FirstChild(); } else if (value == "DeviceID") { pDeviceID = pNode->FirstChild(); } else { nodes.push_back(pNode); } } if (pCmd == NULL || pSN == NULL || pDeviceID == NULL) { return cat_list; } std::string sn = pSN->Value(); if (sn.empty()) { return cat_list; } std::string deviceid = pDeviceID->Value(); if (deviceid.empty()) { return cat_list; } std::string msgType = pRoot->Value(); std::string cmdType = pCmd->Value(); if (msgType == "Response" && cmdType == "Catalog") { cat_list = Decode(nodes); if (cat_list.empty()) { LOG_WARN("消息体未解析出设备!"); } else { // 设置 for (size_t i = 0; i < cat_list.size(); i++) { cat_list[i].parentid = deviceid; } } } return cat_list; } string CCatalogParser::getItemValue(tinyxml2::XMLNode *pItem) { tinyxml2::XMLNode *pValue = pItem->FirstChild(); if( pValue == NULL ) { LOG_DEBUG( "参数值没有设置" ); return ""; } const char *value = pValue->Value(); if( value == NULL ) { LOG_DEBUG( "参数值为空" ); return ""; } return value; } ServerInfo CCatalogParser::DecodeServerConfig(const char* body) { ServerInfo info; if (body == NULL) { return info; } tinyxml2::XMLDocument doc; doc.Parse(body); tinyxml2::XMLElement* pRoot = doc.RootElement(); if (pRoot == NULL) { return info; } for (tinyxml2::XMLNode* pNode = pRoot->FirstChild(); pNode != 0; pNode = pNode->NextSibling()) { std::string type = pNode->Value(); if (CGBMessage::CompareNoCase( type, "Ua" )) { info.setUa(getItemValue(pNode)); } else if (CGBMessage::CompareNoCase( type, "Nonce" )) { info.setNonce(getItemValue(pNode)); } else if (CGBMessage::CompareNoCase( type, "Ip" )) { info.setIp(getItemValue(pNode)); } else if (CGBMessage::CompareNoCase( type, "Port" )) { string str = getItemValue(pNode); int i = atoi(str.c_str()); info.setPort(i); } else if (CGBMessage::CompareNoCase( type, "SipId" )) { info.setSipId(getItemValue(pNode)); } else if (CGBMessage::CompareNoCase( type, "SipRealm" )) { info.setSipRealm(getItemValue(pNode)); } else if (CGBMessage::CompareNoCase( type, "Password" )) { info.setSipPass(getItemValue(pNode)); } else if (CGBMessage::CompareNoCase( type, "Timeout" )) { string str = getItemValue(pNode); int i = atoi(str.c_str()); info.setTimeout(i); } else if (CGBMessage::CompareNoCase( type, "Expiry" )) { string str = getItemValue(pNode); int i = atoi(str.c_str()); info.setExpiry(i); } else if (CGBMessage::CompareNoCase( type, "MinRtpPort" )) { string str = getItemValue(pNode); int i = atoi(str.c_str()); info.setMinRtpPort(i); } else if (CGBMessage::CompareNoCase( type, "MaxRtpPort" )) { string str = getItemValue(pNode); int i = atoi(str.c_str()); info.setMaxRtpPort(i); } else if (CGBMessage::CompareNoCase( type, "WsPort" )) { string str = getItemValue(pNode); int i = atoi(str.c_str()); info.setWsPort(i); } } return info; }