CMessagePush.cpp 8.4 KB

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