CMqttClient.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include "../pch/pch.h"
  3. #include "../order/CWaimaiOrder.h"
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <cstring>
  8. #include <cctype>
  9. #include <thread>
  10. #include <chrono>
  11. #include "mqtt/async_client.h"
  12. class CClientMessage
  13. {
  14. public:
  15. int m_type; //消息类型 0:新的外卖订单
  16. std::string m_username;
  17. std::string m_order_id;
  18. std::string m_order_no;
  19. };
  20. class WaimaiPinterInfo
  21. {
  22. public:
  23. std::string order_id;
  24. std::string order_no;
  25. int print_type; //打印类型 1:新订单自动打印 2:手动打印
  26. int order_type = 1; // 订单类型:1 新订单 2 顾客取消订单 3顾客主动退款
  27. };
  28. class CMqttClient : public virtual mqtt::callback,
  29. public virtual mqtt::iaction_listener
  30. {
  31. private:
  32. //定义一些常量
  33. std::string m_server_address = "post-cn-x0r3kjlsy02.mqtt.aliyuncs.com";
  34. std::string m_instanceId = "post-cn-x0r3kjlsy02";
  35. std::string m_topic = "lewaimai_windows_pos";
  36. std::string m_accessKey = "LTAI4G5oikJPMfhq5PuW26qu";
  37. std::string m_secretKey = "LsIxqepi2o3X0b17FSa7JaANfIMDVY";
  38. std::string m_client_id;
  39. //要保证可靠传输
  40. int MQTT_QOS = 2;
  41. //一直重试
  42. int N_RETRY_ATTEMPTS = 1000000;
  43. //连接阿里云的用户身份,计算出来的
  44. std::string m_UserName;
  45. std::string m_Password;
  46. // Counter for the number of connection retries
  47. int nretry_ = 0;
  48. // The MQTT client
  49. mqtt::async_client* m_async_client;
  50. // Options to use if we need to reconnect
  51. mqtt::connect_options m_connOpts;
  52. bool m_is_mqtt_connected = false;
  53. //工作状态
  54. bool m_is_running = false;
  55. //下面是消息处理相关的
  56. int m_nStopNum = 0;
  57. std::mutex m_nStopNumMutex;
  58. std::queue<int> m_voice_queue;
  59. std::queue<std::string> m_confirm_queue;
  60. std::queue<WaimaiPinterInfo> m_printer_queue;
  61. std::queue<CWaimaiOrder> m_shouyin_printer_queue;
  62. std::queue<CWaimaiOrder> m_biaoqian_printer_queue;
  63. std::queue<CWaimaiOrder> m_chufang_printer_queue;
  64. std::mutex m_voice_mutex;
  65. std::mutex m_confirm_mutex;
  66. std::mutex m_printer_mutex;
  67. std::mutex m_shouyin_printer_mutex;
  68. std::mutex m_biaoqian_printer_mutex;
  69. std::mutex m_chufang_printer_mutex;
  70. HWND m_hwnd;
  71. public:
  72. CMqttClient(HWND hwnd);
  73. ~CMqttClient();
  74. void Start();
  75. void Stop();
  76. void AddPinter(std::string order_id, std::string order_no, int print_type, int order_type = 1);
  77. private:
  78. void Run();
  79. void CalUserInfo();
  80. //处理接收到的MQTT消息推送
  81. void HandleMessage(std::string message);
  82. // This deomonstrates manually reconnecting to the broker by calling
  83. // connect() again. This is a possibility for an application that keeps
  84. // a copy of it's original connect_options, or if the app wants to
  85. // reconnect with different options.
  86. // Another way this can be done manually, if using the same options, is
  87. // to just call the async_client::reconnect() method.
  88. void reconnect();
  89. // 连接、重连、发布、订阅失败回调
  90. void on_failure(const mqtt::token& tok) override;
  91. // 连接、重连、发布、订阅成功回调
  92. void on_success(const mqtt::token& tok) override;
  93. // 第1次连接成功,或者重连成功,都会调用这个函数
  94. void connected(const std::string& cause) override;
  95. // 连接丢失,重新连接
  96. void connection_lost(const std::string& cause) override;
  97. // 接收到了消息,进行处理
  98. void message_arrived(mqtt::const_message_ptr msg) override;
  99. void delivery_complete(mqtt::delivery_token_ptr token) override;
  100. //队列处理
  101. void HandleVoice();
  102. void HandleConfirm();
  103. void HandlePrinter();
  104. void HandleShouyinPrinter();
  105. void HandleBiaoqianPrinter();
  106. void HandleChufangPrinter();
  107. void AddVoice(int voice_type);
  108. void AddConfirm(std::string order_id);
  109. void AddShouyinPrinter(CWaimaiOrder order);
  110. void AddBiaoqianPrinter(CWaimaiOrder order);
  111. void AddChufangPrinter(CWaimaiOrder order);
  112. void AddStopNum();
  113. };