module_call.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. #include "module_call.h"
  2. #include <boost/bind.hpp>
  3. #include <boost/algorithm/string.hpp>
  4. #include <vector>
  5. #include <set>
  6. #include <string>
  7. #include <chrono>
  8. #include <algorithm>
  9. #include <area.h>
  10. #include "common_tool.h"
  11. #include "tool_time.h"
  12. #include "ant.h"
  13. #include "websocket/constdef.h"
  14. #include "websocket/wsClientMgr.h"
  15. #include "crc.h"
  16. #include "mine.h"
  17. #include "event.h"
  18. /*
  19. * @brief 根据卡类型与卡号构造key
  20. * @param 无
  21. * @return 返回生成值
  22. * @note
  23. * @warning
  24. * @bug
  25. * */
  26. uint64_t call_card::to_id64()
  27. {
  28. return tool_other::type_id_to_u64(cardtype, cardid);
  29. }
  30. /*
  31. * @brief 根据卡类型与卡号构造卡字符串,形如:0010000001200
  32. * @param 无
  33. * @return 返回卡号
  34. * @note
  35. * @warning
  36. * @bug
  37. * */
  38. std::string call_card::to_id64_str()
  39. {
  40. return tool_other::type_id_to_str(cardtype, cardid);
  41. }
  42. /*
  43. * @brief 根据卡类型与卡号构造key
  44. * @param 无
  45. * @return 返回生成值
  46. * @note
  47. * @warning
  48. * @bug
  49. * */
  50. bool call_card::is_timeout()
  51. {
  52. uint32_t seconds = tool_time::elapse_seconds(call_time);
  53. return (seconds >= call_time_out*60);
  54. }
  55. bool module_call::call_user::is_timeout()
  56. {
  57. uint32_t seconds = tool_time::elapse_seconds(call_time);
  58. return (seconds >= call_time_out*60);
  59. }
  60. void module_call::to_node_element(rapidjson::Value& out_elemet,
  61. const std::shared_ptr<call_card> card_ptr,
  62. rapidjson::Document::AllocatorType& allocator,bool f)
  63. {
  64. tool_json::push_back(out_elemet, card_ptr->user_name, allocator);
  65. tool_json::push_back(out_elemet, card_ptr->to_id64_str(), allocator);
  66. uint32_t sid= card_ptr->stationid;
  67. if(f)sid=0;
  68. out_elemet.PushBack(sid, allocator);
  69. out_elemet.PushBack(tool_time::to_ms(card_ptr->call_time), allocator);
  70. out_elemet.PushBack(card_ptr->call_type_id, allocator);
  71. out_elemet.PushBack(card_ptr->call_state, allocator);
  72. }
  73. void module_call::run()
  74. {
  75. std::vector<call_card_ptr> arr;
  76. get_all_call_cards(arr);
  77. if(arr.empty())
  78. {
  79. return ;
  80. }
  81. //转发给web
  82. //swsClientMgr.send(JSON_CMD_VALUE_PUSH, to_call_card_list_json(arr));
  83. std::string ac = get_json_anti_collision();
  84. if(!ac.empty()){
  85. log_info("send2web: %s", ac.c_str());
  86. swsClientMgr.send(JSON_CMD_VALUE_PUSH, ac);
  87. }
  88. //将呼叫命令发送给标识卡终端
  89. call_site_map site_map;
  90. get_site_map(arr, site_map);
  91. if(!site_map.empty())
  92. {
  93. send_to_sites(site_map);
  94. }
  95. }
  96. ///发呼叫的报文数据给分站
  97. void module_call::send_to_sites(call_site_map& site_map)
  98. {
  99. auto iter_site = site_map.begin();
  100. for(; iter_site != site_map.end(); ++iter_site)
  101. {
  102. if(iter_site->second->cards.size() > 254)
  103. {
  104. log_error("呼叫:分站下卡数量过多:分站id=%d, 卡数=%d",
  105. iter_site->first, iter_site->second->cards.size());
  106. continue;
  107. }
  108. std::vector<char> arr;
  109. int16_t cmd = 0x77ab;
  110. arr.push_back(cmd>>8 & 0xff);
  111. arr.push_back(cmd & 0xff);
  112. //分站地址 4字节,支持大小分站呼叫协议;added by zhuyf 2018/06/04
  113. //大小分站的全员呼叫,分站id要求4字节全为FF
  114. uint32_t anchor_id = (iter_site->second->is_call_all()? 0xffffffff : static_cast<uint32_t>(iter_site->first));
  115. memcpy_uint32(arr,anchor_id);
  116. //卡数
  117. arr.push_back(static_cast<char>(iter_site->second->cards.size()));
  118. //呼叫类型
  119. arr.push_back(static_cast<char>(iter_site->second->call_type_id));
  120. if(!iter_site->second->is_call_all())
  121. {
  122. //循环添加卡信息
  123. auto it_card = iter_site->second->cards.begin();
  124. for(; it_card != iter_site->second->cards.end(); ++it_card)
  125. {
  126. //呼叫类型
  127. arr.push_back(static_cast<char>(it_card->second->call_level_id));
  128. //卡类型
  129. arr.push_back(static_cast<char>(it_card->second->cardtype));
  130. //卡ID长度
  131. int id_len = 4;
  132. arr.push_back(static_cast<char>(id_len));
  133. //卡ID
  134. memcpy_uint32(arr, it_card->second->cardid);
  135. }
  136. }
  137. auto sit_ptr = sit_list::instance()->get(iter_site->first);
  138. if(!sit_ptr || !sit_ptr->m_clt)
  139. {
  140. log_error("在全局分站列表中找不到分站 或者sit_ptr->m_clt==null:分站id=%d", iter_site->first);
  141. continue;
  142. }
  143. int16_t len=arr.size()+2;
  144. int16_t crc=do_crc_1(arr,0);
  145. auto it= arr.begin();arr.insert(it,len&0xff);
  146. it= arr.begin();arr.insert(it,len>>8&0xff);
  147. arr.push_back(crc>>8&0xff);arr.push_back(crc&0xff);
  148. std::vector<char> arr2 = arr;
  149. sit_ptr->m_clt->send(std::move(arr));
  150. print_test(std::move(arr2), iter_site->first);
  151. }
  152. }
  153. void module_call::print_test(std::vector<char>&& arr, int siteid)
  154. {
  155. std::string str("呼叫发送的数据帧,分站号=");
  156. str.append(std::to_string(siteid));
  157. str.append(":");
  158. char a[4]={0};
  159. for(std::vector<char>::size_type i=0; i<arr.size(); i++)
  160. {
  161. sprintf(a, "%02X ", static_cast<unsigned char>(arr[i]));
  162. str.append(std::string(a));
  163. }
  164. log_info("%s", str.c_str());
  165. }
  166. void module_call::rev_from_card_resp(std::shared_ptr<card_location_base> card_ptr)
  167. {
  168. card_ptr->get_mine_tool()->m_status_call = CALL_SUCCESSED;
  169. }
  170. /*
  171. web发给采集:发起呼叫
  172. {
  173. cmd: 'call_card_req',
  174. data: {
  175. call_type_id: this.cal_type, // 全员呼叫:0 定员呼叫:1
  176. call_time_out: this.choosedTime, // 呼叫时长
  177. call_level_id: this.call_type_id, // 呼叫类型 一般呼叫:1 紧急呼叫:2
  178. user_name: xdata.userName, // 呼叫人
  179. call_time: time, // 呼叫时间戳
  180. stations: [{ stationid: 0 }], // 分站 0为全员
  181. cards: cards // 人员 0为全员 //旧代码为map格式 [{ stationid: 0 }]
  182. }
  183. }
  184. */
  185. void module_call::accept_call(sio::message::ptr const& data)
  186. {
  187. int call_type = -1, call_level = 1, call_time_interval = -1;
  188. uint32_t call_time_out = 0;
  189. std::string user_name = "";
  190. call_time_interval = 5;
  191. if(!tool_map::try_get_value(call_type, JSON_KEY_CALL_CARD_CALL_TYPE, data)
  192. || !tool_map::try_get_value(call_level, JSON_KEY_CALL_CARD_CALL_LEVEL, data)
  193. || !tool_map::try_get_value(call_time_out, JSON_KEY_CALL_CARD_CALL_TIME_OUT, data)
  194. //|| !tool_map::try_get_value(call_time_interval, JSON_KEY_CALL_CARD_CALL_TIME, data)
  195. || !tool_map::try_get_value(user_name, JSON_KEY_CALL_CARD_USER_NAME, data)
  196. )
  197. {
  198. log_error("发起呼叫: 收到的json不对,解析不出call_type = %d, call_level = %d, \
  199. call_time_out=%d, call_time_interval=%d,user_name=%s", call_type, call_level, call_time_out, call_time_interval, user_name.c_str());
  200. return;
  201. }
  202. if(user_name.empty())
  203. {
  204. log_error("发起呼叫: user_name用户名为空");
  205. return;
  206. }
  207. std::vector<call_card_ptr> result_arr;//结果集
  208. call_user_ptr user_ptr;
  209. if(CCT_CALL_ALL==call_type)// 全员
  210. {
  211. user_ptr = call_user_ptr(new call_user());
  212. user_ptr->call_time = std::chrono::system_clock::now();
  213. user_ptr->call_time_out = call_time_out;
  214. user_ptr->call_time_interval = call_time_interval;
  215. user_ptr->call_type_id = call_type;
  216. user_ptr->call_level_id = call_level;
  217. user_ptr->user_name = user_name;
  218. call_card_ptr temp(new call_card());
  219. copy(user_ptr, temp);
  220. temp->cardid = CCT_CALL_ALL; //全员卡
  221. temp->call_state = CALL_ING;
  222. result_arr.push_back(temp);//增加到结果集中
  223. //添加全员卡
  224. user_ptr->cards.insert(std::make_pair(temp->cardid, temp));
  225. //更新用户信息
  226. std::lock_guard<std::mutex> lock(_mutex);
  227. _map[user_name]=user_ptr;
  228. }
  229. else// 定员: 如果有全员,定员不生效
  230. {
  231. std::lock_guard<std::mutex> lock(_mutex);
  232. auto user_map_ptr = _map.find(user_name);
  233. if(_map.end() == user_map_ptr)//没有这个用户就新建并加入
  234. {
  235. user_ptr=call_user_ptr(new call_user());
  236. _map[user_name]=user_ptr;
  237. }
  238. else//有这个用户
  239. {
  240. user_ptr=user_map_ptr->second;
  241. // 如果有全员,定员不生效
  242. auto card_temp = user_ptr->cards.find(CCT_CALL_ALL);
  243. if(user_ptr->cards.end() != card_temp)
  244. {
  245. return;
  246. }
  247. }
  248. //更新用户信息
  249. user_ptr->call_time=std::chrono::system_clock::now();
  250. user_ptr->call_time_out=call_time_out;
  251. user_ptr->call_time_interval = call_time_interval;
  252. user_ptr->call_type_id=call_type;
  253. user_ptr->call_level_id=call_level;
  254. user_ptr->user_name=user_name;
  255. std::vector<sio::message::ptr> card_vec;
  256. if(!tool_map::try_get_value(card_vec, JSON_KEY_CALL_CARD_CARDS, data) || card_vec.size() == 0)
  257. {
  258. log_error("发起呼叫,web发来的数据JSON_KEY_CALL_CARD_CARDS字段为空 或者不是数组");
  259. return;
  260. }
  261. add_cards_to_user(card_vec, user_ptr);
  262. for(auto& it:user_ptr->cards)//添加到结果集中
  263. {
  264. result_arr.push_back(it.second);
  265. }
  266. }
  267. //组装json发送给web
  268. if(!result_arr.empty())
  269. {
  270. response_accept_call(result_arr);
  271. }
  272. }
  273. //采集回复web发起呼叫
  274. //{
  275. // 'cmd': 'call_card_resp',
  276. // 'data': [
  277. // ["fjb", // 发起呼叫人
  278. // "0010000000001", // 呼叫的卡号
  279. // 103, // 分站号
  280. // 23431, // 发起呼叫时间
  281. // 0, //
  282. // 0 // 正在呼叫:2/结束呼叫/
  283. // ]
  284. // ]
  285. //}
  286. void module_call::response_accept_call(std::vector<call_card_ptr> cards)
  287. {
  288. rapidjson::Document doc(rapidjson::kObjectType);
  289. auto& allocator = doc.GetAllocator();
  290. rapidjson::Value node_cards(rapidjson::kArrayType);
  291. //组装json发送给web
  292. to_node_array(node_cards, cards, allocator,false);
  293. doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_CALL_CARD_RESPONSE, allocator);
  294. doc.AddMember(JSON_ROOT_KEY_VERSION, INTERFACE_VERSION, allocator);
  295. doc.AddMember(JSON_ROOT_KEY_DATA, node_cards, allocator);
  296. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  297. }
  298. //web发给采集:取消呼叫
  299. //{
  300. // cmd: 'call_card_cancel_req',
  301. // data: {
  302. // call_type_id: type, // 全员/定员
  303. // user_name: xdata.userName, // 取消人
  304. // call_time: callTime, // 时间戳
  305. // stations: this.stationid, // 分站
  306. // cards: this.cards // 取消呼叫卡 //旧代码为map格式 [{ stationid: 0 }]
  307. // }
  308. //}
  309. void module_call::accept_cancel(sio::message::ptr const& node_data)
  310. {
  311. int call_type = -1;//, call_level = -1;
  312. std::string user_name;
  313. int64_t call_time = 0;
  314. if(!tool_map::try_get_value(call_type, JSON_KEY_CALL_CARD_CALL_TYPE, node_data)
  315. || !tool_map::try_get_value(call_time, JSON_KEY_CALL_CARD_CALL_TIME, node_data)
  316. || !tool_map::try_get_value(user_name, JSON_KEY_CALL_CARD_USER_NAME, node_data)
  317. )
  318. {
  319. log_error("取消呼叫: 收到的json不对,解析不出int call_type = %d, call_time = %d,user_name=%s", call_type, call_time, user_name.c_str());
  320. return;
  321. }
  322. if(user_name.empty())
  323. {
  324. log_error("取消呼叫: user_name用户名为空");
  325. return;
  326. }
  327. call_user_ptr result_user_ptr(new call_user());
  328. result_user_ptr->call_type_id=call_type;
  329. // result_user_ptr->call_level_id=call_level;
  330. result_user_ptr->call_time = std::chrono::system_clock::time_point(std::chrono::milliseconds(call_time));
  331. result_user_ptr->user_name=user_name;
  332. //取消呼叫有两种:
  333. //取消全员呼叫
  334. //对指定卡取消呼叫
  335. if(CCT_CALL_ALL==call_type)// 取消全员呼叫
  336. {
  337. //删除用户
  338. std::lock_guard<std::mutex> lock(_mutex);
  339. _map.erase(user_name);
  340. }
  341. else// 取消定员呼叫
  342. {
  343. std::vector<sio::message::ptr> card_vec;
  344. if(!tool_map::try_get_value(card_vec, JSON_KEY_CALL_CARD_CARDS, node_data) || card_vec.size() == 0)
  345. {
  346. log_error("取消呼叫: web发来的数据cards字段为空 或者不是数组");
  347. return;
  348. }
  349. std::lock_guard<std::mutex> lock(_mutex);
  350. call_user_ptr user_ptr(new call_user());
  351. auto it_map = _map.find(user_name);
  352. if(_map.end()!=it_map)//呼叫用户
  353. {
  354. user_ptr=it_map->second;
  355. }
  356. std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
  357. std::string s_card_id;
  358. for(; it_card != card_vec.end(); ++it_card)
  359. {
  360. if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card)))
  361. {
  362. log_error("取消呼叫: web发来的数据 card_id 格式不对");
  363. continue;
  364. }
  365. uint32_t id = tool_other::id64_to_id(s_card_id);
  366. int type= tool_other::id64_to_type(s_card_id);
  367. log_info("取消呼叫: cardid=%d, cardtype=%d", id, type);
  368. call_card_ptr card_ptr(new call_card());
  369. card_ptr->cardid = id;
  370. card_ptr->cardtype = type;
  371. result_user_ptr->cards[card_ptr->cardid]=card_ptr;//增加到结果集中
  372. user_ptr->cards.erase(card_ptr->cardid);//删除这个卡
  373. }
  374. // 没有呼叫信息,删除该用户记录
  375. if(user_ptr->cards.empty())
  376. {
  377. _map.erase(user_name);
  378. }
  379. }
  380. //如果要取消呼叫的用户下没有卡,建一张CCT_CALL_ALL卡返回
  381. if(result_user_ptr->cards.empty())
  382. {
  383. // result_user_ptr->call_level_id=call_level;
  384. //result_user_ptr->call_time=std::chrono::system_clock::now();
  385. result_user_ptr->call_time_out=0;
  386. result_user_ptr->call_type_id=call_type;
  387. result_user_ptr->user_name=user_name;
  388. call_card_ptr card_ptr(new call_card());
  389. card_ptr->cardid=CCT_CALL_ALL;
  390. result_user_ptr->cards[card_ptr->cardid]=card_ptr;
  391. }
  392. //转发给web
  393. response_accept_cancel(result_user_ptr);
  394. }
  395. /*
  396. 采集回复web:取消呼叫
  397. {
  398. "user_name":"fjb", // 取消人
  399. "call_time": 23432, // 发起呼叫时间
  400. "stations":[{"stationid":102}], // 分站号
  401. "cards":[{"cardid":"0","cardtype":"1"}] // 取消的卡
  402. }
  403. */
  404. void module_call::response_accept_cancel(const call_user_ptr user_ptr)
  405. {
  406. rapidjson::Document doc(rapidjson::kObjectType);
  407. auto& allocator = doc.GetAllocator();
  408. rapidjson::Value node_data(rapidjson::kObjectType);
  409. tool_json::add_member(node_data, JSON_KEY_CALL_CARD_USER_NAME, user_ptr->user_name, allocator);
  410. node_data.AddMember(JSON_KEY_CALL_CARD_CALL_TIME, tool_time::to_ms(user_ptr->call_time), allocator);
  411. node_data.AddMember(JSON_KEY_CALL_CARD_CALL_TYPE, user_ptr->call_type_id, allocator);
  412. rapidjson::Value node_cards(rapidjson::kArrayType);
  413. //加入取消呼叫的卡
  414. auto iter = user_ptr->cards.begin();
  415. for(;iter != user_ptr->cards.end();++iter)
  416. {
  417. rapidjson::Value node_card(rapidjson::kObjectType);
  418. tool_json::add_member(node_card, JSON_KEY_CALL_CARD_CARD_ID, iter->second->to_id64_str(), allocator);
  419. node_card.AddMember(JSON_KEY_CALL_CARD_CARD_TYPE_ID,iter->second->cardtype, allocator);
  420. node_cards.PushBack(node_card,allocator);
  421. }
  422. node_data.AddMember(JSON_KEY_CALL_CARD_CARDS, node_cards, allocator);
  423. doc.AddMember(JSON_ROOT_KEY_CMD, JSON_CMD_VALUE_CALL_CARD_CANCEL_RESPONSE, allocator);
  424. doc.AddMember(JSON_ROOT_KEY_VERSION, INTERFACE_VERSION, allocator);
  425. doc.AddMember(JSON_ROOT_KEY_DATA, node_data, allocator);
  426. //转发给web
  427. swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
  428. }
  429. /*
  430. 登陆时,采集发送web:井下所有呼叫
  431. {"cmd":"callcardlist","data":[
  432. ["fjb", // 发起呼叫人
  433. "0010000000001", // 呼叫的卡号
  434. 103, // 分站号
  435. 23431, // 发起呼叫时间
  436. 0, //
  437. 0 // 正在呼叫:2/结束呼叫/
  438. ]
  439. ]}
  440. */
  441. std::string module_call::accept_login()
  442. {
  443. std::vector<call_card_ptr> arr;
  444. get_all_call_cards(arr);
  445. if(arr.empty())
  446. {
  447. return "";
  448. }
  449. return to_call_card_list_json(arr);
  450. }
  451. std::string module_call::to_call_card_list_json(std::vector<call_card_ptr> arr)
  452. {
  453. rapidjson::Document doc(rapidjson::kObjectType);
  454. auto& allocator = doc.GetAllocator();
  455. rapidjson::Value node_cards(rapidjson::kArrayType);
  456. to_node_array(node_cards, arr, allocator);
  457. doc.AddMember(JSON_ROOT_KEY_CMD, JSON_CMD_VALUE_CALL_CARD_LIST, allocator);
  458. doc.AddMember(JSON_ROOT_KEY_VERSION, INTERFACE_VERSION, allocator);
  459. doc.AddMember(JSON_ROOT_KEY_DATA, node_cards, allocator);
  460. return tool_json::doc_to_json(doc);
  461. }
  462. void module_call::add_cards_to_user(const std::vector<sio::message::ptr>& card_vec, call_user_ptr user_ptr)
  463. {
  464. std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
  465. for(; it_card != card_vec.end(); ++it_card)
  466. {
  467. std::string s_card_id;
  468. if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
  469. ||s_card_id.empty())
  470. {
  471. log_error("发起呼叫,web发来的数据 card_id 格式不对 或为空");
  472. continue;
  473. }
  474. uint32_t id = tool_other::id64_to_id(s_card_id);
  475. int type= tool_other::id64_to_type(s_card_id);
  476. log_info("发起呼叫 cardid=%d, cardtype=%d", id, type);
  477. call_card_ptr card_ptr(new call_card());
  478. card_ptr->cardid = id;
  479. card_ptr->cardtype = type;
  480. copy(user_ptr, card_ptr);
  481. card_ptr->call_state = CALL_ING;
  482. user_ptr->cards[card_ptr->cardid]=card_ptr;
  483. }
  484. }
  485. void module_call::get_user_all_call_cards(call_user_ptr& user_ptr,
  486. const std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>& cardlist,
  487. std::vector<call_card_ptr>& out_data)
  488. {
  489. auto& cards=user_ptr->cards;
  490. int status = CALL_ING;
  491. //如果是全员呼叫,增加所有卡
  492. auto card_it = cards.find(CCT_CALL_ALL);
  493. if(cards.end()!=card_it)
  494. {
  495. if(user_ptr->is_timeout())//呼叫超时, 清空全员卡, 并设置呼叫状态为结束
  496. {
  497. log_info("全员呼叫发送线程:呼叫用户超时,用户名=%s", user_ptr->user_name.c_str());
  498. cards.clear();
  499. status=CALL_END;
  500. }
  501. auto g_it=cardlist.begin();
  502. for(;g_it!=cardlist.end();++g_it)//如果是全员呼叫,增加所有卡
  503. {
  504. auto site_ptr = g_it->second->get_area_tool()->m_site;
  505. if(!site_ptr)
  506. {
  507. continue;
  508. }
  509. call_card_ptr card_ptr(new call_card());
  510. copy(user_ptr, card_ptr);
  511. card_ptr->cardid = g_it->second->m_id;
  512. card_ptr->cardtype = g_it->second->m_type;
  513. card_ptr->stationid=site_ptr->id();
  514. card_ptr->call_state=status;
  515. if(CALL_SUCCESSED == g_it->second->get_mine_tool()->m_status_call)
  516. {
  517. card_ptr->call_state = CALL_SUCCESSED;
  518. }
  519. else
  520. {
  521. g_it->second->get_mine_tool()->m_status_call = CALL_ING;
  522. }
  523. if(card_ptr->is_timeout())
  524. {
  525. g_it->second->get_mine_tool()->m_status_call = 0;
  526. }
  527. card_ptr->is_display = g_it->second->m_display;
  528. out_data.push_back(card_ptr);
  529. }
  530. return;
  531. }
  532. //定员呼叫
  533. auto iter_card = cards.begin();
  534. for(;iter_card!=cards.end();++iter_card)
  535. {
  536. //增加到结果集中
  537. out_data.push_back(iter_card->second);
  538. auto g_card = cardlist.find(iter_card->second->to_id64());
  539. if(cardlist.end()==g_card)//在全局卡列表中没有这张卡,
  540. {
  541. log_error("定员呼叫发送线程:全局卡列表中没有这张卡,卡id=%d,卡类型=%d",
  542. iter_card->second->cardid, iter_card->second->cardtype);
  543. iter_card->second->call_state = CALL_FAILED;
  544. cards.erase(iter_card--); //在用户下删除这张卡
  545. continue;
  546. }
  547. auto site_ptr = g_card->second->get_area_tool()->m_site;
  548. if(!site_ptr)
  549. {
  550. log_error("定员呼叫发送线程:全局卡列表中这张卡已经上井或无效,卡id=%d,卡类型=%d",
  551. iter_card->second->cardid, iter_card->second->cardtype);
  552. iter_card->second->call_state = CALL_FAILED;
  553. cards.erase(iter_card--); //在用户下删除这张卡
  554. continue;
  555. }
  556. //更新卡的分站id
  557. iter_card->second->stationid = site_ptr->id();
  558. if(CALL_SUCCESSED == g_card->second->get_mine_tool()->m_status_call)
  559. {
  560. iter_card->second->call_state = CALL_SUCCESSED;
  561. }
  562. else
  563. {
  564. g_card->second->get_mine_tool()->m_status_call = CALL_ING;
  565. }
  566. if(iter_card->second->is_timeout())//呼叫超时
  567. {
  568. log_info("定员呼叫发送线程:呼叫卡超时,用户名=%s,卡id=%d,卡类型=%d",
  569. iter_card->second->user_name.c_str(),
  570. iter_card->second->cardid, iter_card->second->cardtype);
  571. iter_card->second->call_state = CALL_END;
  572. if(CALL_SUCCESSED == g_card->second->get_mine_tool()->m_status_call)
  573. {
  574. iter_card->second->call_state = CALL_SUCCESSED;
  575. g_card->second->get_mine_tool()->m_status_call = 0;
  576. }
  577. cards.erase(iter_card--);//在用户下删除这张卡
  578. }
  579. }
  580. }
  581. /**
  582. * @brief 系统自动发送呼叫给指定的卡
  583. */
  584. void module_call::system_call_apoint(int card_id,int card_type)
  585. {
  586. int call_type = -1, call_level = 1, call_time_interval=-1;
  587. uint32_t call_time_out = 0;
  588. std::string user_name = "system_apoint";
  589. std::vector<call_card_ptr> result_arr;//结果集
  590. call_user_ptr user_ptr;
  591. std::lock_guard<std::mutex> lock(_mutex);
  592. auto user_map_ptr = _map.find(user_name);
  593. if(_map.end() == user_map_ptr)//没有这个用户就新建并加入
  594. {
  595. user_ptr=call_user_ptr(new call_user());
  596. _map[user_name]=user_ptr;
  597. }
  598. else//有这个用户
  599. {
  600. user_ptr=user_map_ptr->second;
  601. }
  602. //更新用户信息
  603. user_ptr->call_time=std::chrono::system_clock::now();
  604. user_ptr->call_time_out=call_time_out;
  605. user_ptr->call_time_interval = call_time_interval;
  606. user_ptr->call_type_id=call_type;
  607. user_ptr->call_level_id=call_level;
  608. user_ptr->user_name=user_name;
  609. call_card_ptr c_card_ptr(new call_card());
  610. c_card_ptr->cardid = card_id;
  611. c_card_ptr->cardtype = card_type;
  612. copy(user_ptr, c_card_ptr);
  613. c_card_ptr->call_state = CALL_ING;
  614. user_ptr->cards[c_card_ptr->cardid]=c_card_ptr;
  615. for(auto& it:user_ptr->cards)//添加到结果集中
  616. {
  617. result_arr.push_back(it.second);
  618. }
  619. log_info("发起呼叫 cardid=%d, cardtype=%d", card_id,card_type);
  620. //组装json发送给web
  621. response_accept_call(result_arr);
  622. }
  623. /**
  624. * @brief 系统自动发送呼叫给指定的卡
  625. */
  626. void module_call::system_cancel_call_apoint(int card_id,int card_type)
  627. {
  628. int call_type = -1;//, call_level = -1;
  629. std::string user_name = "system_apoint";
  630. int64_t call_time;
  631. call_user_ptr result_user_ptr(new call_user());
  632. result_user_ptr->call_type_id=call_type;
  633. // result_user_ptr->call_level_id=call_level;
  634. result_user_ptr->call_time = std::chrono::system_clock::time_point(std::chrono::milliseconds(call_time));
  635. result_user_ptr->user_name=user_name;
  636. std::lock_guard<std::mutex> lock(_mutex);
  637. call_user_ptr user_ptr(new call_user());
  638. auto it_map = _map.find(user_name);
  639. if(_map.end()!=it_map)//呼叫用户
  640. {
  641. user_ptr=it_map->second;
  642. }
  643. if (user_ptr->cards.find(card_id) == user_ptr->cards.end())
  644. {
  645. return ;
  646. }
  647. call_card_ptr c_card_ptr(new call_card());
  648. c_card_ptr->cardid = card_id;
  649. c_card_ptr->cardtype = card_type;
  650. result_user_ptr->cards[c_card_ptr->cardid]=c_card_ptr;//增加到结果集中
  651. user_ptr->cards.erase(c_card_ptr->cardid);//删除这个卡
  652. log_info("取消呼叫: cardid=%d, cardtype=%d", card_id,card_type);
  653. // 没有呼叫信息,删除该用户记录
  654. if(user_ptr->cards.empty())
  655. {
  656. _map.erase(user_name);
  657. }
  658. response_accept_cancel(result_user_ptr);
  659. }
  660. /*
  661. * @brief 发送防碰撞告警
  662. * @param const std::map<int, call_card>& cards key为车id,val为人卡信息
  663. * @return
  664. * @note
  665. * @warning
  666. * @bug
  667. *
  668. * */
  669. void module_call::send_anti_collision(const std::map<int, call_card>& cards)
  670. {
  671. std::vector<call_card_ptr> vt_cards;
  672. std::string vmsg = "";
  673. std::string pmsg = "";
  674. for(auto c : cards){
  675. char buf[100] = {0};
  676. snprintf(buf, 100, "%d,", c.second.cardid);
  677. switch(c.second.cardtype){
  678. case 1:
  679. pmsg += std::string(buf);
  680. break;
  681. case 2:
  682. vmsg += std::string(buf);
  683. break;
  684. }
  685. vt_cards.push_back(std::make_shared<call_card>(c.second));
  686. }
  687. log_info("[anti_coll] vehicle's cid = %s, person's cid = %s", vmsg.c_str(), pmsg.c_str());
  688. call_site_map site_map;
  689. get_site_map(vt_cards, site_map);
  690. log_info("[anti_coll] site's size = %d", site_map.size());
  691. if(!site_map.empty()){
  692. send_to_sites(site_map);
  693. }
  694. }
  695. std::string module_call::get_json_anti_collision()
  696. {
  697. rapidjson::Document doc(rapidjson::kObjectType);
  698. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator();
  699. doc.SetObject();
  700. rapidjson::Value data(rapidjson::kArrayType);
  701. auto tmp = event_list::instance()->m_map;
  702. for(auto it_ev = tmp.cbegin(); it_ev != tmp.cend(); ++it_ev)
  703. {
  704. if(ET_PERSON_VEHICLE_ANTI_COLLISION != it_ev->second->m_ev_type)
  705. {
  706. continue;
  707. }
  708. rapidjson::Value obj(rapidjson::kObjectType);
  709. //rapidjson::Value vid(rapidjson::kObjectType);
  710. //vid.SetInt(atoi(it_ev->second->m_obj_id.c_str()));
  711. /*std::string str_vid = "";
  712. char szID[32] = {0};
  713. sprintf(szID, "%03d%.10d", 2, atoi(it_ev->second->m_obj_id.c_str()));
  714. str_vid = szID;
  715. */
  716. rapidjson::Value val;
  717. obj.AddMember("vid", val.SetString(it_ev->second->m_obj_id.c_str(), allocator), allocator);
  718. rapidjson::Value elems(rapidjson::kArrayType);
  719. std::string msg = it_ev->second->m_desc;
  720. std::vector<std::string> vt_msg;
  721. boost::split(vt_msg, msg, boost::is_any_of(";"));
  722. for(auto it_elem : vt_msg){
  723. std::vector<std::string> vt_cid_d;
  724. boost::split(vt_cid_d, it_elem, boost::is_any_of(","));
  725. if(vt_cid_d.size()<2){
  726. continue;
  727. }
  728. rapidjson::Value e(rapidjson::kArrayType);
  729. rapidjson::Value o(rapidjson::kObjectType);
  730. //o.SetInt(atoi(vt_cid_d[0].c_str()));
  731. /*memset(szID, 0, 32);
  732. sprintf(szID, "%03d%.10d", 1, atoi(vt_cid_d[0].c_str()));
  733. str_vid = szID;
  734. */
  735. o.SetString(tool_other::to_cid(1, atoi(vt_cid_d[0].c_str())).c_str(), allocator);
  736. e.PushBack(o, allocator);
  737. o.SetDouble(atof(vt_cid_d[1].c_str()));
  738. e.PushBack(o, allocator);
  739. elems.PushBack(e, allocator);
  740. }
  741. obj.AddMember("pid", elems, allocator);
  742. std::string t = tool_time::to_str(it_ev->second->m_cur_time);
  743. tool_json::add_member(obj, "start_time", t, allocator);
  744. data.PushBack(obj, allocator);
  745. }
  746. if(!data.Size()){
  747. return "";
  748. }
  749. doc.AddMember(JSON_ROOT_KEY_CMD, "pv_anti_coll", allocator);
  750. doc.AddMember(JSON_ROOT_KEY_VERSION, INTERFACE_VERSION, allocator);
  751. doc.AddMember(JSON_ROOT_KEY_DATA, data, allocator);
  752. rapidjson::StringBuffer sb;
  753. rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(sb);
  754. doc.Accept(writer);
  755. return sb.GetString();
  756. }