Skip to content

Commit

Permalink
add to_json function for borzoi packets
Browse files Browse the repository at this point in the history
  • Loading branch information
marenz2569 committed Jul 30, 2024
1 parent ef0f885 commit e83f72d
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 22 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_executable(tetra-decoder
src/bit_stream_decoder.cpp
src/iq_stream_decoder.cpp
src/prometheus.cpp
src/borzoi/borzoi_packets.cpp
src/borzoi/borzoi_sender.cpp
src/l2/access_assignment_channel.cpp
src/l2/broadcast_synchronization_channel.cpp
Expand Down
32 changes: 32 additions & 0 deletions include/borzoi/borzoi_packets.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2024 Transit Live Mapping Solutions
* All rights reserved.
*
* Authors:
* Marenz Schmidl
*/

#pragma once

#include "l2/logical_link_control_packet.hpp"
#include "l2/slot.hpp"

struct BorzoiSendTetraPacket {
std::string time;
std::string station;
const std::unique_ptr<LogicalLinkControlPacket>& packet;

/// Construct a packet for Borzoi containing the parsed packet, the current time and the uuid of this instance of
/// tetra decoder.
BorzoiSendTetraPacket(const std::unique_ptr<LogicalLinkControlPacket>& packet, std::string borzoi_uuid);
};

struct BorzoiSendTetraSlots {
std::string time;
std::string station;
const Slots& slots;

/// Construct a packet for Borzoi containing the received slot, the current time and the uuid of this instance of
/// tetra decoder.
BorzoiSendTetraSlots(const Slots& slots, std::string borzoi_uuid);
};
24 changes: 24 additions & 0 deletions include/nlohmann/borzoi_send_tetra_packet.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Transit Live Mapping Solutions
* All rights reserved.
*
* Authors:
* Marenz Schmidl
*/

#pragma once

#include "borzoi/borzoi_packets.hpp"
#include "nlohmann/std_unique_ptr_logical_link_control_packet.hpp"
#include <nlohmann/json.hpp>

namespace nlohmann {
template <> struct adl_serializer<BorzoiSendTetraPacket> {
static void to_json(json& j, const BorzoiSendTetraPacket& bstp) {
j = json::object();
j["time"] = bstp.time;
j["station"] = bstp.station;
adl_serializer<std::unique_ptr<LogicalLinkControlPacket>>::to_json(j, bstp.packet);
}
};
} // namespace nlohmann
24 changes: 24 additions & 0 deletions include/nlohmann/borzoi_send_tetra_slots.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2024 Transit Live Mapping Solutions
* All rights reserved.
*
* Authors:
* Marenz Schmidl
*/

#pragma once

#include "borzoi/borzoi_packets.hpp"
#include "nlohmann/slots.hpp"
#include <nlohmann/json.hpp>

namespace nlohmann {
template <> struct adl_serializer<BorzoiSendTetraSlots> {
static void to_json(json& j, const BorzoiSendTetraSlots& bsts) {
j = json::object();
j["time"] = bsts.time;
j["station"] = bsts.station;
adl_serializer<Slots>::to_json(j, bsts.slots);
}
};
} // namespace nlohmann
30 changes: 30 additions & 0 deletions src/borzoi/borzoi_packets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2024 Transit Live Mapping Solutions
* All rights reserved.
*
* Authors:
* Marenz Schmidl
*/

#include "borzoi/borzoi_packets.hpp"

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();
}

BorzoiSendTetraPacket::BorzoiSendTetraPacket(const std::unique_ptr<LogicalLinkControlPacket>& packet,
std::string borzoi_uuid)
: station(std::move(borzoi_uuid))
, packet(packet) {
time = get_time();
}

BorzoiSendTetraSlots::BorzoiSendTetraSlots(const Slots& slots, std::string borzoi_uuid)
: station(std::move(borzoi_uuid))
, slots(slots) {
time = get_time();
}
33 changes: 11 additions & 22 deletions src/borzoi/borzoi_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
*/

#include "borzoi/borzoi_sender.hpp"
#include "borzoi/borzoi_packets.hpp"
#include "l2/upper_mac_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/slots.hpp" // IWYU pragma: keep
#include "nlohmann/std_unique_ptr_logical_link_control_packet.hpp" // IWYU pragma: keep
#include "nlohmann/borzoi_send_tetra_packet.hpp" // IWYU pragma: keep
#include "nlohmann/borzoi_send_tetra_slots.hpp" // IWYU pragma: keep
#include <cpr/body.h>
#include <cpr/cprtypes.h>
#include <cpr/payload.h>
Expand All @@ -23,20 +24,6 @@
#include <pthread.h>
#endif

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();
}

inline static auto with_time_and_uuid(nlohmann::json& j, const std::string& borzoi_uuid) -> nlohmann::json {
j["time"] = get_time();
j["station"] = borzoi_uuid;
return j;
}

BorzoiSender::BorzoiSender(ThreadSafeFifo<std::variant<std::unique_ptr<LogicalLinkControlPacket>, Slots>>& queue,
std::atomic_bool& termination_flag, const std::string& borzoi_url, std::string borzoi_uuid)
: queue_(queue)
Expand All @@ -55,9 +42,10 @@ 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 = packet;
cpr::Response resp = cpr::Post(borzoi_url_sds_, cpr::Body{with_time_and_uuid(json, borzoi_uuid_).dump()},
cpr::Header{{"Content-Type", "application/json"}});
nlohmann::json json = BorzoiSendTetraPacket(packet, borzoi_uuid_);

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 << " "
Expand All @@ -66,9 +54,10 @@ void BorzoiSender::send_packet(const std::unique_ptr<LogicalLinkControlPacket>&
}

void BorzoiSender::send_failed_slots(const Slots& slots) {
nlohmann::json json = slots;
cpr::Response resp = cpr::Post(borzoi_url_failed_slots_, cpr::Body{with_time_and_uuid(json, borzoi_uuid_).dump()},
cpr::Header{{"Content-Type", "application/json"}});
nlohmann::json json = BorzoiSendTetraSlots(slots, borzoi_uuid_);

cpr::Response resp =
cpr::Post(borzoi_url_failed_slots_, 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 << " "
Expand Down

0 comments on commit e83f72d

Please sign in to comment.