|
@@ -12,17 +12,17 @@
|
|
|
CMqttClient::CMqttClient(HWND hwnd)
|
|
CMqttClient::CMqttClient(HWND hwnd)
|
|
|
{
|
|
{
|
|
|
m_hwnd = hwnd;
|
|
m_hwnd = hwnd;
|
|
|
-
|
|
|
|
|
- m_async_client = new mqtt::async_client(SERVER_ADDRESS, CLIENT_ID);
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
CMqttClient::~CMqttClient()
|
|
CMqttClient::~CMqttClient()
|
|
|
{
|
|
{
|
|
|
- delete m_async_client;
|
|
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CMqttClient::Start()
|
|
void CMqttClient::Start()
|
|
|
{
|
|
{
|
|
|
|
|
+ m_is_running = true;
|
|
|
|
|
+
|
|
|
m_nStopNum = 0;
|
|
m_nStopNum = 0;
|
|
|
|
|
|
|
|
//处理Mqtt消息接收
|
|
//处理Mqtt消息接收
|
|
@@ -96,6 +96,9 @@ void CMqttClient::Stop()
|
|
|
|
|
|
|
|
void CMqttClient::Run()
|
|
void CMqttClient::Run()
|
|
|
{
|
|
{
|
|
|
|
|
+ m_client_id = "GID_LEWAIMAI_WINDOWS_POS@@@" + CSetting::getUsername();
|
|
|
|
|
+ m_async_client = new mqtt::async_client(m_server_address, m_client_id);
|
|
|
|
|
+
|
|
|
// A subscriber often wants the server to remember its messages when its
|
|
// A subscriber often wants the server to remember its messages when its
|
|
|
// disconnected. In that case, it needs a unique ClientID and a
|
|
// disconnected. In that case, it needs a unique ClientID and a
|
|
|
// non-clean session.
|
|
// non-clean session.
|
|
@@ -120,13 +123,12 @@ void CMqttClient::Run()
|
|
|
}
|
|
}
|
|
|
catch(const mqtt::exception& exc)
|
|
catch(const mqtt::exception& exc)
|
|
|
{
|
|
{
|
|
|
- std::string info = "ERROR: Unable to connect to MQTT server:" + SERVER_ADDRESS + exc.get_message();
|
|
|
|
|
|
|
+ std::string info = "ERROR: Unable to connect to MQTT server:" + m_server_address + exc.get_message();
|
|
|
|
|
|
|
|
LOG_INFO(info.c_str());
|
|
LOG_INFO(info.c_str());
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- m_is_running = true;
|
|
|
|
|
while(m_is_running)
|
|
while(m_is_running)
|
|
|
{
|
|
{
|
|
|
//一直循环,一直工作,等待接收消息
|
|
//一直循环,一直工作,等待接收消息
|
|
@@ -145,6 +147,9 @@ void CMqttClient::Run()
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ //销毁客户端
|
|
|
|
|
+ delete m_async_client;
|
|
|
|
|
+
|
|
|
AddStopNum();
|
|
AddStopNum();
|
|
|
|
|
|
|
|
return;
|
|
return;
|
|
@@ -224,11 +229,11 @@ void CMqttClient::connected(const std::string& cause)
|
|
|
{
|
|
{
|
|
|
m_is_mqtt_connected = true;
|
|
m_is_mqtt_connected = true;
|
|
|
|
|
|
|
|
- std::string info = "Connection success, nSubscribing to topic " + TOPIC + " for client " + CLIENT_ID + " using QoS" + to_string(MQTT_QOS);
|
|
|
|
|
|
|
+ std::string info = "Connection success, nSubscribing to topic " + m_topic + " for client " + m_client_id + " using QoS" + to_string(MQTT_QOS);
|
|
|
LOG_INFO(info.c_str());
|
|
LOG_INFO(info.c_str());
|
|
|
|
|
|
|
|
//不管是第一次连接,还是重连,都订阅一次
|
|
//不管是第一次连接,还是重连,都订阅一次
|
|
|
- m_async_client->subscribe(TOPIC, MQTT_QOS, nullptr, *this);
|
|
|
|
|
|
|
+ m_async_client->subscribe(m_topic, MQTT_QOS, nullptr, *this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void CMqttClient::connection_lost(const std::string& cause)
|
|
void CMqttClient::connection_lost(const std::string& cause)
|
|
@@ -269,53 +274,18 @@ void CMqttClient::delivery_complete(mqtt::delivery_token_ptr token)
|
|
|
|
|
|
|
|
void CMqttClient::CalUserInfo()
|
|
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";
|
|
|
|
|
-
|
|
|
|
|
unsigned char tempData[100];
|
|
unsigned char tempData[100];
|
|
|
unsigned int len = 0;
|
|
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
|
|
//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);
|
|
|
|
|
|
|
+ HMAC(EVP_sha1(), m_secretKey.c_str(), m_secretKey.length(), (unsigned char*)m_client_id.c_str(), m_client_id.length(), tempData, &len);
|
|
|
|
|
|
|
|
char resultData[100];
|
|
char resultData[100];
|
|
|
int passWordLen = EVP_EncodeBlock((unsigned char*)resultData, tempData, len);
|
|
int passWordLen = EVP_EncodeBlock((unsigned char*)resultData, tempData, len);
|
|
|
resultData[passWordLen] = '\0';
|
|
resultData[passWordLen] = '\0';
|
|
|
- printf("passWord is %s", resultData);
|
|
|
|
|
|
|
|
|
|
char userNameData[128];
|
|
char userNameData[128];
|
|
|
- sprintf(userNameData, "Signature|%s|%s", accessKey, instanceId);
|
|
|
|
|
|
|
+ sprintf(userNameData, "Signature|%s|%s", m_accessKey.c_str(), m_instanceId.c_str());
|
|
|
|
|
|
|
|
m_UserName = userNameData;
|
|
m_UserName = userNameData;
|
|
|
m_Password = resultData;
|
|
m_Password = resultData;
|
|
@@ -332,20 +302,17 @@ void CMqttClient::HandleMessage(std::string message)
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- std::string type;
|
|
|
|
|
- if(document["msg_type"].IsInt())
|
|
|
|
|
- {
|
|
|
|
|
- type = to_string(document["msg_type"].GetInt());
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- type = document["msg_type"].GetString();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //这个可以到时候用来判断是否消息过期,暂时不处理
|
|
|
|
|
+ std::string timestamp = CLewaimaiJson::ToString(document["timestamp"]);
|
|
|
|
|
+
|
|
|
|
|
+ rapidjson::Value& data = document["data"];
|
|
|
|
|
+
|
|
|
|
|
+ std::string type = CLewaimaiJson::ToString(data["msg_type"]);
|
|
|
|
|
|
|
|
if(type == "1")
|
|
if(type == "1")
|
|
|
{
|
|
{
|
|
|
- std::string order_id = document["waimai_order_id"].GetString();
|
|
|
|
|
- std::string order_no = document["waimai_order_no"].GetString();
|
|
|
|
|
|
|
+ std::string order_id = data["waimai_order_id"].GetString();
|
|
|
|
|
+ std::string order_no = data["waimai_order_no"].GetString();
|
|
|
|
|
|
|
|
//新订单来了,首先判断是否要语音提醒
|
|
//新订单来了,首先判断是否要语音提醒
|
|
|
if(CSetting::GetParam("setting_is_new_waimai_voice") == "1")
|
|
if(CSetting::GetParam("setting_is_new_waimai_voice") == "1")
|
|
@@ -382,8 +349,8 @@ void CMqttClient::HandleMessage(std::string message)
|
|
|
{
|
|
{
|
|
|
AddVoice(4);
|
|
AddVoice(4);
|
|
|
|
|
|
|
|
- std::string order_id = document["waimai_order_id"].GetString();
|
|
|
|
|
- std::string order_no = document["waimai_order_no"].GetString();
|
|
|
|
|
|
|
+ std::string order_id = data["waimai_order_id"].GetString();
|
|
|
|
|
+ std::string order_no = data["waimai_order_no"].GetString();
|
|
|
|
|
|
|
|
AddPinter(order_id, order_no, 1, 3);
|
|
AddPinter(order_id, order_no, 1, 3);
|
|
|
}
|
|
}
|
|
@@ -459,7 +426,7 @@ void CMqttClient::AddChufangPrinter(CWaimaiOrder order)
|
|
|
|
|
|
|
|
void CMqttClient::HandleVoice()
|
|
void CMqttClient::HandleVoice()
|
|
|
{
|
|
{
|
|
|
- while(m_is_work)
|
|
|
|
|
|
|
+ while(m_is_running)
|
|
|
{
|
|
{
|
|
|
m_voice_mutex.lock();
|
|
m_voice_mutex.lock();
|
|
|
|
|
|
|
@@ -506,7 +473,7 @@ void CMqttClient::HandleVoice()
|
|
|
|
|
|
|
|
void CMqttClient::HandleConfirm()
|
|
void CMqttClient::HandleConfirm()
|
|
|
{
|
|
{
|
|
|
- while(m_is_work)
|
|
|
|
|
|
|
+ while(m_is_running)
|
|
|
{
|
|
{
|
|
|
m_confirm_mutex.lock();
|
|
m_confirm_mutex.lock();
|
|
|
|
|
|
|
@@ -532,7 +499,7 @@ void CMqttClient::HandleConfirm()
|
|
|
|
|
|
|
|
void CMqttClient::HandlePrinter()
|
|
void CMqttClient::HandlePrinter()
|
|
|
{
|
|
{
|
|
|
- while(m_is_work)
|
|
|
|
|
|
|
+ while(m_is_running)
|
|
|
{
|
|
{
|
|
|
m_printer_mutex.lock();
|
|
m_printer_mutex.lock();
|
|
|
|
|
|
|
@@ -641,7 +608,7 @@ void CMqttClient::HandlePrinter()
|
|
|
|
|
|
|
|
void CMqttClient::HandleShouyinPrinter()
|
|
void CMqttClient::HandleShouyinPrinter()
|
|
|
{
|
|
{
|
|
|
- while(m_is_work)
|
|
|
|
|
|
|
+ while(m_is_running)
|
|
|
{
|
|
{
|
|
|
m_shouyin_printer_mutex.lock();
|
|
m_shouyin_printer_mutex.lock();
|
|
|
|
|
|
|
@@ -668,7 +635,7 @@ void CMqttClient::HandleShouyinPrinter()
|
|
|
|
|
|
|
|
void CMqttClient::HandleBiaoqianPrinter()
|
|
void CMqttClient::HandleBiaoqianPrinter()
|
|
|
{
|
|
{
|
|
|
- while(m_is_work)
|
|
|
|
|
|
|
+ while(m_is_running)
|
|
|
{
|
|
{
|
|
|
m_biaoqian_printer_mutex.lock();
|
|
m_biaoqian_printer_mutex.lock();
|
|
|
|
|
|
|
@@ -695,7 +662,7 @@ void CMqttClient::HandleBiaoqianPrinter()
|
|
|
|
|
|
|
|
void CMqttClient::HandleChufangPrinter()
|
|
void CMqttClient::HandleChufangPrinter()
|
|
|
{
|
|
{
|
|
|
- while(m_is_work)
|
|
|
|
|
|
|
+ while(m_is_running)
|
|
|
{
|
|
{
|
|
|
m_chufang_printer_mutex.lock();
|
|
m_chufang_printer_mutex.lock();
|
|
|
|
|
|