123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912 |
- #include "jsonBuilder.h"
- #include <cstdlib>
- #include <rapidjson/writer.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/prettywriter.h>
- #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<int, _STAT_DEPT_ITEM_>& dept_map, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Dept )
- {
- std::map<int, _STAT_DEPT_ITEM_>::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<int, _STAT_DEPT_ITEM_>& DeptMap )
- {
- std::map<int, _STAT_DEPT_ITEM_>::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<int, int>::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<int, int>& id_sum_map, const char* szName, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Parent )
- {
- rapidjson::Value Array( rapidjson::kArrayType );
- std::map<int, int>::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<rapidjson::StringBuffer> 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<uint64_t, _CARD_POS_>& CardPosList )
- {
- if(CardPosList.empty()){
- return "";
- }
- static bool p2s=false;
- p2s=p2s?false:true;
- rapidjson::StringBuffer sb;
- rapidjson::Writer<rapidjson::StringBuffer> 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<int, _STAT_DEPT_ITEM_> s_dept_map;
- std::map<int, _STAT_DEPT_ITEM_> v_dept_map;
- _STAT_DEPT_ITEM_ s_glbl_item;
- _STAT_DEPT_ITEM_ v_glbl_item;
- std::map<uint64_t, _CARD_POS_>::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<rapidjson::StringBuffer> 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<uint64_t,_CARD_POS_>& cards)
- {
- rapidjson::StringBuffer sb;
- rapidjson::Writer<rapidjson::StringBuffer> 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<rapidjson::StringBuffer> 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<light_state> lights)
- {
- if(lights.size() == 0){
- return "";
- }
- rapidjson::StringBuffer sb;
- rapidjson::Writer<rapidjson::StringBuffer> 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<device_state> vtd 设备状态列表
- *
- * 返回值
- * 设备状态json
- *
- * */
- std::string jsonBuilder::build_device_state(const std::vector<device_state> vtd)
- {
- if(vtd.size() == 0){
- return "";
- }
- rapidjson::StringBuffer sb;
- rapidjson::Writer<rapidjson::StringBuffer> 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<rapidjson::StringBuffer> 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();
- }
- }
|