module_call.cpp 29 KB

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