1
0

module_call.h 11 KB

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