module_call.cpp 29 KB

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