1
0

module_call.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * @file
  3. * module_call.h
  4. *
  5. * @brief
  6. * 呼叫业务
  7. * @version
  8. * V1.0
  9. * @author
  10. *
  11. * @date
  12. * 2021/05/10
  13. * @note
  14. *
  15. * @warning
  16. * @bug
  17. * @copyright
  18. * */
  19. #ifndef MODULE_CALL_H
  20. #define MODULE_CALL_H
  21. #include <map>
  22. #include <mutex>
  23. #include <string>
  24. #include <chrono>
  25. #include <boost/thread.hpp>
  26. #include <boost/enable_shared_from_this.hpp>
  27. #include <atomic>
  28. #include "rapidjson/document.h"
  29. #include "rapidjson/prettywriter.h"
  30. #include "module_singleton_base.h"
  31. #include "card.h"
  32. #include "websocket/sio/sio_client.h"
  33. #include "module_i_thread.h"
  34. #include "config_file.h"
  35. /**
  36. * @brief 呼叫类型 全员 定员
  37. */
  38. enum CALL_CARD_TYPE
  39. {
  40. /// 全员呼叫
  41. CCT_CALL_ALL = 0,
  42. /// 定员
  43. CCT_CALL_APOINT=1,
  44. };
  45. /**
  46. * @brief 呼叫等级 一般呼叫 紧急呼叫
  47. */
  48. enum CALL_CARD_LEVEL
  49. {
  50. /// 呼叫等级: 1 一般呼叫
  51. CALL_LEVEL_NORMAL=1,
  52. /// 呼叫等级: 2 紧急呼叫
  53. CALL_LEVEL_CRITICAL=2
  54. };
  55. /**
  56. * @brief 呼叫状态
  57. */
  58. enum CALL_STATE{
  59. ///无呼叫信息
  60. CALL_NONE = 0,
  61. ///呼叫成功
  62. CALL_SUCCESSED = 1,
  63. ///呼叫中
  64. CALL_ING = 2,
  65. ///呼叫失败
  66. CALL_FAILED = 3,
  67. ///呼叫结束
  68. CALL_END=100
  69. };
  70. /**
  71. * @brief 呼叫卡的信息
  72. */
  73. struct call_card
  74. {
  75. /// 呼叫卡号 0为全员
  76. uint32_t cardid;
  77. int32_t cardtype;
  78. /// 分站号 0为全员
  79. int stationid;
  80. ///呼叫类型 全员0, 定员1 CALL_CARD_TYPE
  81. int call_type_id;
  82. /// 呼叫类型:一般1,紧急2 CALL_CARD_LEVEL
  83. int call_level_id;
  84. /// 呼叫时长,单位分钟
  85. uint32_t call_time_out;
  86. int call_time_interval;
  87. /// 呼叫开始时间
  88. std::chrono::system_clock::time_point call_time;
  89. /// 呼叫状态,正在呼叫、呼叫成功
  90. int call_state;
  91. ///呼叫人
  92. std::string user_name;
  93. bool is_display = true;
  94. call_card()
  95. {}
  96. call_card(uint32_t id, int32_t type, int level_id, CALL_CARD_TYPE call_type, int sid): cardid(id), cardtype(type), stationid(sid), call_type_id(call_type), call_level_id(level_id)
  97. {
  98. user_name = "system";
  99. }
  100. bool is_call_all()
  101. {
  102. return CCT_CALL_ALL == call_type_id;
  103. }
  104. uint64_t to_id64();
  105. std::string to_id64_str();
  106. bool is_timeout();
  107. };
  108. using call_card_ptr = std::shared_ptr<call_card>;
  109. /**
  110. * @brief 呼叫模块
  111. */
  112. class module_call : public i_thread, public singleton_base<module_call>
  113. {
  114. private:
  115. friend class singleton_base<module_call>;
  116. module_call()
  117. {
  118. }
  119. private:
  120. typedef std::map<int64_t,call_card_ptr> call_card_map;
  121. /**
  122. * @brief 呼叫用户信息
  123. */
  124. struct call_user
  125. {
  126. ///呼叫人
  127. std::string user_name;
  128. ///呼叫类型 全员0, 定员1 CALL_CARD_TYPE
  129. int call_type_id;
  130. /// 呼叫等级:一般1,紧急2 CALL_CARD_LEVEL
  131. int call_level_id;
  132. /// 呼叫时长,单位分钟
  133. uint32_t call_time_out;
  134. int call_time_interval;
  135. /// 呼叫开始时间
  136. std::chrono::system_clock::time_point call_time;
  137. ///呼叫卡列表
  138. call_card_map cards;
  139. bool is_timeout();
  140. bool is_call_all()
  141. {
  142. return CCT_CALL_ALL == call_type_id;
  143. }
  144. };
  145. typedef std::shared_ptr<call_user> call_user_ptr;
  146. typedef std::map<std::string, call_user_ptr> call_user_map;
  147. struct call_site
  148. {
  149. int32_t site_id;
  150. ///呼叫类型 全员0, 定员1 CALL_CARD_TYPE
  151. int call_type_id;
  152. /// 呼叫等级:一般1,紧急2 CALL_CARD_LEVEL
  153. int call_level_id;
  154. call_card_map cards;
  155. bool is_call_all()
  156. {
  157. return CCT_CALL_ALL == call_type_id;
  158. }
  159. };
  160. typedef std::shared_ptr<call_site> call_site_ptr;
  161. typedef std::map<int32_t, call_site_ptr> call_site_map;
  162. public:
  163. /**
  164. * @brief web发给采集:发起呼叫
  165. * @param node_data
  166. */
  167. void accept_call(sio::message::ptr const& data);
  168. /**
  169. * @brief web发给采集:取消呼叫
  170. * @param node_data
  171. */
  172. void accept_cancel(sio::message::ptr const& data);
  173. /**
  174. * @brief 登陆时,采集发送web:请求井下所有呼叫
  175. */
  176. std::string accept_login();
  177. void rev_from_card_resp(std::shared_ptr<card_location_base> card_ptr);
  178. /**
  179. * @brief 系统自动发送呼叫给指定的卡
  180. */
  181. void system_call_apoint(int card_id,int card_type);
  182. /**
  183. * @brief 系统自动发送呼叫给指定的卡
  184. */
  185. void system_cancel_call_apoint(int card_id,int card_type);
  186. void send_anti_collision(const std::map<int, call_card>& cards);
  187. std::string get_json_anti_collision();
  188. /**int card_id,int card_type
  189. * @brief
  190. */
  191. void init(config_file& config)
  192. {
  193. sleep_ms = std::stoi(config.get("service.send_call_interval_ms","5000"));
  194. }
  195. private:
  196. /**
  197. * @brief 采集回复web发起呼叫
  198. * @param arr
  199. */
  200. void response_accept_call(const std::vector<call_card_ptr> arr);
  201. /**
  202. * @brief 采集回复web:取消呼叫
  203. * @param node_data
  204. */
  205. void response_accept_cancel(const call_user_ptr user_ptr);
  206. void add_cards_to_user(const std::vector<sio::message::ptr>& card_vec,
  207. call_user_ptr user_ptr);
  208. /**
  209. * @brief 线程函数
  210. */
  211. void run();
  212. void copy(const call_user_ptr src, call_user_ptr des)
  213. {
  214. des->call_level_id = src->call_level_id;
  215. des->call_time = src->call_time;
  216. des->call_time_out = src->call_time_out;
  217. des->call_type_id = src->call_type_id;
  218. des->user_name = src->user_name;
  219. }
  220. void copy(const call_card_ptr src, call_card_ptr des)
  221. {
  222. des->call_level_id = src->call_level_id;
  223. des->call_state = src->call_state;
  224. des->call_time = src->call_time;
  225. des->call_time_out = src->call_time_out;
  226. des->cardid = src->cardid;
  227. des->cardtype = src->cardtype;
  228. des->user_name = src->user_name;
  229. des->stationid = src->stationid;
  230. des->call_type_id = src->call_type_id;
  231. }
  232. void copy(const call_user_ptr src, call_card_ptr des)
  233. {
  234. des->call_level_id = src->call_level_id;
  235. //des->call_state = src->call_state;
  236. des->call_time = src->call_time;
  237. des->call_time_out = src->call_time_out;
  238. des->call_time_interval = src->call_time_interval;
  239. //des->cardid = src->cardid;
  240. des->user_name = src->user_name;
  241. des->call_type_id = src->call_type_id;
  242. }
  243. /**
  244. * @brief 获取用户下的所有呼叫卡
  245. * @param user_ptr
  246. * @param cardlist
  247. * @param out_data
  248. */
  249. void get_user_all_call_cards(call_user_ptr& user_ptr,
  250. const std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>& cardlist,
  251. std::vector<call_card_ptr>& out_data);
  252. /**
  253. * @brief 获取所有的呼叫卡
  254. * @param user_map
  255. * @param cardlist
  256. * @param out_data
  257. */
  258. void get_all_call_cards(call_user_map& user_map,
  259. const std::unordered_map<uint64_t,std::shared_ptr<card_location_base>>& cardlist,
  260. std::vector<call_card_ptr>& out_data)
  261. {
  262. auto iter_m_map=user_map.begin();
  263. for(;iter_m_map!=user_map.end();++iter_m_map)
  264. {
  265. get_user_all_call_cards(iter_m_map->second, cardlist, out_data);
  266. if(iter_m_map->second->cards.empty())//没有卡了 删除呼叫用户
  267. {
  268. user_map.erase(iter_m_map--);
  269. }
  270. }
  271. }
  272. /**
  273. * @brief 获取所有的呼叫卡
  274. * @param out_data
  275. */
  276. void get_all_call_cards(std::vector<call_card_ptr>& out_data)
  277. {
  278. auto cardlist = card_list::instance()->m_map;
  279. {
  280. std::lock_guard<std::mutex> lock(_mutex);
  281. get_all_call_cards(_map, cardlist, out_data);
  282. }
  283. }
  284. /*
  285. * @brief
  286. * ["fjb", // 发起呼叫人
  287. * "0010000000001", // 呼叫的卡号
  288. * 103, // 分站号
  289. * 23431, // 发起呼叫时间
  290. * 0, //
  291. * 0 // 正在呼叫:2/结束呼叫/
  292. * ]
  293. */
  294. static void to_node_element(rapidjson::Value& out_elemet,
  295. const std::shared_ptr<call_card> card_ptr,
  296. rapidjson::Document::AllocatorType& allocator,bool f);
  297. static void to_node_array(rapidjson::Value& out_array,
  298. const std::vector<std::shared_ptr<call_card>> cards,
  299. rapidjson::Document::AllocatorType& allocator,bool f=true)
  300. {
  301. std::vector<std::shared_ptr<call_card>>::size_type i;
  302. for(i=0; i<cards.size(); i++)
  303. {
  304. if(!cards[i]->is_display)
  305. {
  306. continue;
  307. }
  308. rapidjson::Value node_card(rapidjson::kArrayType);
  309. to_node_element(node_card, cards[i], allocator,f);
  310. out_array.PushBack(node_card, allocator);
  311. }
  312. }
  313. /**
  314. * @brief 生成所有呼叫卡的json
  315. * @param cards
  316. * @return
  317. */
  318. std::string to_call_card_list_json(std::vector<call_card_ptr> cards);
  319. void get_site_map(const std::vector<call_card_ptr>& arr, call_site_map& out_site_map)
  320. {
  321. auto card_ptr = arr.begin();
  322. for(;card_ptr!=arr.end();++card_ptr)
  323. {
  324. if((*card_ptr)->stationid<1)
  325. {
  326. continue;
  327. }
  328. auto iter = out_site_map.find((*card_ptr)->stationid);
  329. if(out_site_map.end()==iter)
  330. {
  331. call_site_ptr site_ptr(new call_site());
  332. site_ptr->site_id = (*card_ptr)->stationid;
  333. site_ptr->call_type_id = (*card_ptr)->call_type_id;
  334. site_ptr->call_level_id = (*card_ptr)->call_level_id;
  335. out_site_map.insert(std::make_pair(site_ptr->site_id, site_ptr));
  336. iter = out_site_map.find((*card_ptr)->stationid);
  337. }
  338. if(iter->second->is_call_all())
  339. {
  340. continue;
  341. }
  342. if((*card_ptr)->is_call_all())
  343. {
  344. iter->second->call_type_id = (*card_ptr)->call_type_id;
  345. iter->second->cards.clear();
  346. continue;
  347. }
  348. iter->second->cards[(*card_ptr)->cardid]=(*card_ptr);
  349. }
  350. }
  351. ///发呼叫的报文数据给分站
  352. void send_to_sites(call_site_map& site_map);
  353. static void print_test(std::vector<char>&& arr, int siteid);
  354. void static memcpy_uint32(std::vector<char>& arr, uint32_t dwSrc)
  355. {
  356. char bt = HIBYTE(HIWORD(dwSrc));
  357. arr.push_back(bt);
  358. bt = LOBYTE(HIWORD(dwSrc));
  359. arr.push_back(bt);
  360. bt = HIBYTE(LOWORD(dwSrc));
  361. arr.push_back(bt);
  362. bt = LOBYTE(LOWORD(dwSrc));
  363. arr.push_back(bt);
  364. }
  365. static char HIBYTE(uint16_t dwSrc)
  366. {
  367. return static_cast<char>(dwSrc>>8);
  368. }
  369. static char LOBYTE(uint16_t dwSrc)
  370. {
  371. return static_cast<char>(dwSrc);
  372. }
  373. static uint16_t HIWORD(uint32_t dwSrc)
  374. {
  375. return static_cast<uint16_t>(dwSrc>>16);
  376. }
  377. static uint16_t LOWORD(uint32_t dwSrc)
  378. {
  379. return static_cast<uint16_t>(dwSrc);
  380. }
  381. private:
  382. /// 呼叫用户列表
  383. call_user_map _map;
  384. };
  385. #endif // MODULE_CALL_H