CMessagePush.cpp 8.8 KB

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