diff --git a/main/devices/Device.hpp b/main/devices/Device.hpp index d01bbe9e..ed094f4e 100644 --- a/main/devices/Device.hpp +++ b/main/devices/Device.hpp @@ -462,10 +462,15 @@ class Device { auto& peripheralsConfig = deviceConfig.peripherals.get(); Log.info("Loading configuration for %d user-configured peripherals", peripheralsConfig.size()); + bool peripheralError = false; for (auto& perpheralConfig : peripheralsConfig) { - peripheralManager.createPeripheral(perpheralConfig.get()); + if (!peripheralManager.createPeripheral(perpheralConfig.get())) { + peripheralError = true; + } } + InitState initState = peripheralError ? InitState::PeripheralError : InitState::Success; + mqttDeviceRoot->publish( "init", [&](JsonObject& json) { @@ -483,6 +488,7 @@ class Device { json["wakeup"] = esp_sleep_get_wakeup_cause(); json["bootCount"] = bootCount++; json["time"] = duration_cast(system_clock::now().time_since_epoch()).count(); + json["state"] = static_cast(initState); json["sleepWhenIdle"] = kernel.sleepManager.sleepWhenIdle; }, MqttDriver::Retention::NoRetain, MqttDriver::QoS::AtLeastOnce, ticks::max()); @@ -512,6 +518,11 @@ class Device { } private: + enum class InitState { + Success = 0, + PeripheralError = 1, + }; + void publishTelemetry() { deviceTelemetryPublisher.publishTelemetry(); peripheralManager.publishTelemetry(); diff --git a/main/peripherals/Peripheral.hpp b/main/peripherals/Peripheral.hpp index e10bce04..1dabf9b6 100644 --- a/main/peripherals/Peripheral.hpp +++ b/main/peripherals/Peripheral.hpp @@ -184,7 +184,7 @@ class PeripheralManager factories.insert(std::make_pair(factory.factoryType, std::reference_wrapper(factory))); } - void createPeripheral(const String& peripheralConfig) { + bool createPeripheral(const String& peripheralConfig) { Log.info("Creating peripheral with config: %s", peripheralConfig.c_str()); PeripheralDeviceConfiguration deviceConfig; @@ -193,7 +193,7 @@ class PeripheralManager } catch (const std::exception& e) { Log.error("Failed to parse peripheral config because %s:\n%s", e.what(), peripheralConfig.c_str()); - return; + return false; } String name = deviceConfig.name.get(); @@ -203,16 +203,19 @@ class PeripheralManager if (state == State::Stopped) { Log.error("Not creating peripheral '%s' because the peripheral manager is stopped", name.c_str()); - return; + return false; } unique_ptr peripheral = createPeripheral(name, type, deviceConfig.params.get().get()); peripherals.push_back(move(peripheral)); + return true; } catch (const std::exception& e) { Log.error("Failed to create '%s' peripheral '%s' because %s", type.c_str(), name.c_str(), e.what()); + return false; } catch (...) { Log.error("Failed to create '%s' peripheral '%s' because of an unknown exception", type.c_str(), name.c_str()); + return false; } }