1
0

jsonBuilder.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  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. DetailItem = Array;
  157. return true;
  158. }
  159. bool jsonBuilder::__BuildDept( const std::map<int, _STAT_DEPT_ITEM_>& dept_map, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Dept )
  160. {
  161. std::map<int, _STAT_DEPT_ITEM_>::const_iterator mit_dept;
  162. for ( mit_dept = dept_map.begin(); mit_dept != dept_map.end(); mit_dept++ )
  163. {
  164. rapidjson::Value DeptItem( rapidjson::kObjectType );
  165. __BuildDeptItem( mit_dept->second, Allocator, DeptItem );
  166. rapidjson::Value dept_id( rapidjson::kObjectType );
  167. char szNum[20] = { 0 };
  168. //itoa( mit_dept->second.DeptID, szNum, 10 );
  169. sprintf(szNum,"%d",mit_dept->second.DeptID);
  170. dept_id.SetString( szNum, strlen( szNum ), Allocator );
  171. Dept.AddMember( dept_id, DeptItem, Allocator );
  172. }
  173. return true;
  174. }
  175. void jsonBuilder::__BuildDeptItem( const _STAT_DEPT_ITEM_ & st_dept_item, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & DeptItem )
  176. {
  177. //Area
  178. __BuildIDSumMap( st_dept_item.Area, JSON_ROOT_KEY_STATISTIC_AREA, Allocator, DeptItem );
  179. //Dept
  180. __BuildIDSumMap( st_dept_item.Dept, JSON_ROOT_KEY_STATISTIC_DEPT, Allocator, DeptItem );
  181. //occupation_level
  182. __BuildIDSumMap( st_dept_item.OcptLvl, JSON_ROOT_KEY_STATISTIC_LEVEL, Allocator, DeptItem );
  183. //sum
  184. rapidjson::Value Sum( rapidjson::kObjectType );
  185. Sum.SetInt( st_dept_item.Sum );
  186. DeptItem.AddMember( JSON_ROOT_KEY_STATISTIC_SUM, Sum, Allocator );
  187. }
  188. void jsonBuilder::__BuildBaseCardItem( const _BASE_CARD_ & Card, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & CardItem )
  189. {
  190. rapidjson::Value TmpObj( rapidjson::kObjectType );
  191. rapidjson::Value CardObj( rapidjson::kArrayType );
  192. //0 卡号
  193. TmpObj.SetString( __FmtCardID( Card ).c_str(), Allocator );
  194. CardObj.PushBack( TmpObj, Allocator );
  195. //1 x坐标
  196. TmpObj.SetDouble( Card.x );
  197. CardObj.PushBack( TmpObj, Allocator );
  198. //2 y坐标
  199. TmpObj.SetDouble( Card.y );
  200. CardObj.PushBack( TmpObj, Allocator );
  201. //3 入井时间戳
  202. TmpObj.SetDouble( Card.down_time );
  203. CardObj.PushBack( TmpObj, Allocator );
  204. //4 进入区域时间戳
  205. TmpObj.SetDouble( Card.enter_area_time );
  206. CardObj.PushBack( TmpObj, Allocator );
  207. //5 接收时间戳
  208. TmpObj.SetDouble( Card.rec_time );
  209. CardObj.PushBack( TmpObj, Allocator );
  210. //6 工作时长
  211. TmpObj.SetDouble( Card.work_time );
  212. CardObj.PushBack( TmpObj, Allocator );
  213. //7 地图编号
  214. TmpObj.SetInt( Card.map_id );
  215. CardObj.PushBack( TmpObj, Allocator );
  216. //8 区域编号
  217. TmpObj.SetInt( Card.area_id );
  218. CardObj.PushBack( TmpObj, Allocator );
  219. //9 部门编号
  220. TmpObj.SetInt( Card.dept_id );
  221. CardObj.PushBack( TmpObj, Allocator );
  222. //10 状态
  223. TmpObj.SetInt( Card.stat );
  224. CardObj.PushBack( TmpObj, Allocator );
  225. //11 运行状态
  226. TmpObj.SetInt( Card.running_stat );
  227. CardObj.PushBack( TmpObj, Allocator );
  228. //12 业务状态
  229. TmpObj.SetInt( Card.biz_stat );
  230. CardObj.PushBack( TmpObj, Allocator );
  231. //13 速度
  232. TmpObj.SetDouble( Card.speed );
  233. CardObj.PushBack( TmpObj, Allocator );
  234. CardItem = CardObj;
  235. }
  236. void jsonBuilder::__CreateDeptMap( const _CARD_POS_ & CardPos, std::map<int, _STAT_DEPT_ITEM_>& DeptMap )
  237. {
  238. std::map<int, _STAT_DEPT_ITEM_>::iterator mit_dept_map;
  239. mit_dept_map = DeptMap.find( CardPos.dept_id );
  240. if ( mit_dept_map == DeptMap.end() )
  241. {
  242. _STAT_DEPT_ITEM_ sdItem;
  243. sdItem.DeptID = CardPos.dept_id;
  244. for(const auto &a:CardPos.area_info)
  245. sdItem.Area.insert( std::make_pair(a.first, 1 ) );
  246. sdItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) );
  247. sdItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) );
  248. sdItem.Sum = 1;
  249. DeptMap.insert( std::make_pair( sdItem.DeptID, sdItem ) );
  250. }
  251. else
  252. {
  253. __CreateDeptItem( CardPos, mit_dept_map->second );
  254. }
  255. }
  256. void jsonBuilder::__CreateDeptItem( const _CARD_POS_ & CardPos, _STAT_DEPT_ITEM_ & DeptItem )
  257. {
  258. std::map<int, int>::iterator mit_id_sum;
  259. //area
  260. for(const auto &a:CardPos.area_info)
  261. {
  262. int areaid = a.first;
  263. mit_id_sum = DeptItem.Area.find(areaid);
  264. if ( mit_id_sum == DeptItem.Area.end() )
  265. {
  266. DeptItem.Area.insert( std::make_pair(areaid, 1 ) );
  267. }
  268. else
  269. {
  270. mit_id_sum->second++;
  271. }
  272. }
  273. //Dept
  274. mit_id_sum = DeptItem.Dept.find( CardPos.dept_id );
  275. if ( mit_id_sum == DeptItem.Dept.end() )
  276. {
  277. DeptItem.Dept.insert( std::make_pair( CardPos.dept_id, 1 ) );
  278. }
  279. else
  280. {
  281. mit_id_sum->second++;
  282. }
  283. //occupation_level
  284. mit_id_sum = DeptItem.OcptLvl.find( CardPos.level_id );
  285. if ( mit_id_sum == DeptItem.OcptLvl.end() )
  286. {
  287. DeptItem.OcptLvl.insert( std::make_pair( CardPos.level_id, 1 ) );
  288. }
  289. else
  290. {
  291. mit_id_sum->second++;
  292. }
  293. //Sum
  294. DeptItem.Sum++;
  295. }
  296. void jsonBuilder::__BuildIDSumMap( const std::map<int, int>& id_sum_map, const char* szName, rapidjson::Document::AllocatorType & Allocator, rapidjson::Value & Parent )
  297. {
  298. rapidjson::Value Array( rapidjson::kArrayType );
  299. std::map<int, int>::const_iterator mit_id_sum;
  300. for ( mit_id_sum = id_sum_map.begin(); mit_id_sum != id_sum_map.end(); mit_id_sum++ )
  301. {
  302. rapidjson::Value Item( rapidjson::kArrayType );
  303. rapidjson::Value tmpobj( rapidjson::kObjectType );
  304. tmpobj.SetInt( mit_id_sum->first );
  305. Item.PushBack( tmpobj, Allocator );
  306. tmpobj.SetInt( mit_id_sum->second );
  307. Item.PushBack( tmpobj, Allocator );
  308. Array.PushBack( Item, Allocator );
  309. }
  310. Parent.AddMember( rapidjson::StringRef( szName ), Array, Allocator );
  311. }
  312. std::string jsonBuilder::BuildLogin( const _JS_LOGIN_& Login )
  313. {
  314. rapidjson::StringBuffer sb;
  315. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  316. rapidjson::Document doc;
  317. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  318. rapidjson::Value root( rapidjson::kObjectType );
  319. rapidjson::Value child( rapidjson::kObjectType );
  320. rapidjson::Value value( rapidjson::kStringType );
  321. __SetCmd( JSON_CMD_VALUE_LOGIN, root, Allocator );
  322. value.SetString( Login.user_name.c_str(), Allocator );
  323. child.AddMember( JSON_KEY_USERNAME, value, Allocator );
  324. value.SetString( Login.user_password.c_str(), Allocator );
  325. child.AddMember( JSON_KEY_PASSWORD, value, Allocator );
  326. root.AddMember( JSON_ROOT_KEY_DATA, child, Allocator );
  327. root.Accept( writer );
  328. return sb.GetString();
  329. }
  330. std::string jsonBuilder::BuildCardPos( const std::map<uint64_t, _CARD_POS_>& CardPosList )
  331. {
  332. if(CardPosList.empty()){
  333. return "";
  334. }
  335. static bool p2s=false;
  336. p2s=p2s?false:true;
  337. rapidjson::StringBuffer sb;
  338. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  339. rapidjson::Document doc;
  340. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  341. rapidjson::Value root( rapidjson::kObjectType );
  342. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, Allocator );
  343. rapidjson::Value s( rapidjson::kObjectType );
  344. rapidjson::Value v( rapidjson::kObjectType );
  345. rapidjson::Value s_Detail( rapidjson::kArrayType );
  346. rapidjson::Value v_Detail( rapidjson::kArrayType );
  347. rapidjson::Value s_dept( rapidjson::kObjectType );
  348. rapidjson::Value v_dept( rapidjson::kObjectType );
  349. std::map<int, _STAT_DEPT_ITEM_> s_dept_map;
  350. std::map<int, _STAT_DEPT_ITEM_> v_dept_map;
  351. _STAT_DEPT_ITEM_ s_glbl_item;
  352. _STAT_DEPT_ITEM_ v_glbl_item;
  353. std::map<uint64_t, _CARD_POS_>::const_iterator mit_card;
  354. for ( mit_card = CardPosList.begin(); mit_card != CardPosList.end(); mit_card++ )
  355. {
  356. //不显示的卡不往前端推送
  357. //if ( !mit_card->second.display )
  358. //{
  359. // continue;
  360. //}
  361. //if(p2s && mit_card->second.Type == 1)continue;
  362. rapidjson::Value DetailItem;
  363. if ( !__BuildDetail( mit_card->second, Allocator, DetailItem ) )
  364. {
  365. continue;
  366. }
  367. if( mit_card->second.Type == 1)
  368. {
  369. s_Detail.PushBack( DetailItem, Allocator );
  370. __CreateDeptMap( mit_card->second, s_dept_map );
  371. __CreateDeptItem( mit_card->second, s_glbl_item );
  372. }
  373. else //车卡、采煤机等等
  374. {
  375. v_Detail.PushBack( DetailItem, Allocator );
  376. __CreateDeptMap( mit_card->second, v_dept_map );
  377. __CreateDeptItem( mit_card->second, v_glbl_item );
  378. }
  379. }
  380. s.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, s_Detail, Allocator );
  381. v.AddMember( JSON_ROOT_KEY_STATISTIC_DETAIL, v_Detail, Allocator );
  382. //stat
  383. rapidjson::Value s_stat( rapidjson::kObjectType );
  384. rapidjson::Value v_stat( rapidjson::kObjectType );
  385. if ( __BuildDept( s_dept_map, Allocator, s_dept ) )
  386. {
  387. s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, s_dept, Allocator );
  388. }
  389. if ( __BuildDept( v_dept_map, Allocator, v_dept ) )
  390. {
  391. v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_DEPT, v_dept, Allocator );
  392. }
  393. //glbl
  394. rapidjson::Value s_glbl( rapidjson::kObjectType );
  395. rapidjson::Value v_glbl( rapidjson::kObjectType );
  396. __BuildDeptItem( s_glbl_item, Allocator, s_glbl );
  397. __BuildDeptItem( v_glbl_item, Allocator, v_glbl );
  398. //s.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator );
  399. //v.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator );
  400. s_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, s_glbl, Allocator );
  401. v_stat.AddMember( JSON_ROOT_KEY_STATISTIC_GLOBAL, v_glbl, Allocator );
  402. s.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, s_stat, Allocator );
  403. v.AddMember( JSON_ROOT_KEY_STATISTIC_STAT, v_stat, Allocator );
  404. rapidjson::Value Data( rapidjson::kObjectType );
  405. Data.AddMember( JSON_ROOT_KEY_STATISTIC_STAFF_DATA, s, Allocator );
  406. Data.AddMember( JSON_ROOT_KEY_STATISTIC_VEHICLE_DATA, v, Allocator );
  407. root.AddMember( JSON_ROOT_KEY_DATA, Data, Allocator );
  408. __AddVersion( root, Allocator );
  409. root.Accept( writer );
  410. return sb.GetString();
  411. }
  412. std::string jsonBuilder::build_ios_card_pos(const _CARD_POS_& card)
  413. {
  414. rapidjson::StringBuffer sb;
  415. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  416. rapidjson::Document doc;
  417. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  418. rapidjson::Value root( rapidjson::kObjectType );
  419. // "cmd":
  420. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  421. // "version"
  422. __AddVersion(root, allocator);
  423. // "item"
  424. rapidjson::Value item(rapidjson::kObjectType);
  425. rapidjson::Value value;
  426. value.SetString(std::to_string(card.ID).c_str(), allocator);
  427. item.AddMember("cardid", value, allocator);
  428. item.AddMember("x", card.x, allocator);
  429. item.AddMember("y", card.y, allocator);
  430. item.AddMember("z", card.z, allocator);
  431. item.AddMember("acc", card.m_acc, allocator);
  432. item.AddMember("speed", card.speed, allocator);
  433. item.AddMember("acc_x", card.m_tri_acc[0], allocator);
  434. item.AddMember("acc_y", card.m_tri_acc[1], allocator);
  435. item.AddMember("acc_z", card.m_tri_acc[2], allocator);
  436. item.AddMember("ang_x", card.m_tri_ang[0], allocator);
  437. item.AddMember("ang_y", card.m_tri_ang[1], allocator);
  438. item.AddMember("ang_z", card.m_tri_ang[2], allocator);
  439. item.AddMember("steps", card.m_walking_step, allocator);
  440. item.AddMember("jump", card.m_jump_count, allocator);
  441. item.AddMember("hang_time", card.m_hang_time, allocator);
  442. item.AddMember("hang_height", card.m_hang_height, allocator);
  443. /*rapidjson::Value data(rapidjson::kObjectType);
  444. data.AddMember("card_type", 1 , allocator);
  445. data.AddMember("cardid", "10", allocator);
  446. data.AddMember("x", 10.0 , allocator);
  447. data.AddMember("y", 20.0 , allocator);
  448. data.AddMember("z", 30.0, allocator);
  449. data.AddMember("acc" , 10.0 , allocator);
  450. data.AddMember("speed", 3.5 , allocator);
  451. data.AddMember("acc_x", 0.1, allocator);
  452. data.AddMember("acc_y", 0.2 , allocator);
  453. data.AddMember("acc_z", 0.3 , allocator);
  454. data.AddMember("ang_x", 0.4, allocator);
  455. data.AddMember("ang_y", 0.5 , allocator);
  456. data.AddMember("ang_z", 0.6 , allocator);
  457. data.AddMember("steps", 7, allocator);
  458. data.AddMember("jump", 8 , allocator);
  459. data.AddMember("hang_time", 3 , allocator);
  460. data.AddMember("hang_height", 4, allocator);
  461. */
  462. root.AddMember("data", item, allocator);
  463. root.Accept(writer);
  464. return sb.GetString();
  465. }
  466. std::string jsonBuilder::build_tmp_card_pos(const std::map<uint64_t,_CARD_POS_>& cards)
  467. {
  468. rapidjson::StringBuffer sb;
  469. rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
  470. rapidjson::Document doc;
  471. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  472. rapidjson::Value root(rapidjson::kObjectType);
  473. // "cmd":
  474. __SetCmd( JSON_CMD_VALUE_POS_MAP, root, allocator );
  475. // "data"
  476. rapidjson::Value item(rapidjson::kArrayType);
  477. for(auto it = cards.begin(); it != cards.end(); ++it)
  478. {
  479. rapidjson::Value elem(rapidjson::kArrayType);
  480. rapidjson::Value tmp_obj(rapidjson::kObjectType);
  481. // 卡号
  482. tmp_obj.SetString(__FmtCardID(it->second).c_str(), allocator);
  483. elem.PushBack(tmp_obj, allocator);
  484. // x坐标
  485. tmp_obj.SetDouble(it->second.x);
  486. elem.PushBack(tmp_obj, allocator);
  487. // y坐标
  488. tmp_obj.SetDouble(it->second.y);
  489. elem.PushBack(tmp_obj, allocator);
  490. // z坐标
  491. tmp_obj.SetDouble(it->second.z);
  492. elem.PushBack(tmp_obj, allocator);
  493. // 时间戳
  494. tmp_obj.SetDouble(it->second.rec_time);
  495. elem.PushBack(tmp_obj, allocator);
  496. // 加速度
  497. tmp_obj.SetDouble(it->second.m_acc);
  498. elem.PushBack(tmp_obj, allocator);
  499. // 速度
  500. tmp_obj.SetDouble(it->second.speed);
  501. elem.PushBack(tmp_obj, allocator);
  502. item.PushBack(elem, allocator);
  503. }
  504. root.AddMember("data", item, allocator);
  505. // "version"
  506. __AddVersion(root, allocator);
  507. root.Accept(writer);
  508. return sb.GetString();
  509. }
  510. std::string jsonBuilder::BuildSpecialAreaProcess( const _BASE_CARD_ & stCard )
  511. {
  512. rapidjson::StringBuffer sb;
  513. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  514. rapidjson::Document doc;
  515. rapidjson::Document::AllocatorType& Allocator = doc.GetAllocator();
  516. rapidjson::Value root( rapidjson::kObjectType );
  517. __SetCmd( JSON_CMD_VALUE_SPECIAL_AREA_UP_MINE, root, Allocator );
  518. rapidjson::Value CardItem( rapidjson::kObjectType );
  519. rapidjson::Value CardList( rapidjson::kArrayType );
  520. __BuildBaseCardItem( stCard, Allocator, CardItem );
  521. CardList.PushBack( CardItem, Allocator );
  522. root.AddMember( JSON_ROOT_KEY_DATA, CardList, Allocator );
  523. root.Accept( writer );
  524. return sb.GetString();
  525. }
  526. bool jsonBuilder::ParseCall_Card_Req( const std::string& JasonText, _JS_CALL_CARD_REQ_& CallCardReq, std::string& Error )
  527. {
  528. rapidjson::Document doc;
  529. if ( doc.Parse( JasonText.c_str() ).HasParseError() )
  530. {
  531. Error = "Failed to parse jason text!";
  532. return false;
  533. }
  534. rapidjson::Value::MemberIterator itValue;
  535. itValue = doc.FindMember( "data" );
  536. if ( itValue == doc.MemberEnd() )
  537. {
  538. Error = "Failed to find member: data!";
  539. return false;
  540. }
  541. rapidjson::Value Data;
  542. rapidjson::Document::AllocatorType allocator;
  543. Data.CopyFrom( itValue->value, allocator );
  544. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TYPE ) )
  545. {
  546. Error = "Failed to find value: ";
  547. Error += JSON_KEY_CALL_CARD_CALL_TYPE;
  548. return false;
  549. }
  550. CallCardReq.call_type_id = Data[JSON_KEY_CALL_CARD_CALL_TYPE].GetInt();
  551. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_TIME_OUT ) )
  552. {
  553. Error = "Failed to find value: ";
  554. Error += JSON_KEY_CALL_CARD_CALL_TIME_OUT;
  555. return false;
  556. }
  557. CallCardReq.call_time_out = Data[JSON_KEY_CALL_CARD_CALL_TIME_OUT].GetInt();
  558. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CALL_LEVEL ) )
  559. {
  560. Error = "Failed to find value: ";
  561. Error += JSON_KEY_CALL_CARD_CALL_LEVEL;
  562. return false;
  563. }
  564. CallCardReq.call_time_interval = Data[JSON_KEY_CALL_CARD_CALL_LEVEL].GetInt();
  565. if ( !Data.HasMember( JSON_KEY_CALL_CARD_USER_NAME ) )
  566. {
  567. Error = "Failed to find value: ";
  568. Error += JSON_KEY_CALL_CARD_USER_NAME;
  569. return false;
  570. }
  571. CallCardReq.user_name = Data[JSON_KEY_CALL_CARD_USER_NAME].GetString();
  572. //data::stations
  573. if ( !Data.HasMember( JSON_KEY_CALL_CARD_STATIONS ) )
  574. {
  575. Error = "Failed to find value: ";
  576. Error += JSON_KEY_CALL_CARD_STATIONS;
  577. return false;
  578. }
  579. rapidjson::Value & stations = Data[JSON_KEY_CALL_CARD_STATIONS];
  580. if ( !stations.IsArray() )
  581. {
  582. Error = "data::stations is not a valid array!";
  583. return false;
  584. }
  585. for ( int i = 0; i < (int)stations.Size(); ++i )
  586. {
  587. rapidjson::Value & v = stations[i];
  588. if ( !v.IsObject() )
  589. {
  590. char szError[1024] = { 0 };
  591. sprintf( szError, "data::stations[%d] is not a object!", i );
  592. Error = szError;
  593. return false;
  594. }
  595. _JS_STATION_ Station;
  596. if ( !v.HasMember( JSON_KEY_CALL_CARD_STATION_ID ) && v[JSON_KEY_CALL_CARD_STATION_ID].IsInt() )
  597. {
  598. Error = "Error, failed to found value: ";
  599. Error += JSON_KEY_CALL_CARD_STATION_ID;
  600. return false;
  601. }
  602. Station.stationid = v[JSON_KEY_CALL_CARD_STATION_ID].GetInt();
  603. CallCardReq.stations.push_back( Station );
  604. }
  605. //data::cards
  606. if ( !Data.HasMember( JSON_KEY_CALL_CARD_CARDS ) )
  607. {
  608. Error = "Failed to find value: ";
  609. Error += JSON_KEY_CALL_CARD_CARDS;
  610. return false;
  611. }
  612. rapidjson::Value & cards = Data[JSON_KEY_CALL_CARD_CARDS];
  613. for ( int i = 0; i < (int)cards.Size(); ++i )
  614. {
  615. rapidjson::Value & v = cards[i];
  616. if ( !v.IsObject() )
  617. {
  618. char szError[1024] = { 0 };
  619. sprintf( szError, "data::cards[%d] is not a object!", i );
  620. Error = szError;
  621. return false;
  622. }
  623. _JS_CARD_ Card;
  624. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_ID ) && v[JSON_KEY_CALL_CARD_CARD_ID].IsInt() )
  625. {
  626. Error = "Error, failed to found value: ";
  627. Error += JSON_KEY_CALL_CARD_CARD_ID;
  628. return false;
  629. }
  630. Card.cardid = v[JSON_KEY_CALL_CARD_CARD_ID].GetInt();
  631. if ( !v.HasMember( JSON_KEY_CALL_CARD_CARD_TYPE_ID ) && v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].IsInt() )
  632. {
  633. Error = "Error, failed to found value: ";
  634. Error += JSON_KEY_CALL_CARD_CARD_TYPE_ID;
  635. return false;
  636. }
  637. Card.cardtype = v[JSON_KEY_CALL_CARD_CARD_TYPE_ID].GetInt();
  638. CallCardReq.cards.push_back( Card );
  639. }
  640. return true;
  641. }
  642. std::string jsonBuilder::build_traffic_light(const std::vector<light_state> lights)
  643. {
  644. if(lights.size() == 0){
  645. return "";
  646. }
  647. rapidjson::StringBuffer sb;
  648. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  649. rapidjson::Document doc;
  650. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  651. rapidjson::Value root(rapidjson::kObjectType );
  652. __SetCmd(JSON_KEY_CALL_LIGHT_LIGHT_STATE, root, allocator);
  653. rapidjson::Value data(rapidjson::kArrayType);
  654. for(auto it : lights){
  655. rapidjson::Value elem(rapidjson::kArrayType);
  656. rapidjson::Value tmp_object(rapidjson::kObjectType);
  657. tmp_object.SetInt(it.m_group_id);
  658. elem.PushBack(tmp_object, allocator);
  659. tmp_object.SetInt(it.m_light_id);
  660. elem.PushBack(tmp_object, allocator);
  661. tmp_object.SetInt(it.m_light_state);
  662. elem.PushBack(tmp_object, allocator);
  663. tmp_object.SetString(it.m_card_id.c_str(), allocator);
  664. elem.PushBack(tmp_object, allocator);
  665. data.PushBack(elem, allocator);
  666. }
  667. root.AddMember("data", data, allocator);
  668. __AddVersion(root, allocator);
  669. root.Accept(writer);
  670. return sb.GetString();
  671. }
  672. /*
  673. * 构建设备状态json
  674. *
  675. * 参数
  676. * const std::vector<device_state> vtd 设备状态列表
  677. *
  678. * 返回值
  679. * 设备状态json
  680. *
  681. * */
  682. std::string jsonBuilder::build_device_state(const std::vector<device_state> vtd)
  683. {
  684. if(vtd.size() == 0){
  685. return "";
  686. }
  687. rapidjson::StringBuffer sb;
  688. rapidjson::Writer<rapidjson::StringBuffer> writer( sb );
  689. rapidjson::Document doc;
  690. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  691. rapidjson::Value root(rapidjson::kObjectType );
  692. __SetCmd(JSON_CMD_VALUE_DEV_STATE, root, allocator);
  693. rapidjson::Value data(rapidjson::kArrayType);
  694. for(auto it : vtd){
  695. rapidjson::Value elem(rapidjson::kArrayType);
  696. rapidjson::Value tmp_object(rapidjson::kObjectType);
  697. tmp_object.SetInt(it.m_id);
  698. elem.PushBack(tmp_object, allocator);
  699. tmp_object.SetInt(it.m_type);
  700. elem.PushBack(tmp_object, allocator);
  701. tmp_object.SetInt(it.m_state);
  702. elem.PushBack(tmp_object, allocator);
  703. tmp_object.SetDouble(it.m_cur_time);
  704. elem.PushBack(tmp_object, allocator);
  705. data.PushBack(elem, allocator);
  706. }
  707. root.AddMember("data", data, allocator);
  708. __AddVersion(root, allocator);
  709. root.Accept(writer);
  710. return sb.GetString();
  711. }
  712. }