Skip to content

Commit

Permalink
adapt borzoi to_json converter
Browse files Browse the repository at this point in the history
  • Loading branch information
marenz2569 committed Jul 25, 2024
1 parent d9e7f70 commit 66d8049
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 57 deletions.
8 changes: 6 additions & 2 deletions include/borzoi/borzoi_converter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@

#pragma once

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

struct BorzoiConverter {
static auto to_json(ShortDataServicePacket* packet) -> nlohmann::json;
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;
};
72 changes: 33 additions & 39 deletions src/borzoi/borzoi_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
*/

#include "borzoi/borzoi_converter.hpp"
#include "utils/bit_vector.hpp"
#include <nlohmann/json_fwd.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"

inline static auto get_time() -> std::string {
auto t = std::time(nullptr);
Expand All @@ -18,47 +20,39 @@ inline static auto get_time() -> std::string {
return ss.str();
}

auto BorzoiConverter::to_json(ShortDataServicePacket* packet) -> nlohmann::json {
auto message = nlohmann::json::object();
/// TODO: this may throw
message["source_ssi"] = static_cast<unsigned>(packet->sds_data_->address_.ssi().value());
message["destination_ssi"] = static_cast<unsigned>(packet->address_.ssi().value());
message["protocol_identifier"] = static_cast<unsigned>(packet->protocol_identifier_);
message["telegram_type"] = "SDS";
message["data"] = nlohmann::json::array();
auto data = BitVector(packet->sds_data_->data_);
while (data.bits_left() >= 8) {
unsigned bits = data.take<8>();
message["data"].push_back(bits);
}
message["arbitrary"] = nlohmann::json::object();
if (data.bits_left() > 0) {
message["arbitrary"]["bits_in_last_byte"] = data.bits_left();
message["data"].push_back(data.take_all());
} else {
message["arbitrary"]["bits_in_last_byte"] = 8;
}
message["arbitrary"]["optional_fields"] = nlohmann::json::object();
for (const auto& [key, value] : packet->sds_data_->optional_elements_) {
auto& vec = message["arbitrary"]["optional_fields"][to_string(key)];
vec = nlohmann::json::object();
vec["repeated_elements"] = value.repeated_elements;
vec["unparsed_bits"] = nlohmann::json::array();
auto data = BitVector(value.unparsed_bits);
while (data.bits_left() >= 8) {
unsigned bits = data.take<8>();
vec["unparsed_bits"].push_back(bits);
}
if (data.bits_left() > 0) {
vec["bits_in_last_byte"] = data.bits_left();
vec["unparsed_bits"].push_back(data.take_all());
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 {
vec["bits_in_last_byte"] = 8;
// Emit MobileLinkEntityPacket packet to json
data["key"] = "MobileLinkEntityPacket";
data["value"] = *mle;
}
} else {
// Emit LogicalLinkControlPacket packet to json
data["key"] = "LogicalLinkControlPacket";
data["value"] = *packet;
}
message["time"] = get_time();

return message;
return data;
}

auto BorzoiConverter::to_json(const Slots& slots) -> nlohmann::json {
Expand Down
22 changes: 6 additions & 16 deletions src/borzoi/borzoi_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,13 @@ BorzoiSender::BorzoiSender(ThreadSafeFifo<std::variant<std::unique_ptr<LogicalLi
BorzoiSender::~BorzoiSender() { worker_thread_.join(); }

void BorzoiSender::send_packet(const std::unique_ptr<LogicalLinkControlPacket>& packet) {
if (auto* sds = dynamic_cast<ShortDataServicePacket*>(packet.get())) {
nlohmann::json json;
try {
json = BorzoiConverter::to_json(sds);
json["station"] = borzoi_uuid_;
/// TODO: add json to post request
} catch (std::exception& e) {
std::cout << "Failed to convert packet to json. Error: " << e.what() << std::endl;
return;
}
cpr::Response resp =
cpr::Post(borzoi_url_sds_, cpr::Body{json.dump()}, cpr::Header{{"Content-Type", "application/json"}});
nlohmann::json json = BorzoiConverter::to_json(packet);
cpr::Response resp =
cpr::Post(borzoi_url_sds_, cpr::Body{json.dump()}, cpr::Header{{"Content-Type", "application/json"}});

if (resp.status_code != 200) {
std::cout << "Failed to send packet to Borzoi: " << json.dump() << " Error: " << resp.status_code << " "
<< resp.error.message << std::endl;
}
if (resp.status_code != 200) {
std::cout << "Failed to send packet to Borzoi: " << json.dump() << " Error: " << resp.status_code << " "
<< resp.error.message << std::endl;
}
}

Expand Down

0 comments on commit 66d8049

Please sign in to comment.