CMessagePush.cpp 8.0 KB

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