Skip to content

Commit

Permalink
Merge pull request #80 from kivancsikert/peripherals/respond-to-ping
Browse files Browse the repository at this point in the history
Send peripheral telemetry in response to ping
  • Loading branch information
lptr authored Jan 31, 2024
2 parents 1dcc73b + 3f12397 commit ea6615e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
14 changes: 9 additions & 5 deletions src/devices/Device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions src/kernel/Command.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <chrono>
#include <functional>

#include <ArduinoJson.h>
#include <ArduinoLog.h>
Expand All @@ -11,7 +12,6 @@

#include <kernel/FileSystem.hpp>
#include <kernel/Named.hpp>
#include <kernel/Telemetry.hpp>

using namespace std::chrono;

Expand Down Expand Up @@ -43,18 +43,24 @@ class EchoCommand : public Command {

class PingCommand : public Command {
public:
PingCommand(TelemetryPublisher& telemetryPublisher)
PingCommand(const std::function<void()> 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<void()> pingResponse;
};

class RestartCommand : public Command {
Expand Down

0 comments on commit ea6615e

Please sign in to comment.