Skip to content

Commit

Permalink
Publish device init state
Browse files Browse the repository at this point in the history
Fixes #237.
  • Loading branch information
lptr committed Nov 8, 2024
1 parent 95f18af commit fff9349
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 12 additions & 1 deletion main/devices/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -483,6 +488,7 @@ class Device {
json["wakeup"] = esp_sleep_get_wakeup_cause();
json["bootCount"] = bootCount++;
json["time"] = duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
json["state"] = static_cast<int>(initState);
json["sleepWhenIdle"] = kernel.sleepManager.sleepWhenIdle;
},
MqttDriver::Retention::NoRetain, MqttDriver::QoS::AtLeastOnce, ticks::max());
Expand Down Expand Up @@ -512,6 +518,11 @@ class Device {
}

private:
enum class InitState {
Success = 0,
PeripheralError = 1,
};

void publishTelemetry() {
deviceTelemetryPublisher.publishTelemetry();
peripheralManager.publishTelemetry();
Expand Down
9 changes: 6 additions & 3 deletions main/peripherals/Peripheral.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class PeripheralManager
factories.insert(std::make_pair(factory.factoryType, std::reference_wrapper<PeripheralFactoryBase>(factory)));
}

void createPeripheral(const String& peripheralConfig) {
bool createPeripheral(const String& peripheralConfig) {
Log.info("Creating peripheral with config: %s",
peripheralConfig.c_str());
PeripheralDeviceConfiguration deviceConfig;
Expand All @@ -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();
Expand All @@ -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<PeripheralBase> 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;
}
}

Expand Down

0 comments on commit fff9349

Please sign in to comment.