jsonBuilder.cpp 29 KB

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