|
|
@@ -1,109 +1,736 @@
|
|
|
+#include "../pch/pch.h"
|
|
|
#include "CMqttClient.h"
|
|
|
|
|
|
#include <openssl/hmac.h>
|
|
|
#include <openssl/bio.h>
|
|
|
|
|
|
+#include "../tool/CPosPrinter.h"
|
|
|
+#include "../order/CWaimaiOrder.h"
|
|
|
|
|
|
-CMqttClient::CMqttClient()
|
|
|
+#include "mmsystem.h"
|
|
|
+
|
|
|
+CMqttClient::CMqttClient(HWND hwnd)
|
|
|
{
|
|
|
-}
|
|
|
+ m_hwnd = hwnd;
|
|
|
|
|
|
+ m_async_client = new mqtt::async_client(SERVER_ADDRESS, CLIENT_ID);
|
|
|
+}
|
|
|
|
|
|
CMqttClient::~CMqttClient()
|
|
|
{
|
|
|
+ delete m_async_client;
|
|
|
}
|
|
|
|
|
|
void CMqttClient::Start()
|
|
|
{
|
|
|
- std::thread(&CMqttClient::Run, this).detach();
|
|
|
+ m_nStopNum = 0;
|
|
|
+
|
|
|
+ //处理Mqtt消息接收
|
|
|
+ std::thread(&CMqttClient::Run, this).detach();
|
|
|
+
|
|
|
+ //处理声音提醒
|
|
|
+ std::thread(&CMqttClient::HandleVoice, this).detach();
|
|
|
+
|
|
|
+ //处理订单确认
|
|
|
+ std::thread(&CMqttClient::HandleConfirm, this).detach();
|
|
|
+
|
|
|
+ //处理打印
|
|
|
+ std::thread(&CMqttClient::HandlePrinter, this).detach();
|
|
|
+
|
|
|
+ //处理收银打印
|
|
|
+ std::thread(&CMqttClient::HandleShouyinPrinter, this).detach();
|
|
|
+
|
|
|
+ //处理标签打印
|
|
|
+ std::thread(&CMqttClient::HandleBiaoqianPrinter, this).detach();
|
|
|
+
|
|
|
+ //处理厨房打印
|
|
|
+ std::thread(&CMqttClient::HandleChufangPrinter, this).detach();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::Stop()
|
|
|
+{
|
|
|
+ m_is_running = false;
|
|
|
+
|
|
|
+ m_voice_mutex.lock();
|
|
|
+ while(!m_voice_queue.empty())
|
|
|
+ {
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ }
|
|
|
+ m_voice_mutex.unlock();
|
|
|
+
|
|
|
+ m_confirm_mutex.lock();
|
|
|
+ while(!m_confirm_queue.empty())
|
|
|
+ {
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ }
|
|
|
+ m_confirm_mutex.unlock();
|
|
|
+
|
|
|
+ m_printer_mutex.lock();
|
|
|
+ while(!m_printer_queue.empty())
|
|
|
+ {
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ }
|
|
|
+ m_printer_mutex.unlock();
|
|
|
+
|
|
|
+ m_shouyin_printer_mutex.lock();
|
|
|
+ while(!m_shouyin_printer_queue.empty())
|
|
|
+ {
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ }
|
|
|
+ m_shouyin_printer_mutex.unlock();
|
|
|
+
|
|
|
+ m_biaoqian_printer_mutex.lock();
|
|
|
+ while(!m_biaoqian_printer_queue.empty())
|
|
|
+ {
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ }
|
|
|
+ m_biaoqian_printer_mutex.unlock();
|
|
|
+
|
|
|
+ m_chufang_printer_mutex.lock();
|
|
|
+ while(!m_chufang_printer_queue.empty())
|
|
|
+ {
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ }
|
|
|
+ m_chufang_printer_mutex.unlock();
|
|
|
}
|
|
|
|
|
|
void CMqttClient::Run()
|
|
|
{
|
|
|
- // A subscriber often wants the server to remember its messages when its
|
|
|
- // disconnected. In that case, it needs a unique ClientID and a
|
|
|
- // non-clean session.
|
|
|
+ // A subscriber often wants the server to remember its messages when its
|
|
|
+ // disconnected. In that case, it needs a unique ClientID and a
|
|
|
+ // non-clean session.
|
|
|
+ m_connOpts.set_clean_session(false);
|
|
|
+
|
|
|
+ //计算用户名和密码
|
|
|
+ this->CalUserInfo();
|
|
|
+
|
|
|
+ m_connOpts.set_user_name(m_UserName.c_str());
|
|
|
+ m_connOpts.set_password(m_Password.c_str());
|
|
|
|
|
|
- mqtt::async_client cli(SERVER_ADDRESS, CLIENT_ID);
|
|
|
+ // Install the callback(s) before connecting.
|
|
|
+ m_async_client->set_callback(*this);
|
|
|
|
|
|
- mqtt::connect_options connOpts;
|
|
|
- connOpts.set_clean_session(false);
|
|
|
+ // Start the connection.
|
|
|
+ // When completed, the callback will subscribe to topic.
|
|
|
|
|
|
- this->CalUserInfo();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ LOG_INFO("Connecting to the MQTT server...");
|
|
|
+ m_async_client->connect(m_connOpts, nullptr, *this);
|
|
|
+ }
|
|
|
+ catch(const mqtt::exception& exc)
|
|
|
+ {
|
|
|
+ std::string info = "ERROR: Unable to connect to MQTT server:" + SERVER_ADDRESS + exc.get_message();
|
|
|
|
|
|
- connOpts.set_user_name(m_UserName.c_str());
|
|
|
- connOpts.set_password(m_Password.c_str());
|
|
|
+ LOG_INFO(info.c_str());
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // Install the callback(s) before connecting.
|
|
|
- callback cb(cli, connOpts);
|
|
|
- cli.set_callback(cb);
|
|
|
+ m_is_running = true;
|
|
|
+ while(m_is_running)
|
|
|
+ {
|
|
|
+ //一直循环,一直工作,等待接收消息
|
|
|
+ }
|
|
|
|
|
|
- // Start the connection.
|
|
|
- // When completed, the callback will subscribe to topic.
|
|
|
+ //代码走到这里,说明退出登录了,要暂停推送了
|
|
|
+ try
|
|
|
+ {
|
|
|
+ LOG_INFO("Disconnecting from the MQTT server...");
|
|
|
+ m_async_client->disconnect()->wait();
|
|
|
+ LOG_INFO("OK...");
|
|
|
+ }
|
|
|
+ catch(const mqtt::exception& exc)
|
|
|
+ {
|
|
|
+ LOG_INFO(("disconnect error, exc:" + exc.get_message()).c_str());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+
|
|
|
+ return;
|
|
|
+}
|
|
|
|
|
|
- try {
|
|
|
- std::cout << "Connecting to the MQTT server..." << std::flush;
|
|
|
- cli.connect(connOpts, nullptr, cb);
|
|
|
+void CMqttClient::reconnect()
|
|
|
+{
|
|
|
+ //暂停10秒后重连
|
|
|
+ std::this_thread::sleep_for(std::chrono::milliseconds(10000));
|
|
|
+ try
|
|
|
+ {
|
|
|
+ m_async_client->connect(m_connOpts, nullptr, *this);
|
|
|
+ }
|
|
|
+ catch(const mqtt::exception& exc)
|
|
|
+ {
|
|
|
+ //重连异常了
|
|
|
+ std::string err = "Error: ";
|
|
|
+ err += exc.what();
|
|
|
+ LOG_INFO(err.c_str());
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Re-connection failure
|
|
|
+void CMqttClient::on_failure(const mqtt::token& tok)
|
|
|
+{
|
|
|
+ if (m_is_mqtt_connected == false)
|
|
|
+ {
|
|
|
+ //连接失败了
|
|
|
+ LOG_INFO("Connection attempt failed");
|
|
|
+ if (++nretry_ > N_RETRY_ATTEMPTS)
|
|
|
+ {
|
|
|
+ //重连次数超过了最大重连试错的次数,暂时不做任何处理,还是继续重连
|
|
|
+ }
|
|
|
+
|
|
|
+ reconnect();
|
|
|
}
|
|
|
- catch (const mqtt::exception& exc) {
|
|
|
- std::cerr << "\nERROR: Unable to connect to MQTT server: '"
|
|
|
- << SERVER_ADDRESS << "'" << exc << std::endl;
|
|
|
- return;
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //已经连接,那就是订阅失败了
|
|
|
+ std::string info = "Subscription failure";
|
|
|
+ if (tok.get_message_id() != 0)
|
|
|
+ {
|
|
|
+ info += " for token: [" + to_string(tok.get_message_id()) + "]";
|
|
|
+ }
|
|
|
+
|
|
|
+ LOG_INFO(info.c_str());
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
- // Just block till user tells us to quit.
|
|
|
+void CMqttClient::on_success(const mqtt::token& tok)
|
|
|
+{
|
|
|
+ if (m_is_mqtt_connected == false)
|
|
|
+ {
|
|
|
+ //连接成功了,再connected里面处理
|
|
|
+
|
|
|
+ int a = 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //订阅成功了
|
|
|
+ std::string info = "Subscription success";
|
|
|
+ if (tok.get_message_id() != 0)
|
|
|
+ {
|
|
|
+ info += " for token: [" + to_string(tok.get_message_id()) + "]";
|
|
|
+ }
|
|
|
+ auto top = tok.get_topics();
|
|
|
+ if (top && !top->empty())
|
|
|
+ {
|
|
|
+ info += ",token topic: '" + (*top)[0] + "', ...";
|
|
|
+ }
|
|
|
|
|
|
- while (true)
|
|
|
- ;
|
|
|
+ LOG_INFO(info.c_str());
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- return;
|
|
|
+void CMqttClient::connected(const std::string& cause)
|
|
|
+{
|
|
|
+ m_is_mqtt_connected = true;
|
|
|
+
|
|
|
+ std::string info = "Connection success, nSubscribing to topic " + TOPIC + " for client " + CLIENT_ID + " using QoS" + to_string(MQTT_QOS);
|
|
|
+ LOG_INFO(info.c_str());
|
|
|
+
|
|
|
+ //不管是第一次连接,还是重连,都订阅一次
|
|
|
+ m_async_client->subscribe(TOPIC, MQTT_QOS, nullptr, *this);
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::connection_lost(const std::string& cause)
|
|
|
+{
|
|
|
+ m_is_mqtt_connected = false;
|
|
|
+
|
|
|
+ std::string info = "Connection lost";
|
|
|
+ if(!cause.empty())
|
|
|
+ {
|
|
|
+ info += ", cause: " + cause;
|
|
|
+ }
|
|
|
+
|
|
|
+ info += ",Reconnecting...";
|
|
|
+
|
|
|
+ LOG_INFO(info.c_str());
|
|
|
+
|
|
|
+ nretry_ = 0;
|
|
|
+ reconnect();
|
|
|
+}
|
|
|
+
|
|
|
+// Callback for when a message arrives.
|
|
|
+void CMqttClient::message_arrived(mqtt::const_message_ptr msg)
|
|
|
+{
|
|
|
+ std::string info = "Message arrived, ttopic: '" + msg->get_topic() + "', payload: '" + msg->to_string();
|
|
|
+
|
|
|
+ LOG_INFO(info.c_str());
|
|
|
+
|
|
|
+ //要的就是这个,收到消息了
|
|
|
+ std::string content = msg->to_string();
|
|
|
+
|
|
|
+ //对接收到的消息进行处理
|
|
|
+ this->HandleMessage(content);
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::delivery_complete(mqtt::delivery_token_ptr token)
|
|
|
+{
|
|
|
}
|
|
|
|
|
|
void CMqttClient::CalUserInfo()
|
|
|
{
|
|
|
- //实例 ID,购买后从控制台获取
|
|
|
- char * instanceId = "post-cn-x0r3kjlsy02";
|
|
|
- //测试收发消息的 Topic
|
|
|
- char* topic = "lewaimai_windows_pos";
|
|
|
- //接入点域名,从控制台获取
|
|
|
- char *host = "post-cn-x0r3kjlsy02.mqtt.aliyuncs.com";
|
|
|
- //客户端使用的 GroupID,从控制台申请
|
|
|
- char *groupId = "GID_LEWAIMAI_WINDOWS_POS";
|
|
|
- //客户端 ClientID 的后缀,由业务自行指定,只需要保证全局唯一即可
|
|
|
- char *deviceId = "abcde";
|
|
|
- /**
|
|
|
- * 账号 accesskey,从账号系统控制台获取
|
|
|
- * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
|
|
|
- * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
|
|
|
- * 可以把AccessKey ID和AccessKey Secret保存在环境变量。运行本代码示例之前,请先配置环境变量MQTT_AK_ENV和MQTT_SK_ENV
|
|
|
- * 例如:export MQTT_AK_ENV=<access_key_id>
|
|
|
- * export MQTT_SK_ENV=<access_key_secret>
|
|
|
- * 需要将<access_key_id>替换为已准备好的AccessKey ID,<access_key_secret>替换为AccessKey Secret。
|
|
|
- */
|
|
|
- char *accessKey = "LTAI4G5oikJPMfhq5PuW26qu";
|
|
|
- //账号 SecretKey,从账号控制台获取
|
|
|
- char *secretKey = "LsIxqepi2o3X0b17FSa7JaANfIMDVY";
|
|
|
- //使用的协议端口,默认 tcp 协议使用1883,如果需要使用 SSL 加密,端口设置成8883,具体协议和端口参考文档链接https://help.aliyun.com/document_detail/44867.html?spm=a2c4g.11186623.6.547.38d81cf7XRnP0C
|
|
|
- int port = 1883;
|
|
|
- int qos = 0;
|
|
|
- int cleanSession = 1;
|
|
|
- int rc = 0;
|
|
|
- unsigned char tempData[100];
|
|
|
- unsigned int len = 0;
|
|
|
- //ClientID要求使用 GroupId 和 DeviceId 拼接而成,长度不得超过64个字符
|
|
|
- char clientIdUrl[64];
|
|
|
- sprintf(clientIdUrl, "%s@@@%s", groupId, deviceId);
|
|
|
-
|
|
|
- const unsigned char*abc = (unsigned char*)clientIdUrl;
|
|
|
-
|
|
|
- //username和 Password 签名模式下的设置方法,参考文档 https://help.aliyun.com/document_detail/48271.html?spm=a2c4g.11186623.6.553.217831c3BSFry7
|
|
|
- HMAC(EVP_sha1(), secretKey, strlen(secretKey), abc, strlen(clientIdUrl), tempData, &len);
|
|
|
- char resultData[100];
|
|
|
- int passWordLen = EVP_EncodeBlock((unsigned char *)resultData, tempData, len);
|
|
|
- resultData[passWordLen] = '\0';
|
|
|
- printf("passWord is %s", resultData);
|
|
|
- char userNameData[128];
|
|
|
- sprintf(userNameData, "Signature|%s|%s", accessKey, instanceId);
|
|
|
- m_UserName = userNameData;
|
|
|
- m_Password = resultData;
|
|
|
+ //实例 ID,购买后从控制台获取
|
|
|
+ char* instanceId = "post-cn-x0r3kjlsy02";
|
|
|
+
|
|
|
+ //测试收发消息的 Topic
|
|
|
+ char* topic = "lewaimai_windows_pos";
|
|
|
+
|
|
|
+ //接入点域名,从控制台获取
|
|
|
+ char* host = "post-cn-x0r3kjlsy02.mqtt.aliyuncs.com";
|
|
|
+
|
|
|
+ //客户端使用的 GroupID,从控制台申请
|
|
|
+ char* groupId = "GID_LEWAIMAI_WINDOWS_POS";
|
|
|
+
|
|
|
+ //客户端 ClientID 的后缀,由业务自行指定,只需要保证全局唯一即可
|
|
|
+ char* deviceId = "abcde";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 账号 accesskey,从账号系统控制台获取
|
|
|
+ * 阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
|
|
|
+ * 强烈建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
|
|
|
+ * 可以把AccessKey ID和AccessKey Secret保存在环境变量。运行本代码示例之前,请先配置环境变量MQTT_AK_ENV和MQTT_SK_ENV
|
|
|
+ * 例如:export MQTT_AK_ENV=<access_key_id>
|
|
|
+ * export MQTT_SK_ENV=<access_key_secret>
|
|
|
+ * 需要将<access_key_id>替换为已准备好的AccessKey ID,<access_key_secret>替换为AccessKey Secret。
|
|
|
+ */
|
|
|
+ char* accessKey = "LTAI4G5oikJPMfhq5PuW26qu";
|
|
|
+ //账号 SecretKey,从账号控制台获取
|
|
|
+ char* secretKey = "LsIxqepi2o3X0b17FSa7JaANfIMDVY";
|
|
|
+
|
|
|
+ unsigned char tempData[100];
|
|
|
+ unsigned int len = 0;
|
|
|
+
|
|
|
+ //ClientID要求使用 GroupId 和 DeviceId 拼接而成,长度不得超过64个字符
|
|
|
+ char clientIdUrl[64];
|
|
|
+ sprintf(clientIdUrl, "%s@@@%s", groupId, deviceId);
|
|
|
+
|
|
|
+ const unsigned char* abc = (unsigned char*)clientIdUrl;
|
|
|
+
|
|
|
+ //username和 Password 签名模式下的设置方法,参考文档 https://help.aliyun.com/document_detail/48271.html?spm=a2c4g.11186623.6.553.217831c3BSFry7
|
|
|
+ HMAC(EVP_sha1(), secretKey, strlen(secretKey), abc, strlen(clientIdUrl), tempData, &len);
|
|
|
+
|
|
|
+ char resultData[100];
|
|
|
+ int passWordLen = EVP_EncodeBlock((unsigned char*)resultData, tempData, len);
|
|
|
+ resultData[passWordLen] = '\0';
|
|
|
+ printf("passWord is %s", resultData);
|
|
|
+
|
|
|
+ char userNameData[128];
|
|
|
+ sprintf(userNameData, "Signature|%s|%s", accessKey, instanceId);
|
|
|
+
|
|
|
+ m_UserName = userNameData;
|
|
|
+ m_Password = resultData;
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandleMessage(std::string message)
|
|
|
+{
|
|
|
+ //收到服务器的消息,对服务器的消息进行处理
|
|
|
+ rapidjson::Document document;
|
|
|
+ document.Parse(message.c_str());
|
|
|
+ if(!document.IsObject())
|
|
|
+ {
|
|
|
+ LOG_INFO("message 非法!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ std::string type;
|
|
|
+ if(document["msg_type"].IsInt())
|
|
|
+ {
|
|
|
+ type = to_string(document["msg_type"].GetInt());
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ type = document["msg_type"].GetString();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(type == "1")
|
|
|
+ {
|
|
|
+ std::string order_id = document["waimai_order_id"].GetString();
|
|
|
+ std::string order_no = document["waimai_order_no"].GetString();
|
|
|
+
|
|
|
+ //新订单来了,首先判断是否要语音提醒
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_voice") == "1")
|
|
|
+ {
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_autoconfirm") == "1")
|
|
|
+ {
|
|
|
+ AddVoice(2);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ AddVoice(1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断是否要自动确认
|
|
|
+ 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, 1);
|
|
|
+ }
|
|
|
+ else if(type == "2")
|
|
|
+ {
|
|
|
+ AddVoice(3);
|
|
|
+ }
|
|
|
+ else if(type == "3")
|
|
|
+ {
|
|
|
+ AddVoice(4);
|
|
|
+
|
|
|
+ std::string order_id = document["waimai_order_id"].GetString();
|
|
|
+ std::string order_no = document["waimai_order_no"].GetString();
|
|
|
+
|
|
|
+ AddPinter(order_id, order_no, 1, 3);
|
|
|
+ }
|
|
|
+ else if(type == "0")
|
|
|
+ {
|
|
|
+ //这个表示被人挤下线了
|
|
|
+ PostMessage(m_hwnd, WM_LOGIN_AGAIN_OUT, 0, 0);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ *类型 1:新外卖订单 2:新外卖订单自动接单 3:外卖订单取消 4:外卖订单退款
|
|
|
+ **/
|
|
|
+void CMqttClient::AddVoice(int voice_type)
|
|
|
+{
|
|
|
+ m_voice_mutex.lock();
|
|
|
+
|
|
|
+ m_voice_queue.push(voice_type);
|
|
|
+
|
|
|
+ m_voice_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::AddConfirm(std::string order_id)
|
|
|
+{
|
|
|
+ m_confirm_mutex.lock();
|
|
|
+
|
|
|
+ m_confirm_queue.push(order_id);
|
|
|
+
|
|
|
+ m_confirm_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::AddPinter(std::string order_id, std::string order_no, int print_type, int order_type)
|
|
|
+{
|
|
|
+ m_printer_mutex.lock();
|
|
|
+
|
|
|
+ WaimaiPinterInfo newPrinter;
|
|
|
+ newPrinter.order_id = order_id;
|
|
|
+ newPrinter.order_no = order_no;
|
|
|
+ newPrinter.print_type = print_type;
|
|
|
+ newPrinter.order_type = order_type;
|
|
|
+
|
|
|
+ m_printer_queue.push(newPrinter);
|
|
|
+
|
|
|
+ m_printer_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::AddShouyinPrinter(CWaimaiOrder order)
|
|
|
+{
|
|
|
+ m_shouyin_printer_mutex.lock();
|
|
|
+
|
|
|
+ m_shouyin_printer_queue.push(order);
|
|
|
+
|
|
|
+ m_shouyin_printer_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::AddBiaoqianPrinter(CWaimaiOrder order)
|
|
|
+{
|
|
|
+ m_biaoqian_printer_mutex.lock();
|
|
|
+
|
|
|
+ m_biaoqian_printer_queue.push(order);
|
|
|
+
|
|
|
+ m_biaoqian_printer_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::AddChufangPrinter(CWaimaiOrder order)
|
|
|
+{
|
|
|
+ m_chufang_printer_mutex.lock();
|
|
|
+
|
|
|
+ m_chufang_printer_queue.push(order);
|
|
|
+
|
|
|
+ m_chufang_printer_mutex.unlock();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandleVoice()
|
|
|
+{
|
|
|
+ while(m_is_work)
|
|
|
+ {
|
|
|
+ m_voice_mutex.lock();
|
|
|
+
|
|
|
+ if(m_voice_queue.empty())
|
|
|
+ {
|
|
|
+ m_voice_mutex.unlock();
|
|
|
+
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ int voice_type = m_voice_queue.front();
|
|
|
+ m_voice_queue.pop();
|
|
|
+
|
|
|
+ m_voice_mutex.unlock();
|
|
|
+
|
|
|
+ if(voice_type == 1)
|
|
|
+ {
|
|
|
+ wstring path = CSystem::GetProgramDir() + L"\\music\\waimai_new.wav";
|
|
|
+ PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
|
|
|
+ }
|
|
|
+ else if(voice_type == 2)
|
|
|
+ {
|
|
|
+ wstring path = CSystem::GetProgramDir() + L"\\music\\waimai_new_auto.wav";
|
|
|
+ PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
|
|
|
+ }
|
|
|
+ else if(voice_type == 3)
|
|
|
+ {
|
|
|
+ wstring path = CSystem::GetProgramDir() + L"\\music\\waimai_quxiao.wav";
|
|
|
+ PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
|
|
|
+ }
|
|
|
+ else if(voice_type == 4)
|
|
|
+ {
|
|
|
+ wstring path = CSystem::GetProgramDir() + L"\\music\\waimai_tuikuan.wav";
|
|
|
+ PlaySound(path.c_str(), NULL, SND_FILENAME | SND_ASYNC);
|
|
|
+ }
|
|
|
+
|
|
|
+ //8秒内最多播放一次
|
|
|
+ CSystem::my_sleep(8);
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandleConfirm()
|
|
|
+{
|
|
|
+ while(m_is_work)
|
|
|
+ {
|
|
|
+ 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.front();
|
|
|
+ m_confirm_queue.pop();
|
|
|
+
|
|
|
+ m_confirm_mutex.unlock();
|
|
|
+
|
|
|
+ CWaimaiOrder newOrder;
|
|
|
+ newOrder.ConfirmeOrder(order_id);
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandlePrinter()
|
|
|
+{
|
|
|
+ while(m_is_work)
|
|
|
+ {
|
|
|
+ m_printer_mutex.lock();
|
|
|
+
|
|
|
+ if(m_printer_queue.empty())
|
|
|
+ {
|
|
|
+ m_printer_mutex.unlock();
|
|
|
+
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ WaimaiPinterInfo printerInfo = m_printer_queue.front();
|
|
|
+ std::string order_id = printerInfo.order_id;
|
|
|
+ std::string order_no = printerInfo.order_no;
|
|
|
+ int print_type = printerInfo.print_type;
|
|
|
+ int order_type = printerInfo.order_type;
|
|
|
+
|
|
|
+ m_printer_queue.pop();
|
|
|
+
|
|
|
+ m_printer_mutex.unlock();
|
|
|
+
|
|
|
+ if(order_type == 1)
|
|
|
+ {
|
|
|
+ CWaimaiOrder order;
|
|
|
+ if(print_type == 1)
|
|
|
+ {
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_printer") == "1" || CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1" || CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
|
|
|
+ {
|
|
|
+ bool ret = order.InitData(order_id, order_no);
|
|
|
+
|
|
|
+ if(ret == false)
|
|
|
+ {
|
|
|
+ this->AddPinter(order_id, order_no, print_type);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断是否自动打印收银小票
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_printer") == "1")
|
|
|
+ {
|
|
|
+ AddShouyinPrinter(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断是否自动打印标签小票
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1")
|
|
|
+ {
|
|
|
+ AddBiaoqianPrinter(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断是否进行自动的厨房打印
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
|
|
|
+ {
|
|
|
+ AddChufangPrinter(order);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ bool ret = order.InitData(order_id, order_no);
|
|
|
+
|
|
|
+ if(ret == false)
|
|
|
+ {
|
|
|
+ this->AddPinter(order_id, order_no, print_type);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ //手动打印的模式下,收银小票一定打印,标签如果没开就不打印
|
|
|
+ AddShouyinPrinter(order);
|
|
|
+
|
|
|
+ //手动打印的模式下,厨房小票一定打印
|
|
|
+ AddChufangPrinter(order);
|
|
|
+
|
|
|
+ //判断是否进行自动的厨房打印
|
|
|
+ /*
|
|
|
+ if (CSetting::GetParam("setting_is_new_waimai_chufang_printer") == "1")
|
|
|
+ {
|
|
|
+ AddChufangPrinter(order);
|
|
|
+ }
|
|
|
+ }*/
|
|
|
+
|
|
|
+ //判断是否自动打印标签小票
|
|
|
+ if(CSetting::GetParam("setting_is_new_waimai_biaoqian_printer") == "1")
|
|
|
+ {
|
|
|
+ AddBiaoqianPrinter(order);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(order_type == 3)
|
|
|
+ {
|
|
|
+ //顾客申请退款的
|
|
|
+ CWaimaiOrder order;
|
|
|
+ bool ret = order.InitData(order_id, order_no);
|
|
|
+
|
|
|
+ if(ret == false)
|
|
|
+ {
|
|
|
+ this->AddPinter(order_id, order_no, print_type, order_type);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ CPosPrinter printer;
|
|
|
+ printer.PrintWaimaiOrderTuikuan(order);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandleShouyinPrinter()
|
|
|
+{
|
|
|
+ while(m_is_work)
|
|
|
+ {
|
|
|
+ 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.front();
|
|
|
+
|
|
|
+ m_shouyin_printer_queue.pop();
|
|
|
+
|
|
|
+ m_shouyin_printer_mutex.unlock();
|
|
|
+
|
|
|
+ CPosPrinter printer;
|
|
|
+ printer.PrintWaimaiOrderShouyin(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandleBiaoqianPrinter()
|
|
|
+{
|
|
|
+ while(m_is_work)
|
|
|
+ {
|
|
|
+ m_biaoqian_printer_mutex.lock();
|
|
|
+
|
|
|
+ if(m_biaoqian_printer_queue.empty())
|
|
|
+ {
|
|
|
+ m_biaoqian_printer_mutex.unlock();
|
|
|
+
|
|
|
+ CSystem::my_sleep(1);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ CWaimaiOrder order = m_biaoqian_printer_queue.front();
|
|
|
+
|
|
|
+ m_biaoqian_printer_queue.pop();
|
|
|
+
|
|
|
+ m_biaoqian_printer_mutex.unlock();
|
|
|
+
|
|
|
+ CPosPrinter printer;
|
|
|
+ printer.PrintWaimaiOrderBiaoqian(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::HandleChufangPrinter()
|
|
|
+{
|
|
|
+ while(m_is_work)
|
|
|
+ {
|
|
|
+ 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.front();
|
|
|
+
|
|
|
+ m_chufang_printer_queue.pop();
|
|
|
+
|
|
|
+ m_chufang_printer_mutex.unlock();
|
|
|
+
|
|
|
+ CPosPrinter printer;
|
|
|
+ printer.PrintWaimaiOrderChufang(order);
|
|
|
+ }
|
|
|
+
|
|
|
+ AddStopNum();
|
|
|
+}
|
|
|
+
|
|
|
+void CMqttClient::AddStopNum()
|
|
|
+{
|
|
|
+ m_nStopNumMutex.lock();
|
|
|
+
|
|
|
+ m_nStopNum++;
|
|
|
+
|
|
|
+ m_nStopNumMutex.unlock();
|
|
|
+
|
|
|
+ if(m_nStopNum == 7)
|
|
|
+ {
|
|
|
+ //确认所有子线程都退出了,再删除自己
|
|
|
+ delete this;
|
|
|
+ }
|
|
|
}
|