Skip to content
Snippets Groups Projects
Commit 5c32b030 authored by Valerio Pastore's avatar Valerio Pastore
Browse files

Added json support

parent c7c75319
No related branches found
No related tags found
No related merge requests found
...@@ -7,6 +7,7 @@ option(ASTRIPACKET_BUILD_SHARED "Build astripacket as a shared library." OFF) ...@@ -7,6 +7,7 @@ option(ASTRIPACKET_BUILD_SHARED "Build astripacket as a shared library." OFF)
set(SOURCES set(SOURCES
src/PacketStructureTxt.cpp src/PacketStructureTxt.cpp
src/PacketStructureJson.cpp
src/AstriPacketGeneric.cpp src/AstriPacketGeneric.cpp
) )
......
{
"crc_flag": 1,
"TelescopeID": 7,
"Type": 4,
"Subtype": 4,
"SSC": 16,
"length": 16,
"DATASOURCE": {
"size": 8192,
"fields": {
"binary": 64
}
}
}
\ No newline at end of file
{
"crc_flag": 1,
"TelescopeID": 7,
"Type": 4,
"SubType": 4,
"SSC": 16,
"length": 16,
"year": 6,
"month": 4,
"day": 5,
"hh_msb": 1,
"hh_lsb": 4,
"minutes": 6,
"seconds": 6,
"vt": 1,
"timetag_nanosec": 31,
"event_counter": 32,
"lid": 1,
"fib_st": 1,
"fib_cont": 1,
"fib_puls": 1,
"rgb_cont": 2,
"rgb_puls": 2,
"spare_0": 8,
"pixel_trigg_disc_high_pe": 16,
"pixel_trigg_disc_low_pe": 16,
"spare_1": 7,
"trg_type": 1,
"trigger_config": 8,
"PDM_BLOCK": {
"size": 37,
"fields": {
"pdm_Val": 1,
"trg_enb": 1,
"trg_pdm": 1,
"spare_2": 7,
"pdm_id": 6,
"high_gains": {
"size": 64,
"fields": {
"high_gain_pixel": 16
}
},
"low_gains": {
"size": 64,
"fields": {
"low_gain_pixel": 16
}
},
"time_triggers": {
"size": 64,
"fields": {
"time_trigg_pixel": 8
}
},
"hit_reg_1": 32,
"hit_reg_2": 32,
"pixel_trigg_disc_high_DA": 16,
"pixel_trigg_disc_low_DAC": 16,
"trigger_mask_1": 32,
"trigger_mask_2": 32,
"SiPM_temp": {
"size": 3,
"fields": {
"temp": 16
}
},
"SiPM_high_volt": 16,
"SiPM_current": 16
}
},
"crc": 16
}
\ No newline at end of file
crc_flag 1
TelescopeID 7
Type 4
SubType 4
SSC 16
length 16
year 6
month 4
day 5
hh_msb 1
hh_lsb 4
minutes 6
seconds 6
vt 1
timetag_nanosec 31
event_counter 32
lid 1
fib_st 1
fib_cont 1
fib_puls 1
rgb_cont 2
rgb_puls 2
spare_0 8
pixel_trigg_disc_high_pe 16
pixel_trigg_disc_low_pe 16
spare_1 7
trg_type 1
trigger_config 8
array : 37 {
pdm_Val 1
trg_enb 1
trg_pdm 1
spare_2 7
pdm_id 6
array : 64 {
high_gain_pixel 16
}
array : 64 {
low_gain_pixel 16
}
array : 64 {
time_trigger_pixel 8
}
hit_reg_1 32
hit_reg_2 32
pixel_trigg_disc_high_DAC 16
pixel_trigg_disc_low_DAC 16
trigger_mask_1 32
trigger_mask_2 32
array : 3 {
SiPM_temp 16
}
SiPM_high_volt 16
SiPM_current 16
}
crc 16
\ No newline at end of file
...@@ -2,10 +2,6 @@ ...@@ -2,10 +2,6 @@
#define INCLUDE_INAF_OAS_PACKETS_ASTRI_PACKER_GENERIC_H_ #define INCLUDE_INAF_OAS_PACKETS_ASTRI_PACKER_GENERIC_H_
#include <Base_Packet.h> #include <Base_Packet.h>
#include <fstream>
#include <algorithm>
namespace inaf::oasbo::Packets { namespace inaf::oasbo::Packets {
......
/*
* PacketLib.h
*
* Created on: Nov 24, 2022
* Author: valerio
*/
#ifndef INCLUDE_INAF_OAS_PACKET_STRUCTURE_JSON_H_
#define INCLUDE_INAF_OAS_PACKET_STRUCTURE_JSON_H_
#include <Base_Packet.h>
#include <fstream>
#include <nlohmann/json.hpp>
namespace inaf::oasbo::Packets {
class PacketStructureJson : public inaf::oasbo::PacketLib::BasePacketStructure {
private:
std::vector<std::tuple<int, std::string, int>> structure;
std::vector<std::tuple<int, std::string, int>> convertToTupleVector(const nlohmann::ordered_json &data,
size_t &count);
public:
PacketStructureJson(std::string filePath);
std::vector<std::tuple<int, std::string, int>> getPacketStructure(){ return structure; }
};
}
#endif /* INCLUDE_INAF_OAS_PACKET_STRUCTURE_JSON_H_ */
...@@ -5,15 +5,15 @@ ...@@ -5,15 +5,15 @@
* Author: valerio * Author: valerio
*/ */
#ifndef INCLUDE_INAF_OAS_PACKET_STRUCTURE_H_ #ifndef INCLUDE_INAF_OAS_PACKET_STRUCTURE_TXT_H_
#define INCLUDE_INAF_OAS_PACKET_STRUCTURE_H_ #define INCLUDE_INAF_OAS_PACKET_STRUCTURE_TXT_H_
#include <Base_Packet.h> #include <Base_Packet.h>
#include <fstream> #include <fstream>
namespace inaf::oasbo::Packets { namespace inaf::oasbo::Packets {
class PacketStructureTxt { class PacketStructureTxt : public inaf::oasbo::PacketLib::BasePacketStructure {
private: private:
std::vector<std::string> stringArrayFile; std::vector<std::string> stringArrayFile;
......
...@@ -14,7 +14,7 @@ size_t AstriPacketGeneric::getHeaderSize(){ ...@@ -14,7 +14,7 @@ size_t AstriPacketGeneric::getHeaderSize(){
} }
size_t AstriPacketGeneric::getPayloadSize(){ size_t AstriPacketGeneric::getPayloadSize(){
return this->operator []("length")-1; return this->operator [](5)-1;
} }
size_t AstriPacketGeneric::getTailSize(){ size_t AstriPacketGeneric::getTailSize(){
......
/*
* PacketStructure.cpp
*
* Created on: Dec 7, 2022
* Author: valerio
*/
#include <PacketStructureJson.h>
using namespace inaf::oasbo::Packets;
PacketStructureJson::PacketStructureJson(std::string filePath) {
std::ifstream file;
file.open(filePath, std::ios::in);
if (!file.is_open()) {
std::cerr << "Error: Could not open file: " << filePath << std::endl;
exit (EXIT_FAILURE);
}
size_t count = 0;
nlohmann::ordered_json data;
file >> data;
this->structure = convertToTupleVector(data, count);
file.close();
}
std::vector<std::tuple<int, std::string, int>> PacketStructureJson::convertToTupleVector(
const nlohmann::ordered_json &data, size_t &count) {
std::vector<std::tuple<int, std::string, int>> out;
for (auto it = data.begin(); it != data.end(); ++it) {
if (it.value().is_object()) {
if (!it.value().count("size") || !it.value().count("fields")
|| it.value().size() != 2) {
std::cerr << "Error in file: " << it.value().dump()
<< std::endl;
exit (EXIT_FAILURE);
}
for (auto i = 0; i < it.value()["size"]; i++) {
std::vector<std::tuple<int, std::string, int>> subArray =
convertToTupleVector(it.value()["fields"], count);
std::for_each(subArray.begin(), subArray.end(),
[i](std::tuple<int, std::string, int> &line) {
std::get < 1
> (line).append("_#").append(
std::to_string(i));
});
out.insert(out.end(), subArray.begin(), subArray.end());
}
} else {
if (!it.value().is_number_integer()) {
std::cerr << "Error in value, not an int: " << it.value().dump()
<< std::endl;
exit (EXIT_FAILURE);
}
std::string fieldName(it.key());
std::transform(fieldName.begin(), fieldName.end(),
fieldName.begin(), ::tolower);
out.push_back(std::make_tuple(count, fieldName, it.value()));
count += 1;
}
}
return out;
}
...@@ -12,6 +12,10 @@ using namespace inaf::oasbo::Packets; ...@@ -12,6 +12,10 @@ using namespace inaf::oasbo::Packets;
PacketStructureTxt::PacketStructureTxt(std::string filePath) { PacketStructureTxt::PacketStructureTxt(std::string filePath) {
std::ifstream file; std::ifstream file;
file.open(filePath, std::ios::in); file.open(filePath, std::ios::in);
if (!file.is_open()) {
std::cerr << "Error: Could not open file: " << filePath << std::endl;
exit(EXIT_FAILURE);
}
stringArrayFile = this->toStringArray(file); stringArrayFile = this->toStringArray(file);
size_t start = 0; size_t start = 0;
size_t count = 0; size_t count = 0;
...@@ -62,6 +66,7 @@ std::vector<std::tuple<int, std::string, int>> PacketStructureTxt::convertToTupl ...@@ -62,6 +66,7 @@ std::vector<std::tuple<int, std::string, int>> PacketStructureTxt::convertToTupl
std::remove_if(fieldName.begin(), fieldName.end(), std::remove_if(fieldName.begin(), fieldName.end(),
::isspace), fieldName.end()); ::isspace), fieldName.end());
std::transform(fieldName.begin(), fieldName.end(), fieldName.begin(), ::tolower);
out.push_back( out.push_back(
std::make_tuple(count, fieldName, std::make_tuple(count, fieldName,
std::stoi(line.substr(line.find(" "))))); std::stoi(line.substr(line.find(" ")))));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment