diff --git a/src/devices/Device.hpp b/src/devices/Device.hpp index 8aab4919..ed400a0d 100644 --- a/src/devices/Device.hpp +++ b/src/devices/Device.hpp @@ -272,15 +272,17 @@ class Device : ConsoleProvider { json["bootCount"] = bootCount++; json["time"] = time(nullptr); }); - Task::loop("telemetry", 8192, [this](Task& task) { publishTelemetry(task); }); + Task::loop("telemetry", 8192, [this](Task& task) { + publishTelemetry(); + // TODO Configure telemetry heartbeat interval + task.delayUntil(milliseconds(60000)); + }); } private: - void publishTelemetry(Task& task) { + void publishTelemetry() { deviceTelemetryPublisher.publishTelemetry(); peripheralManager.publishTelemetry(); - // TODO Configure telemetry heartbeat interval - task.delayUntil(milliseconds(60000)); } TDeviceDefinition deviceDefinition; @@ -291,7 +293,9 @@ class Device : ConsoleProvider { TelemetryCollector deviceTelemetryCollector; MqttTelemetryPublisher deviceTelemetryPublisher { mqttDeviceRoot, deviceTelemetryCollector }; - PingCommand pingCommand { deviceTelemetryPublisher }; + PingCommand pingCommand { [this]() { + publishTelemetry(); + } }; #if defined(FARMHUB_DEBUG) || defined(FARMHUB_REPORT_MEMORY) MemoryTelemetryProvider memoryTelemetryProvider; diff --git a/src/kernel/Command.hpp b/src/kernel/Command.hpp index 9bc82cb8..00416ef0 100644 --- a/src/kernel/Command.hpp +++ b/src/kernel/Command.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -11,7 +12,6 @@ #include #include -#include using namespace std::chrono; @@ -43,18 +43,24 @@ class EchoCommand : public Command { class PingCommand : public Command { public: - PingCommand(TelemetryPublisher& telemetryPublisher) + PingCommand(const std::function pingResponse) : Command("ping") - , telemetryPublisher(telemetryPublisher) { + , pingResponse(pingResponse) { } void handle(const JsonObject& request, JsonObject& response) override { - telemetryPublisher.publishTelemetry(); + try { + pingResponse(); + } catch (const std::exception& e) { + Log.errorln("Failed to send ping response: %s", e.what()); + } catch (...) { + Log.errorln("Failed to send ping response"); + } response["pong"] = millis(); } private: - TelemetryPublisher& telemetryPublisher; + const std::function pingResponse; }; class RestartCommand : public Command {