CMessagePush.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. std::thread t(&CMessagePush::Run, this);
  12. t.detach();
  13. }
  14. void CMessagePush::Run()
  15. {
  16. try
  17. {
  18. char host[] = "127.0.0.1";
  19. char port[] = "9001";
  20. tcp::resolver resolver(m_io_context);
  21. tcp::resolver::results_type endpoints =
  22. resolver.resolve(tcp::v4(), host, port);
  23. boost::asio::connect(socket_, endpoints);
  24. //发送身份信息
  25. rapidjson::Document doc;
  26. doc.SetObject();
  27. rapidjson::Document::AllocatorType &allocator = doc.GetAllocator(); //获取分配器
  28. doc.AddMember("username", "zhangyang", allocator);
  29. rapidjson::StringBuffer buffer;
  30. rapidjson::Writer<StringBuffer> writer(buffer);
  31. doc.Accept(writer);
  32. //返回给接入层的消息
  33. std::string m_login_msg = buffer.GetString();
  34. //异步发送消息
  35. PushMessage(m_login_msg);
  36. //同时启动一个异步接受消息
  37. socket_.async_read_some(boost::asio::buffer(data_, max_length),
  38. boost::bind(&CMessagePush::handle_read, this,
  39. boost::asio::placeholders::error,
  40. boost::asio::placeholders::bytes_transferred));
  41. m_io_context.run();
  42. }
  43. catch (std::exception& e)
  44. {
  45. std::cerr << "Exception: " << e.what() << "\n";
  46. }
  47. }
  48. void CMessagePush::PushMessage(std::string msg)
  49. {
  50. boost::asio::async_write(socket_,
  51. boost::asio::buffer(msg.c_str(), msg.length()),
  52. boost::bind(&CMessagePush::handle_write, this,
  53. boost::asio::placeholders::error));
  54. }
  55. void CMessagePush::HandleMessage(std::string msg)
  56. {
  57. try
  58. {
  59. //收到服务器的消息,对服务器的消息进行处理
  60. rapidjson::Document document;
  61. document.Parse(msg.c_str());
  62. if (!document.IsObject())
  63. {
  64. LOG_INFO("message 非法!");
  65. return;
  66. }
  67. int type = document["type"].GetInt();
  68. if (type == 0)
  69. {
  70. std::string status = document["status"].GetString();
  71. if (status == "ok")
  72. {
  73. //表示连接服务器成功了
  74. }
  75. }
  76. else if (type == 1)
  77. {
  78. std::string username = document["username"].GetString();
  79. std::string order_id = document["order_id"].GetString();
  80. std::string order_no = document["order_no"].GetString();
  81. //新订单来了,首先判断是否要语音提醒
  82. if (CSetting::GetParam("setting_is_new_waimai_voice") == "1")
  83. {
  84. //需要语音提醒
  85. std::wstring filepath = L"music/new_wamai_order.mp3";
  86. std::wstring Open = L"OPEN " + filepath + L" ALIAS MUSIC";
  87. mciSendString(Open.c_str(), NULL, 0, 0);
  88. std::wstring Play = L"PLAY MUSIC FROM 0";
  89. mciSendString(Play.c_str(), NULL, 0, 0);
  90. std::wstring Close = L"CLOSE MUSIC";
  91. mciSendString(Close.c_str(), NULL, 0, 0);
  92. }
  93. //判断是否要自动确认
  94. if (CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1")
  95. {
  96. CWaimaiOrder newOrder;
  97. newOrder.ConfirmeOrder(order_id);
  98. }
  99. //判断是否右下角弹框提醒
  100. if (CSetting::GetParam("setting_is_new_waimai_dialog") == "1")
  101. {
  102. }
  103. //判断是否自动打印
  104. if (CSetting::GetParam("setting_is_new_waimai_printer") == "1")
  105. {
  106. CPosPrinter printer;
  107. //判断打印规格和联数
  108. std::string guige = CSetting::GetParam("setting_printer_guige");
  109. std::string lianshu = CSetting::GetParam("setting_printer_lianshu");
  110. int n_lianshu = atoi(lianshu.c_str());
  111. for (int i = 0; i < n_lianshu; i++)
  112. {
  113. if (guige == "58")
  114. {
  115. printer.PrintWaimaiOrder(order_id, order_no, 1);
  116. }
  117. else
  118. {
  119. printer.PrintWaimaiOrder(order_id, order_no, 2);
  120. }
  121. }
  122. }
  123. }
  124. }
  125. catch (std::exception& e)
  126. {
  127. std::cerr << "Exception: " << e.what() << "\n";
  128. }
  129. }
  130. void CMessagePush::handle_read(const boost::system::error_code& error,
  131. size_t bytes_transferred)
  132. {
  133. if (!error)
  134. {
  135. string s_reply = data_;
  136. HandleMessage(s_reply);
  137. memset(data_, 0, max_length);
  138. socket_.async_read_some(boost::asio::buffer(data_, max_length),
  139. boost::bind(&CMessagePush::handle_read, this,
  140. boost::asio::placeholders::error,
  141. boost::asio::placeholders::bytes_transferred));
  142. }
  143. else
  144. {
  145. delete this;
  146. }
  147. }
  148. void CMessagePush::handle_write(const boost::system::error_code& error)
  149. {
  150. if (!error)
  151. {
  152. LOG_INFO("send success!");
  153. }
  154. else
  155. {
  156. delete this;
  157. }
  158. }