protocol.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "protocol.h"
  2. #include <esp_log.h>
  3. #define TAG "Protocol"
  4. void Protocol::OnIncomingJson(std::function<void(const cJSON* root)> callback) {
  5. on_incoming_json_ = callback;
  6. }
  7. void Protocol::OnIncomingAudio(std::function<void(std::vector<uint8_t>&& data)> callback) {
  8. on_incoming_audio_ = callback;
  9. }
  10. void Protocol::OnAudioChannelOpened(std::function<void()> callback) {
  11. on_audio_channel_opened_ = callback;
  12. }
  13. void Protocol::OnAudioChannelClosed(std::function<void()> callback) {
  14. on_audio_channel_closed_ = callback;
  15. }
  16. void Protocol::OnNetworkError(std::function<void(const std::string& message)> callback) {
  17. on_network_error_ = callback;
  18. }
  19. void Protocol::SetError(const std::string& message) {
  20. error_occurred_ = true;
  21. if (on_network_error_ != nullptr) {
  22. on_network_error_(message);
  23. }
  24. }
  25. void Protocol::SendAbortSpeaking(AbortReason reason) {
  26. std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"abort\"";
  27. if (reason == kAbortReasonWakeWordDetected) {
  28. message += ",\"reason\":\"wake_word_detected\"";
  29. }
  30. message += "}";
  31. SendText(message);
  32. }
  33. void Protocol::SendWakeWordDetected(const std::string& wake_word) {
  34. std::string json = "{\"session_id\":\"" + session_id_ +
  35. "\",\"type\":\"listen\",\"state\":\"detect\",\"text\":\"" + wake_word + "\"}";
  36. SendText(json);
  37. }
  38. void Protocol::SendStartListening(ListeningMode mode) {
  39. std::string message = "{\"session_id\":\"" + session_id_ + "\"";
  40. message += ",\"type\":\"listen\",\"state\":\"start\"";
  41. if (mode == kListeningModeAlwaysOn) {
  42. message += ",\"mode\":\"realtime\"";
  43. } else if (mode == kListeningModeAutoStop) {
  44. message += ",\"mode\":\"auto\"";
  45. } else {
  46. message += ",\"mode\":\"manual\"";
  47. }
  48. message += "}";
  49. SendText(message);
  50. }
  51. void Protocol::SendStopListening() {
  52. std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"listen\",\"state\":\"stop\"}";
  53. SendText(message);
  54. }
  55. void Protocol::SendIotDescriptors(const std::string& descriptors) {
  56. cJSON* root = cJSON_Parse(descriptors.c_str());
  57. if (root == nullptr) {
  58. ESP_LOGE(TAG, "Failed to parse IoT descriptors: %s", descriptors.c_str());
  59. return;
  60. }
  61. if (!cJSON_IsArray(root)) {
  62. ESP_LOGE(TAG, "IoT descriptors should be an array");
  63. cJSON_Delete(root);
  64. return;
  65. }
  66. int arraySize = cJSON_GetArraySize(root);
  67. for (int i = 0; i < arraySize; ++i) {
  68. cJSON* descriptor = cJSON_GetArrayItem(root, i);
  69. if (descriptor == nullptr) {
  70. ESP_LOGE(TAG, "Failed to get IoT descriptor at index %d", i);
  71. continue;
  72. }
  73. cJSON* messageRoot = cJSON_CreateObject();
  74. cJSON_AddStringToObject(messageRoot, "session_id", session_id_.c_str());
  75. cJSON_AddStringToObject(messageRoot, "type", "iot");
  76. cJSON_AddBoolToObject(messageRoot, "update", true);
  77. cJSON* descriptorArray = cJSON_CreateArray();
  78. cJSON_AddItemToArray(descriptorArray, cJSON_Duplicate(descriptor, 1));
  79. cJSON_AddItemToObject(messageRoot, "descriptors", descriptorArray);
  80. char* message = cJSON_PrintUnformatted(messageRoot);
  81. if (message == nullptr) {
  82. ESP_LOGE(TAG, "Failed to print JSON message for IoT descriptor at index %d", i);
  83. cJSON_Delete(messageRoot);
  84. continue;
  85. }
  86. SendText(std::string(message));
  87. cJSON_free(message);
  88. cJSON_Delete(messageRoot);
  89. }
  90. cJSON_Delete(root);
  91. }
  92. void Protocol::SendIotStates(const std::string& states) {
  93. std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"iot\",\"update\":true,\"states\":" + states + "}";
  94. SendText(message);
  95. }
  96. bool Protocol::IsTimeout() const {
  97. const int kTimeoutSeconds = 120;
  98. auto now = std::chrono::steady_clock::now();
  99. auto duration = std::chrono::duration_cast<std::chrono::seconds>(now - last_incoming_time_);
  100. bool timeout = duration.count() > kTimeoutSeconds;
  101. if (timeout) {
  102. ESP_LOGE(TAG, "Channel timeout %lld seconds", duration.count());
  103. }
  104. return timeout;
  105. }