1+ #include " dual_network_board.h"
2+ #include " application.h"
3+ #include " display.h"
4+ #include " assets/lang_config.h"
5+ #include " settings.h"
6+ #include < esp_log.h>
7+
8+ static const char *TAG = " DualNetworkBoard" ;
9+
10+ DualNetworkBoard::DualNetworkBoard (gpio_num_t ml307_tx_pin, gpio_num_t ml307_rx_pin, size_t ml307_rx_buffer_size)
11+ : Board(),
12+ ml307_tx_pin_(ml307_tx_pin),
13+ ml307_rx_pin_(ml307_rx_pin),
14+ ml307_rx_buffer_size_(ml307_rx_buffer_size) {
15+
16+ // 从Settings加载网络类型
17+ network_type_ = LoadNetworkTypeFromSettings ();
18+
19+ // 只初始化当前网络类型对应的板卡
20+ InitializeCurrentBoard ();
21+ }
22+
23+ NetworkType DualNetworkBoard::LoadNetworkTypeFromSettings () {
24+ Settings settings (" network" , true );
25+ int network_type = settings.GetInt (" type" , 1 ); // 默认使用ML307 (1)
26+
27+ ESP_LOGI (TAG, " 从Settings加载网络类型: %d" , network_type);
28+
29+ return network_type == 1 ? NetworkType::ML307 : NetworkType::WIFI;
30+ }
31+
32+ void DualNetworkBoard::SaveNetworkTypeToSettings (NetworkType type) {
33+ Settings settings (" network" , true );
34+ int network_type = (type == NetworkType::ML307) ? 1 : 0 ;
35+
36+ ESP_LOGI (TAG, " 保存网络类型到Settings: %d" , network_type);
37+
38+ settings.SetInt (" type" , network_type);
39+ }
40+
41+ void DualNetworkBoard::InitializeCurrentBoard () {
42+ if (network_type_ == NetworkType::ML307) {
43+ ESP_LOGI (TAG, " 初始化ML307板卡" );
44+ current_board_ = std::make_unique<Ml307Board>(ml307_tx_pin_, ml307_rx_pin_, ml307_rx_buffer_size_);
45+ } else {
46+ ESP_LOGI (TAG, " 初始化WiFi板卡" );
47+ current_board_ = std::make_unique<WifiBoard>();
48+ }
49+ }
50+
51+ void DualNetworkBoard::SwitchNetType () {
52+ if (network_type_ == NetworkType::WIFI) {
53+ ESP_LOGI (TAG, " 切换到ML307模式" );
54+ SaveNetworkTypeToSettings (NetworkType::ML307);
55+ } else {
56+ ESP_LOGI (TAG, " 切换到WiFi模式" );
57+ SaveNetworkTypeToSettings (NetworkType::WIFI);
58+ }
59+ }
60+
61+
62+ std::string DualNetworkBoard::GetBoardType () {
63+ return current_board_->GetBoardType ();
64+ }
65+
66+ void DualNetworkBoard::StartNetwork () {
67+ auto display = Board::GetInstance ().GetDisplay ();
68+
69+ if (network_type_ == NetworkType::WIFI) {
70+ display->SetStatus (Lang::Strings::CONNECTING);
71+ } else {
72+ display->SetStatus (Lang::Strings::DETECTING_MODULE);
73+ }
74+ current_board_->StartNetwork ();
75+ }
76+
77+ Http* DualNetworkBoard::CreateHttp () {
78+ return current_board_->CreateHttp ();
79+ }
80+
81+ WebSocket* DualNetworkBoard::CreateWebSocket () {
82+ return current_board_->CreateWebSocket ();
83+ }
84+
85+ Mqtt* DualNetworkBoard::CreateMqtt () {
86+ return current_board_->CreateMqtt ();
87+ }
88+
89+ Udp* DualNetworkBoard::CreateUdp () {
90+ return current_board_->CreateUdp ();
91+ }
92+
93+ const char * DualNetworkBoard::GetNetworkStateIcon () {
94+ return current_board_->GetNetworkStateIcon ();
95+ }
96+
97+ void DualNetworkBoard::SetPowerSaveMode (bool enabled) {
98+ current_board_->SetPowerSaveMode (enabled);
99+ }
100+
101+ std::string DualNetworkBoard::GetBoardJson () {
102+ return current_board_->GetBoardJson ();
103+ }
0 commit comments