Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish device init state #240

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 0 additions & 1 deletion main/peripherals/valve/ValveConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ class ValveDeviceConfig
}

PwmMotorDriver& motor = motorOwner->findMotor(this->motor.get());
ValveControlStrategy* strategy;

auto switchDuration = this->switchDuration.get();
auto holdDuty = this->holdDuty.get() / 100.0;
Expand Down
Loading