1
0

jsonBuilder.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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 sys
  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.z);
  89. tmp_object.SetDouble( CardPos.work_time );
  90. Array.PushBack( tmp_object, Allocator );
  91. //7 地图编号
  92. tmp_object.SetInt( CardPos.map_id );
  93. Array.PushBack( tmp_object, Allocator );
  94. //8 区域编号
  95. //tmp_object.SetInt( CardPos.area_id );
  96. //Array.PushBack( tmp_object, Allocator );
  97. //9 部门编号
  98. tmp_object.SetInt( CardPos.dept_id );
  99. Array.PushBack( tmp_object, Allocator );
  100. //10 电量状态
  101. //tmp_object.SetInt( CardPos.stat );
  102. tmp_object.SetInt(CardPos.battery_val); // 电量值,1~10
  103. Array.PushBack( tmp_object, Allocator );
  104. //11 运行状态
  105. tmp_object.SetInt( CardPos.running_stat );
  106. Array.PushBack( tmp_object, Allocator );
  107. //12 业务状态
  108. tmp_object.SetInt( CardPos.biz_stat );
  109. Array.PushBack( tmp_object, Allocator );
  110. //13 速度
  111. tmp_object.SetDouble( CardPos.speed );
  112. Array.PushBack( tmp_object, Allocator );
  113. //14 地标编号
  114. //tmp_object.SetInt( CardPos.landmark_id );
  115. //Array.PushBack( tmp_object, Allocator );
  116. //15 地标方向
  117. //tmp_object.SetInt( CardPos.lm_direction );
  118. //Array.PushBack( tmp_object, Allocator );
  119. //16 距离地标的距离
  120. //tmp_object.SetDouble( CardPos.landmark_dis );
  121. //Array.PushBack( tmp_object, Allocator );
  122. //17 级别编号,对于不要级别编号的(可以用于传输接收分站的区域id)
  123. //tmp_object.SetInt( CardPos.level_id );
  124. tmp_object.SetInt(CardPos.area_id);
  125. Array.PushBack( tmp_object, Allocator );
  126. //18 车辆当天出勤的标识
  127. tmp_object.SetInt( CardPos.is_on_duty );
  128. Array.PushBack( tmp_object, Allocator );
  129. //log_debug("websocket..%d",CardPos.area_info.size());
  130. rapidjson::Value Array_1( rapidjson::kArrayType );
  131. for(const auto & a : CardPos.area_info)
  132. {
  133. rapidjson::Value _DetailItem;
  134. rapidjson::Value Array_2( rapidjson::kArrayType );
  135. const auto lm = a.second;
  136. //8 区域编号
  137. tmp_object.SetInt(std::get<0>(lm));
  138. Array_2.PushBack( tmp_object, Allocator );
  139. //14 地标编号
  140. tmp_object.SetInt(std::get<1>(lm));
  141. Array_2.PushBack( tmp_object, Allocator );
  142. //15 地标方向
  143. tmp_object.SetInt(std::get<2>(lm));
  144. Array_2.PushBack( tmp_object, Allocator );
  145. //16 距离地标的距离
  146. tmp_object.SetDouble(std::get<3>(lm));
  147. Array_2.PushBack( tmp_object, Allocator );
  148. //5 最后接收时间戳
  149. tmp_object.SetDouble(std::get<4>(lm));
  150. Array_2.PushBack( tmp_object, Allocator );
  151. _DetailItem=Array_2;
  152. Array_1.PushBack(_DetailItem, Allocator);
  153. }
  154. DetailItem = Array_1;
  155. Array.PushBack(DetailItem, Allocator );
  156. //19 cell index
  157. tmp_object.SetInt(CardPos.cell_index);
  158. Array.PushBack(tmp_object, 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. // 处理长时间运行时,字符串截断问题
  414. std::string json_str = std::string(sb.GetString(), sb.GetSize());
  415. return json_str;
  416. }
  417. std::string jsonBuilder::build_ios_card_pos(const _CARD_POS_& card)
  418. {
  419. rapidjson::StringBuffer sb;
  420. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  421. rapidjson::Document doc;
  422. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  423. rapidjson::Value root( rapidjson::kObjectType );
  424. // "cmd":
  425. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  426. // "version"
  427. __AddVersion(root, allocator);
  428. // "item"
  429. rapidjson::Value item(rapidjson::kObjectType);
  430. rapidjson::Value value;
  431. value.SetString(std::to_string(card.ID).c_str(), allocator);
  432. item.AddMember("cardid", value, allocator);
  433. item.AddMember("x", card.x, allocator);
  434. item.AddMember("y", card.y, allocator);
  435. item.AddMember("z", card.z, allocator);
  436. item.AddMember("acc", card.m_acc, allocator);
  437. item.AddMember("speed", card.speed, allocator);
  438. item.AddMember("acc_x", card.m_tri_acc[0], allocator);
  439. item.AddMember("acc_y", card.m_tri_acc[1], allocator);
  440. item.AddMember("acc_z", card.m_tri_acc[2], allocator);
  441. item.AddMember("ang_x", card.m_tri_ang[0], allocator);
  442. item.AddMember("ang_y", card.m_tri_ang[1], allocator);
  443. item.AddMember("ang_z", card.m_tri_ang[2], allocator);
  444. item.AddMember("steps", card.m_walking_step, allocator);
  445. item.AddMember("jump", card.m_jump_count, allocator);
  446. item.AddMember("hang_time", card.m_hang_time, allocator);
  447. item.AddMember("hang_height", card.m_hang_height, allocator);
  448. /*rapidjson::Value data(rapidjson::kObjectType);
  449. data.AddMember("card_type", 1 , allocator);
  450. data.AddMember("cardid", "10", allocator);
  451. data.AddMember("x", 10.0 , allocator);
  452. data.AddMember("y", 20.0 , allocator);
  453. data.AddMember("z", 30.0, allocator);
  454. data.AddMember("acc" , 10.0 , allocator);
  455. data.AddMember("speed", 3.5 , allocator);
  456. data.AddMember("acc_x", 0.1, allocator);
  457. data.AddMember("acc_y", 0.2 , allocator);
  458. data.AddMember("acc_z", 0.3 , allocator);
  459. data.AddMember("ang_x", 0.4, allocator);
  460. data.AddMember("ang_y", 0.5 , allocator);
  461. data.AddMember("ang_z", 0.6 , allocator);
  462. data.AddMember("steps", 7, allocator);
  463. data.AddMember("jump", 8 , allocator);
  464. data.AddMember("hang_time", 3 , allocator);
  465. data.AddMember("hang_height", 4, allocator);
  466. */
  467. root.AddMember("data", item, allocator);
  468. root.Accept(writer);
  469. return sb.GetString();
  470. }
  471. std::string jsonBuilder::build_tmp_card_pos(const std::map<uint64_t,_CARD_POS_>& cards)
  472. {
  473. rapidjson::StringBuffer sb;
  474. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  475. rapidjson::Document doc;
  476. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  477. rapidjson::Value root(rapidjson::kObjectType);
  478. // "cmd":
  479. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  480. // "data"
  481. rapidjson::Value item(rapidjson::kArrayType);
  482. for(auto it = cards.begin(); it != cards.end(); ++it)
  483. {
  484. rapidjson::Value elem(rapidjson::kArrayType);
  485. rapidjson::Value tmp_obj(rapidjson::kObjectType);
  486. // 卡号
  487. tmp_obj.SetString(__FmtCardID(it->second).c_str(), allocator);
  488. elem.PushBack(tmp_obj, allocator);
  489. // x坐标
  490. tmp_obj.SetDouble(it->second.x);
  491. elem.PushBack(tmp_obj, allocator);
  492. // y坐标
  493. tmp_obj.SetDouble(it->second.y);
  494. elem.PushBack(tmp_obj, allocator);
  495. // z坐标
  496. tmp_obj.SetDouble(it->second.z);
  497. elem.PushBack(tmp_obj, allocator);
  498. // 时间戳
  499. tmp_obj.SetDouble(it->second.rec_time);
  500. elem.PushBack(tmp_obj, allocator);
  501. // 加速度
  502. tmp_obj.SetDouble(it->second.m_acc);
  503. elem.PushBack(tmp_obj, allocator);
  504. // 速度
  505. tmp_obj.SetDouble(it->second.speed);
  506. elem.PushBack(tmp_obj, allocator);
  507. item.PushBack(elem, allocator);
  508. }
  509. root.AddMember("data", item, allocator);
  510. // "version"
  511. __AddVersion(root, allocator);
  512. root.Accept(writer);
  513. return sb.GetString();
  514. }
  515. std::string jsonBuilder::BuildSpecialAreaProcess( const _BASE_CARD_ & stCard )
  516. {
  517. rapidjson::StringBuffer sb;
  518. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  519. rapidjson::Document doc;
  520. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  521. rapidjson::Value root( rapidjson::kObjectType );
  522. __SetCmd( JSON_CMD_VALUE_SPECIAL_AREA_UP_MINE, root, Allocator );
  523. rapidjson::Value CardItem( rapidjson::kObjectType );
  524. rapidjson::Value CardList( rapidjson::kArrayType );
  525. __BuildBaseCardItem( stCard, Allocator, CardItem );
  526. CardList.PushBack( CardItem, Allocator );
  527. root.AddMember( JSON_ROOT_KEY_DATA, CardList, Allocator );
  528. root.Accept( writer );
  529. return sb.GetString();
  530. }
  531. bool jsonBuilder::ParseCall_Card_Req( const std::string& JasonText, _JS_CALL_CARD_REQ_& CallCardReq, std::string& Error )
  532. {
  533. rapidjson::Document doc;
  534. if ( doc.Parse( JasonText.c_str() ).HasParseError() )
  535. {
  536. Error = "Failed to parse jason text!";
  537. return false;
  538. }
  539. rapidjson::Value::MemberIterator itValue;
  540. itValue = doc.FindMember( "data" );
  541. if ( itValue == doc.MemberEnd() )
  542. {
  543. Error = "Failed to find member: data!";
  544. return false;
  545. }
  546. rapidjson::Value Data;
  547. rapidjson::Document::AllocatorType allocator;
  548. Data.CopyFrom( itValue->value, allocator );
  549. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TYPE ) )
  550. {
  551. Error = "Failed to find value: ";
  552. Error += JSON_KEY_CALL_CARD_CALL_TYPE;
  553. return false;
  554. }
  555. CallCardReq.call_type_id = Data[JSON_KEY_CALL_CARD_CALL_TYPE].GetInt();
  556. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TIME_OUT ) )
  557. {
  558. Error = "Failed to find value: ";
  559. Error += JSON_KEY_CALL_CARD_CALL_TIME_OUT;
  560. return false;
  561. }
  562. CallCardReq.call_time_out = Data[JSON_KEY_CALL_CARD_CALL_TIME_OUT].GetInt();
  563. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_LEVEL ) )
  564. {
  565. Error = "Failed to find value: ";
  566. Error += JSON_KEY_CALL_CARD_CALL_LEVEL;
  567. return false;
  568. }
  569. CallCardReq.call_time_interval = Data[JSON_KEY_CALL_CARD_CALL_LEVEL].GetInt();
  570. if ( !Data.HasMember( JSON_KEY_CALL_CARD_USER_NAME ) )
  571. {
  572. Error = "Failed to find value: ";
  573. Error += JSON_KEY_CALL_CARD_USER_NAME;
  574. return false;
  575. }
  576. CallCardReq.user_name = Data[JSON_KEY_CALL_CARD_USER_NAME].GetString();
  577. //data::stations
  578. if ( !Data.HasMember( JSON_KEY_CALL_CARD_STATIONS ) )
  579. {
  580. Error = "Failed to find value: ";
  581. Error += JSON_KEY_CALL_CARD_STATIONS;
  582. return false;
  583. }
  584. rapidjson::Value & stations = Data[JSON_KEY_CALL_CARD_STATIONS];
  585. if ( !stations.IsArray() )
  586. {
  587. Error = "data::stations is not a valid array!";
  588. return false;
  589. }
  590. for ( int i = 0; i < (int)stations.Size(); ++i )
  591. {
  592. rapidjson::Value & v = stations[i];
  593. if ( !v.IsObject() )
  594. {
  595. char szError[1024] = { 0 };
  596. sprintf( szError, "data::stations[%d] is not a object!", i );
  597. Error = szError;
  598. return false;
  599. }
  600. _JS_STATION_ Station;
  601. if ( !v.HasMember( JSON_KEY_CALL_CARD_STATION_ID ) && v[JSON_KEY_CALL_CARD_STATION_ID].IsInt() )
  602. {
  603. Error = "Error, failed to found value: ";
  604. Error += JSON_KEY_CALL_CARD_STATION_ID;
  605. return false;
  606. }
  607. Station.stationid = v[JSON_KEY_CALL_CARD_STATION_ID].GetInt();
  608. CallCardReq.stations.push_back( Station );
  609. }
  610. //data::cards
  611. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CARDS ) )
  612. {
  613. Error = "Failed to find value: ";
  614. Error += JSON_KEY_CALL_CARD_CARDS;
  615. return false;
  616. }
  617. rapidjson::Value & cards = Data[JSON_KEY_CALL_CARD_CARDS];
  618. for ( int i = 0; i < (int)cards.Size(); ++i )
  619. {
  620. rapidjson::Value & v = cards[i];
  621. if ( !v.IsObject() )
  622. {
  623. char szError[1024] = { 0 };
  624. sprintf( szError, "data::cards[%d] is not a object!", i );
  625. Error = szError;
  626. return false;
  627. }
  628. _JS_CARD_ Card;
  629. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_ID ) && v[JSON_KEY_CALL_CARD_CARD_ID].IsInt() )
  630. {
  631. Error = "Error, failed to found value: ";
  632. Error += JSON_KEY_CALL_CARD_CARD_ID;
  633. return false;
  634. }
  635. Card.cardid = v[JSON_KEY_CALL_CARD_CARD_ID].GetInt();
  636. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_TYPE_ID ) && v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].IsInt() )
  637. {
  638. Error = "Error, failed to found value: ";
  639. Error += JSON_KEY_CALL_CARD_CARD_TYPE_ID;
  640. return false;
  641. }
  642. Card.cardtype = v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].GetInt();
  643. CallCardReq.cards.push_back( Card );
  644. }
  645. return true;
  646. }
  647. std::string jsonBuilder::build_traffic_light(const std::vector<light_state> lights)
  648. {
  649. if(lights.size() == 0){
  650. return "";
  651. }
  652. rapidjson::StringBuffer sb;
  653. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  654. rapidjson::Document doc;
  655. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  656. rapidjson::Value root(rapidjson::kObjectType );
  657. __SetCmd(JSON_KEY_CALL_LIGHT_LIGHT_STATE, root, allocator);
  658. rapidjson::Value data(rapidjson::kArrayType);
  659. for(auto it : lights){
  660. rapidjson::Value elem(rapidjson::kArrayType);
  661. rapidjson::Value tmp_object(rapidjson::kObjectType);
  662. tmp_object.SetInt(it.m_group_id);
  663. elem.PushBack(tmp_object, allocator);
  664. tmp_object.SetInt(it.m_light_id);
  665. elem.PushBack(tmp_object, allocator);
  666. tmp_object.SetInt(it.m_light_state);
  667. elem.PushBack(tmp_object, allocator);
  668. tmp_object.SetString(it.m_card_id.c_str(), allocator);
  669. elem.PushBack(tmp_object, allocator);
  670. data.PushBack(elem, allocator);
  671. }
  672. root.AddMember("data", data, allocator);
  673. __AddVersion(root, allocator);
  674. root.Accept(writer);
  675. return sb.GetString();
  676. }
  677. /*
  678. * 构建设备状态json
  679. *
  680. * 参数
  681. * const std::vector<device_state> vtd 设备状态列表
  682. *
  683. * 返回值
  684. * 设备状态json
  685. *
  686. * */
  687. std::string jsonBuilder::build_device_state(const std::vector<device_state> vtd)
  688. {
  689. if(vtd.size() == 0){
  690. return "";
  691. }
  692. rapidjson::StringBuffer sb;
  693. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  694. rapidjson::Document doc;
  695. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  696. rapidjson::Value root(rapidjson::kObjectType );
  697. __SetCmd(JSON_CMD_VALUE_DEV_STATE, root, allocator);
  698. rapidjson::Value data(rapidjson::kArrayType);
  699. for(auto it : vtd){
  700. rapidjson::Value elem(rapidjson::kArrayType);
  701. rapidjson::Value tmp_object(rapidjson::kObjectType);
  702. tmp_object.SetInt(it.m_id);
  703. elem.PushBack(tmp_object, allocator);
  704. tmp_object.SetInt(it.m_type);
  705. elem.PushBack(tmp_object, allocator);
  706. tmp_object.SetInt(it.m_state);
  707. elem.PushBack(tmp_object, allocator);
  708. tmp_object.SetDouble(it.m_cur_time);
  709. elem.PushBack(tmp_object, allocator);
  710. data.PushBack(elem, allocator);
  711. }
  712. root.AddMember("data", data, allocator);
  713. __AddVersion(root, allocator);
  714. root.Accept(writer);
  715. return sb.GetString();
  716. }
  717. std::string jsonBuilder::build_load_history_completed()
  718. {
  719. rapidjson::StringBuffer sb;
  720. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  721. rapidjson::Document doc;
  722. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  723. rapidjson::Value root(rapidjson::kObjectType);
  724. __SetCmd(JSON_KEY_CALL_LOAD_HISTORY_COMPLETED, root, allocator);
  725. rapidjson::Value data(rapidjson::kArrayType);
  726. root.AddMember("data", data, allocator);
  727. __AddVersion(root, allocator);
  728. root.Accept(writer);
  729. return sb.GetString();
  730. }
  731. }