|
1 | 1 | #include "application.h"
|
| 2 | +#include "board.h" |
| 3 | +#include "display.h" |
2 | 4 | #include "system_info.h"
|
3 | 5 | #include "ml307_ssl_transport.h"
|
4 | 6 | #include "audio_codec.h"
|
5 | 7 | #include "mqtt_protocol.h"
|
6 | 8 | #include "websocket_protocol.h"
|
7 | 9 | #include "font_awesome_symbols.h"
|
| 10 | +#include "iot/thing_manager.h" |
8 | 11 |
|
9 | 12 | #include <cstring>
|
10 | 13 | #include <esp_log.h>
|
@@ -183,8 +186,6 @@ void Application::StopListening() {
|
183 | 186 |
|
184 | 187 | void Application::Start() {
|
185 | 188 | auto& board = Board::GetInstance();
|
186 |
| - board.Initialize(); |
187 |
| - |
188 | 189 | auto builtin_led = board.GetBuiltinLed();
|
189 | 190 | builtin_led->SetBlue();
|
190 | 191 | builtin_led->StartContinuousBlink(100);
|
@@ -308,18 +309,22 @@ void Application::Start() {
|
308 | 309 | }
|
309 | 310 | });
|
310 | 311 | protocol_->OnAudioChannelOpened([this, codec, &board]() {
|
| 312 | + board.SetPowerSaveMode(false); |
311 | 313 | if (protocol_->server_sample_rate() != codec->output_sample_rate()) {
|
312 | 314 | ESP_LOGW(TAG, "服务器的音频采样率 %d 与设备输出的采样率 %d 不一致,重采样后可能会失真",
|
313 | 315 | protocol_->server_sample_rate(), codec->output_sample_rate());
|
314 | 316 | }
|
315 | 317 | SetDecodeSampleRate(protocol_->server_sample_rate());
|
316 |
| - board.SetPowerSaveMode(false); |
| 318 | + // 物联网设备描述符 |
| 319 | + last_iot_states_.clear(); |
| 320 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 321 | + protocol_->SendIotDescriptors(thing_manager.GetDescriptorsJson()); |
317 | 322 | });
|
318 | 323 | protocol_->OnAudioChannelClosed([this, &board]() {
|
| 324 | + board.SetPowerSaveMode(true); |
319 | 325 | Schedule([this]() {
|
320 | 326 | SetChatState(kChatStateIdle);
|
321 | 327 | });
|
322 |
| - board.SetPowerSaveMode(true); |
323 | 328 | });
|
324 | 329 | protocol_->OnIncomingJson([this, display](const cJSON* root) {
|
325 | 330 | // Parse JSON data
|
@@ -363,6 +368,15 @@ void Application::Start() {
|
363 | 368 | if (emotion != NULL) {
|
364 | 369 | display->SetEmotion(emotion->valuestring);
|
365 | 370 | }
|
| 371 | + } else if (strcmp(type->valuestring, "iot") == 0) { |
| 372 | + auto commands = cJSON_GetObjectItem(root, "commands"); |
| 373 | + if (commands != NULL) { |
| 374 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 375 | + for (int i = 0; i < cJSON_GetArraySize(commands); ++i) { |
| 376 | + auto command = cJSON_GetArrayItem(commands, i); |
| 377 | + thing_manager.Invoke(command); |
| 378 | + } |
| 379 | + } |
366 | 380 | }
|
367 | 381 | });
|
368 | 382 |
|
@@ -557,6 +571,7 @@ void Application::SetChatState(ChatState state) {
|
557 | 571 | #if CONFIG_IDF_TARGET_ESP32S3
|
558 | 572 | audio_processor_.Start();
|
559 | 573 | #endif
|
| 574 | + UpdateIotStates(); |
560 | 575 | break;
|
561 | 576 | case kChatStateSpeaking:
|
562 | 577 | builtin_led->SetGreen();
|
@@ -591,3 +606,12 @@ void Application::SetDecodeSampleRate(int sample_rate) {
|
591 | 606 | output_resampler_.Configure(opus_decode_sample_rate_, codec->output_sample_rate());
|
592 | 607 | }
|
593 | 608 | }
|
| 609 | + |
| 610 | +void Application::UpdateIotStates() { |
| 611 | + auto& thing_manager = iot::ThingManager::GetInstance(); |
| 612 | + auto states = thing_manager.GetStatesJson(); |
| 613 | + if (states != last_iot_states_) { |
| 614 | + last_iot_states_ = states; |
| 615 | + protocol_->SendIotStates(states); |
| 616 | + } |
| 617 | +} |
0 commit comments