CMessagePush.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #include "../pch/pch.h"
  2. #include "CMessagePush.h"
  3. #include "../tool/CPosPrinter.h"
  4. #include "../order/CWaimaiOrder.h"
  5. #include "mmsystem.h"
  6. CMessagePush::~CMessagePush()
  7. {
  8. }
  9. void CMessagePush::Start()
  10. {
  11. m_is_work = true;
  12. m_nStopNum = 0;
  13. //心跳包
  14. std::thread(&CMessagePush::KeepAlive, this).detach();
  15. //接收消息
  16. std::thread(&CMessagePush::ReceiveMessage, this).detach();
  17. //处理声音提醒
  18. std::thread(&CMessagePush::HandleVoice, this).detach();
  19. //处理订单确认
  20. std::thread(&CMessagePush::HandleConfirm, this).detach();
  21. //处理打印
  22. std::thread(&CMessagePush::HandlePrinter, this).detach();
  23. //处理收银打印
  24. std::thread(&CMessagePush::HandleShouyinPrinter, this).detach();
  25. //处理厨房打印
  26. std::thread(&CMessagePush::HandleChufangPrinter, this).detach();
  27. }
  28. void CMessagePush::Stop()
  29. {
  30. m_is_work = false;
  31. socket_.close();
  32. m_voice_mutex.lock();
  33. while (!m_voice_queue.empty())
  34. {
  35. m_voice_queue.pop();
  36. }
  37. m_voice_mutex.unlock();
  38. m_confirm_mutex.lock();
  39. while (!m_confirm_queue.empty())
  40. {
  41. m_confirm_queue.pop();
  42. }
  43. m_confirm_mutex.unlock();
  44. m_printer_mutex.lock();
  45. while (!m_printer_queue.empty())
  46. {
  47. m_printer_queue.pop();
  48. }
  49. m_printer_mutex.unlock();
  50. m_shouyin_printer_mutex.lock();
  51. while (!m_shouyin_printer_queue.empty())
  52. {
  53. m_shouyin_printer_queue.pop();
  54. }
  55. m_shouyin_printer_mutex.unlock();
  56. m_chufang_printer_mutex.lock();
  57. while (!m_chufang_printer_queue.empty())
  58. {
  59. m_chufang_printer_queue.pop();
  60. }
  61. m_chufang_printer_mutex.unlock();
  62. }
  63. void CMessagePush::KeepAlive()
  64. {
  65. while (m_is_work)
  66. {
  67. //生成心跳包
  68. rapidjson::Document doc;
  69. doc.SetObject();
  70. rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); //获取分配器
  71. std::string username = CSetting::getUsername();
  72. std::string timestamp = to_string(time(NULL));
  73. doc.AddMember("username", StringRef(username.c_str(), username.length()), allocator);
  74. doc.AddMember("timestamp", StringRef(timestamp.c_str(), timestamp.length()), allocator);
  75. doc.AddMember("is_login", "0", allocator);
  76. rapidjson::StringBuffer buffer;
  77. rapidjson::Writer<StringBuffer> writer(buffer);
  78. doc.Accept(writer);
  79. //返回给接入层的消息
  80. std::string m_keepalive_msg = buffer.GetString();
  81. try
  82. {
  83. socket_.write_some(boost::asio::buffer(m_keepalive_msg.c_str(), m_keepalive_msg.length()));
  84. }
  85. catch (const std::exception& e)
  86. {
  87. //走到这里来说明心跳包发送失败了,socket失效了
  88. std::string err = e.what();
  89. //先把socket关闭掉
  90. socket_.close();
  91. try
  92. {
  93. //发送失败,重新建立连接
  94. char host[] = "127.0.0.1";
  95. char port[] = "9001";
  96. tcp::resolver resolver(m_io_context);
  97. tcp::resolver::results_type endpoints =
  98. resolver.resolve(tcp::v4(), host, port);
  99. boost::asio::connect(socket_, endpoints);
  100. //刚刚连接上,这是第一次登陆,发送登陆信息
  101. rapidjson::Document doc_1;
  102. doc_1.SetObject();
  103. rapidjson::Document::AllocatorType& allocator_1 = doc_1.GetAllocator(); //获取分配器
  104. std::string username_1 = CSetting::getUsername();
  105. std::string timestamp_1 = to_string(time(NULL));
  106. doc_1.AddMember("username", StringRef(username_1.c_str(), username_1.length()), allocator_1);
  107. doc_1.AddMember("timestamp", StringRef(timestamp_1.c_str(), timestamp_1.length()), allocator_1);
  108. doc_1.AddMember("is_login", "1", allocator_1);
  109. rapidjson::StringBuffer buffer_1;
  110. rapidjson::Writer<StringBuffer> writer_1(buffer_1);
  111. doc_1.Accept(writer_1);
  112. //返回给接入层的消息
  113. std::string m_login_message = buffer_1.GetString();
  114. socket_.write_some(boost::asio::buffer(m_login_message.c_str(), m_login_message.length()));
  115. }
  116. catch (const std::exception& e)
  117. {
  118. //重新连接或者重新发送又失败了,可能是网络断了
  119. std::string err = e.what();
  120. //LOG_INFO("write err info:" << err.c_str());
  121. //关闭无效的连接
  122. socket_.close();
  123. //10秒后再重试
  124. CSystem::my_sleep(10);
  125. continue;
  126. }
  127. }
  128. //走到这里,说明心跳包发送成功了,socket是连通的
  129. //休眠10秒钟,之后再发心跳包
  130. CSystem::my_sleep(10);
  131. }
  132. AddStopNum();
  133. }
  134. void CMessagePush::ReceiveMessage()
  135. {
  136. while (m_is_work)
  137. {
  138. try
  139. {
  140. memset(data_, 0, max_length);
  141. socket_.read_some(boost::asio::buffer(data_, max_length));
  142. std::string msg = data_;
  143. //收到服务器的消息,对服务器的消息进行处理
  144. rapidjson::Document document;
  145. document.Parse(msg.c_str());
  146. if (!document.IsObject())
  147. {
  148. //LOG_INFO("message 非法!");
  149. return;
  150. }
  151. int type = document["msg_type"].GetInt();
  152. if (type == 1)
  153. {
  154. std::string order_id = document["waimai_order_id"].GetString();
  155. std::string order_no = document["waimai_order_no"].GetString();
  156. //新订单来了,首先判断是否要语音提醒
  157. if (CSetting::GetParam("setting_is_new_waimai_voice") == "1")
  158. {
  159. AddVoice(order_id);
  160. }
  161. //判断是否要自动确认
  162. if (CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1")
  163. {
  164. AddConfirm(order_id);
  165. }
  166. //判断是否右下角弹框提醒
  167. if (CSetting::GetParam("setting_is_new_waimai_dialog") == "1")
  168. {
  169. }
  170. AddPinter(order_id, order_no);
  171. }
  172. //处理完了,接着处理下一条
  173. continue;
  174. }
  175. catch (std::exception& e)
  176. {
  177. std::string err = e.what();
  178. LOG_INFO("read err:" << err.c_str());
  179. //如果这里异常了,说明socket失效了,等2秒重新读
  180. CSystem::my_sleep(2);
  181. continue;
  182. }
  183. }
  184. AddStopNum();
  185. }
  186. void CMessagePush::AddVoice(std::string order_id)
  187. {
  188. m_voice_mutex.lock();
  189. m_voice_queue.push(order_id);
  190. m_voice_mutex.unlock();
  191. }
  192. void CMessagePush::AddConfirm(std::string order_id)
  193. {
  194. m_confirm_mutex.lock();
  195. m_confirm_queue.push(order_id);
  196. m_confirm_mutex.unlock();
  197. }
  198. void CMessagePush::AddPinter(std::string order_id, std::string order_no)
  199. {
  200. m_printer_mutex.lock();
  201. WaimaiPinterInfo newPrinter;
  202. newPrinter.order_id = order_id;
  203. newPrinter.order_no = order_no;
  204. m_printer_queue.push(newPrinter);
  205. m_printer_mutex.unlock();
  206. }
  207. void CMessagePush::AddShouyinPrinter(CWaimaiOrder order)
  208. {
  209. m_shouyin_printer_mutex.lock();
  210. m_shouyin_printer_queue.push(order);
  211. m_shouyin_printer_mutex.unlock();
  212. }
  213. void CMessagePush::AddChufangPrinter(CWaimaiOrder order)
  214. {
  215. m_chufang_printer_mutex.lock();
  216. m_chufang_printer_queue.push(order);
  217. m_chufang_printer_mutex.unlock();
  218. }
  219. void CMessagePush::HandleVoice()
  220. {
  221. while(m_is_work)
  222. {
  223. m_voice_mutex.lock();
  224. if(m_voice_queue.empty())
  225. {
  226. m_voice_mutex.unlock();
  227. CSystem::my_sleep(1);
  228. continue;
  229. }
  230. std::string order_id = m_voice_queue.front();
  231. m_voice_queue.pop();
  232. m_voice_mutex.unlock();
  233. wstring path = CSystem::GetProgramDir() + L"\\music\\new_wamai_order.wav";
  234. PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
  235. //8秒内最多播放一次
  236. CSystem::my_sleep(8);
  237. }
  238. AddStopNum();
  239. }
  240. void CMessagePush::HandleConfirm()
  241. {
  242. while(m_is_work)
  243. {
  244. m_confirm_mutex.lock();
  245. if(m_confirm_queue.empty())
  246. {
  247. m_confirm_mutex.unlock();
  248. CSystem::my_sleep(1);
  249. continue;
  250. }
  251. std::string order_id = m_confirm_queue.front();
  252. m_confirm_queue.pop();
  253. m_confirm_mutex.unlock();
  254. CWaimaiOrder newOrder;
  255. newOrder.ConfirmeOrder(order_id);
  256. }
  257. AddStopNum();
  258. }
  259. void CMessagePush::HandlePrinter()
  260. {
  261. while(m_is_work)
  262. {
  263. m_printer_mutex.lock();
  264. if(m_printer_queue.empty())
  265. {
  266. m_printer_mutex.unlock();
  267. CSystem::my_sleep(1);
  268. continue;
  269. }
  270. WaimaiPinterInfo printerInfo = m_printer_queue.front();
  271. std::string order_id = printerInfo.order_id;
  272. std::string order_no = printerInfo.order_no;
  273. m_printer_queue.pop();
  274. m_printer_mutex.unlock();
  275. CWaimaiOrder order;
  276. if(CSetting::GetParam("setting_is_new_waimai_printer") == "1" || CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
  277. {
  278. order.InitData(order_id, order_no);
  279. }
  280. //判断是否自动打印收银小票
  281. if(CSetting::GetParam("setting_is_new_waimai_printer") == "1")
  282. {
  283. AddShouyinPrinter(order);
  284. }
  285. //判断是否进行自动的厨房打印
  286. if(CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
  287. {
  288. AddChufangPrinter(order);
  289. }
  290. }
  291. AddStopNum();
  292. }
  293. void CMessagePush::HandleShouyinPrinter()
  294. {
  295. while(m_is_work)
  296. {
  297. m_shouyin_printer_mutex.lock();
  298. if(m_shouyin_printer_queue.empty())
  299. {
  300. m_shouyin_printer_mutex.unlock();
  301. CSystem::my_sleep(1);
  302. continue;
  303. }
  304. CWaimaiOrder order = m_shouyin_printer_queue.front();
  305. m_shouyin_printer_queue.pop();
  306. m_shouyin_printer_mutex.unlock();
  307. CPosPrinter printer;
  308. printer.PrintWaimaiOrderShouyin(order);
  309. }
  310. AddStopNum();
  311. }
  312. void CMessagePush::HandleChufangPrinter()
  313. {
  314. while(m_is_work)
  315. {
  316. m_chufang_printer_mutex.lock();
  317. if(m_chufang_printer_queue.empty())
  318. {
  319. m_chufang_printer_mutex.unlock();
  320. CSystem::my_sleep(1);
  321. continue;
  322. }
  323. CWaimaiOrder order = m_chufang_printer_queue.front();
  324. m_chufang_printer_queue.pop();
  325. m_chufang_printer_mutex.unlock();
  326. CPosPrinter printer;
  327. printer.PrintWaimaiOrderChufang(order);
  328. }
  329. AddStopNum();
  330. }
  331. void CMessagePush::AddStopNum()
  332. {
  333. m_nStopNumMutex.lock();
  334. m_nStopNum++;
  335. m_nStopNumMutex.unlock();
  336. if (m_nStopNum == 7)
  337. {
  338. //确认所有子线程都退出了,再删除自己
  339. delete this;
  340. }
  341. }