| 12 | #include <nvs_flash.h> |
| 13 | |
| 14 | extern "C" void app_main() { |
| 15 | // Set global log level initially. This is later set from saved config. |
| 16 | esp_log_level_set("*", ESP_LOG_DEBUG); |
| 17 | |
| 18 | ESP_LOGI("Main", "Starting NSPanel Manager firmware. Version " NSPM_VERSION "."); |
| 19 | |
| 20 | esp_event_loop_create_default(); |
| 21 | |
| 22 | nvs_flash_init(); |
| 23 | if (LittleFS::mount() != ESP_OK) { |
| 24 | ESP_LOGE("Main", "Failed to mount LittleFS!"); |
| 25 | } |
| 26 | |
| 27 | ConfigManager::create_default(); // Set default values on all config entities |
| 28 | if (ConfigManager::load_config() != ESP_OK) { |
| 29 | ESP_LOGE("Main", "Failed to load config from LittleFS. If this is the first time running the panel this is normal as not config has been saved yet."); |
| 30 | ESP_LOGI("Main", "Default config values has been applied, will save those to create a config file."); |
| 31 | esp_err_t config_save_result = ConfigManager::save_config(); |
| 32 | if (config_save_result != ESP_OK) { |
| 33 | ESP_LOGE("Main", "Failed to save config to LittleFS, got error %s!", esp_err_to_name(config_save_result)); |
| 34 | } |
| 35 | } else { |
| 36 | if (ConfigManager::wifi_ssid.empty()) { |
| 37 | ESP_LOGE("Main", "Successfully loaded config from LittleFS but the config is not valid. Empty WiFi SSID, will load default values and start Access Point."); |
| 38 | ConfigManager::create_default(); |
| 39 | |
| 40 | WiFiManager::start_ap(&ConfigManager::wifi_hostname); |
| 41 | } else { |
| 42 | ESP_LOGI("Main", "Config loaded successfully. Starting NSPanel as '%s'.", ConfigManager::wifi_hostname.c_str()); |
| 43 | // Set global log level |
| 44 | esp_log_level_set("*", static_cast<esp_log_level_t>(ConfigManager::log_level)); |
| 45 | |
| 46 | // Start task that handles WiFi connection |
| 47 | WiFiManager::start_client(&ConfigManager::wifi_ssid, &ConfigManager::wifi_psk, &ConfigManager::wifi_hostname); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Initialize PageManager memory |
| 52 | PageManager::init(); |
| 53 | |
| 54 | // Start task that handles MQTT connection |
| 55 | MqttManager::start(&ConfigManager::mqtt_server, &ConfigManager::mqtt_port, &ConfigManager::mqtt_username, &ConfigManager::mqtt_password); |
| 56 | |
| 57 | // Start RoomManager |
| 58 | RoomManager::init(); |
| 59 | |
| 60 | // Start task that handles the HTTP server |
| 61 | WebManager::start(); |
| 62 | |
| 63 | // Start the interface and load config |
| 64 | InterfaceManager::init(); |
| 65 | } |