jsonBuilder.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. namespace YA
  9. {
  10. jsonBuilder::jsonBuilder()
  11. {
  12. }
  13. jsonBuilder::~jsonBuilder()
  14. {
  15. }
  16. std::string jsonBuilder::__FmtCardID( const _BASE_CARD_ & CardPos )
  17. {
  18. char szID[32] = { 0 };
  19. sprintf( szID, "%03d%.10d", CardPos.Type, CardPos.ID );
  20. return szID;
  21. }
  22. void jsonBuilder::__AddVersion( rapidjson::Value& root, rapidjson::Document::AllocatorType& Allocator )
  23. {
  24. rapidjson::Value value( rapidjson::kStringType );
  25. value.SetString( INTERFACE_VERSION_1_4, Allocator );
  26. root.AddMember( JSON_ROOT_KEY_VERSION, value, Allocator );
  27. }
  28. void jsonBuilder::__SetCmd( const char* szCmd, rapidjson::Value & root, rapidjson::Document::AllocatorType & Allocator )
  29. {
  30. rapidjson::Value value( rapidjson::kStringType );
  31. value.SetString( szCmd, Allocator );
  32. root.AddMember( JSON_ROOT_KEY_CMD, value, Allocator );
  33. }
  34. #if 0
  35. detail=
  36. [
  37. //0 卡号
  38. //1 x坐标
  39. //2 y坐标
  40. //3 入井时间戳
  41. //4 接收时间戳
  42. //5 工作时长
  43. //6 地图编号
  44. //7 部门编号
  45. //8 状态
  46. //9 运行状态
  47. //10 业务状态
  48. //11 速度
  49. //12 area_info_array
  50. ]
  51. area_info_array=[
  52. area_info,
  53. area_info,
  54. area_info,
  55. area_info
  56. ]
  57. area_info=[
  58. //0 区域编号
  59. //1 地标编号
  60. //2 地标方向
  61. //3 距离地标的距离
  62. //4 进入区域时间戳
  63. ]
  64. #endif
  65. bool jsonBuilder::__BuildDetail( const _CARD_POS_ & CardPos, rapidjson::Document::AllocatorType& Allocator, rapidjson::Value & DetailItem )
  66. {
  67. rapidjson::Value Array( rapidjson::kArrayType );
  68. rapidjson::Value tmp_object( rapidjson::kObjectType );
  69. //0 ID
  70. tmp_object.SetString( __FmtCardID( CardPos ).c_str(), Allocator );
  71. Array.PushBack( tmp_object, Allocator );
  72. //1 x
  73. tmp_object.SetDouble( CardPos.x );
  74. Array.PushBack( tmp_object, Allocator );
  75. //2 y
  76. tmp_object.SetDouble( CardPos.y );
  77. Array.PushBack( tmp_object, Allocator );
  78. //3 入井时间戳
  79. tmp_object.SetDouble( CardPos.down_time );
  80. Array.PushBack( tmp_object, Allocator );
  81. //4 进入区域时间戳
  82. //tmp_object.SetDouble( CardPos.enter_area_time );
  83. //Array.PushBack( tmp_object, Allocator );
  84. //5 最后接收时间戳
  85. tmp_object.SetDouble( CardPos.rec_time );
  86. Array.PushBack( tmp_object, Allocator );
  87. //6 工作时长
  88. tmp_object.SetDouble( CardPos.work_time );
  89. Array.PushBack( tmp_object, Allocator );
  90. //7 地图编号
  91. tmp_object.SetInt( CardPos.map_id );
  92. Array.PushBack( tmp_object, Allocator );
  93. //8 区域编号
  94. //tmp_object.SetInt( CardPos.area_id );
  95. //Array.PushBack( tmp_object, Allocator );
  96. //9 部门编号
  97. tmp_object.SetInt( CardPos.dept_id );
  98. Array.PushBack( tmp_object, Allocator );
  99. //10 电量状态
  100. tmp_object.SetInt( CardPos.stat );
  101. Array.PushBack( tmp_object, Allocator );
  102. //11 运行状态
  103. tmp_object.SetInt( CardPos.running_stat );
  104. Array.PushBack( tmp_object, Allocator );
  105. //12 业务状态
  106. tmp_object.SetInt( CardPos.biz_stat );
  107. Array.PushBack( tmp_object, Allocator );
  108. //13 速度
  109. tmp_object.SetDouble( CardPos.speed );
  110. Array.PushBack( tmp_object, Allocator );
  111. //14 地标编号
  112. //tmp_object.SetInt( CardPos.landmark_id );
  113. //Array.PushBack( tmp_object, Allocator );
  114. //15 地标方向
  115. //tmp_object.SetInt( CardPos.lm_direction );
  116. //Array.PushBack( tmp_object, Allocator );
  117. //16 距离地标的距离
  118. //tmp_object.SetDouble( CardPos.landmark_dis );
  119. //Array.PushBack( tmp_object, Allocator );
  120. //17 级别编号
  121. tmp_object.SetInt( CardPos.level_id );
  122. Array.PushBack( tmp_object, Allocator );
  123. //18 车辆当天出勤的标识
  124. tmp_object.SetInt( CardPos.is_on_duty );
  125. Array.PushBack( tmp_object, Allocator );
  126. //log_debug("websocket..%d",CardPos.area_info.size());
  127. rapidjson::Value Array_1( rapidjson::kArrayType );
  128. for(const auto &a:CardPos.area_info)
  129. {
  130. rapidjson::Value _DetailItem;
  131. rapidjson::Value Array_2( rapidjson::kArrayType );
  132. const auto lm = a.second;
  133. //8 区域编号
  134. tmp_object.SetInt(std::get<0>(lm));
  135. Array_2.PushBack( tmp_object, Allocator );
  136. //14 地标编号
  137. tmp_object.SetInt(std::get<1>(lm));
  138. Array_2.PushBack( tmp_object, Allocator );
  139. //15 地标方向
  140. tmp_object.SetInt(std::get<2>(lm));
  141. Array_2.PushBack( tmp_object, Allocator );
  142. //16 距离地标的距离
  143. tmp_object.SetDouble(std::get<3>(lm));
  144. Array_2.PushBack( tmp_object, Allocator );
  145. //5 最后接收时间戳
  146. tmp_object.SetDouble(std::get<4>(lm));
  147. Array_2.PushBack( tmp_object, Allocator );
  148. _DetailItem=Array_2;
  149. Array_1.PushBack(_DetailItem, Allocator);
  150. }
  151. DetailItem = Array_1;
  152. Array.PushBack(DetailItem, Allocator );
  153. DetailItem = Array;
  154. return true;
  155. }
  156. bool jsonBuilder::__BuildDept( const std::map<int, _STAT_DEPT_ITEM_>& dept_map, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Dept )
  157. {
  158. std::map<int, _STAT_DEPT_ITEM_>::const_iterator mit_dept;
  159. for ( mit_dept = dept_map.begin(); mit_dept != dept_map.end(); mit_dept++ )
  160. {
  161. rapidjson::Value DeptItem( rapidjson::kObjectType );
  162. __BuildDeptItem( mit_dept->second, Allocator, DeptItem );
  163. rapidjson::Value dept_id( rapidjson::kObjectType );
  164. char szNum[20] = { 0 };
  165. //itoa( mit_dept->second.DeptID, szNum, 10 );
  166. sprintf(szNum,"%d",mit_dept->second.DeptID);
  167. dept_id.SetString( szNum, strlen( szNum ), Allocator );
  168. Dept.AddMember( dept_id, DeptItem, Allocator );
  169. }
  170. return true;
  171. }
  172. void jsonBuilder::__BuildDeptItem( const _STAT_DEPT_ITEM_ & st_dept_item, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & DeptItem )
  173. {
  174. //Area
  175. __BuildIDSumMap( st_dept_item.Area, JSON_ROOT_KEY_STATISTIC_AREA, Allocator, DeptItem );
  176. //Dept
  177. __BuildIDSumMap( st_dept_item.Dept, JSON_ROOT_KEY_STATISTIC_DEPT, Allocator, DeptItem );
  178. //occupation_level
  179. __BuildIDSumMap( st_dept_item.OcptLvl, JSON_ROOT_KEY_STATISTIC_LEVEL, Allocator, DeptItem );
  180. //sum
  181. rapidjson::Value Sum( rapidjson::kObjectType );
  182. Sum.SetInt( st_dept_item.Sum );
  183. DeptItem.AddMember( JSON_ROOT_KEY_STATISTIC_SUM, Sum, Allocator );
  184. }
  185. void jsonBuilder::__BuildBaseCardItem( const _BASE_CARD_ & Card, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & CardItem )
  186. {
  187. rapidjson::Value TmpObj( rapidjson::kObjectType );
  188. rapidjson::Value CardObj( rapidjson::kArrayType );
  189. //0 卡号
  190. TmpObj.SetString( __FmtCardID( Card ).c_str(), Allocator );
  191. CardObj.PushBack( TmpObj, Allocator );
  192. //1 x坐标
  193. TmpObj.SetDouble( Card.x );
  194. CardObj.PushBack( TmpObj, Allocator );
  195. //2 y坐标
  196. TmpObj.SetDouble( Card.y );
  197. CardObj.PushBack( TmpObj, Allocator );
  198. //3 入井时间戳
  199. TmpObj.SetDouble( Card.down_time );
  200. CardObj.PushBack( TmpObj, Allocator );
  201. //4 进入区域时间戳
  202. TmpObj.SetDouble( Card.enter_area_time );
  203. CardObj.PushBack( TmpObj, Allocator );
  204. //5 接收时间戳
  205. TmpObj.SetDouble( Card.rec_time );
  206. CardObj.PushBack( TmpObj, Allocator );
  207. //6 工作时长
  208. TmpObj.SetDouble( Card.work_time );
  209. CardObj.PushBack( TmpObj, Allocator );
  210. //7 地图编号
  211. TmpObj.SetInt( Card.map_id );
  212. CardObj.PushBack( TmpObj, Allocator );
  213. //8 区域编号
  214. TmpObj.SetInt( Card.area_id );
  215. CardObj.PushBack( TmpObj, Allocator );
  216. //9 部门编号
  217. TmpObj.SetInt( Card.dept_id );
  218. CardObj.PushBack( TmpObj, Allocator );
  219. //10 状态
  220. TmpObj.SetInt( Card.stat );
  221. CardObj.PushBack( TmpObj, Allocator );
  222. //11 运行状态
  223. TmpObj.SetInt( Card.running_stat );
  224. CardObj.PushBack( TmpObj, Allocator );
  225. //12 业务状态
  226. TmpObj.SetInt( Card.biz_stat );
  227. CardObj.PushBack( TmpObj, Allocator );
  228. //13 速度
  229. TmpObj.SetDouble( Card.speed );
  230. CardObj.PushBack( TmpObj, Allocator );
  231. CardItem = CardObj;
  232. }
  233. void jsonBuilder::__CreateDeptMap( const _CARD_POS_ & CardPos, std::map<int, _STAT_DEPT_ITEM_>& DeptMap )
  234. {
  235. std::map<int, _STAT_DEPT_ITEM_>::iterator mit_dept_map;
  236. mit_dept_map = DeptMap.find( CardPos.dept_id );
  237. if ( mit_dept_map == DeptMap.end() )
  238. {
  239. _STAT_DEPT_ITEM_ sdItem;
  240. sdItem.DeptID = CardPos.dept_id;
  241. for(const auto &a:CardPos.area_info)
  242. sdItem.Area.insert( std::make_pair(a.first, 1 ) );
  243. sdItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) );
  244. sdItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) );
  245. sdItem.Sum = 1;
  246. DeptMap.insert( std::make_pair( sdItem.DeptID, sdItem ) );
  247. }
  248. else
  249. {
  250. __CreateDeptItem( CardPos, mit_dept_map->second );
  251. }
  252. }
  253. void jsonBuilder::__CreateDeptItem( const _CARD_POS_ & CardPos, _STAT_DEPT_ITEM_ & DeptItem )
  254. {
  255. std::map<int, int>::iterator mit_id_sum;
  256. //area
  257. for(const auto &a:CardPos.area_info)
  258. {
  259. int areaid = a.first;
  260. mit_id_sum = DeptItem.Area.find(areaid);
  261. if ( mit_id_sum == DeptItem.Area.end() )
  262. {
  263. DeptItem.Area.insert( std::make_pair(areaid, 1 ) );
  264. }
  265. else
  266. {
  267. mit_id_sum->second++;
  268. }
  269. }
  270. //Dept
  271. mit_id_sum = DeptItem.Dept.find( CardPos.dept_id );
  272. if ( mit_id_sum == DeptItem.Dept.end() )
  273. {
  274. DeptItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) );
  275. }
  276. else
  277. {
  278. mit_id_sum->second++;
  279. }
  280. //occupation_level
  281. mit_id_sum = DeptItem.OcptLvl.find( CardPos.level_id );
  282. if ( mit_id_sum == DeptItem.OcptLvl.end() )
  283. {
  284. DeptItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) );
  285. }
  286. else
  287. {
  288. mit_id_sum->second++;
  289. }
  290. //Sum
  291. DeptItem.Sum++;
  292. }
  293. void jsonBuilder::__BuildIDSumMap( const std::map<int, int>& id_sum_map, const char* szName, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Parent )
  294. {
  295. rapidjson::Value Array( rapidjson::kArrayType );
  296. std::map<int, int>::const_iterator mit_id_sum;
  297. for ( mit_id_sum = id_sum_map.begin(); mit_id_sum != id_sum_map.end(); mit_id_sum++ )
  298. {
  299. rapidjson::Value Item( rapidjson::kArrayType );
  300. rapidjson::Value tmpobj( rapidjson::kObjectType );
  301. tmpobj.SetInt( mit_id_sum->first );
  302. Item.PushBack( tmpobj, Allocator );
  303. tmpobj.SetInt( mit_id_sum->second );
  304. Item.PushBack( tmpobj, Allocator );
  305. Array.PushBack( Item, Allocator );
  306. }
  307. Parent.AddMember( rapidjson::StringRef( szName ), Array, Allocator );
  308. }
  309. std::string jsonBuilder::BuildLogin( const _JS_LOGIN_& Login )
  310. {
  311. rapidjson::StringBuffer sb;
  312. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  313. rapidjson::Document doc;
  314. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  315. rapidjson::Value root( rapidjson::kObjectType );
  316. rapidjson::Value child( rapidjson::kObjectType );
  317. rapidjson::Value value( rapidjson::kStringType );
  318. __SetCmd( JSON_CMD_VALUE_LOGIN, root, Allocator );
  319. value.SetString( Login.user_name.c_str(), Allocator );
  320. child.AddMember( JSON_KEY_USERNAME, value, Allocator );
  321. value.SetString( Login.user_password.c_str(), Allocator );
  322. child.AddMember( JSON_KEY_PASSWORD, value, Allocator );
  323. root.AddMember( JSON_ROOT_KEY_DATA, child, Allocator );
  324. root.Accept( writer );
  325. return sb.GetString();
  326. }
  327. std::string jsonBuilder::BuildCardPos( const std::map<uint64_t, _CARD_POS_>& CardPosList )
  328. {
  329. static bool p2s=false;
  330. p2s=p2s?false:true;
  331. rapidjson::StringBuffer sb;
  332. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  333. rapidjson::Document doc;
  334. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  335. rapidjson::Value root( rapidjson::kObjectType );
  336. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, Allocator );
  337. rapidjson::Value s( rapidjson::kObjectType );
  338. rapidjson::Value v( rapidjson::kObjectType );
  339. rapidjson::Value s_Detail( rapidjson::kArrayType );
  340. rapidjson::Value v_Detail( rapidjson::kArrayType );
  341. rapidjson::Value s_dept( rapidjson::kObjectType );
  342. rapidjson::Value v_dept( rapidjson::kObjectType );
  343. std::map<int, _STAT_DEPT_ITEM_> s_dept_map;
  344. std::map<int, _STAT_DEPT_ITEM_> v_dept_map;
  345. _STAT_DEPT_ITEM_ s_glbl_item;
  346. _STAT_DEPT_ITEM_ v_glbl_item;
  347. std::map<uint64_t, _CARD_POS_>::const_iterator mit_card;
  348. for ( mit_card = CardPosList.begin(); mit_card != CardPosList.end(); mit_card++ )
  349. {
  350. //不显示的卡不往前端推送
  351. //if ( !mit_card->second.display )
  352. //{
  353. // continue;
  354. //}
  355. if(p2s && mit_card->second.Type == 1)continue;
  356. rapidjson::Value DetailItem;
  357. if ( !__BuildDetail( mit_card->second, Allocator, DetailItem ) )
  358. {
  359. continue;
  360. }
  361. if( mit_card->second.Type == 1)
  362. {
  363. s_Detail.PushBack( DetailItem, Allocator );
  364. __CreateDeptMap( mit_card->second, s_dept_map );
  365. __CreateDeptItem( mit_card->second, s_glbl_item );
  366. }
  367. else //车卡、采煤机等等
  368. {
  369. v_Detail.PushBack( DetailItem, Allocator );
  370. __CreateDeptMap( mit_card->second, v_dept_map );
  371. __CreateDeptItem( mit_card->second, v_glbl_item );
  372. }
  373. }
  374. s.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, s_Detail, Allocator );
  375. v.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, v_Detail, Allocator );
  376. //stat
  377. rapidjson::Value s_stat( rapidjson::kObjectType );
  378. rapidjson::Value v_stat( rapidjson::kObjectType );
  379. if ( __BuildDept( s_dept_map, Allocator, s_dept ) )
  380. {
  381. s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, s_dept, Allocator );
  382. }
  383. if ( __BuildDept( v_dept_map, Allocator, v_dept ) )
  384. {
  385. v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, v_dept, Allocator );
  386. }
  387. //glbl
  388. rapidjson::Value s_glbl( rapidjson::kObjectType );
  389. rapidjson::Value v_glbl( rapidjson::kObjectType );
  390. __BuildDeptItem( s_glbl_item, Allocator, s_glbl );
  391. __BuildDeptItem( v_glbl_item, Allocator, v_glbl );
  392. //s.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator );
  393. //v.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator );
  394. s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator );
  395. v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator );
  396. s.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, s_stat, Allocator );
  397. v.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, v_stat, Allocator );
  398. rapidjson::Value Data( rapidjson::kObjectType );
  399. Data.AddMember( JSON_ROOT_KEY_STATISTIC_STAFF_DATA, s, Allocator );
  400. Data.AddMember( JSON_ROOT_KEY_STATISTIC_VEHICLE_DATA, v, Allocator );
  401. root.AddMember( JSON_ROOT_KEY_DATA, Data, Allocator );
  402. __AddVersion( root, Allocator );
  403. root.Accept( writer );
  404. return sb.GetString();
  405. }
  406. std::string jsonBuilder::build_ios_card_pos(const _CARD_POS_& card)
  407. {
  408. rapidjson::StringBuffer sb;
  409. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  410. rapidjson::Document doc;
  411. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  412. rapidjson::Value root( rapidjson::kObjectType );
  413. // "cmd":
  414. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  415. // "version"
  416. __AddVersion(root, allocator);
  417. // "item"
  418. rapidjson::Value item(rapidjson::kObjectType);
  419. rapidjson::Value value;
  420. value.SetString(std::to_string(card.ID).c_str(), allocator);
  421. item.AddMember("cardid", value, allocator);
  422. item.AddMember("x", card.x, allocator);
  423. item.AddMember("y", card.y, allocator);
  424. item.AddMember("z", card.z, allocator);
  425. item.AddMember("acc", card.m_acc, allocator);
  426. item.AddMember("speed", card.speed, allocator);
  427. item.AddMember("acc_x", card.m_tri_acc[0], allocator);
  428. item.AddMember("acc_y", card.m_tri_acc[1], allocator);
  429. item.AddMember("acc_z", card.m_tri_acc[2], allocator);
  430. item.AddMember("ang_x", card.m_tri_ang[0], allocator);
  431. item.AddMember("ang_y", card.m_tri_ang[1], allocator);
  432. item.AddMember("ang_z", card.m_tri_ang[2], allocator);
  433. item.AddMember("steps", card.m_walking_step, allocator);
  434. item.AddMember("jump", card.m_jump_count, allocator);
  435. item.AddMember("hang_time", card.m_hang_time, allocator);
  436. item.AddMember("hang_height", card.m_hang_height, allocator);
  437. /*rapidjson::Value data(rapidjson::kObjectType);
  438. data.AddMember("card_type", 1 , allocator);
  439. data.AddMember("cardid", "10", allocator);
  440. data.AddMember("x", 10.0 , allocator);
  441. data.AddMember("y", 20.0 , allocator);
  442. data.AddMember("z", 30.0, allocator);
  443. data.AddMember("acc" , 10.0 , allocator);
  444. data.AddMember("speed", 3.5 , allocator);
  445. data.AddMember("acc_x", 0.1, allocator);
  446. data.AddMember("acc_y", 0.2 , allocator);
  447. data.AddMember("acc_z", 0.3 , allocator);
  448. data.AddMember("ang_x", 0.4, allocator);
  449. data.AddMember("ang_y", 0.5 , allocator);
  450. data.AddMember("ang_z", 0.6 , allocator);
  451. data.AddMember("steps", 7, allocator);
  452. data.AddMember("jump", 8 , allocator);
  453. data.AddMember("hang_time", 3 , allocator);
  454. data.AddMember("hang_height", 4, allocator);
  455. */
  456. root.AddMember("data", item, allocator);
  457. root.Accept(writer);
  458. return sb.GetString();
  459. }
  460. std::string jsonBuilder::build_tmp_card_pos(const std::map<uint64_t,_CARD_POS_>& cards)
  461. {
  462. rapidjson::StringBuffer sb;
  463. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  464. rapidjson::Document doc;
  465. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  466. rapidjson::Value root(rapidjson::kObjectType);
  467. // "cmd":
  468. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  469. // "data"
  470. rapidjson::Value item(rapidjson::kArrayType);
  471. for(auto it = cards.begin(); it != cards.end(); ++it)
  472. {
  473. rapidjson::Value elem(rapidjson::kArrayType);
  474. rapidjson::Value tmp_obj(rapidjson::kObjectType);
  475. // 卡号
  476. tmp_obj.SetString(__FmtCardID(it->second).c_str(), allocator);
  477. elem.PushBack(tmp_obj, allocator);
  478. // x坐标
  479. tmp_obj.SetDouble(it->second.x);
  480. elem.PushBack(tmp_obj, allocator);
  481. // y坐标
  482. tmp_obj.SetDouble(it->second.y);
  483. elem.PushBack(tmp_obj, allocator);
  484. // z坐标
  485. tmp_obj.SetDouble(it->second.z);
  486. elem.PushBack(tmp_obj, allocator);
  487. // 时间戳
  488. tmp_obj.SetDouble(it->second.rec_time);
  489. elem.PushBack(tmp_obj, allocator);
  490. // 加速度
  491. tmp_obj.SetDouble(it->second.m_acc);
  492. elem.PushBack(tmp_obj, allocator);
  493. // 速度
  494. tmp_obj.SetDouble(it->second.speed);
  495. elem.PushBack(tmp_obj, allocator);
  496. item.PushBack(elem, allocator);
  497. }
  498. root.AddMember("data", item, allocator);
  499. // "version"
  500. __AddVersion(root, allocator);
  501. root.Accept(writer);
  502. return sb.GetString();
  503. }
  504. std::string jsonBuilder::BuildSpecialAreaProcess( const _BASE_CARD_ & stCard )
  505. {
  506. rapidjson::StringBuffer sb;
  507. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  508. rapidjson::Document doc;
  509. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  510. rapidjson::Value root( rapidjson::kObjectType );
  511. __SetCmd( JSON_CMD_VALUE_SPECIAL_AREA_UP_MINE, root, Allocator );
  512. rapidjson::Value CardItem( rapidjson::kObjectType );
  513. rapidjson::Value CardList( rapidjson::kArrayType );
  514. __BuildBaseCardItem( stCard, Allocator, CardItem );
  515. CardList.PushBack( CardItem, Allocator );
  516. root.AddMember( JSON_ROOT_KEY_DATA, CardList, Allocator );
  517. root.Accept( writer );
  518. return sb.GetString();
  519. }
  520. bool jsonBuilder::ParseCall_Card_Req( const std::string& JasonText, _JS_CALL_CARD_REQ_& CallCardReq, std::string& Error )
  521. {
  522. rapidjson::Document doc;
  523. if ( doc.Parse( JasonText.c_str() ).HasParseError() )
  524. {
  525. Error = "Failed to parse jason text!";
  526. return false;
  527. }
  528. rapidjson::Value::MemberIterator itValue;
  529. itValue = doc.FindMember( "data" );
  530. if ( itValue == doc.MemberEnd() )
  531. {
  532. Error = "Failed to find member: data!";
  533. return false;
  534. }
  535. rapidjson::Value Data;
  536. rapidjson::Document::AllocatorType allocator;
  537. Data.CopyFrom( itValue->value, allocator );
  538. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TYPE ) )
  539. {
  540. Error = "Failed to find value: ";
  541. Error += JSON_KEY_CALL_CARD_CALL_TYPE;
  542. return false;
  543. }
  544. CallCardReq.call_type_id = Data[JSON_KEY_CALL_CARD_CALL_TYPE].GetInt();
  545. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TIME_OUT ) )
  546. {
  547. Error = "Failed to find value: ";
  548. Error += JSON_KEY_CALL_CARD_CALL_TIME_OUT;
  549. return false;
  550. }
  551. CallCardReq.call_time_out = Data[JSON_KEY_CALL_CARD_CALL_TIME_OUT].GetInt();
  552. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_LEVEL ) )
  553. {
  554. Error = "Failed to find value: ";
  555. Error += JSON_KEY_CALL_CARD_CALL_LEVEL;
  556. return false;
  557. }
  558. CallCardReq.call_time_interval = Data[JSON_KEY_CALL_CARD_CALL_LEVEL].GetInt();
  559. if ( !Data.HasMember( JSON_KEY_CALL_CARD_USER_NAME ) )
  560. {
  561. Error = "Failed to find value: ";
  562. Error += JSON_KEY_CALL_CARD_USER_NAME;
  563. return false;
  564. }
  565. CallCardReq.user_name = Data[JSON_KEY_CALL_CARD_USER_NAME].GetString();
  566. //data::stations
  567. if ( !Data.HasMember( JSON_KEY_CALL_CARD_STATIONS ) )
  568. {
  569. Error = "Failed to find value: ";
  570. Error += JSON_KEY_CALL_CARD_STATIONS;
  571. return false;
  572. }
  573. rapidjson::Value & stations = Data[JSON_KEY_CALL_CARD_STATIONS];
  574. if ( !stations.IsArray() )
  575. {
  576. Error = "data::stations is not a valid array!";
  577. return false;
  578. }
  579. for ( int i = 0; i < (int)stations.Size(); ++i )
  580. {
  581. rapidjson::Value & v = stations[i];
  582. if ( !v.IsObject() )
  583. {
  584. char szError[1024] = { 0 };
  585. sprintf( szError, "data::stations[%d] is not a object!", i );
  586. Error = szError;
  587. return false;
  588. }
  589. _JS_STATION_ Station;
  590. if ( !v.HasMember( JSON_KEY_CALL_CARD_STATION_ID ) && v[JSON_KEY_CALL_CARD_STATION_ID].IsInt() )
  591. {
  592. Error = "Error, failed to found value: ";
  593. Error += JSON_KEY_CALL_CARD_STATION_ID;
  594. return false;
  595. }
  596. Station.stationid = v[JSON_KEY_CALL_CARD_STATION_ID].GetInt();
  597. CallCardReq.stations.push_back( Station );
  598. }
  599. //data::cards
  600. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CARDS ) )
  601. {
  602. Error = "Failed to find value: ";
  603. Error += JSON_KEY_CALL_CARD_CARDS;
  604. return false;
  605. }
  606. rapidjson::Value & cards = Data[JSON_KEY_CALL_CARD_CARDS];
  607. for ( int i = 0; i < (int)cards.Size(); ++i )
  608. {
  609. rapidjson::Value & v = cards[i];
  610. if ( !v.IsObject() )
  611. {
  612. char szError[1024] = { 0 };
  613. sprintf( szError, "data::cards[%d] is not a object!", i );
  614. Error = szError;
  615. return false;
  616. }
  617. _JS_CARD_ Card;
  618. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_ID ) && v[JSON_KEY_CALL_CARD_CARD_ID].IsInt() )
  619. {
  620. Error = "Error, failed to found value: ";
  621. Error += JSON_KEY_CALL_CARD_CARD_ID;
  622. return false;
  623. }
  624. Card.cardid = v[JSON_KEY_CALL_CARD_CARD_ID].GetInt();
  625. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_TYPE_ID ) && v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].IsInt() )
  626. {
  627. Error = "Error, failed to found value: ";
  628. Error += JSON_KEY_CALL_CARD_CARD_TYPE_ID;
  629. return false;
  630. }
  631. Card.cardtype = v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].GetInt();
  632. CallCardReq.cards.push_back( Card );
  633. }
  634. return true;
  635. }
  636. std::string jsonBuilder::build_traffic_light(const std::vector<light_state> lights)
  637. {
  638. if(lights.size() == 0){
  639. return "";
  640. }
  641. rapidjson::StringBuffer sb;
  642. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  643. rapidjson::Document doc;
  644. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  645. rapidjson::Value root(rapidjson::kObjectType );
  646. __SetCmd(JSON_KEY_CALL_LIGHT_LIGHT_STATE, root, allocator);
  647. rapidjson::Value data(rapidjson::kArrayType);
  648. for(auto it : lights){
  649. rapidjson::Value elem(rapidjson::kArrayType);
  650. rapidjson::Value tmp_object(rapidjson::kObjectType);
  651. tmp_object.SetInt(it.m_group_id);
  652. elem.PushBack(tmp_object, allocator);
  653. tmp_object.SetInt(it.m_light_id);
  654. elem.PushBack(tmp_object, allocator);
  655. tmp_object.SetInt(it.m_light_state);
  656. elem.PushBack(tmp_object, allocator);
  657. tmp_object.SetString(it.m_card_id.c_str(), allocator);
  658. elem.PushBack(tmp_object, allocator);
  659. data.PushBack(elem, allocator);
  660. }
  661. root.AddMember("data", data, allocator);
  662. __AddVersion(root, allocator);
  663. root.Accept(writer);
  664. return sb.GetString();
  665. }
  666. }