Skip to content

Commit

Permalink
refactor borzoi converter into nlohmann json function
Browse files Browse the repository at this point in the history
  • Loading branch information
marenz2569 committed Jul 26, 2024
1 parent 66d8049 commit 2042a52
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 41 deletions.
5 changes: 0 additions & 5 deletions include/borzoi/borzoi_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@

#pragma once

#include "l2/logical_link_control_packet.hpp"
#include "l2/slot.hpp"
#include <nlohmann/json_fwd.hpp>

struct BorzoiConverter {
static constexpr const int kPacketApiVersion = 0;

static auto to_json(const Slots& slots) -> nlohmann::json;

static auto to_json(const std::unique_ptr<LogicalLinkControlPacket>& packet) -> nlohmann::json;
};
85 changes: 85 additions & 0 deletions include/nlohmann_std_unique_ptr_logical_link_control_packet.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2024 Transit Live Mapping Solutions
* All rights reserved.
*
* Authors:
* Marenz Schmidl
*/

#pragma once

#include "l2/logical_link_control_packet.hpp"
#include "l3/circuit_mode_control_entity_packet.hpp"
#include "l3/mobile_link_entity_packet.hpp"
#include "l3/mobile_management_packet.hpp"
#include "l3/short_data_service_packet.hpp"
#include <nlohmann/json.hpp>

static constexpr const int kPacketApiVersion = 0;

inline static auto get_time() -> std::string {
auto t = std::time(nullptr);
auto tm = *std::localtime(&t);
std::stringstream ss;
ss << std::put_time(&tm, "%FT%T%z");
return ss.str();
}

namespace nlohmann {
template <> struct adl_serializer<std::unique_ptr<LogicalLinkControlPacket>> {
static void to_json(json& j, const std::unique_ptr<LogicalLinkControlPacket>& packet) {
j["protocol_version"] = kPacketApiVersion;
j["time"] = get_time();

if (auto* mle = dynamic_cast<MobileLinkEntityPacket*>(packet.get())) {
if (auto* cmce = dynamic_cast<CircuitModeControlEntityPacket*>(mle)) {
if (auto* sds = dynamic_cast<ShortDataServicePacket*>(mle)) {
// Emit ShortDataServicePacket packet to json
j["key"] = "ShortDataServicePacket";
j["value"] = *sds;
} else {
// Emit CircuitModeControlEntityPacket packet to json
j["key"] = "CircuitModeControlEntityPacket";
j["value"] = *cmce;
}
} else if (auto* mm = dynamic_cast<MobileManagementPacket*>(mle)) {
// Emit MobileManagementPacket packet to json
j["key"] = "MobileManagementPacket";
j["value"] = *mm;
} else {
// Emit MobileLinkEntityPacket packet to json
j["key"] = "MobileLinkEntityPacket";
j["value"] = *mle;
}
} else {
// Emit LogicalLinkControlPacket packet to json
j["key"] = "LogicalLinkControlPacket";
j["value"] = *packet;
}
}

static void from_json(const json& j, std::unique_ptr<LogicalLinkControlPacket>& packet) {
auto protocol_version = j["protocol_version"].template get<int>();
if (protocol_version != kPacketApiVersion) {
throw std::runtime_error("Cannot process packets different API version.");
}

auto key = j["key"].template get<std::string>();

if (key == "LogicalLinkControlPacket") {
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<LogicalLinkControlPacket>());
} else if (key == "MobileLinkEntityPacket") {
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<MobileLinkEntityPacket>());
} else if (key == "MobileManagementPacket") {
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<MobileManagementPacket>());
} else if (key == "CircuitModeControlEntityPacket") {
packet =
std::make_unique<LogicalLinkControlPacket>(j["value"].template get<CircuitModeControlEntityPacket>());
} else if (key == "ShortDataServicePacket") {
packet = std::make_unique<LogicalLinkControlPacket>(j["value"].template get<ShortDataServicePacket>());
} else {
throw std::runtime_error("Unknown packet type: " + key);
}
}
};
} // namespace nlohmann
35 changes: 0 additions & 35 deletions src/borzoi/borzoi_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,6 @@ inline static auto get_time() -> std::string {
return ss.str();
}

auto BorzoiConverter::to_json(const std::unique_ptr<LogicalLinkControlPacket>& packet) -> nlohmann::json {
nlohmann::json data = nlohmann::json::object();

data["protocol_version"] = BorzoiConverter::kPacketApiVersion;
data["time"] = get_time();

if (auto* mle = dynamic_cast<MobileLinkEntityPacket*>(packet.get())) {
if (auto* cmce = dynamic_cast<CircuitModeControlEntityPacket*>(mle)) {
if (auto* sds = dynamic_cast<ShortDataServicePacket*>(mle)) {
// Emit ShortDataServicePacket packet to json
data["key"] = "ShortDataServicePacket";
data["value"] = *sds;
} else {
// Emit CircuitModeControlEntityPacket packet to json
data["key"] = "CircuitModeControlEntityPacket";
data["value"] = *cmce;
}
} else if (auto* mm = dynamic_cast<MobileManagementPacket*>(mle)) {
// Emit MobileManagementPacket packet to json
data["key"] = "MobileManagementPacket";
data["value"] = *mm;
} else {
// Emit MobileLinkEntityPacket packet to json
data["key"] = "MobileLinkEntityPacket";
data["value"] = *mle;
}
} else {
// Emit LogicalLinkControlPacket packet to json
data["key"] = "LogicalLinkControlPacket";
data["value"] = *packet;
}

return data;
}

auto BorzoiConverter::to_json(const Slots& slots) -> nlohmann::json {
auto message = nlohmann::json::object();

Expand Down
3 changes: 2 additions & 1 deletion src/borzoi/borzoi_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "l3/mobile_link_entity_packet.hpp"
#include "l3/mobile_management_packet.hpp"
#include "l3/short_data_service_packet.hpp"
#include "nlohmann_std_unique_ptr_logical_link_control_packet.hpp" // IWYU pragma: keep
#include <cpr/body.h>
#include <cpr/cprtypes.h>
#include <cpr/payload.h>
Expand Down Expand Up @@ -41,7 +42,7 @@ BorzoiSender::BorzoiSender(ThreadSafeFifo<std::variant<std::unique_ptr<LogicalLi
BorzoiSender::~BorzoiSender() { worker_thread_.join(); }

void BorzoiSender::send_packet(const std::unique_ptr<LogicalLinkControlPacket>& packet) {
nlohmann::json json = BorzoiConverter::to_json(packet);
nlohmann::json json = packet;
cpr::Response resp =
cpr::Post(borzoi_url_sds_, cpr::Body{json.dump()}, cpr::Header{{"Content-Type", "application/json"}});

Expand Down

0 comments on commit 2042a52

Please sign in to comment.