Skip to content

Commit

Permalink
Publish telemetry when valve state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr committed Feb 1, 2024
1 parent 6c71e60 commit f12b883
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/peripherals/flow_control/FlowControl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ class FlowControl : public Peripheral<FlowControlConfig> {
PwmMotorDriver& controller, ValveControlStrategy& strategy,
gpio_num_t pin, double qFactor, milliseconds measurementFrequency)
: Peripheral<FlowControlConfig>(name, mqttRoot)
, valve(name, controller, strategy, mqttRoot)
, valve(name, controller, strategy, mqttRoot, [this]() {
publishTelemetry();
})
, flowMeter(name, mqttRoot, pin, qFactor, measurementFrequency) {
}

Expand Down
4 changes: 3 additions & 1 deletion src/peripherals/valve/Valve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class Valve
public:
Valve(const String& name, PwmMotorDriver& controller, ValveControlStrategy& strategy, shared_ptr<MqttDriver::MqttRoot> mqttRoot)
: Peripheral<ValveConfig>(name, mqttRoot)
, valve(name, controller, strategy, mqttRoot) {
, valve(name, controller, strategy, mqttRoot, [this]() {
publishTelemetry();
}) {
}

void configure(const ValveConfig& config) override {
Expand Down
8 changes: 6 additions & 2 deletions src/peripherals/valve/ValveComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <kernel/Concurrent.hpp>
#include <kernel/Service.hpp>
#include <kernel/Task.hpp>
#include <kernel/Time.hpp>
#include <kernel/Telemetry.hpp>
#include <kernel/drivers/MotorDriver.hpp>

Expand Down Expand Up @@ -153,10 +154,11 @@ class LatchingValveControlStrategy

class ValveComponent : public Component {
public:
ValveComponent(const String& name, PwmMotorDriver& controller, ValveControlStrategy& strategy, shared_ptr<MqttDriver::MqttRoot> mqttRoot)
ValveComponent(const String& name, PwmMotorDriver& controller, ValveControlStrategy& strategy, shared_ptr<MqttDriver::MqttRoot> mqttRoot, std::function<void()> publishTelemetry)
: Component(name, mqttRoot)
, controller(controller)
, strategy(strategy) {
, strategy(strategy)
, publishTelemetry(publishTelemetry) {
Log.infoln("Creating valve '%s' with strategy %s",
name.c_str(), strategy.describe().c_str());

Expand Down Expand Up @@ -269,10 +271,12 @@ class ValveComponent : public Component {
mqttRoot->publish("events/state", [=](JsonObject& json) {
json["state"] = state;
});
publishTelemetry();
}

PwmMotorDriver& controller;
ValveControlStrategy& strategy;
std::function<void()> publishTelemetry;

ValveState state = ValveState::NONE;

Expand Down

0 comments on commit f12b883

Please sign in to comment.