#include "jsonBuilder.h" #include #include #include #include #include "constdef.h" #include "log.h" #include #include #include "../tool_time.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 ); 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 ); return sb.GetString(); } 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(); } /* * 构建tof_data的json数据 * */ std::string jsonBuilder::build_tof_data(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("tof_data", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); for(const auto& it : vtd){ rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); // 卡号 value.SetString(it.cid.c_str(), it.cid.length(), allocator); elem.PushBack(value, allocator); // 天线1间测距距离 value.SetDouble(it.ant_dist[0]); elem.PushBack(value, allocator); // 天线2的测距距离 value.SetDouble(it.ant_dist[1]); elem.PushBack(value, allocator); // 天线12间距离 value.SetDouble(it.ant_diff); elem.PushBack(value, allocator); // 基站号 value.SetInt(it.sid); elem.PushBack(value, allocator); // // 一维位置 // value.SetDouble(it.y); // elem.PushBack(value, allocator); // 接收时间 value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } /* * 构建tof_data的json数据 * */ std::string jsonBuilder::build_tof_data_oneAntenna(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("tof_data_oneAntenna", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); for (const auto& it : vtd) { rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); // 卡号 value.SetString(it.cid.c_str(), it.cid.length(), allocator); elem.PushBack(value, allocator); for (auto iterSiteDist = it.map_site_id_dist.begin(); iterSiteDist != it.map_site_id_dist.end(); iterSiteDist++) { // 距离 value.SetDouble(iterSiteDist->second); elem.PushBack(value, allocator); // 基站号 value.SetInt(iterSiteDist->first); elem.PushBack(value, allocator); } // // 距离 // value.SetDouble(it.dist2); // elem.PushBack(value, allocator); // // // 基站号 // value.SetInt(it.sid2); // elem.PushBack(value, allocator); // // if (it.sid3 != 0) // { // // 距离 // value.SetDouble(it.dist3); // elem.PushBack(value, allocator); // // // 基站号 // value.SetInt(it.sid3); // elem.PushBack(value, allocator); // } // // if (it.sid4 != 0) // { // // 距离 // value.SetDouble(it.dist4); // elem.PushBack(value, allocator); // // // 基站号 // value.SetInt(it.sid4); // elem.PushBack(value, allocator); // } // 接收时间 value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } /* * 构建tof_data第三方接口的json数据 * */ std::string jsonBuilder::build_tof_data_oneAntenna_third_part(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("tof_data_doubleAntenna", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); for (const auto& it : vtd) { rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); // 卡号 value.SetString(it.cid.c_str(), it.cid.length(), allocator); elem.PushBack(value, allocator); // x坐标 value.SetDouble(it.x); elem.PushBack(value, allocator); // y坐标 value.SetDouble(it.y); elem.PushBack(value, allocator); int index = 0; for (auto iterSiteDist = it.map_site_id_dist.begin(); iterSiteDist != it.map_site_id_dist.end(); iterSiteDist++) { if (0 == index) { // 基站号 value.SetInt(iterSiteDist->first / 10); elem.PushBack(value, allocator); } // 距离 value.SetDouble(iterSiteDist->second); elem.PushBack(value, allocator); index++; } // // 距离 // value.SetDouble(it.dist2); // elem.PushBack(value, allocator); // // // 基站号 // value.SetInt(it.sid2); // elem.PushBack(value, allocator); // // if (it.sid3 != 0) // { // // 距离 // value.SetDouble(it.dist3); // elem.PushBack(value, allocator); // // // 基站号 // value.SetInt(it.sid3); // elem.PushBack(value, allocator); // } // // if (it.sid4 != 0) // { // // 距离 // value.SetDouble(it.dist4); // elem.PushBack(value, allocator); // // // 基站号 // value.SetInt(it.sid4); // elem.PushBack(value, allocator); // } // 是否运动 value.SetInt(0); elem.PushBack(value, allocator); // 接收时间 value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } std::string jsonBuilder::build_tof_data_oneAntenna_third_part2(int site_id,int card_id,float dist) { //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("tof_data_doubleAntenna", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); //log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); char buf[33]; sprintf(buf, "%d", card_id); std::string cid = buf; // 卡号 value.SetString(cid.c_str(), cid.length(), allocator); elem.PushBack(value, allocator); // x坐标 value.SetDouble(dist); elem.PushBack(value, allocator); // y坐标 value.SetDouble(0); elem.PushBack(value, allocator); int index = 0; // 基站号 value.SetInt(site_id); elem.PushBack(value, allocator); // 是否运动 value.SetInt(0); elem.PushBack(value, allocator); // 接收时间 std::string now = tool_time::to_str_ex(tool_time::now_to_ms()); std::string cur_time = now; value.SetString(cur_time.c_str(), cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } /* * 构建solid_ball_data的json数据 * */ std::string jsonBuilder::build_sb_data(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("sb_raw_data", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); for(const auto& it : vtd){ rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); value.SetString(it.cid.c_str(), it.cid.length(), allocator); elem.PushBack(value, allocator); value.SetDouble(it.x); elem.PushBack(value, allocator); value.SetDouble(it.y); elem.PushBack(value, allocator); value.SetDouble(it.z); elem.PushBack(value, allocator); value.SetDouble(it.speed); elem.PushBack(value, allocator); value.SetString(it.site_info.c_str(), it.site_info.length(), allocator); elem.PushBack(value, allocator); value.SetDouble(it.acc); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[0]); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[1]); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[2]); elem.PushBack(value, allocator); value.SetDouble(it.ang); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[0]); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[1]); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[2]); elem.PushBack(value, allocator); value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } // 2d tdoa's json std::string jsonBuilder::build_tdoa_2d_data(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("tdoa_2d_raw_data", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); for (const auto& it : vtd) { rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); value.SetString(it.cid.c_str(), it.cid.length(), allocator); elem.PushBack(value, allocator); value.SetDouble(it.x); elem.PushBack(value, allocator); value.SetDouble(it.y); elem.PushBack(value, allocator); value.SetDouble(it.d1); elem.PushBack(value, allocator); value.SetDouble(it.d2); elem.PushBack(value, allocator); value.SetDouble(it.d3); elem.PushBack(value, allocator); value.SetDouble(it.acc); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[0]); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[1]); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[2]); elem.PushBack(value, allocator); value.SetDouble(it.ang); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[0]); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[1]); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[2]); elem.PushBack(value, allocator); value.SetDouble(it.dx_v); elem.PushBack(value, allocator); value.SetDouble(it.dy_v); elem.PushBack(value, allocator); value.SetInt(it.move_state); elem.PushBack(value, allocator); // value.SetString(it.site_info.c_str(), it.site_info.length(), allocator); // elem.PushBack(value, allocator); value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } // 3d tof's json std::string jsonBuilder::build_tof_3d_data(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("pdoa_3d_raw_data", root, allocator); __AddVersion(root, allocator); rapidjson::Value data(rapidjson::kArrayType); for(const auto& it : vtd){ rapidjson::Value elem(rapidjson::kArrayType); rapidjson::Value value(rapidjson::kObjectType); log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length()); value.SetString(it.cid.c_str(), it.cid.length(), allocator); elem.PushBack(value, allocator); value.SetDouble(it.x); elem.PushBack(value, allocator); value.SetDouble(it.y); elem.PushBack(value, allocator); value.SetDouble(it.z); elem.PushBack(value, allocator); value.SetDouble(it.acc); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[0]); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[1]); elem.PushBack(value, allocator); value.SetDouble(it.m_acc_data[2]); elem.PushBack(value, allocator); value.SetDouble(it.ang); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[0]); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[1]); elem.PushBack(value, allocator); value.SetDouble(it.m_ang_data[2]); elem.PushBack(value, allocator); value.SetDouble(it.dx_v); elem.PushBack(value, allocator); value.SetDouble(it.dy_v); elem.PushBack(value, allocator); value.SetDouble(it.dz_v); elem.PushBack(value, allocator); value.SetInt(it.move_state); elem.PushBack(value, allocator); value.SetString(it.site_info.c_str(), it.site_info.length(), allocator); elem.PushBack(value, allocator); value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator); elem.PushBack(value, allocator); data.PushBack(elem, allocator); } root.AddMember("data", data, allocator); root.Accept(writer); return sb.GetString(); } }