jsonBuilder.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. #include "jsonBuilder.h"
  2. #include <cstdlib>
  3. #include <rapidjson/writer.h>
  4. #include <rapidjson/stringbuffer.h>
  5. #include <rapidjson/prettywriter.h>
  6. #include "constdef.h"
  7. #include "log.h"
  8. #include <cmath>
  9. #include <fstream>
  10. #include "../tool_time.h"
  11. namespace sys
  12. {
  13. jsonBuilder::jsonBuilder()
  14. {
  15. }
  16. jsonBuilder::~jsonBuilder()
  17. {
  18. }
  19. std::string jsonBuilder::__FmtCardID( const _BASE_CARD_ & CardPos )
  20. {
  21. char szID[32] = { 0 };
  22. sprintf( szID, "%03d%.10d", CardPos.Type, CardPos.ID );
  23. return szID;
  24. }
  25. void jsonBuilder::__AddVersion( rapidjson::Value& root, rapidjson::Document::AllocatorType& Allocator )
  26. {
  27. rapidjson::Value value( rapidjson::kStringType );
  28. value.SetString( INTERFACE_VERSION_1_4, Allocator );
  29. root.AddMember( JSON_ROOT_KEY_VERSION, value, Allocator );
  30. }
  31. void jsonBuilder::__SetCmd( const char* szCmd, rapidjson::Value & root, rapidjson::Document::AllocatorType & Allocator )
  32. {
  33. rapidjson::Value value( rapidjson::kStringType );
  34. value.SetString( szCmd, Allocator );
  35. root.AddMember( JSON_ROOT_KEY_CMD, value, Allocator );
  36. }
  37. #if 0
  38. detail=
  39. [
  40. //0 卡号
  41. //1 x坐标
  42. //2 y坐标
  43. //3 入井时间戳
  44. //4 接收时间戳
  45. //5 工作时长
  46. //6 地图编号
  47. //7 部门编号
  48. //8 状态
  49. //9 运行状态
  50. //10 业务状态
  51. //11 速度
  52. //12 area_info_array
  53. ]
  54. area_info_array=[
  55. area_info,
  56. area_info,
  57. area_info,
  58. area_info
  59. ]
  60. area_info=[
  61. //0 区域编号
  62. //1 地标编号
  63. //2 地标方向
  64. //3 距离地标的距离
  65. //4 进入区域时间戳
  66. ]
  67. #endif
  68. bool jsonBuilder::__BuildDetail( const _CARD_POS_ & CardPos, rapidjson::Document::AllocatorType& Allocator, rapidjson::Value & DetailItem )
  69. {
  70. rapidjson::Value Array( rapidjson::kArrayType );
  71. rapidjson::Value tmp_object( rapidjson::kObjectType );
  72. //0 ID
  73. tmp_object.SetString( __FmtCardID( CardPos ).c_str(), Allocator );
  74. Array.PushBack( tmp_object, Allocator );
  75. //1 x
  76. tmp_object.SetDouble( CardPos.x );
  77. Array.PushBack( tmp_object, Allocator );
  78. //2 y
  79. tmp_object.SetDouble( CardPos.y );
  80. Array.PushBack( tmp_object, Allocator );
  81. //3 入井时间戳
  82. tmp_object.SetDouble( CardPos.down_time );
  83. Array.PushBack( tmp_object, Allocator );
  84. //4 进入区域时间戳
  85. //tmp_object.SetDouble( CardPos.enter_area_time );
  86. //Array.PushBack( tmp_object, Allocator );
  87. //5 最后接收时间戳
  88. tmp_object.SetDouble( CardPos.rec_time );
  89. Array.PushBack( tmp_object, Allocator );
  90. //6 工作时长
  91. //tmp_object.SetDouble(CardPos.z);
  92. tmp_object.SetDouble( CardPos.work_time );
  93. Array.PushBack( tmp_object, Allocator );
  94. //7 地图编号
  95. tmp_object.SetInt( CardPos.map_id );
  96. Array.PushBack( tmp_object, Allocator );
  97. //8 区域编号
  98. //tmp_object.SetInt( CardPos.area_id );
  99. //Array.PushBack( tmp_object, Allocator );
  100. //9 部门编号
  101. tmp_object.SetInt( CardPos.dept_id );
  102. Array.PushBack( tmp_object, Allocator );
  103. //10 电量状态
  104. //tmp_object.SetInt( CardPos.stat );
  105. tmp_object.SetInt(CardPos.battery_val); // 电量值,1~10
  106. Array.PushBack( tmp_object, Allocator );
  107. //11 运行状态
  108. tmp_object.SetInt( CardPos.running_stat );
  109. Array.PushBack( tmp_object, Allocator );
  110. //12 业务状态
  111. tmp_object.SetInt( CardPos.biz_stat );
  112. Array.PushBack( tmp_object, Allocator );
  113. //13 速度
  114. tmp_object.SetDouble( CardPos.speed );
  115. Array.PushBack( tmp_object, Allocator );
  116. //14 地标编号
  117. //tmp_object.SetInt( CardPos.landmark_id );
  118. //Array.PushBack( tmp_object, Allocator );
  119. //15 地标方向
  120. //tmp_object.SetInt( CardPos.lm_direction );
  121. //Array.PushBack( tmp_object, Allocator );
  122. //16 距离地标的距离
  123. //tmp_object.SetDouble( CardPos.landmark_dis );
  124. //Array.PushBack( tmp_object, Allocator );
  125. //17 级别编号,对于不要级别编号的(可以用于传输接收分站的区域id)
  126. //tmp_object.SetInt( CardPos.level_id );
  127. tmp_object.SetInt(CardPos.area_id);
  128. Array.PushBack( tmp_object, Allocator );
  129. //18 车辆当天出勤的标识
  130. tmp_object.SetInt( CardPos.is_on_duty );
  131. Array.PushBack( tmp_object, Allocator );
  132. //log_debug("websocket..%d",CardPos.area_info.size());
  133. rapidjson::Value Array_1( rapidjson::kArrayType );
  134. for(const auto & a : CardPos.area_info)
  135. {
  136. rapidjson::Value _DetailItem;
  137. rapidjson::Value Array_2( rapidjson::kArrayType );
  138. const auto lm = a.second;
  139. //8 区域编号
  140. tmp_object.SetInt(std::get<0>(lm));
  141. Array_2.PushBack( tmp_object, Allocator );
  142. //14 地标编号
  143. tmp_object.SetInt(std::get<1>(lm));
  144. Array_2.PushBack( tmp_object, Allocator );
  145. //15 地标方向
  146. tmp_object.SetInt(std::get<2>(lm));
  147. Array_2.PushBack( tmp_object, Allocator );
  148. //16 距离地标的距离
  149. tmp_object.SetDouble(std::get<3>(lm));
  150. Array_2.PushBack( tmp_object, Allocator );
  151. //5 最后接收时间戳
  152. tmp_object.SetDouble(std::get<4>(lm));
  153. Array_2.PushBack( tmp_object, Allocator );
  154. _DetailItem=Array_2;
  155. Array_1.PushBack(_DetailItem, Allocator);
  156. }
  157. DetailItem = Array_1;
  158. Array.PushBack(DetailItem, Allocator );
  159. DetailItem = Array;
  160. return true;
  161. }
  162. bool jsonBuilder::__BuildDept( const std::map<int, _STAT_DEPT_ITEM_>& dept_map, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Dept )
  163. {
  164. std::map<int, _STAT_DEPT_ITEM_>::const_iterator mit_dept;
  165. for ( mit_dept = dept_map.begin(); mit_dept != dept_map.end(); mit_dept++ )
  166. {
  167. rapidjson::Value DeptItem( rapidjson::kObjectType );
  168. __BuildDeptItem( mit_dept->second, Allocator, DeptItem );
  169. rapidjson::Value dept_id( rapidjson::kObjectType );
  170. char szNum[20] = { 0 };
  171. //itoa( mit_dept->second.DeptID, szNum, 10 );
  172. sprintf(szNum,"%d",mit_dept->second.DeptID);
  173. dept_id.SetString( szNum, strlen( szNum ), Allocator );
  174. Dept.AddMember( dept_id, DeptItem, Allocator );
  175. }
  176. return true;
  177. }
  178. void jsonBuilder::__BuildDeptItem( const _STAT_DEPT_ITEM_ & st_dept_item, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & DeptItem )
  179. {
  180. //Area
  181. __BuildIDSumMap( st_dept_item.Area, JSON_ROOT_KEY_STATISTIC_AREA, Allocator, DeptItem );
  182. //Dept
  183. __BuildIDSumMap( st_dept_item.Dept, JSON_ROOT_KEY_STATISTIC_DEPT, Allocator, DeptItem );
  184. //occupation_level
  185. __BuildIDSumMap( st_dept_item.OcptLvl, JSON_ROOT_KEY_STATISTIC_LEVEL, Allocator, DeptItem );
  186. //sum
  187. rapidjson::Value Sum( rapidjson::kObjectType );
  188. Sum.SetInt( st_dept_item.Sum );
  189. DeptItem.AddMember( JSON_ROOT_KEY_STATISTIC_SUM, Sum, Allocator );
  190. }
  191. void jsonBuilder::__BuildBaseCardItem( const _BASE_CARD_ & Card, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & CardItem )
  192. {
  193. rapidjson::Value TmpObj( rapidjson::kObjectType );
  194. rapidjson::Value CardObj( rapidjson::kArrayType );
  195. //0 卡号
  196. TmpObj.SetString( __FmtCardID( Card ).c_str(), Allocator );
  197. CardObj.PushBack( TmpObj, Allocator );
  198. //1 x坐标
  199. TmpObj.SetDouble( Card.x );
  200. CardObj.PushBack( TmpObj, Allocator );
  201. //2 y坐标
  202. TmpObj.SetDouble( Card.y );
  203. CardObj.PushBack( TmpObj, Allocator );
  204. //3 入井时间戳
  205. TmpObj.SetDouble( Card.down_time );
  206. CardObj.PushBack( TmpObj, Allocator );
  207. //4 进入区域时间戳
  208. TmpObj.SetDouble( Card.enter_area_time );
  209. CardObj.PushBack( TmpObj, Allocator );
  210. //5 接收时间戳
  211. TmpObj.SetDouble( Card.rec_time );
  212. CardObj.PushBack( TmpObj, Allocator );
  213. //6 工作时长
  214. TmpObj.SetDouble( Card.work_time );
  215. CardObj.PushBack( TmpObj, Allocator );
  216. //7 地图编号
  217. TmpObj.SetInt( Card.map_id );
  218. CardObj.PushBack( TmpObj, Allocator );
  219. //8 区域编号
  220. TmpObj.SetInt( Card.area_id );
  221. CardObj.PushBack( TmpObj, Allocator );
  222. //9 部门编号
  223. TmpObj.SetInt( Card.dept_id );
  224. CardObj.PushBack( TmpObj, Allocator );
  225. //10 状态
  226. TmpObj.SetInt( Card.stat );
  227. CardObj.PushBack( TmpObj, Allocator );
  228. //11 运行状态
  229. TmpObj.SetInt( Card.running_stat );
  230. CardObj.PushBack( TmpObj, Allocator );
  231. //12 业务状态
  232. TmpObj.SetInt( Card.biz_stat );
  233. CardObj.PushBack( TmpObj, Allocator );
  234. //13 速度
  235. TmpObj.SetDouble( Card.speed );
  236. CardObj.PushBack( TmpObj, Allocator );
  237. CardItem = CardObj;
  238. }
  239. void jsonBuilder::__CreateDeptMap( const _CARD_POS_ & CardPos, std::map<int, _STAT_DEPT_ITEM_>& DeptMap )
  240. {
  241. std::map<int, _STAT_DEPT_ITEM_>::iterator mit_dept_map;
  242. mit_dept_map = DeptMap.find( CardPos.dept_id );
  243. if ( mit_dept_map == DeptMap.end() )
  244. {
  245. _STAT_DEPT_ITEM_ sdItem;
  246. sdItem.DeptID = CardPos.dept_id;
  247. for(const auto &a:CardPos.area_info)
  248. sdItem.Area.insert( std::make_pair(a.first, 1 ) );
  249. sdItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) );
  250. sdItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) );
  251. sdItem.Sum = 1;
  252. DeptMap.insert( std::make_pair( sdItem.DeptID, sdItem ) );
  253. }
  254. else
  255. {
  256. __CreateDeptItem( CardPos, mit_dept_map->second );
  257. }
  258. }
  259. void jsonBuilder::__CreateDeptItem( const _CARD_POS_ & CardPos, _STAT_DEPT_ITEM_ & DeptItem )
  260. {
  261. std::map<int, int>::iterator mit_id_sum;
  262. //area
  263. for(const auto &a:CardPos.area_info)
  264. {
  265. int areaid = a.first;
  266. mit_id_sum = DeptItem.Area.find(areaid);
  267. if ( mit_id_sum == DeptItem.Area.end() )
  268. {
  269. DeptItem.Area.insert( std::make_pair(areaid, 1 ) );
  270. }
  271. else
  272. {
  273. mit_id_sum->second++;
  274. }
  275. }
  276. //Dept
  277. mit_id_sum = DeptItem.Dept.find( CardPos.dept_id );
  278. if ( mit_id_sum == DeptItem.Dept.end() )
  279. {
  280. DeptItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) );
  281. }
  282. else
  283. {
  284. mit_id_sum->second++;
  285. }
  286. //occupation_level
  287. mit_id_sum = DeptItem.OcptLvl.find( CardPos.level_id );
  288. if ( mit_id_sum == DeptItem.OcptLvl.end() )
  289. {
  290. DeptItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) );
  291. }
  292. else
  293. {
  294. mit_id_sum->second++;
  295. }
  296. //Sum
  297. DeptItem.Sum++;
  298. }
  299. void jsonBuilder::__BuildIDSumMap( const std::map<int, int>& id_sum_map, const char* szName, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Parent )
  300. {
  301. rapidjson::Value Array( rapidjson::kArrayType );
  302. std::map<int, int>::const_iterator mit_id_sum;
  303. for ( mit_id_sum = id_sum_map.begin(); mit_id_sum != id_sum_map.end(); mit_id_sum++ )
  304. {
  305. rapidjson::Value Item( rapidjson::kArrayType );
  306. rapidjson::Value tmpobj( rapidjson::kObjectType );
  307. tmpobj.SetInt( mit_id_sum->first );
  308. Item.PushBack( tmpobj, Allocator );
  309. tmpobj.SetInt( mit_id_sum->second );
  310. Item.PushBack( tmpobj, Allocator );
  311. Array.PushBack( Item, Allocator );
  312. }
  313. Parent.AddMember( rapidjson::StringRef( szName ), Array, Allocator );
  314. }
  315. std::string jsonBuilder::BuildLogin( const _JS_LOGIN_& Login )
  316. {
  317. rapidjson::StringBuffer sb;
  318. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  319. rapidjson::Document doc;
  320. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  321. rapidjson::Value root( rapidjson::kObjectType );
  322. rapidjson::Value child( rapidjson::kObjectType );
  323. rapidjson::Value value( rapidjson::kStringType );
  324. __SetCmd( JSON_CMD_VALUE_LOGIN, root, Allocator );
  325. value.SetString( Login.user_name.c_str(), Allocator );
  326. child.AddMember( JSON_KEY_USERNAME, value, Allocator );
  327. value.SetString( Login.user_password.c_str(), Allocator );
  328. child.AddMember( JSON_KEY_PASSWORD, value, Allocator );
  329. root.AddMember( JSON_ROOT_KEY_DATA, child, Allocator );
  330. root.Accept( writer );
  331. return sb.GetString();
  332. }
  333. std::string jsonBuilder::BuildCardPos( const std::map<uint64_t, _CARD_POS_>& CardPosList )
  334. {
  335. if(CardPosList.empty()){
  336. return "";
  337. }
  338. static bool p2s=false;
  339. p2s=p2s?false:true;
  340. rapidjson::StringBuffer sb;
  341. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  342. rapidjson::Document doc;
  343. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  344. rapidjson::Value root( rapidjson::kObjectType );
  345. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, Allocator );
  346. rapidjson::Value s( rapidjson::kObjectType );
  347. rapidjson::Value v( rapidjson::kObjectType );
  348. rapidjson::Value s_Detail( rapidjson::kArrayType );
  349. rapidjson::Value v_Detail( rapidjson::kArrayType );
  350. rapidjson::Value s_dept( rapidjson::kObjectType );
  351. rapidjson::Value v_dept( rapidjson::kObjectType );
  352. std::map<int, _STAT_DEPT_ITEM_> s_dept_map;
  353. std::map<int, _STAT_DEPT_ITEM_> v_dept_map;
  354. _STAT_DEPT_ITEM_ s_glbl_item;
  355. _STAT_DEPT_ITEM_ v_glbl_item;
  356. std::map<uint64_t, _CARD_POS_>::const_iterator mit_card;
  357. for ( mit_card = CardPosList.begin(); mit_card != CardPosList.end(); mit_card++ )
  358. {
  359. //不显示的卡不往前端推送
  360. //if ( !mit_card->second.display )
  361. //{
  362. // continue;
  363. //}
  364. //if(p2s && mit_card->second.Type == 1)continue;
  365. rapidjson::Value DetailItem;
  366. if ( !__BuildDetail( mit_card->second, Allocator, DetailItem ) )
  367. {
  368. continue;
  369. }
  370. if( mit_card->second.Type == 1)
  371. {
  372. s_Detail.PushBack( DetailItem, Allocator );
  373. __CreateDeptMap( mit_card->second, s_dept_map );
  374. __CreateDeptItem( mit_card->second, s_glbl_item );
  375. }
  376. else //车卡、采煤机等等
  377. {
  378. v_Detail.PushBack( DetailItem, Allocator );
  379. __CreateDeptMap( mit_card->second, v_dept_map );
  380. __CreateDeptItem( mit_card->second, v_glbl_item );
  381. }
  382. }
  383. s.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, s_Detail, Allocator );
  384. v.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, v_Detail, Allocator );
  385. //stat
  386. rapidjson::Value s_stat( rapidjson::kObjectType );
  387. rapidjson::Value v_stat( rapidjson::kObjectType );
  388. if ( __BuildDept( s_dept_map, Allocator, s_dept ) )
  389. {
  390. s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, s_dept, Allocator );
  391. }
  392. if ( __BuildDept( v_dept_map, Allocator, v_dept ) )
  393. {
  394. v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, v_dept, Allocator );
  395. }
  396. //glbl
  397. rapidjson::Value s_glbl( rapidjson::kObjectType );
  398. rapidjson::Value v_glbl( rapidjson::kObjectType );
  399. __BuildDeptItem( s_glbl_item, Allocator, s_glbl );
  400. __BuildDeptItem( v_glbl_item, Allocator, v_glbl );
  401. //s.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator );
  402. //v.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator );
  403. s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator );
  404. v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator );
  405. s.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, s_stat, Allocator );
  406. v.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, v_stat, Allocator );
  407. rapidjson::Value Data( rapidjson::kObjectType );
  408. Data.AddMember( JSON_ROOT_KEY_STATISTIC_STAFF_DATA, s, Allocator );
  409. Data.AddMember( JSON_ROOT_KEY_STATISTIC_VEHICLE_DATA, v, Allocator );
  410. root.AddMember( JSON_ROOT_KEY_DATA, Data, Allocator );
  411. __AddVersion( root, Allocator );
  412. root.Accept( writer );
  413. return sb.GetString();
  414. }
  415. std::string jsonBuilder::build_ios_card_pos(const _CARD_POS_& card)
  416. {
  417. rapidjson::StringBuffer sb;
  418. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  419. rapidjson::Document doc;
  420. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  421. rapidjson::Value root( rapidjson::kObjectType );
  422. // "cmd":
  423. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  424. // "version"
  425. __AddVersion(root, allocator);
  426. // "item"
  427. rapidjson::Value item(rapidjson::kObjectType);
  428. rapidjson::Value value;
  429. value.SetString(std::to_string(card.ID).c_str(), allocator);
  430. item.AddMember("cardid", value, allocator);
  431. item.AddMember("x", card.x, allocator);
  432. item.AddMember("y", card.y, allocator);
  433. item.AddMember("z", card.z, allocator);
  434. item.AddMember("acc", card.m_acc, allocator);
  435. item.AddMember("speed", card.speed, allocator);
  436. item.AddMember("acc_x", card.m_tri_acc[0], allocator);
  437. item.AddMember("acc_y", card.m_tri_acc[1], allocator);
  438. item.AddMember("acc_z", card.m_tri_acc[2], allocator);
  439. item.AddMember("ang_x", card.m_tri_ang[0], allocator);
  440. item.AddMember("ang_y", card.m_tri_ang[1], allocator);
  441. item.AddMember("ang_z", card.m_tri_ang[2], allocator);
  442. item.AddMember("steps", card.m_walking_step, allocator);
  443. item.AddMember("jump", card.m_jump_count, allocator);
  444. item.AddMember("hang_time", card.m_hang_time, allocator);
  445. item.AddMember("hang_height", card.m_hang_height, allocator);
  446. /*rapidjson::Value data(rapidjson::kObjectType);
  447. data.AddMember("card_type", 1 , allocator);
  448. data.AddMember("cardid", "10", allocator);
  449. data.AddMember("x", 10.0 , allocator);
  450. data.AddMember("y", 20.0 , allocator);
  451. data.AddMember("z", 30.0, allocator);
  452. data.AddMember("acc" , 10.0 , allocator);
  453. data.AddMember("speed", 3.5 , allocator);
  454. data.AddMember("acc_x", 0.1, allocator);
  455. data.AddMember("acc_y", 0.2 , allocator);
  456. data.AddMember("acc_z", 0.3 , allocator);
  457. data.AddMember("ang_x", 0.4, allocator);
  458. data.AddMember("ang_y", 0.5 , allocator);
  459. data.AddMember("ang_z", 0.6 , allocator);
  460. data.AddMember("steps", 7, allocator);
  461. data.AddMember("jump", 8 , allocator);
  462. data.AddMember("hang_time", 3 , allocator);
  463. data.AddMember("hang_height", 4, allocator);
  464. */
  465. root.AddMember("data", item, allocator);
  466. root.Accept(writer);
  467. return sb.GetString();
  468. }
  469. std::string jsonBuilder::build_tmp_card_pos(const std::map<uint64_t,_CARD_POS_>& cards)
  470. {
  471. rapidjson::StringBuffer sb;
  472. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  473. rapidjson::Document doc;
  474. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  475. rapidjson::Value root(rapidjson::kObjectType);
  476. // "cmd":
  477. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  478. // "data"
  479. rapidjson::Value item(rapidjson::kArrayType);
  480. for(auto it = cards.begin(); it != cards.end(); ++it)
  481. {
  482. rapidjson::Value elem(rapidjson::kArrayType);
  483. rapidjson::Value tmp_obj(rapidjson::kObjectType);
  484. // 卡号
  485. tmp_obj.SetString(__FmtCardID(it->second).c_str(), allocator);
  486. elem.PushBack(tmp_obj, allocator);
  487. // x坐标
  488. tmp_obj.SetDouble(it->second.x);
  489. elem.PushBack(tmp_obj, allocator);
  490. // y坐标
  491. tmp_obj.SetDouble(it->second.y);
  492. elem.PushBack(tmp_obj, allocator);
  493. // z坐标
  494. tmp_obj.SetDouble(it->second.z);
  495. elem.PushBack(tmp_obj, allocator);
  496. // 时间戳
  497. tmp_obj.SetDouble(it->second.rec_time);
  498. elem.PushBack(tmp_obj, allocator);
  499. // 加速度
  500. tmp_obj.SetDouble(it->second.m_acc);
  501. elem.PushBack(tmp_obj, allocator);
  502. // 速度
  503. tmp_obj.SetDouble(it->second.speed);
  504. elem.PushBack(tmp_obj, allocator);
  505. item.PushBack(elem, allocator);
  506. }
  507. root.AddMember("data", item, allocator);
  508. // "version"
  509. __AddVersion(root, allocator);
  510. root.Accept(writer);
  511. return sb.GetString();
  512. }
  513. std::string jsonBuilder::BuildSpecialAreaProcess( const _BASE_CARD_ & stCard )
  514. {
  515. rapidjson::StringBuffer sb;
  516. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  517. rapidjson::Document doc;
  518. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  519. rapidjson::Value root( rapidjson::kObjectType );
  520. __SetCmd( JSON_CMD_VALUE_SPECIAL_AREA_UP_MINE, root, Allocator );
  521. rapidjson::Value CardItem( rapidjson::kObjectType );
  522. rapidjson::Value CardList( rapidjson::kArrayType );
  523. __BuildBaseCardItem( stCard, Allocator, CardItem );
  524. CardList.PushBack( CardItem, Allocator );
  525. root.AddMember( JSON_ROOT_KEY_DATA, CardList, Allocator );
  526. root.Accept( writer );
  527. return sb.GetString();
  528. }
  529. bool jsonBuilder::ParseCall_Card_Req( const std::string& JasonText, _JS_CALL_CARD_REQ_& CallCardReq, std::string& Error )
  530. {
  531. rapidjson::Document doc;
  532. if ( doc.Parse( JasonText.c_str() ).HasParseError() )
  533. {
  534. Error = "Failed to parse jason text!";
  535. return false;
  536. }
  537. rapidjson::Value::MemberIterator itValue;
  538. itValue = doc.FindMember( "data" );
  539. if ( itValue == doc.MemberEnd() )
  540. {
  541. Error = "Failed to find member: data!";
  542. return false;
  543. }
  544. rapidjson::Value Data;
  545. rapidjson::Document::AllocatorType allocator;
  546. Data.CopyFrom( itValue->value, allocator );
  547. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TYPE ) )
  548. {
  549. Error = "Failed to find value: ";
  550. Error += JSON_KEY_CALL_CARD_CALL_TYPE;
  551. return false;
  552. }
  553. CallCardReq.call_type_id = Data[JSON_KEY_CALL_CARD_CALL_TYPE].GetInt();
  554. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TIME_OUT ) )
  555. {
  556. Error = "Failed to find value: ";
  557. Error += JSON_KEY_CALL_CARD_CALL_TIME_OUT;
  558. return false;
  559. }
  560. CallCardReq.call_time_out = Data[JSON_KEY_CALL_CARD_CALL_TIME_OUT].GetInt();
  561. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_LEVEL ) )
  562. {
  563. Error = "Failed to find value: ";
  564. Error += JSON_KEY_CALL_CARD_CALL_LEVEL;
  565. return false;
  566. }
  567. CallCardReq.call_time_interval = Data[JSON_KEY_CALL_CARD_CALL_LEVEL].GetInt();
  568. if ( !Data.HasMember( JSON_KEY_CALL_CARD_USER_NAME ) )
  569. {
  570. Error = "Failed to find value: ";
  571. Error += JSON_KEY_CALL_CARD_USER_NAME;
  572. return false;
  573. }
  574. CallCardReq.user_name = Data[JSON_KEY_CALL_CARD_USER_NAME].GetString();
  575. //data::stations
  576. if ( !Data.HasMember( JSON_KEY_CALL_CARD_STATIONS ) )
  577. {
  578. Error = "Failed to find value: ";
  579. Error += JSON_KEY_CALL_CARD_STATIONS;
  580. return false;
  581. }
  582. rapidjson::Value & stations = Data[JSON_KEY_CALL_CARD_STATIONS];
  583. if ( !stations.IsArray() )
  584. {
  585. Error = "data::stations is not a valid array!";
  586. return false;
  587. }
  588. for ( int i = 0; i < (int)stations.Size(); ++i )
  589. {
  590. rapidjson::Value & v = stations[i];
  591. if ( !v.IsObject() )
  592. {
  593. char szError[1024] = { 0 };
  594. sprintf( szError, "data::stations[%d] is not a object!", i );
  595. Error = szError;
  596. return false;
  597. }
  598. _JS_STATION_ Station;
  599. if ( !v.HasMember( JSON_KEY_CALL_CARD_STATION_ID ) && v[JSON_KEY_CALL_CARD_STATION_ID].IsInt() )
  600. {
  601. Error = "Error, failed to found value: ";
  602. Error += JSON_KEY_CALL_CARD_STATION_ID;
  603. return false;
  604. }
  605. Station.stationid = v[JSON_KEY_CALL_CARD_STATION_ID].GetInt();
  606. CallCardReq.stations.push_back( Station );
  607. }
  608. //data::cards
  609. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CARDS ) )
  610. {
  611. Error = "Failed to find value: ";
  612. Error += JSON_KEY_CALL_CARD_CARDS;
  613. return false;
  614. }
  615. rapidjson::Value & cards = Data[JSON_KEY_CALL_CARD_CARDS];
  616. for ( int i = 0; i < (int)cards.Size(); ++i )
  617. {
  618. rapidjson::Value & v = cards[i];
  619. if ( !v.IsObject() )
  620. {
  621. char szError[1024] = { 0 };
  622. sprintf( szError, "data::cards[%d] is not a object!", i );
  623. Error = szError;
  624. return false;
  625. }
  626. _JS_CARD_ Card;
  627. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_ID ) && v[JSON_KEY_CALL_CARD_CARD_ID].IsInt() )
  628. {
  629. Error = "Error, failed to found value: ";
  630. Error += JSON_KEY_CALL_CARD_CARD_ID;
  631. return false;
  632. }
  633. Card.cardid = v[JSON_KEY_CALL_CARD_CARD_ID].GetInt();
  634. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_TYPE_ID ) && v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].IsInt() )
  635. {
  636. Error = "Error, failed to found value: ";
  637. Error += JSON_KEY_CALL_CARD_CARD_TYPE_ID;
  638. return false;
  639. }
  640. Card.cardtype = v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].GetInt();
  641. CallCardReq.cards.push_back( Card );
  642. }
  643. return true;
  644. }
  645. std::string jsonBuilder::build_traffic_light(const std::vector<light_state> lights)
  646. {
  647. if(lights.size() == 0){
  648. return "";
  649. }
  650. rapidjson::StringBuffer sb;
  651. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  652. rapidjson::Document doc;
  653. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  654. rapidjson::Value root(rapidjson::kObjectType );
  655. __SetCmd(JSON_KEY_CALL_LIGHT_LIGHT_STATE, root, allocator);
  656. rapidjson::Value data(rapidjson::kArrayType);
  657. for(auto it : lights){
  658. rapidjson::Value elem(rapidjson::kArrayType);
  659. rapidjson::Value tmp_object(rapidjson::kObjectType);
  660. tmp_object.SetInt(it.m_group_id);
  661. elem.PushBack(tmp_object, allocator);
  662. tmp_object.SetInt(it.m_light_id);
  663. elem.PushBack(tmp_object, allocator);
  664. tmp_object.SetInt(it.m_light_state);
  665. elem.PushBack(tmp_object, allocator);
  666. tmp_object.SetString(it.m_card_id.c_str(), allocator);
  667. elem.PushBack(tmp_object, allocator);
  668. data.PushBack(elem, allocator);
  669. }
  670. root.AddMember("data", data, allocator);
  671. __AddVersion(root, allocator);
  672. root.Accept(writer);
  673. return sb.GetString();
  674. }
  675. /*
  676. * 构建设备状态json
  677. *
  678. * 参数
  679. * const std::vector<device_state> vtd 设备状态列表
  680. *
  681. * 返回值
  682. * 设备状态json
  683. *
  684. * */
  685. std::string jsonBuilder::build_device_state(const std::vector<device_state> vtd)
  686. {
  687. if(vtd.size() == 0){
  688. return "";
  689. }
  690. rapidjson::StringBuffer sb;
  691. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  692. rapidjson::Document doc;
  693. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  694. rapidjson::Value root(rapidjson::kObjectType );
  695. __SetCmd(JSON_CMD_VALUE_DEV_STATE, root, allocator);
  696. rapidjson::Value data(rapidjson::kArrayType);
  697. for(auto it : vtd){
  698. rapidjson::Value elem(rapidjson::kArrayType);
  699. rapidjson::Value tmp_object(rapidjson::kObjectType);
  700. tmp_object.SetInt(it.m_id);
  701. elem.PushBack(tmp_object, allocator);
  702. tmp_object.SetInt(it.m_type);
  703. elem.PushBack(tmp_object, allocator);
  704. tmp_object.SetInt(it.m_state);
  705. elem.PushBack(tmp_object, allocator);
  706. tmp_object.SetDouble(it.m_cur_time);
  707. elem.PushBack(tmp_object, allocator);
  708. data.PushBack(elem, allocator);
  709. }
  710. root.AddMember("data", data, allocator);
  711. __AddVersion(root, allocator);
  712. root.Accept(writer);
  713. return sb.GetString();
  714. }
  715. /*
  716. * 构建tof_data的json数据
  717. * */
  718. std::string jsonBuilder::build_tof_data(const std::vector<tof_data>& vtd)
  719. {
  720. if(vtd.size() == 0){
  721. return "";
  722. }
  723. rapidjson::StringBuffer sb;
  724. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  725. rapidjson::Document doc;
  726. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  727. rapidjson::Value root(rapidjson::kObjectType);
  728. __SetCmd("tof_data", root, allocator);
  729. __AddVersion(root, allocator);
  730. rapidjson::Value data(rapidjson::kArrayType);
  731. for(const auto& it : vtd){
  732. rapidjson::Value elem(rapidjson::kArrayType);
  733. rapidjson::Value value(rapidjson::kObjectType);
  734. log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  735. // 卡号
  736. value.SetString(it.cid.c_str(), it.cid.length(), allocator);
  737. elem.PushBack(value, allocator);
  738. // 天线1间测距距离
  739. value.SetDouble(it.ant_dist[0]);
  740. elem.PushBack(value, allocator);
  741. // 天线2的测距距离
  742. value.SetDouble(it.ant_dist[1]);
  743. elem.PushBack(value, allocator);
  744. // 天线12间距离
  745. value.SetDouble(it.ant_diff);
  746. elem.PushBack(value, allocator);
  747. // 基站号
  748. value.SetInt(it.sid);
  749. elem.PushBack(value, allocator);
  750. // // 一维位置
  751. // value.SetDouble(it.y);
  752. // elem.PushBack(value, allocator);
  753. // 接收时间
  754. value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator);
  755. elem.PushBack(value, allocator);
  756. data.PushBack(elem, allocator);
  757. }
  758. root.AddMember("data", data, allocator);
  759. root.Accept(writer);
  760. return sb.GetString();
  761. }
  762. /*
  763. * 构建tof_data的json数据
  764. * */
  765. std::string jsonBuilder::build_tof_data_oneAntenna(const std::vector<tof_data_oneAntenna>& vtd)
  766. {
  767. if (vtd.size() == 0) {
  768. return "";
  769. }
  770. rapidjson::StringBuffer sb;
  771. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  772. rapidjson::Document doc;
  773. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  774. rapidjson::Value root(rapidjson::kObjectType);
  775. __SetCmd("tof_data_oneAntenna", root, allocator);
  776. __AddVersion(root, allocator);
  777. rapidjson::Value data(rapidjson::kArrayType);
  778. for (const auto& it : vtd) {
  779. rapidjson::Value elem(rapidjson::kArrayType);
  780. rapidjson::Value value(rapidjson::kObjectType);
  781. log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  782. // 卡号
  783. value.SetString(it.cid.c_str(), it.cid.length(), allocator);
  784. elem.PushBack(value, allocator);
  785. for (auto iterSiteDist = it.map_site_id_dist.begin(); iterSiteDist != it.map_site_id_dist.end(); iterSiteDist++)
  786. {
  787. // 距离
  788. value.SetDouble(iterSiteDist->second);
  789. elem.PushBack(value, allocator);
  790. // 基站号
  791. value.SetInt(iterSiteDist->first);
  792. elem.PushBack(value, allocator);
  793. }
  794. // // 距离
  795. // value.SetDouble(it.dist2);
  796. // elem.PushBack(value, allocator);
  797. //
  798. // // 基站号
  799. // value.SetInt(it.sid2);
  800. // elem.PushBack(value, allocator);
  801. //
  802. // if (it.sid3 != 0)
  803. // {
  804. // // 距离
  805. // value.SetDouble(it.dist3);
  806. // elem.PushBack(value, allocator);
  807. //
  808. // // 基站号
  809. // value.SetInt(it.sid3);
  810. // elem.PushBack(value, allocator);
  811. // }
  812. //
  813. // if (it.sid4 != 0)
  814. // {
  815. // // 距离
  816. // value.SetDouble(it.dist4);
  817. // elem.PushBack(value, allocator);
  818. //
  819. // // 基站号
  820. // value.SetInt(it.sid4);
  821. // elem.PushBack(value, allocator);
  822. // }
  823. // 接收时间
  824. value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator);
  825. elem.PushBack(value, allocator);
  826. data.PushBack(elem, allocator);
  827. }
  828. root.AddMember("data", data, allocator);
  829. root.Accept(writer);
  830. return sb.GetString();
  831. }
  832. /*
  833. * 构建tof_data第三方接口的json数据
  834. * */
  835. std::string jsonBuilder::build_tof_data_oneAntenna_third_part(const std::vector<tof_data_oneAntenna>& vtd)
  836. {
  837. if (vtd.size() == 0) {
  838. return "";
  839. }
  840. rapidjson::StringBuffer sb;
  841. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  842. rapidjson::Document doc;
  843. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  844. rapidjson::Value root(rapidjson::kObjectType);
  845. __SetCmd("tof_data_doubleAntenna", root, allocator);
  846. __AddVersion(root, allocator);
  847. rapidjson::Value data(rapidjson::kArrayType);
  848. for (const auto& it : vtd) {
  849. rapidjson::Value elem(rapidjson::kArrayType);
  850. rapidjson::Value value(rapidjson::kObjectType);
  851. log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  852. // 卡号
  853. value.SetString(it.cid.c_str(), it.cid.length(), allocator);
  854. elem.PushBack(value, allocator);
  855. // x坐标
  856. value.SetDouble(it.x);
  857. elem.PushBack(value, allocator);
  858. // y坐标
  859. value.SetDouble(it.y);
  860. elem.PushBack(value, allocator);
  861. int index = 0;
  862. for (auto iterSiteDist = it.map_site_id_dist.begin(); iterSiteDist != it.map_site_id_dist.end(); iterSiteDist++)
  863. {
  864. if (0 == index)
  865. {
  866. // 基站号
  867. value.SetInt(iterSiteDist->first / 10);
  868. elem.PushBack(value, allocator);
  869. }
  870. // 距离
  871. value.SetDouble(iterSiteDist->second);
  872. elem.PushBack(value, allocator);
  873. index++;
  874. }
  875. // // 距离
  876. // value.SetDouble(it.dist2);
  877. // elem.PushBack(value, allocator);
  878. //
  879. // // 基站号
  880. // value.SetInt(it.sid2);
  881. // elem.PushBack(value, allocator);
  882. //
  883. // if (it.sid3 != 0)
  884. // {
  885. // // 距离
  886. // value.SetDouble(it.dist3);
  887. // elem.PushBack(value, allocator);
  888. //
  889. // // 基站号
  890. // value.SetInt(it.sid3);
  891. // elem.PushBack(value, allocator);
  892. // }
  893. //
  894. // if (it.sid4 != 0)
  895. // {
  896. // // 距离
  897. // value.SetDouble(it.dist4);
  898. // elem.PushBack(value, allocator);
  899. //
  900. // // 基站号
  901. // value.SetInt(it.sid4);
  902. // elem.PushBack(value, allocator);
  903. // }
  904. // 是否运动
  905. value.SetInt(0);
  906. elem.PushBack(value, allocator);
  907. // 接收时间
  908. value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator);
  909. elem.PushBack(value, allocator);
  910. data.PushBack(elem, allocator);
  911. }
  912. root.AddMember("data", data, allocator);
  913. root.Accept(writer);
  914. return sb.GetString();
  915. }
  916. std::string jsonBuilder::build_tof_data_oneAntenna_third_part2(int site_id,int card_id,float dist)
  917. {
  918. //if (vtd.size() == 0) {
  919. // return "";
  920. //}
  921. rapidjson::StringBuffer sb;
  922. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  923. rapidjson::Document doc;
  924. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  925. rapidjson::Value root(rapidjson::kObjectType);
  926. __SetCmd("tof_data_doubleAntenna", root, allocator);
  927. __AddVersion(root, allocator);
  928. rapidjson::Value data(rapidjson::kArrayType);
  929. rapidjson::Value elem(rapidjson::kArrayType);
  930. rapidjson::Value value(rapidjson::kObjectType);
  931. //log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  932. char buf[33];
  933. sprintf(buf, "%d", card_id);
  934. std::string cid = buf;
  935. // 卡号
  936. value.SetString(cid.c_str(), cid.length(), allocator);
  937. elem.PushBack(value, allocator);
  938. // x坐标
  939. value.SetDouble(dist);
  940. elem.PushBack(value, allocator);
  941. // y坐标
  942. value.SetDouble(0);
  943. elem.PushBack(value, allocator);
  944. int index = 0;
  945. // 基站号
  946. value.SetInt(site_id);
  947. elem.PushBack(value, allocator);
  948. // 是否运动
  949. value.SetInt(0);
  950. elem.PushBack(value, allocator);
  951. // 接收时间
  952. std::string now = tool_time::to_str_ex(tool_time::now_to_ms());
  953. std::string cur_time = now;
  954. value.SetString(cur_time.c_str(), cur_time.length(), allocator);
  955. elem.PushBack(value, allocator);
  956. data.PushBack(elem, allocator);
  957. root.AddMember("data", data, allocator);
  958. root.Accept(writer);
  959. return sb.GetString();
  960. }
  961. /*
  962. * 构建solid_ball_data的json数据
  963. * */
  964. std::string jsonBuilder::build_sb_data(const std::vector<sb_data>& vtd)
  965. {
  966. if(vtd.size() == 0){
  967. return "";
  968. }
  969. rapidjson::StringBuffer sb;
  970. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  971. rapidjson::Document doc;
  972. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  973. rapidjson::Value root(rapidjson::kObjectType);
  974. __SetCmd("sb_raw_data", root, allocator);
  975. __AddVersion(root, allocator);
  976. rapidjson::Value data(rapidjson::kArrayType);
  977. for(const auto& it : vtd){
  978. rapidjson::Value elem(rapidjson::kArrayType);
  979. rapidjson::Value value(rapidjson::kObjectType);
  980. log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  981. value.SetString(it.cid.c_str(), it.cid.length(), allocator);
  982. elem.PushBack(value, allocator);
  983. value.SetDouble(it.x);
  984. elem.PushBack(value, allocator);
  985. value.SetDouble(it.y);
  986. elem.PushBack(value, allocator);
  987. value.SetDouble(it.z);
  988. elem.PushBack(value, allocator);
  989. value.SetDouble(it.speed);
  990. elem.PushBack(value, allocator);
  991. value.SetString(it.site_info.c_str(), it.site_info.length(), allocator);
  992. elem.PushBack(value, allocator);
  993. value.SetDouble(it.acc);
  994. elem.PushBack(value, allocator);
  995. value.SetDouble(it.m_acc_data[0]);
  996. elem.PushBack(value, allocator);
  997. value.SetDouble(it.m_acc_data[1]);
  998. elem.PushBack(value, allocator);
  999. value.SetDouble(it.m_acc_data[2]);
  1000. elem.PushBack(value, allocator);
  1001. value.SetDouble(it.ang);
  1002. elem.PushBack(value, allocator);
  1003. value.SetDouble(it.m_ang_data[0]);
  1004. elem.PushBack(value, allocator);
  1005. value.SetDouble(it.m_ang_data[1]);
  1006. elem.PushBack(value, allocator);
  1007. value.SetDouble(it.m_ang_data[2]);
  1008. elem.PushBack(value, allocator);
  1009. value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator);
  1010. elem.PushBack(value, allocator);
  1011. data.PushBack(elem, allocator);
  1012. }
  1013. root.AddMember("data", data, allocator);
  1014. root.Accept(writer);
  1015. return sb.GetString();
  1016. }
  1017. // 2d tdoa's json
  1018. std::string jsonBuilder::build_tdoa_2d_data(const std::vector<tdoa_2d_data>& vtd)
  1019. {
  1020. if (vtd.size() == 0) {
  1021. return "";
  1022. }
  1023. rapidjson::StringBuffer sb;
  1024. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  1025. rapidjson::Document doc;
  1026. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  1027. rapidjson::Value root(rapidjson::kObjectType);
  1028. __SetCmd("tdoa_2d_raw_data", root, allocator);
  1029. __AddVersion(root, allocator);
  1030. rapidjson::Value data(rapidjson::kArrayType);
  1031. for (const auto& it : vtd) {
  1032. rapidjson::Value elem(rapidjson::kArrayType);
  1033. rapidjson::Value value(rapidjson::kObjectType);
  1034. log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  1035. value.SetString(it.cid.c_str(), it.cid.length(), allocator);
  1036. elem.PushBack(value, allocator);
  1037. value.SetDouble(it.x);
  1038. elem.PushBack(value, allocator);
  1039. value.SetDouble(it.y);
  1040. elem.PushBack(value, allocator);
  1041. value.SetDouble(it.d1);
  1042. elem.PushBack(value, allocator);
  1043. value.SetDouble(it.d2);
  1044. elem.PushBack(value, allocator);
  1045. value.SetDouble(it.d3);
  1046. elem.PushBack(value, allocator);
  1047. value.SetDouble(it.acc);
  1048. elem.PushBack(value, allocator);
  1049. value.SetDouble(it.m_acc_data[0]);
  1050. elem.PushBack(value, allocator);
  1051. value.SetDouble(it.m_acc_data[1]);
  1052. elem.PushBack(value, allocator);
  1053. value.SetDouble(it.m_acc_data[2]);
  1054. elem.PushBack(value, allocator);
  1055. value.SetDouble(it.ang);
  1056. elem.PushBack(value, allocator);
  1057. value.SetDouble(it.m_ang_data[0]);
  1058. elem.PushBack(value, allocator);
  1059. value.SetDouble(it.m_ang_data[1]);
  1060. elem.PushBack(value, allocator);
  1061. value.SetDouble(it.m_ang_data[2]);
  1062. elem.PushBack(value, allocator);
  1063. value.SetDouble(it.dx_v);
  1064. elem.PushBack(value, allocator);
  1065. value.SetDouble(it.dy_v);
  1066. elem.PushBack(value, allocator);
  1067. value.SetInt(it.move_state);
  1068. elem.PushBack(value, allocator);
  1069. // value.SetString(it.site_info.c_str(), it.site_info.length(), allocator);
  1070. // elem.PushBack(value, allocator);
  1071. value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator);
  1072. elem.PushBack(value, allocator);
  1073. data.PushBack(elem, allocator);
  1074. }
  1075. root.AddMember("data", data, allocator);
  1076. root.Accept(writer);
  1077. return sb.GetString();
  1078. }
  1079. // 3d tof's json
  1080. std::string jsonBuilder::build_tof_3d_data(const std::vector<tof_3d_data>& vtd)
  1081. {
  1082. if(vtd.size() == 0){
  1083. return "";
  1084. }
  1085. rapidjson::StringBuffer sb;
  1086. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  1087. rapidjson::Document doc;
  1088. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  1089. rapidjson::Value root(rapidjson::kObjectType);
  1090. __SetCmd("pdoa_3d_raw_data", root, allocator);
  1091. __AddVersion(root, allocator);
  1092. rapidjson::Value data(rapidjson::kArrayType);
  1093. for(const auto& it : vtd){
  1094. rapidjson::Value elem(rapidjson::kArrayType);
  1095. rapidjson::Value value(rapidjson::kObjectType);
  1096. log_info("[error] cid = %s, length = %d", it.cid.c_str(), it.cid.length());
  1097. value.SetString(it.cid.c_str(), it.cid.length(), allocator);
  1098. elem.PushBack(value, allocator);
  1099. value.SetDouble(it.x);
  1100. elem.PushBack(value, allocator);
  1101. value.SetDouble(it.y);
  1102. elem.PushBack(value, allocator);
  1103. value.SetDouble(it.z);
  1104. elem.PushBack(value, allocator);
  1105. value.SetDouble(it.acc);
  1106. elem.PushBack(value, allocator);
  1107. value.SetDouble(it.m_acc_data[0]);
  1108. elem.PushBack(value, allocator);
  1109. value.SetDouble(it.m_acc_data[1]);
  1110. elem.PushBack(value, allocator);
  1111. value.SetDouble(it.m_acc_data[2]);
  1112. elem.PushBack(value, allocator);
  1113. value.SetDouble(it.ang);
  1114. elem.PushBack(value, allocator);
  1115. value.SetDouble(it.m_ang_data[0]);
  1116. elem.PushBack(value, allocator);
  1117. value.SetDouble(it.m_ang_data[1]);
  1118. elem.PushBack(value, allocator);
  1119. value.SetDouble(it.m_ang_data[2]);
  1120. elem.PushBack(value, allocator);
  1121. value.SetDouble(it.dx_v);
  1122. elem.PushBack(value, allocator);
  1123. value.SetDouble(it.dy_v);
  1124. elem.PushBack(value, allocator);
  1125. value.SetDouble(it.dz_v);
  1126. elem.PushBack(value, allocator);
  1127. value.SetInt(it.move_state);
  1128. elem.PushBack(value, allocator);
  1129. value.SetString(it.site_info.c_str(), it.site_info.length(), allocator);
  1130. elem.PushBack(value, allocator);
  1131. value.SetString(it.cur_time.c_str(), it.cur_time.length(), allocator);
  1132. elem.PushBack(value, allocator);
  1133. data.PushBack(elem, allocator);
  1134. }
  1135. root.AddMember("data", data, allocator);
  1136. root.Accept(writer);
  1137. return sb.GetString();
  1138. }
  1139. }