module_call.h 11 KB

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