1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030 |
- #include "jsonBuilder.h"
- #include <cstdlib>
- #include <rapidjson/writer.h>
- #include <rapidjson/stringbuffer.h>
- #include <rapidjson/prettywriter.h>
- #include "constdef.h"
- #include "log.h"
- #include <cmath>
- 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<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 );
- return sb.GetString();
- }
- 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();
- }
- /*
- * 构建tof_data的json数据
- * */
- std::string jsonBuilder::build_tof_data(const std::vector<tof_data>& 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("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());
- if(it.cid.length()> 1000){
- int a = 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();
- }
- /*
- * 构建solid_ball_data的json数据
- * */
- std::string jsonBuilder::build_sb_data(const std::vector<sb_data>& 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("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();
- }
- }
|