2024-11-16 05:49:35 +08:00
|
|
|
#include "protocol.h"
|
|
|
|
|
|
|
|
|
|
#include <esp_log.h>
|
|
|
|
|
|
|
|
|
|
#define TAG "Protocol"
|
|
|
|
|
|
|
|
|
|
void Protocol::OnIncomingJson(std::function<void(const cJSON* root)> callback) {
|
|
|
|
|
on_incoming_json_ = callback;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 02:12:20 +08:00
|
|
|
void Protocol::OnIncomingAudio(std::function<void(std::vector<uint8_t>&& data)> callback) {
|
2024-11-16 05:49:35 +08:00
|
|
|
on_incoming_audio_ = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Protocol::OnAudioChannelOpened(std::function<void()> callback) {
|
|
|
|
|
on_audio_channel_opened_ = callback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Protocol::OnAudioChannelClosed(std::function<void()> callback) {
|
|
|
|
|
on_audio_channel_closed_ = callback;
|
|
|
|
|
}
|
2024-11-20 03:28:52 +08:00
|
|
|
|
|
|
|
|
void Protocol::OnNetworkError(std::function<void(const std::string& message)> callback) {
|
|
|
|
|
on_network_error_ = callback;
|
|
|
|
|
}
|
2024-11-25 00:59:03 +08:00
|
|
|
|
2025-03-04 05:32:11 +08:00
|
|
|
void Protocol::SetError(const std::string& message) {
|
|
|
|
|
error_occurred_ = true;
|
|
|
|
|
if (on_network_error_ != nullptr) {
|
|
|
|
|
on_network_error_(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-25 00:59:03 +08:00
|
|
|
void Protocol::SendAbortSpeaking(AbortReason reason) {
|
|
|
|
|
std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"abort\"";
|
|
|
|
|
if (reason == kAbortReasonWakeWordDetected) {
|
|
|
|
|
message += ",\"reason\":\"wake_word_detected\"";
|
|
|
|
|
}
|
|
|
|
|
message += "}";
|
|
|
|
|
SendText(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Protocol::SendWakeWordDetected(const std::string& wake_word) {
|
|
|
|
|
std::string json = "{\"session_id\":\"" + session_id_ +
|
|
|
|
|
"\",\"type\":\"listen\",\"state\":\"detect\",\"text\":\"" + wake_word + "\"}";
|
|
|
|
|
SendText(json);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Protocol::SendStartListening(ListeningMode mode) {
|
|
|
|
|
std::string message = "{\"session_id\":\"" + session_id_ + "\"";
|
|
|
|
|
message += ",\"type\":\"listen\",\"state\":\"start\"";
|
|
|
|
|
if (mode == kListeningModeAlwaysOn) {
|
|
|
|
|
message += ",\"mode\":\"realtime\"";
|
|
|
|
|
} else if (mode == kListeningModeAutoStop) {
|
|
|
|
|
message += ",\"mode\":\"auto\"";
|
|
|
|
|
} else {
|
|
|
|
|
message += ",\"mode\":\"manual\"";
|
|
|
|
|
}
|
|
|
|
|
message += "}";
|
|
|
|
|
SendText(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Protocol::SendStopListening() {
|
|
|
|
|
std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"listen\",\"state\":\"stop\"}";
|
|
|
|
|
SendText(message);
|
|
|
|
|
}
|
2024-12-06 11:08:49 +08:00
|
|
|
|
|
|
|
|
void Protocol::SendIotDescriptors(const std::string& descriptors) {
|
|
|
|
|
std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"iot\",\"descriptors\":" + descriptors + "}";
|
|
|
|
|
SendText(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Protocol::SendIotStates(const std::string& states) {
|
|
|
|
|
std::string message = "{\"session_id\":\"" + session_id_ + "\",\"type\":\"iot\",\"states\":" + states + "}";
|
|
|
|
|
SendText(message);
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-04 05:32:11 +08:00
|
|
|
bool Protocol::IsTimeout() const {
|
|
|
|
|
const int kTimeoutSeconds = 120;
|
|
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
|
auto duration = std::chrono::duration_cast<std::chrono::seconds>(now - last_incoming_time_);
|
|
|
|
|
bool timeout = duration.count() > kTimeoutSeconds;
|
|
|
|
|
if (timeout) {
|
|
|
|
|
ESP_LOGE(TAG, "Channel timeout %lld seconds", duration.count());
|
|
|
|
|
}
|
|
|
|
|
return timeout;
|
|
|
|
|
}
|
|
|
|
|
|