CMessagePush.cpp 10 KB

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