CMessagePush.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. rapidjson::StringBuffer buffer;
  76. rapidjson::Writer<StringBuffer> writer(buffer);
  77. doc.Accept(writer);
  78. //返回给接入层的消息
  79. std::string m_keepalive_msg = buffer.GetString();
  80. try
  81. {
  82. socket_.write_some(boost::asio::buffer(m_keepalive_msg.c_str(), m_keepalive_msg.length()));
  83. }
  84. catch (const std::exception& e)
  85. {
  86. //走到这里来说明心跳包发送失败了,socket失效了
  87. std::string err = e.what();
  88. //先把socket关闭掉
  89. socket_.close();
  90. try
  91. {
  92. //发送失败,重新建立连接
  93. char host[] = "127.0.0.1";
  94. char port[] = "9001";
  95. tcp::resolver resolver(m_io_context);
  96. tcp::resolver::results_type endpoints =
  97. resolver.resolve(tcp::v4(), host, port);
  98. boost::asio::connect(socket_, endpoints);
  99. socket_.write_some(boost::asio::buffer(m_keepalive_msg.c_str(), m_keepalive_msg.length()));
  100. }
  101. catch (const std::exception& e)
  102. {
  103. //重新连接或者重新发送又失败了,可能是网络断了
  104. std::string err = e.what();
  105. //LOG_INFO("write err info:" << err.c_str());
  106. //关闭无效的连接
  107. socket_.close();
  108. //10秒后再重试
  109. CSystem::my_sleep(10);
  110. continue;
  111. }
  112. }
  113. //走到这里,说明心跳包发送成功了,socket是连通的
  114. //休眠10秒钟,之后再发心跳包
  115. CSystem::my_sleep(10);
  116. }
  117. AddStopNum();
  118. }
  119. void CMessagePush::ReceiveMessage()
  120. {
  121. while (m_is_work)
  122. {
  123. try
  124. {
  125. memset(data_, 0, max_length);
  126. socket_.read_some(boost::asio::buffer(data_, max_length));
  127. std::string msg = data_;
  128. //收到服务器的消息,对服务器的消息进行处理
  129. rapidjson::Document document;
  130. document.Parse(msg.c_str());
  131. if (!document.IsObject())
  132. {
  133. //LOG_INFO("message 非法!");
  134. return;
  135. }
  136. int type = document["type"].GetInt();
  137. if (type == 1)
  138. {
  139. std::string username = document["username"].GetString();
  140. std::string order_id = document["order_id"].GetString();
  141. std::string order_no = document["order_no"].GetString();
  142. //新订单来了,首先判断是否要语音提醒
  143. if (CSetting::GetParam("setting_is_new_waimai_voice") == "1")
  144. {
  145. AddVoice(order_id);
  146. }
  147. //判断是否要自动确认
  148. if (CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1")
  149. {
  150. AddConfirm(order_id);
  151. }
  152. //判断是否右下角弹框提醒
  153. if (CSetting::GetParam("setting_is_new_waimai_dialog") == "1")
  154. {
  155. }
  156. AddPinter(order_id, order_no);
  157. }
  158. //处理完了,接着处理下一条
  159. continue;
  160. }
  161. catch (std::exception& e)
  162. {
  163. std::string err = e.what();
  164. //LOG_INFO("read err:" << err.c_str());
  165. //如果这里异常了,说明socket失效了,等2秒重新读
  166. CSystem::my_sleep(2);
  167. continue;
  168. }
  169. }
  170. AddStopNum();
  171. }
  172. void CMessagePush::AddVoice(std::string order_id)
  173. {
  174. m_voice_mutex.lock();
  175. m_voice_queue.push(order_id);
  176. m_voice_mutex.unlock();
  177. }
  178. void CMessagePush::AddConfirm(std::string order_id)
  179. {
  180. m_confirm_mutex.lock();
  181. m_confirm_queue.push(order_id);
  182. m_confirm_mutex.unlock();
  183. }
  184. void CMessagePush::AddPinter(std::string order_id, std::string order_no)
  185. {
  186. m_printer_mutex.lock();
  187. WaimaiPinterInfo newPrinter;
  188. newPrinter.order_id = order_id;
  189. newPrinter.order_no = order_no;
  190. m_printer_queue.push(newPrinter);
  191. m_printer_mutex.unlock();
  192. }
  193. void CMessagePush::AddShouyinPrinter(CWaimaiOrder order)
  194. {
  195. m_shouyin_printer_mutex.lock();
  196. m_shouyin_printer_queue.push(order);
  197. m_shouyin_printer_mutex.unlock();
  198. }
  199. void CMessagePush::AddChufangPrinter(CWaimaiOrder order)
  200. {
  201. m_chufang_printer_mutex.lock();
  202. m_chufang_printer_queue.push(order);
  203. m_chufang_printer_mutex.unlock();
  204. }
  205. void CMessagePush::HandleVoice()
  206. {
  207. while(m_is_work)
  208. {
  209. m_voice_mutex.lock();
  210. if(m_voice_queue.empty())
  211. {
  212. m_voice_mutex.unlock();
  213. CSystem::my_sleep(1);
  214. continue;
  215. }
  216. std::string order_id = m_voice_queue.front();
  217. m_voice_queue.pop();
  218. m_voice_mutex.unlock();
  219. wstring path = CSystem::GetProgramDir() + L"\\music\\new_wamai_order.wav";
  220. PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
  221. //8秒内最多播放一次
  222. CSystem::my_sleep(8);
  223. }
  224. AddStopNum();
  225. }
  226. void CMessagePush::HandleConfirm()
  227. {
  228. while(m_is_work)
  229. {
  230. m_confirm_mutex.lock();
  231. if(m_confirm_queue.empty())
  232. {
  233. m_confirm_mutex.unlock();
  234. CSystem::my_sleep(1);
  235. continue;
  236. }
  237. std::string order_id = m_confirm_queue.front();
  238. m_confirm_queue.pop();
  239. m_confirm_mutex.unlock();
  240. CWaimaiOrder newOrder;
  241. newOrder.ConfirmeOrder(order_id);
  242. }
  243. AddStopNum();
  244. }
  245. void CMessagePush::HandlePrinter()
  246. {
  247. while(m_is_work)
  248. {
  249. m_printer_mutex.lock();
  250. if(m_printer_queue.empty())
  251. {
  252. m_printer_mutex.unlock();
  253. CSystem::my_sleep(1);
  254. continue;
  255. }
  256. WaimaiPinterInfo printerInfo = m_printer_queue.front();
  257. std::string order_id = printerInfo.order_id;
  258. std::string order_no = printerInfo.order_no;
  259. m_printer_queue.pop();
  260. m_printer_mutex.unlock();
  261. CWaimaiOrder order;
  262. if(CSetting::GetParam("setting_is_new_waimai_printer") == "1" || CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
  263. {
  264. order.InitData(order_id, order_no);
  265. }
  266. //判断是否自动打印收银小票
  267. if(CSetting::GetParam("setting_is_new_waimai_printer") == "1")
  268. {
  269. AddShouyinPrinter(order);
  270. }
  271. //判断是否进行自动的厨房打印
  272. if(CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
  273. {
  274. AddChufangPrinter(order);
  275. }
  276. }
  277. AddStopNum();
  278. }
  279. void CMessagePush::HandleShouyinPrinter()
  280. {
  281. while(m_is_work)
  282. {
  283. m_shouyin_printer_mutex.lock();
  284. if(m_shouyin_printer_queue.empty())
  285. {
  286. m_shouyin_printer_mutex.unlock();
  287. CSystem::my_sleep(1);
  288. continue;
  289. }
  290. CWaimaiOrder order = m_shouyin_printer_queue.front();
  291. m_shouyin_printer_queue.pop();
  292. m_shouyin_printer_mutex.unlock();
  293. CPosPrinter printer;
  294. printer.PrintWaimaiOrderShouyin(order);
  295. }
  296. AddStopNum();
  297. }
  298. void CMessagePush::HandleChufangPrinter()
  299. {
  300. while(m_is_work)
  301. {
  302. m_chufang_printer_mutex.lock();
  303. if(m_chufang_printer_queue.empty())
  304. {
  305. m_chufang_printer_mutex.unlock();
  306. CSystem::my_sleep(1);
  307. continue;
  308. }
  309. CWaimaiOrder order = m_chufang_printer_queue.front();
  310. m_chufang_printer_queue.pop();
  311. m_chufang_printer_mutex.unlock();
  312. CPosPrinter printer;
  313. printer.PrintWaimaiOrderChufang(order);
  314. }
  315. AddStopNum();
  316. }
  317. void CMessagePush::AddStopNum()
  318. {
  319. m_nStopNumMutex.lock();
  320. m_nStopNum++;
  321. m_nStopNumMutex.unlock();
  322. if (m_nStopNum == 7)
  323. {
  324. //确认所有子线程都退出了,再删除自己
  325. delete this;
  326. }
  327. }