#include "jsonBuilder.h" #include #include #include #include #include "constdef.h" #include "log.h" namespace sys { jsonBuilder::jsonBuilder() { } jsonBuilder::~jsonBuilder() { } std::string jsonBuilder::__FmtCardID( const _BASE_CARD_ & CardPos ) { char szID[32] = { 0 }; sprintf( szID, "%03d%.10d", CardPos.Type, CardPos.ID ); return szID; } void jsonBuilder::__AddVersion( rapidjson::Value& root, rapidjson::Document::AllocatorType& Allocator ) { rapidjson::Value value( rapidjson::kStringType ); value.SetString( INTERFACE_VERSION_1_4, Allocator ); root.AddMember( JSON_ROOT_KEY_VERSION, value, Allocator ); } void jsonBuilder::__SetCmd( const char* szCmd, rapidjson::Value & root, rapidjson::Document::AllocatorType & Allocator ) { rapidjson::Value value( rapidjson::kStringType ); value.SetString( szCmd, Allocator ); root.AddMember( JSON_ROOT_KEY_CMD, value, Allocator ); } #if 0 detail= [ //0 卡号 //1 x坐标 //2 y坐标 //3 入井时间戳 //4 接收时间戳 //5 工作时长 //6 地图编号 //7 部门编号 //8 状态 //9 运行状态 //10 业务状态 //11 速度 //12 area_info_array ] area_info_array=[ area_info, area_info, area_info, area_info ] area_info=[ //0 区域编号 //1 地标编号 //2 地标方向 //3 距离地标的距离 //4 进入区域时间戳 ] #endif bool jsonBuilder::__BuildDetail( const _CARD_POS_ & CardPos, rapidjson::Document::AllocatorType& Allocator, rapidjson::Value & DetailItem ) { rapidjson::Value Array( rapidjson::kArrayType ); rapidjson::Value tmp_object( rapidjson::kObjectType ); //0 ID tmp_object.SetString( __FmtCardID( CardPos ).c_str(), Allocator ); Array.PushBack( tmp_object, Allocator ); //1 x tmp_object.SetDouble( CardPos.x ); Array.PushBack( tmp_object, Allocator ); //2 y tmp_object.SetDouble( CardPos.y ); Array.PushBack( tmp_object, Allocator ); //3 入井时间戳 tmp_object.SetDouble( CardPos.down_time ); Array.PushBack( tmp_object, Allocator ); //4 进入区域时间戳 //tmp_object.SetDouble( CardPos.enter_area_time ); //Array.PushBack( tmp_object, Allocator ); //5 最后接收时间戳 tmp_object.SetDouble( CardPos.rec_time ); Array.PushBack( tmp_object, Allocator ); //6 工作时长 //tmp_object.SetDouble(CardPos.z); tmp_object.SetDouble( CardPos.work_time ); Array.PushBack( tmp_object, Allocator ); //7 地图编号 tmp_object.SetInt( CardPos.map_id ); Array.PushBack( tmp_object, Allocator ); //8 区域编号 //tmp_object.SetInt( CardPos.area_id ); //Array.PushBack( tmp_object, Allocator ); //9 部门编号 tmp_object.SetInt( CardPos.dept_id ); Array.PushBack( tmp_object, Allocator ); //10 电量状态 //tmp_object.SetInt( CardPos.stat ); tmp_object.SetInt(CardPos.battery_val); // 电量值,1~10 Array.PushBack( tmp_object, Allocator ); //11 运行状态 tmp_object.SetInt( CardPos.running_stat ); Array.PushBack( tmp_object, Allocator ); //12 业务状态 tmp_object.SetInt( CardPos.biz_stat ); Array.PushBack( tmp_object, Allocator ); //13 速度 tmp_object.SetDouble( CardPos.speed ); Array.PushBack( tmp_object, Allocator ); //14 地标编号 //tmp_object.SetInt( CardPos.landmark_id ); //Array.PushBack( tmp_object, Allocator ); //15 地标方向 //tmp_object.SetInt( CardPos.lm_direction ); //Array.PushBack( tmp_object, Allocator ); //16 距离地标的距离 //tmp_object.SetDouble( CardPos.landmark_dis ); //Array.PushBack( tmp_object, Allocator ); //17 级别编号,对于不要级别编号的(可以用于传输接收分站的区域id) //tmp_object.SetInt( CardPos.level_id ); tmp_object.SetInt(CardPos.area_id); Array.PushBack( tmp_object, Allocator ); //18 车辆当天出勤的标识 tmp_object.SetInt( CardPos.is_on_duty ); Array.PushBack( tmp_object, Allocator ); //log_debug("websocket..%d",CardPos.area_info.size()); rapidjson::Value Array_1( rapidjson::kArrayType ); for(const auto & a : CardPos.area_info) { rapidjson::Value _DetailItem; rapidjson::Value Array_2( rapidjson::kArrayType ); const auto lm = a.second; //8 区域编号 tmp_object.SetInt(std::get<0>(lm)); Array_2.PushBack( tmp_object, Allocator ); //14 地标编号 tmp_object.SetInt(std::get<1>(lm)); Array_2.PushBack( tmp_object, Allocator ); //15 地标方向 tmp_object.SetInt(std::get<2>(lm)); Array_2.PushBack( tmp_object, Allocator ); //16 距离地标的距离 tmp_object.SetDouble(std::get<3>(lm)); Array_2.PushBack( tmp_object, Allocator ); //5 最后接收时间戳 tmp_object.SetDouble(std::get<4>(lm)); Array_2.PushBack( tmp_object, Allocator ); _DetailItem=Array_2; Array_1.PushBack(_DetailItem, Allocator); } DetailItem = Array_1; Array.PushBack(DetailItem, Allocator ); //19 cell index tmp_object.SetInt(CardPos.cell_index); Array.PushBack(tmp_object, Allocator); DetailItem = Array; return true; } bool jsonBuilder::__BuildDept( const std::map& dept_map, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Dept ) { std::map::const_iterator mit_dept; for ( mit_dept = dept_map.begin(); mit_dept != dept_map.end(); mit_dept++ ) { rapidjson::Value DeptItem( rapidjson::kObjectType ); __BuildDeptItem( mit_dept->second, Allocator, DeptItem ); rapidjson::Value dept_id( rapidjson::kObjectType ); char szNum[20] = { 0 }; //itoa( mit_dept->second.DeptID, szNum, 10 ); sprintf(szNum,"%d",mit_dept->second.DeptID); dept_id.SetString( szNum, strlen( szNum ), Allocator ); Dept.AddMember( dept_id, DeptItem, Allocator ); } return true; } void jsonBuilder::__BuildDeptItem( const _STAT_DEPT_ITEM_ & st_dept_item, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & DeptItem ) { //Area __BuildIDSumMap( st_dept_item.Area, JSON_ROOT_KEY_STATISTIC_AREA, Allocator, DeptItem ); //Dept __BuildIDSumMap( st_dept_item.Dept, JSON_ROOT_KEY_STATISTIC_DEPT, Allocator, DeptItem ); //occupation_level __BuildIDSumMap( st_dept_item.OcptLvl, JSON_ROOT_KEY_STATISTIC_LEVEL, Allocator, DeptItem ); //sum rapidjson::Value Sum( rapidjson::kObjectType ); Sum.SetInt( st_dept_item.Sum ); DeptItem.AddMember( JSON_ROOT_KEY_STATISTIC_SUM, Sum, Allocator ); } void jsonBuilder::__BuildBaseCardItem( const _BASE_CARD_ & Card, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & CardItem ) { rapidjson::Value TmpObj( rapidjson::kObjectType ); rapidjson::Value CardObj( rapidjson::kArrayType ); //0 卡号 TmpObj.SetString( __FmtCardID( Card ).c_str(), Allocator ); CardObj.PushBack( TmpObj, Allocator ); //1 x坐标 TmpObj.SetDouble( Card.x ); CardObj.PushBack( TmpObj, Allocator ); //2 y坐标 TmpObj.SetDouble( Card.y ); CardObj.PushBack( TmpObj, Allocator ); //3 入井时间戳 TmpObj.SetDouble( Card.down_time ); CardObj.PushBack( TmpObj, Allocator ); //4 进入区域时间戳 TmpObj.SetDouble( Card.enter_area_time ); CardObj.PushBack( TmpObj, Allocator ); //5 接收时间戳 TmpObj.SetDouble( Card.rec_time ); CardObj.PushBack( TmpObj, Allocator ); //6 工作时长 TmpObj.SetDouble( Card.work_time ); CardObj.PushBack( TmpObj, Allocator ); //7 地图编号 TmpObj.SetInt( Card.map_id ); CardObj.PushBack( TmpObj, Allocator ); //8 区域编号 TmpObj.SetInt( Card.area_id ); CardObj.PushBack( TmpObj, Allocator ); //9 部门编号 TmpObj.SetInt( Card.dept_id ); CardObj.PushBack( TmpObj, Allocator ); //10 状态 TmpObj.SetInt( Card.stat ); CardObj.PushBack( TmpObj, Allocator ); //11 运行状态 TmpObj.SetInt( Card.running_stat ); CardObj.PushBack( TmpObj, Allocator ); //12 业务状态 TmpObj.SetInt( Card.biz_stat ); CardObj.PushBack( TmpObj, Allocator ); //13 速度 TmpObj.SetDouble( Card.speed ); CardObj.PushBack( TmpObj, Allocator ); CardItem = CardObj; } void jsonBuilder::__CreateDeptMap( const _CARD_POS_ & CardPos, std::map& DeptMap ) { std::map::iterator mit_dept_map; mit_dept_map = DeptMap.find( CardPos.dept_id ); if ( mit_dept_map == DeptMap.end() ) { _STAT_DEPT_ITEM_ sdItem; sdItem.DeptID = CardPos.dept_id; for(const auto &a:CardPos.area_info) sdItem.Area.insert( std::make_pair(a.first, 1 ) ); sdItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) ); sdItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) ); sdItem.Sum = 1; DeptMap.insert( std::make_pair( sdItem.DeptID, sdItem ) ); } else { __CreateDeptItem( CardPos, mit_dept_map->second ); } } void jsonBuilder::__CreateDeptItem( const _CARD_POS_ & CardPos, _STAT_DEPT_ITEM_ & DeptItem ) { std::map::iterator mit_id_sum; //area for(const auto &a:CardPos.area_info) { int areaid = a.first; mit_id_sum = DeptItem.Area.find(areaid); if ( mit_id_sum == DeptItem.Area.end() ) { DeptItem.Area.insert( std::make_pair(areaid, 1 ) ); } else { mit_id_sum->second++; } } //Dept mit_id_sum = DeptItem.Dept.find( CardPos.dept_id ); if ( mit_id_sum == DeptItem.Dept.end() ) { DeptItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) ); } else { mit_id_sum->second++; } //occupation_level mit_id_sum = DeptItem.OcptLvl.find( CardPos.level_id ); if ( mit_id_sum == DeptItem.OcptLvl.end() ) { DeptItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) ); } else { mit_id_sum->second++; } //Sum DeptItem.Sum++; } void jsonBuilder::__BuildIDSumMap( const std::map& id_sum_map, const char* szName, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Parent ) { rapidjson::Value Array( rapidjson::kArrayType ); std::map::const_iterator mit_id_sum; for ( mit_id_sum = id_sum_map.begin(); mit_id_sum != id_sum_map.end(); mit_id_sum++ ) { rapidjson::Value Item( rapidjson::kArrayType ); rapidjson::Value tmpobj( rapidjson::kObjectType ); tmpobj.SetInt( mit_id_sum->first ); Item.PushBack( tmpobj, Allocator ); tmpobj.SetInt( mit_id_sum->second ); Item.PushBack( tmpobj, Allocator ); Array.PushBack( Item, Allocator ); } Parent.AddMember( rapidjson::StringRef( szName ), Array, Allocator ); } std::string jsonBuilder::BuildLogin( const _JS_LOGIN_& Login ) { rapidjson::StringBuffer sb; rapidjson::Writer writer( sb ); rapidjson::Document doc; rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator(); rapidjson::Value root( rapidjson::kObjectType ); rapidjson::Value child( rapidjson::kObjectType ); rapidjson::Value value( rapidjson::kStringType ); __SetCmd( JSON_CMD_VALUE_LOGIN, root, Allocator ); value.SetString( Login.user_name.c_str(), Allocator ); child.AddMember( JSON_KEY_USERNAME, value, Allocator ); value.SetString( Login.user_password.c_str(), Allocator ); child.AddMember( JSON_KEY_PASSWORD, value, Allocator ); root.AddMember( JSON_ROOT_KEY_DATA, child, Allocator ); root.Accept( writer ); return sb.GetString(); } std::string jsonBuilder::BuildCardPos( const std::map& CardPosList ) { if(CardPosList.empty()){ return ""; } static bool p2s=false; p2s=p2s?false:true; rapidjson::StringBuffer sb; rapidjson::Writer writer( sb ); rapidjson::Document doc; rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator(); rapidjson::Value root( rapidjson::kObjectType ); __SetCmd( JSON_CMD_VALUE_POS_MAP, root, Allocator ); rapidjson::Value s( rapidjson::kObjectType ); rapidjson::Value v( rapidjson::kObjectType ); rapidjson::Value s_Detail( rapidjson::kArrayType ); rapidjson::Value v_Detail( rapidjson::kArrayType ); rapidjson::Value s_dept( rapidjson::kObjectType ); rapidjson::Value v_dept( rapidjson::kObjectType ); std::map s_dept_map; std::map v_dept_map; _STAT_DEPT_ITEM_ s_glbl_item; _STAT_DEPT_ITEM_ v_glbl_item; std::map::const_iterator mit_card; for ( mit_card = CardPosList.begin(); mit_card != CardPosList.end(); mit_card++ ) { //不显示的卡不往前端推送 //if ( !mit_card->second.display ) //{ // continue; //} //if(p2s && mit_card->second.Type == 1)continue; rapidjson::Value DetailItem; if ( !__BuildDetail( mit_card->second, Allocator, DetailItem ) ) { continue; } if( mit_card->second.Type == 1) { s_Detail.PushBack( DetailItem, Allocator ); __CreateDeptMap( mit_card->second, s_dept_map ); __CreateDeptItem( mit_card->second, s_glbl_item ); } else //车卡、采煤机等等 { v_Detail.PushBack( DetailItem, Allocator ); __CreateDeptMap( mit_card->second, v_dept_map ); __CreateDeptItem( mit_card->second, v_glbl_item ); } } s.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, s_Detail, Allocator ); v.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, v_Detail, Allocator ); //stat rapidjson::Value s_stat( rapidjson::kObjectType ); rapidjson::Value v_stat( rapidjson::kObjectType ); if ( __BuildDept( s_dept_map, Allocator, s_dept ) ) { s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, s_dept, Allocator ); } if ( __BuildDept( v_dept_map, Allocator, v_dept ) ) { v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, v_dept, Allocator ); } //glbl rapidjson::Value s_glbl( rapidjson::kObjectType ); rapidjson::Value v_glbl( rapidjson::kObjectType ); __BuildDeptItem( s_glbl_item, Allocator, s_glbl ); __BuildDeptItem( v_glbl_item, Allocator, v_glbl ); //s.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator ); //v.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator ); s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator ); v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator ); s.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, s_stat, Allocator ); v.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, v_stat, Allocator ); rapidjson::Value Data( rapidjson::kObjectType ); Data.AddMember( JSON_ROOT_KEY_STATISTIC_STAFF_DATA, s, Allocator ); Data.AddMember( JSON_ROOT_KEY_STATISTIC_VEHICLE_DATA, v, Allocator ); root.AddMember( JSON_ROOT_KEY_DATA, Data, Allocator ); __AddVersion( root, Allocator ); root.Accept( writer ); // 处理长时间运行时,字符串截断问题 std::string json_str = std::string(sb.GetString(), sb.GetSize()); return json_str; } std::string jsonBuilder::build_ios_card_pos(const _CARD_POS_& card) { rapidjson::StringBuffer sb; rapidjson::Writer writer(sb); rapidjson::Document doc; rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value root( rapidjson::kObjectType ); // "cmd": __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator ); // "version" __AddVersion(root, allocator); // "item" rapidjson::Value item(rapidjson::kObjectType); rapidjson::Value value; value.SetString(std::to_string(card.ID).c_str(), allocator); item.AddMember("cardid", value, allocator); item.AddMember("x", card.x, allocator); item.AddMember("y", card.y, allocator); item.AddMember("z", card.z, allocator); item.AddMember("acc", card.m_acc, allocator); item.AddMember("speed", card.speed, allocator); item.AddMember("acc_x", card.m_tri_acc[0], allocator); item.AddMember("acc_y", card.m_tri_acc[1], allocator); item.AddMember("acc_z", card.m_tri_acc[2], allocator); item.AddMember("ang_x", card.m_tri_ang[0], allocator); item.AddMember("ang_y", card.m_tri_ang[1], allocator); item.AddMember("ang_z", card.m_tri_ang[2], allocator); item.AddMember("steps", card.m_walking_step, allocator); item.AddMember("jump", card.m_jump_count, allocator); item.AddMember("hang_time", card.m_hang_time, allocator); item.AddMember("hang_height", card.m_hang_height, allocator); /*rapidjson::Value data(rapidjson::kObjectType); data.AddMember("card_type", 1 , allocator); data.AddMember("cardid", "10", allocator); data.AddMember("x", 10.0 , allocator); data.AddMember("y", 20.0 , allocator); data.AddMember("z", 30.0, allocator); data.AddMember("acc" , 10.0 , allocator); data.AddMember("speed", 3.5 , allocator); data.AddMember("acc_x", 0.1, allocator); data.AddMember("acc_y", 0.2 , allocator); data.AddMember("acc_z", 0.3 , allocator); data.AddMember("ang_x", 0.4, allocator); data.AddMember("ang_y", 0.5 , allocator); data.AddMember("ang_z", 0.6 , allocator); data.AddMember("steps", 7, allocator); data.AddMember("jump", 8 , allocator); data.AddMember("hang_time", 3 , allocator); data.AddMember("hang_height", 4, allocator); */ root.AddMember("data", item, allocator); root.Accept(writer); return sb.GetString(); } std::string jsonBuilder::build_tmp_card_pos(const std::map& cards) { rapidjson::StringBuffer sb; rapidjson::Writer writer(sb); rapidjson::Document doc; rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value root(rapidjson::kObjectType); // "cmd": __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator ); // "data" rapidjson::Value item(rapidjson::kArrayType); for(auto it = cards.begin(); it != cards.end(); ++it) { rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value tmp_obj(rapidjson::kObjectType); // 卡号 tmp_obj.SetString(__FmtCardID(it->second).c_str(), allocator); elem.PushBack(tmp_obj, allocator); // x坐标 tmp_obj.SetDouble(it->second.x); elem.PushBack(tmp_obj, allocator); // y坐标 tmp_obj.SetDouble(it->second.y); elem.PushBack(tmp_obj, allocator); // z坐标 tmp_obj.SetDouble(it->second.z); elem.PushBack(tmp_obj, allocator); // 时间戳 tmp_obj.SetDouble(it->second.rec_time); elem.PushBack(tmp_obj, allocator); // 加速度 tmp_obj.SetDouble(it->second.m_acc); elem.PushBack(tmp_obj, allocator); // 速度 tmp_obj.SetDouble(it->second.speed); elem.PushBack(tmp_obj, allocator); item.PushBack(elem, allocator); } root.AddMember("data", item, allocator); // "version" __AddVersion(root, allocator); root.Accept(writer); return sb.GetString(); } std::string jsonBuilder::BuildSpecialAreaProcess( const _BASE_CARD_ & stCard ) { rapidjson::StringBuffer sb; rapidjson::Writer writer( sb ); rapidjson::Document doc; rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator(); rapidjson::Value root( rapidjson::kObjectType ); __SetCmd( JSON_CMD_VALUE_SPECIAL_AREA_UP_MINE, root, Allocator ); rapidjson::Value CardItem( rapidjson::kObjectType ); rapidjson::Value CardList( rapidjson::kArrayType ); __BuildBaseCardItem( stCard, Allocator, CardItem ); CardList.PushBack( CardItem, Allocator ); root.AddMember( JSON_ROOT_KEY_DATA, CardList, Allocator ); root.Accept( writer ); return sb.GetString(); } bool jsonBuilder::ParseCall_Card_Req( const std::string& JasonText, _JS_CALL_CARD_REQ_& CallCardReq, std::string& Error ) { rapidjson::Document doc; if ( doc.Parse( JasonText.c_str() ).HasParseError() ) { Error = "Failed to parse jason text!"; return false; } rapidjson::Value::MemberIterator itValue; itValue = doc.FindMember( "data" ); if ( itValue == doc.MemberEnd() ) { Error = "Failed to find member: data!"; return false; } rapidjson::Value Data; rapidjson::Document::AllocatorType allocator; Data.CopyFrom( itValue->value, allocator ); if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TYPE ) ) { Error = "Failed to find value: "; Error += JSON_KEY_CALL_CARD_CALL_TYPE; return false; } CallCardReq.call_type_id = Data[JSON_KEY_CALL_CARD_CALL_TYPE].GetInt(); if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TIME_OUT ) ) { Error = "Failed to find value: "; Error += JSON_KEY_CALL_CARD_CALL_TIME_OUT; return false; } CallCardReq.call_time_out = Data[JSON_KEY_CALL_CARD_CALL_TIME_OUT].GetInt(); if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_LEVEL ) ) { Error = "Failed to find value: "; Error += JSON_KEY_CALL_CARD_CALL_LEVEL; return false; } CallCardReq.call_time_interval = Data[JSON_KEY_CALL_CARD_CALL_LEVEL].GetInt(); if ( !Data.HasMember( JSON_KEY_CALL_CARD_USER_NAME ) ) { Error = "Failed to find value: "; Error += JSON_KEY_CALL_CARD_USER_NAME; return false; } CallCardReq.user_name = Data[JSON_KEY_CALL_CARD_USER_NAME].GetString(); //data::stations if ( !Data.HasMember( JSON_KEY_CALL_CARD_STATIONS ) ) { Error = "Failed to find value: "; Error += JSON_KEY_CALL_CARD_STATIONS; return false; } rapidjson::Value & stations = Data[JSON_KEY_CALL_CARD_STATIONS]; if ( !stations.IsArray() ) { Error = "data::stations is not a valid array!"; return false; } for ( int i = 0; i < (int)stations.Size(); ++i ) { rapidjson::Value & v = stations[i]; if ( !v.IsObject() ) { char szError[1024] = { 0 }; sprintf( szError, "data::stations[%d] is not a object!", i ); Error = szError; return false; } _JS_STATION_ Station; if ( !v.HasMember( JSON_KEY_CALL_CARD_STATION_ID ) && v[JSON_KEY_CALL_CARD_STATION_ID].IsInt() ) { Error = "Error, failed to found value: "; Error += JSON_KEY_CALL_CARD_STATION_ID; return false; } Station.stationid = v[JSON_KEY_CALL_CARD_STATION_ID].GetInt(); CallCardReq.stations.push_back( Station ); } //data::cards if ( !Data.HasMember( JSON_KEY_CALL_CARD_CARDS ) ) { Error = "Failed to find value: "; Error += JSON_KEY_CALL_CARD_CARDS; return false; } rapidjson::Value & cards = Data[JSON_KEY_CALL_CARD_CARDS]; for ( int i = 0; i < (int)cards.Size(); ++i ) { rapidjson::Value & v = cards[i]; if ( !v.IsObject() ) { char szError[1024] = { 0 }; sprintf( szError, "data::cards[%d] is not a object!", i ); Error = szError; return false; } _JS_CARD_ Card; if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_ID ) && v[JSON_KEY_CALL_CARD_CARD_ID].IsInt() ) { Error = "Error, failed to found value: "; Error += JSON_KEY_CALL_CARD_CARD_ID; return false; } Card.cardid = v[JSON_KEY_CALL_CARD_CARD_ID].GetInt(); if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_TYPE_ID ) && v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].IsInt() ) { Error = "Error, failed to found value: "; Error += JSON_KEY_CALL_CARD_CARD_TYPE_ID; return false; } Card.cardtype = v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].GetInt(); CallCardReq.cards.push_back( Card ); } return true; } std::string jsonBuilder::build_traffic_light(const std::vector lights) { if(lights.size() == 0){ return ""; } rapidjson::StringBuffer sb; rapidjson::Writer writer( sb ); rapidjson::Document doc; rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value root(rapidjson::kObjectType ); __SetCmd(JSON_KEY_CALL_LIGHT_LIGHT_STATE, root, allocator); rapidjson::Value data(rapidjson::kArrayType); for(auto it : lights){ rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value tmp_object(rapidjson::kObjectType); tmp_object.SetInt(it.m_group_id); elem.PushBack(tmp_object, allocator); tmp_object.SetInt(it.m_light_id); elem.PushBack(tmp_object, allocator); tmp_object.SetInt(it.m_light_state); elem.PushBack(tmp_object, allocator); tmp_object.SetString(it.m_card_id.c_str(), allocator); elem.PushBack(tmp_object, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); __AddVersion(root, allocator); root.Accept(writer); return sb.GetString(); } /* * 构建设备状态json * * 参数 * const std::vector vtd 设备状态列表 * * 返回值 * 设备状态json * * */ std::string jsonBuilder::build_device_state(const std::vector vtd) { if(vtd.size() == 0){ return ""; } rapidjson::StringBuffer sb; rapidjson::Writer writer( sb ); rapidjson::Document doc; rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value root(rapidjson::kObjectType ); __SetCmd(JSON_CMD_VALUE_DEV_STATE, root, allocator); rapidjson::Value data(rapidjson::kArrayType); for(auto it : vtd){ rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value tmp_object(rapidjson::kObjectType); tmp_object.SetInt(it.m_id); elem.PushBack(tmp_object, allocator); tmp_object.SetInt(it.m_type); elem.PushBack(tmp_object, allocator); tmp_object.SetInt(it.m_state); elem.PushBack(tmp_object, allocator); tmp_object.SetDouble(it.m_cur_time); elem.PushBack(tmp_object, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); __AddVersion(root, allocator); root.Accept(writer); return sb.GetString(); } std::string jsonBuilder::build_load_history_completed() { rapidjson::StringBuffer sb; rapidjson::Writer writer(sb); rapidjson::Document doc; rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); rapidjson::Value root(rapidjson::kObjectType); __SetCmd(JSON_KEY_CALL_LOAD_HISTORY_COMPLETED, root, allocator); rapidjson::Value data(rapidjson::kArrayType); root.AddMember("data", data, allocator); __AddVersion(root, allocator); root.Accept(writer); return sb.GetString(); } }