-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52eb714
commit 706de15
Showing
4 changed files
with
75 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "latency.hpp" | ||
|
||
#include <core/wall_clock.hpp> | ||
|
||
namespace hyped::telemetry { | ||
|
||
Latency::Latency(std::shared_ptr<core::IMqtt> mqtt) | ||
{ | ||
mqtt_->subscribe(core::MqttTopic::kStateRequest); | ||
} | ||
|
||
void Latency::respond() | ||
{ | ||
const auto nextMessage = mqtt_->getMessage(); | ||
if (!nextMessage) { return; } | ||
|
||
const auto payload = nextMessage->payload; | ||
const auto topic = core::MqttTopic::kLatencyResponse; | ||
const core::MqttMessage::Header header{.timestamp = 0, | ||
.priority = core::MqttMessagePriority::kNormal}; | ||
const core::MqttMessage message{topic, header, payload}; | ||
mqtt_->publish(message, core::MqttMessageQos::kAtLeastOnce); | ||
} | ||
|
||
void Latency::run() | ||
{ | ||
mqtt_->consume(); | ||
respond(); | ||
} | ||
|
||
core::Result Latency::startNode(const std::string &mqtt_ip, const std::uint32_t mqtt_port) | ||
{ | ||
core::WallClock wall_clock; | ||
core::Logger logger("LATENCY", core::LogLevel::kDebug, wall_clock); | ||
auto optional_mqtt = core::Mqtt::create(logger, "latency", mqtt_ip, mqtt_port); | ||
if (!optional_mqtt) { | ||
logger.log(core::LogLevel::kFatal, "Failed to create MQTT client"); | ||
return core::Result::kFailure; | ||
} | ||
auto mqtt = *optional_mqtt; | ||
Latency latency(mqtt); | ||
latency.run(); | ||
return core::Result::kSuccess; | ||
} | ||
|
||
} // namespace hyped::telemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <core/mqtt.hpp> | ||
#include <core/types.hpp> | ||
|
||
namespace hyped::telemetry { | ||
|
||
class Latency { | ||
public: | ||
Latency(std::shared_ptr<core::IMqtt> mqtt); | ||
void respond(); | ||
void run(); | ||
static core::Result startNode(const std::string &mqtt_ip, const std::uint32_t mqtt_port); | ||
|
||
const std::shared_ptr<core::IMqtt> mqtt_; | ||
}; | ||
|
||
} // namespace hyped::telemetry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters