module_call.h 11 KB

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