diff --git a/include/nlohmann/slots.hpp b/include/nlohmann/slots.hpp index b65ae78..a061b86 100644 --- a/include/nlohmann/slots.hpp +++ b/include/nlohmann/slots.hpp @@ -11,6 +11,26 @@ #include "l2/slot.hpp" #include +static auto stob(const std::string& str) -> bool { + bool retval = false; + + // convert integer (0 and 1) to bool + std::istringstream is(str); + is >> retval; + + // convert boolean string (false and true) to bool + if (is.fail()) { + is.clear(); + is >> std::boolalpha >> retval; + } + + if (is.fail()) { + throw std::invalid_argument("Cannot convert: " + str + " to bool."); + } + + return retval; +}; + namespace nlohmann { template <> struct adl_serializer { static void to_json(json& j, const Slots& slots) { @@ -32,12 +52,13 @@ template <> struct adl_serializer { } static void from_json(const json& j, Slots& slots) { - auto burst_type = j["burst_type"].template get(); - auto slot_type = j["slot_type"].template get(); - auto first_slot_logical_channel = j["first_slot_logical_channel"].template get(); + auto burst_type = BurstType(std::stoi(j["burst_type"].template get())); + auto slot_type = SlotType(std::stoi(j["slot_type"].template get())); + auto first_slot_logical_channel = + LogicalChannel(std::stoi(j["first_slot_logical_channel"].template get())); auto first_slot_data = j["first_slot_data"].template get(); - auto first_slot_crc_ok = j["first_slot_crc_ok"].template get(); - auto second_slot_present = j["second_slot_present"].template get(); + auto first_slot_crc_ok = stob(j["first_slot_crc_ok"].template get()); + auto second_slot_present = stob(j["second_slot_present"].template get()); auto first_slot = Slot(LogicalChannelDataAndCrc{ .channel = first_slot_logical_channel, @@ -46,9 +67,10 @@ template <> struct adl_serializer { }); if (second_slot_present) { - auto second_slot_logical_channel = j["second_slot_logical_channel"].template get(); + auto second_slot_logical_channel = + LogicalChannel(std::stoi(j["second_slot_logical_channel"].template get())); auto second_slot_data = j["second_slot_data"].template get(); - auto second_slot_crc_ok = j["second_slot_crc_ok"].template get(); + auto second_slot_crc_ok = stob(j["second_slot_crc_ok"].template get()); auto second_slot = Slot(LogicalChannelDataAndCrc{ .channel = second_slot_logical_channel, diff --git a/src/experiments/CMakeLists.txt b/src/experiments/CMakeLists.txt index fd68018..91eaf0d 100644 --- a/src/experiments/CMakeLists.txt +++ b/src/experiments/CMakeLists.txt @@ -3,4 +3,9 @@ add_executable(packet-parser-example src/experiments/packet_parser_example.cpp) -target_link_libraries(packet-parser-example tetra-decoder-library) \ No newline at end of file +target_link_libraries(packet-parser-example tetra-decoder-library) + +add_executable(slots-parser-example + src/experiments/slots_parser_example.cpp) + +target_link_libraries(slots-parser-example tetra-decoder-library) \ No newline at end of file diff --git a/src/experiments/README.md b/src/experiments/README.md index 3f28912..4176af6 100644 --- a/src/experiments/README.md +++ b/src/experiments/README.md @@ -13,5 +13,4 @@ To download the selected data as CSV click on: `Query inspector` -> `Data` -> `D Query: `SELECT * FROM tetra_failed_slots WHERE $__timeFilter(time) ORDER BY time desc`. -There is no example application provided showing how to convert the csv from grafana to C++ structures. -However, the principle is the same as in the `packet_parser_example`. \ No newline at end of file +The application `slots_parser_example` show how to convert this data back into the correct C++ structures. \ No newline at end of file diff --git a/src/experiments/packet_parser_example.cpp b/src/experiments/packet_parser_example.cpp index bb6cdf3..cf7be05 100644 --- a/src/experiments/packet_parser_example.cpp +++ b/src/experiments/packet_parser_example.cpp @@ -20,7 +20,7 @@ auto main(int argc, char** argv) -> int { std::string datafile; - cxxopts::Options options("parser-example", "Reads a csv file and extracts the contained packets."); + cxxopts::Options options("packet-parser-example", "Reads a csv file and extracts the contained packets."); // clang-format off options.add_options()