#include "../pch/pch.h" #include "CMessagePush.h" #include "../tool/CPosPrinter.h" #include "../order/CWaimaiOrder.h" #include "mmsystem.h" CMessagePush::~CMessagePush() { } void CMessagePush::Start() { //开始处理接收消息 std::thread t_1(&CMessagePush::Run, this); t_1.detach(); //处理声音提醒 std::thread t_2(&CMessagePush::HandleVoice, this); t_2.detach(); //处理订单确认 std::thread t_3(&CMessagePush::HandleConfirm, this); t_3.detach(); //处理收银打印 std::thread t_4(&CMessagePush::HandleShouyinPrinter, this); t_4.detach(); //处理厨房打印 std::thread t_5(&CMessagePush::HandleChufangPrinter, this); t_5.detach(); std::thread t_6(&CMessagePush::HandlePrinter, this); t_6.detach(); } void CMessagePush::Run() { try { char host[] = "127.0.0.1"; char port[] = "9001"; tcp::resolver resolver(m_io_context); tcp::resolver::results_type endpoints = resolver.resolve(tcp::v4(), host, port); boost::asio::connect(socket_, endpoints); //发送身份信息 rapidjson::Document doc; doc.SetObject(); rapidjson::Document::AllocatorType& allocator = doc.GetAllocator(); //获取分配器 doc.AddMember("username", "zhangyang", allocator); rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); doc.Accept(writer); //返回给接入层的消息 std::string m_login_msg = buffer.GetString(); //异步发送消息 PushMessage(m_login_msg); //同时启动一个异步接受消息 socket_.async_read_some(boost::asio::buffer(data_, max_length), boost::bind(&CMessagePush::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); m_io_context.run(); } catch(std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } } void CMessagePush::PushMessage(std::string msg) { boost::asio::async_write(socket_, boost::asio::buffer(msg.c_str(), msg.length()), boost::bind(&CMessagePush::handle_write, this, boost::asio::placeholders::error)); } void CMessagePush::HandleMessage(std::string msg) { try { //收到服务器的消息,对服务器的消息进行处理 rapidjson::Document document; document.Parse(msg.c_str()); if(!document.IsObject()) { LOG_INFO("message 非法!"); return; } int type = document["type"].GetInt(); if(type == 0) { std::string status = document["status"].GetString(); if(status == "ok") { //表示连接服务器成功了 } } else if(type == 1) { std::string username = document["username"].GetString(); std::string order_id = document["order_id"].GetString(); std::string order_no = document["order_no"].GetString(); //新订单来了,首先判断是否要语音提醒 if(CSetting::GetParam("setting_is_new_waimai_voice") == "1") { AddVoice(order_id); } //判断是否要自动确认 if(CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1") { AddConfirm(order_id); } //判断是否右下角弹框提醒 if(CSetting::GetParam("setting_is_new_waimai_dialog") == "1") { } AddPinter(order_id, order_no); } } catch(std::exception& e) { std::cerr << "Exception: " << e.what() << "\n"; } } void CMessagePush::handle_read(const boost::system::error_code& error, size_t bytes_transferred) { if(!error) { string s_reply = data_; HandleMessage(s_reply); memset(data_, 0, max_length); socket_.async_read_some(boost::asio::buffer(data_, max_length), boost::bind(&CMessagePush::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } else { //这里有时会出现,看看是什么错误,可能是断开了跟服务器的连接 } } void CMessagePush::handle_write(const boost::system::error_code& error) { if(!error) { LOG_INFO("send success!"); } else { //这里有时会出现,看看是什么错误,可能是断开了跟服务器的连接 } } void CMessagePush::AddVoice(std::string order_id) { m_voice_mutex.lock(); m_voice_queue.push(order_id); m_voice_mutex.unlock(); } void CMessagePush::AddConfirm(std::string order_id) { m_confirm_mutex.lock(); m_confirm_queue.push(order_id); m_confirm_mutex.unlock(); } void CMessagePush::AddPinter(std::string order_id, std::string order_no) { m_printer_mutex.lock(); WaimaiPinterInfo newPrinter; newPrinter.order_id = order_id; newPrinter.order_no = order_no; m_printer_queue.push(newPrinter); m_printer_mutex.unlock(); } void CMessagePush::AddShouyinPrinter(CWaimaiOrder order) { m_shouyin_printer_mutex.lock(); m_shouyin_printer_queue.push(order); m_shouyin_printer_mutex.unlock(); } void CMessagePush::AddChufangPrinter(CWaimaiOrder order) { m_chufang_printer_mutex.lock(); m_chufang_printer_queue.push(order); m_chufang_printer_mutex.unlock(); } void CMessagePush::HandleVoice() { while(true) { m_voice_mutex.lock(); if(m_voice_queue.empty()) { m_voice_mutex.unlock(); CSystem::my_sleep(1); continue; } std::string order_id = m_voice_queue.back(); m_voice_queue.pop(); m_voice_mutex.unlock(); wstring path = CSystem::GetProgramDir() + L"\\music\\new_wamai_order.wav"; PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC); //8秒内最多播放一次 CSystem::my_sleep(8); } } void CMessagePush::HandleConfirm() { while(true) { m_confirm_mutex.lock(); if(m_confirm_queue.empty()) { m_confirm_mutex.unlock(); CSystem::my_sleep(1); continue; } std::string order_id = m_confirm_queue.back(); m_confirm_queue.pop(); m_confirm_mutex.unlock(); CWaimaiOrder newOrder; newOrder.ConfirmeOrder(order_id); } } void CMessagePush::HandlePrinter() { while(true) { m_printer_mutex.lock(); if(m_printer_queue.empty()) { m_printer_mutex.unlock(); CSystem::my_sleep(1); continue; } WaimaiPinterInfo printerInfo = m_printer_queue.back(); std::string order_id = printerInfo.order_id; std::string order_no = printerInfo.order_no; m_printer_queue.pop(); m_printer_mutex.unlock(); CWaimaiOrder order; if(CSetting::GetParam("setting_is_new_waimai_printer") == "1" || CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1") { order.InitData(order_id, order_no); } //判断是否自动打印收银小票 if(CSetting::GetParam("setting_is_new_waimai_printer") == "1") { AddShouyinPrinter(order); } //判断是否进行自动的厨房打印 if(CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1") { AddChufangPrinter(order); } } } void CMessagePush::HandleShouyinPrinter() { while(true) { m_shouyin_printer_mutex.lock(); if(m_shouyin_printer_queue.empty()) { m_shouyin_printer_mutex.unlock(); CSystem::my_sleep(1); continue; } CWaimaiOrder order = m_shouyin_printer_queue.back(); m_shouyin_printer_queue.pop(); m_shouyin_printer_mutex.unlock(); CPosPrinter printer; printer.PrintWaimaiOrderShouyin(order); } } void CMessagePush::HandleChufangPrinter() { while(true) { m_chufang_printer_mutex.lock(); if(m_chufang_printer_queue.empty()) { m_chufang_printer_mutex.unlock(); CSystem::my_sleep(1); continue; } CWaimaiOrder order = m_chufang_printer_queue.back(); m_chufang_printer_queue.pop(); m_chufang_printer_mutex.unlock(); CPosPrinter printer; printer.PrintWaimaiOrderChufang(order); } }