forked from xiaozhi/xiaozhi-esp32
97 lines
2.8 KiB
C++
97 lines
2.8 KiB
C++
#include "dual_network_board.h"
|
|
#include "application.h"
|
|
#include "display.h"
|
|
#include "assets/lang_config.h"
|
|
#include "settings.h"
|
|
#include <esp_log.h>
|
|
|
|
static const char *TAG = "DualNetworkBoard";
|
|
|
|
DualNetworkBoard::DualNetworkBoard(gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size)
|
|
: Board(),
|
|
ml307_tx_pin_(ml307_tx_pin),
|
|
ml307_rx_pin_(ml307_rx_pin),
|
|
ml307_rx_buffer_size_(ml307_rx_buffer_size) {
|
|
|
|
// 从Settings加载网络类型
|
|
network_type_ = LoadNetworkTypeFromSettings();
|
|
|
|
// 只初始化当前网络类型对应的板卡
|
|
InitializeCurrentBoard();
|
|
}
|
|
|
|
NetworkType DualNetworkBoard::LoadNetworkTypeFromSettings() {
|
|
Settings settings("network", true);
|
|
int network_type = settings.GetInt("type", 1); // 默认使用ML307 (1)
|
|
return network_type == 1 ? NetworkType::ML307 : NetworkType::WIFI;
|
|
}
|
|
|
|
void DualNetworkBoard::SaveNetworkTypeToSettings(NetworkType type) {
|
|
Settings settings("network", true);
|
|
int network_type = (type == NetworkType::ML307) ? 1 : 0;
|
|
settings.SetInt("type", network_type);
|
|
}
|
|
|
|
void DualNetworkBoard::InitializeCurrentBoard() {
|
|
if (network_type_ == NetworkType::ML307) {
|
|
ESP_LOGI(TAG, "Initialize ML307 board");
|
|
current_board_ = std::make_unique<Ml307Board>(ml307_tx_pin_, ml307_rx_pin_, ml307_rx_buffer_size_);
|
|
} else {
|
|
ESP_LOGI(TAG, "Initialize WiFi board");
|
|
current_board_ = std::make_unique<WifiBoard>();
|
|
}
|
|
}
|
|
|
|
void DualNetworkBoard::SwitchNetType() {
|
|
if (network_type_ == NetworkType::WIFI) {
|
|
ESP_LOGI(TAG, "Switch to ML307 mode");
|
|
SaveNetworkTypeToSettings(NetworkType::ML307);
|
|
} else {
|
|
ESP_LOGI(TAG, "Switch to WiFi mode");
|
|
SaveNetworkTypeToSettings(NetworkType::WIFI);
|
|
}
|
|
}
|
|
|
|
|
|
std::string DualNetworkBoard::GetBoardType() {
|
|
return current_board_->GetBoardType();
|
|
}
|
|
|
|
void DualNetworkBoard::StartNetwork() {
|
|
auto display = Board::GetInstance().GetDisplay();
|
|
|
|
if (network_type_ == NetworkType::WIFI) {
|
|
display->SetStatus(Lang::Strings::CONNECTING);
|
|
} else {
|
|
display->SetStatus(Lang::Strings::DETECTING_MODULE);
|
|
}
|
|
current_board_->StartNetwork();
|
|
}
|
|
|
|
Http* DualNetworkBoard::CreateHttp() {
|
|
return current_board_->CreateHttp();
|
|
}
|
|
|
|
WebSocket* DualNetworkBoard::CreateWebSocket() {
|
|
return current_board_->CreateWebSocket();
|
|
}
|
|
|
|
Mqtt* DualNetworkBoard::CreateMqtt() {
|
|
return current_board_->CreateMqtt();
|
|
}
|
|
|
|
Udp* DualNetworkBoard::CreateUdp() {
|
|
return current_board_->CreateUdp();
|
|
}
|
|
|
|
const char* DualNetworkBoard::GetNetworkStateIcon() {
|
|
return current_board_->GetNetworkStateIcon();
|
|
}
|
|
|
|
void DualNetworkBoard::SetPowerSaveMode(bool enabled) {
|
|
current_board_->SetPowerSaveMode(enabled);
|
|
}
|
|
|
|
std::string DualNetworkBoard::GetBoardJson() {
|
|
return current_board_->GetBoardJson();
|
|
}
|