Skip to content

Commit b5c6ee3

Browse files
committed
Added helper functions and program defaults
One helper function is to check if a given jobId exists at the given path the other one checks whether the job data JSON is available. Added two global defaults one for config file name the other one for job data file name.
1 parent 46168aa commit b5c6ee3

10 files changed

Lines changed: 59 additions & 36 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@ add_executable(
6666
src/global_variables/cli_defaults.hpp
6767
src/executors/board_runner.cpp src/executors/board_runner.hpp
6868
src/executors/recording_runner.cpp src/executors/recording_runner.hpp
69-
src/executors/validation_runner.cpp
70-
src/executors/validation_runner.hpp
71-
src/executors/calibration_runner.cpp
72-
src/executors/calibration_runner.hpp
69+
src/executors/validation_runner.cpp src/executors/validation_runner.hpp
70+
src/executors/calibration_runner.cpp src/executors/calibration_runner.hpp
71+
7372
)
7473

7574
target_link_libraries(

src/config/orchestrator.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#include "orchestrator.hpp"
22

3+
#include "../global_variables/program_defaults.hpp"
4+
35
#include "../recoding/recorders/camera_worker.hpp"
46

57
namespace YACCP::Config {
68
void loadConfig(FileConfig& config, const std::filesystem::path& path, const bool boardCreation) {
79
// Load TOML configuration file.
810
toml::table tbl;
911
try {
10-
tbl = toml::parse_file((path / "config.toml").string());
12+
tbl = toml::parse_file((path / GlobalVariables::configFileName).string());
1113
} catch (const toml::parse_error& err) {
1214
std::stringstream ss{};
13-
ss << err.description() << "\n At: " << err.source() << "\n";
15+
ss << err.description() << "\n At: " << err.source();
1416
throw std::runtime_error(ss.str());
1517
}
1618

@@ -24,10 +26,10 @@ namespace YACCP::Config {
2426
// Load TOML configuration file.
2527
toml::table tbl;
2628
try {
27-
tbl = toml::parse_file((path / "config.toml").string());
29+
tbl = toml::parse_file((path / GlobalVariables::configFileName).string());
2830
} catch (const toml::parse_error& err) {
2931
std::stringstream ss{};
30-
ss << err.description() << "\n At: " << err.source() << "\n";
32+
ss << err.description() << "\n At: " << err.source();
3133
throw std::runtime_error(ss.str());
3234
}
3335

src/executors/board_runner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,20 @@ namespace YACCP::Executor {
3535
// Check whether the given job exists.
3636
if (!is_directory(jobPath)) {
3737
std::stringstream ss;
38-
ss << "Job: " << jobPath.string() << " does not exist in the given path: " << dataPath << "\n";
38+
ss << "Job: " << jobPath.string() << " does not exist in the given path: " << dataPath;
3939
throw std::runtime_error(ss.str());
4040
}
4141

4242
// Load config from JSON file
43-
nlohmann::json j = YACCP::Utility::loadJsonFromFile(jobPath, "job_data.json");
43+
nlohmann::json j = Utility::loadJobDataFromFile(jobPath);
4444
j.at("config").get_to(fileConfig);
4545
}
4646

4747
CreateBoard::charuco(fileConfig, cliCmdConfig.boardCreationCmdConfig, jobPath);
4848

4949
// Save the board creation variables to a JSON
5050
std::vector<CamData> empty{};
51-
Utility::saveJsonToFile(jobPath, fileConfig, empty);
51+
Utility::saveJobDataToFile(jobPath, fileConfig, empty);
5252
}
5353
}
5454
} // YACCP::Executor

src/executors/recording_runner.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "../utility.hpp"
44

5+
#include "../global_variables/program_defaults.hpp"
6+
57
// #include "../recoding/detection_validator.hpp"
68
// #include "../recoding/job_data.hpp"
79
// #include "../recoding/video_viewer.hpp"
@@ -55,10 +57,10 @@ namespace YACCP::Executor {
5557
"The most recent job ID already has recoding data, creating a new job and copying job_config.json from previous one. \n";
5658
jobPath = dataPath / ("job_" + dateTime.str());
5759
std::filesystem::create_directories(jobPath);
58-
std::filesystem::copy(jobPathMostRecent / "job_data.json", jobPath / "job_data.json");
60+
std::filesystem::copy(jobPathMostRecent / GlobalVariables::jobDataFileName, jobPath / GlobalVariables::jobDataFileName);
5961

6062
// Load config from JSON file
61-
nlohmann::json j = Utility::loadJsonFromFile(jobPath, "job_data.json");
63+
nlohmann::json j = Utility::loadJobDataFromFile(jobPath);
6264
j.at("config").get_to(fileConfig);
6365

6466
// Load config from TOML file
@@ -71,7 +73,7 @@ namespace YACCP::Executor {
7173
Config::FileConfig jsonConfig;
7274

7375
// Load config from JSON file
74-
nlohmann::json j = Utility::loadJsonFromFile(jobPath, "job_data.json");
76+
nlohmann::json j = Utility::loadJobDataFromFile(jobPath);
7577
j.at("config").get_to(jsonConfig);
7678

7779
// Load config from TOML file
@@ -104,7 +106,7 @@ namespace YACCP::Executor {
104106
}
105107

106108
// Load config from JSON file
107-
nlohmann::json j = Utility::loadJsonFromFile(jobPath, "job_data.json");
109+
nlohmann::json j = Utility::loadJobDataFromFile(jobPath);
108110
j.at("config").get_to(fileConfig);
109111

110112
// Load config from TOML file
@@ -239,7 +241,7 @@ namespace YACCP::Executor {
239241

240242
// Create a JSON object with all information on this job,
241243
// that includes the configured parameters in the config.toml and information about the job itself.
242-
Utility::saveJsonToFile(jobPath, fileConfig, camDatas);
244+
Utility::saveJobDataToFile(jobPath, fileConfig, camDatas);
243245
}
244246
return 0;
245247
}

src/global_variables/cli_defaults.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
namespace YACCP::GlobalVariables {
55
// Default boardCreationCmd variables.
6-
76
inline constexpr auto generateImage{true};
87
inline constexpr auto generateVideo{false};
98
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef YACCP_GLOBAL_VARIABLES_PROGRAM_DEFAULTS_HPP
2+
#define YACCP_GLOBAL_VARIABLES_PROGRAM_DEFAULTS_HPP
3+
4+
namespace YACCP::GlobalVariables {
5+
inline constexpr auto jobDataFileName("job_data.json");
6+
inline constexpr auto configFileName("config.toml");
7+
}
8+
9+
#endif //YACCP_GLOBAL_VARIABLES_PROGRAM_DEFAULTS_HPP

src/tools/image_validator.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,11 @@ namespace YACCP {
5454
int resolutionHeight,
5555
const std::filesystem::path& dataPath,
5656
const std::string& jobId) {
57+
Utility::checkJobPath(dataPath, jobId);
5758
jobPath_ = dataPath / jobId;
58-
if (!exists(jobPath_)) {
59-
std::stringstream ss;
60-
ss << "Job: " << jobId << " does not exist in the given path: " << dataPath << "\n";
61-
throw std::runtime_error(ss.str());
62-
}
6359

6460
if (!Utility::isNonEmptyDirectory(jobPath_ / "images" / "raw")) {
65-
throw std::runtime_error("\nNo raw images found for job: " + jobId + "\n");
61+
throw std::runtime_error("\nNo raw images found for job: " + jobId);
6662
}
6763

6864
std::vector<std::filesystem::path> cams;
@@ -81,11 +77,7 @@ namespace YACCP {
8177

8278
std::ranges::sort(images);
8379

84-
if (!exists(jobPath_ / "job_data.json")) {
85-
throw std::runtime_error("No camera data was found.\nStopping! \n");
86-
}
87-
88-
nlohmann::json j = Utility::loadJsonFromFile(jobPath_, "job_data.json");
80+
nlohmann::json j = Utility::loadJobDataFromFile(jobPath_);
8981
j.at("config").get_to(fileConfig);
9082

9183
std::vector<CamData::Info> camDatas(cams.size());
@@ -96,7 +88,6 @@ namespace YACCP {
9688

9789
Metavision::FrameComposer frameComposer;
9890
for (const auto& cam : camDatas) {
99-
10091
const int topLeftX = cam.ViewData.windowX;
10192
const int topLeftY = cam.ViewData.windowY;
10293
const unsigned width = cam.resolution.width;

src/utility.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#include "utility.hpp"
22

3+
#include "global_variables/program_defaults.hpp"
4+
35
#include <filesystem>
46
#include <fstream>
57

68
#include <opencv2/core/mat.hpp>
79
#include <opencv2/objdetect/aruco_detector.hpp>
810

11+
912
namespace YACCP::Utility {
1013
void clearScreen() {
1114
// "\x1b" Escape character
@@ -59,9 +62,23 @@ namespace YACCP::Utility {
5962
return file;
6063
}
6164

62-
nlohmann::json loadJsonFromFile(const std::filesystem::path& path, const std::string& fileName) {
65+
void checkJobPath(const std::filesystem::path& dataPath, const std::string& jobId) {
66+
if (!exists(dataPath / jobId)) {
67+
throw std::runtime_error("Job: " + jobId + " does not exist in the given path: " + dataPath.string());
68+
}
69+
}
70+
71+
void checkJobDataAvailable(const std::filesystem::path& jobPath) {
72+
if (!exists(jobPath / GlobalVariables::jobDataFileName)) {
73+
throw std::runtime_error("No " + static_cast<std::string>(GlobalVariables::jobDataFileName) + " was found.");
74+
}
75+
}
76+
77+
nlohmann::json loadJobDataFromFile(const std::filesystem::path& path) {
78+
79+
checkJobDataAvailable(path);
6380

64-
std::ifstream file{openFile(path, fileName)};
81+
std::ifstream file{openFile(path, GlobalVariables::jobDataFileName)};
6582

6683
nlohmann::json j;
6784
try {
@@ -75,7 +92,7 @@ namespace YACCP::Utility {
7592
return j;
7693
}
7794

78-
void saveJsonToFile(const std::filesystem::path& jobPath,
95+
void saveJobDataToFile(const std::filesystem::path& jobPath,
7996
Config::FileConfig& fileConfig,
8097
std::vector<CamData>& camDatas) {
8198
// Create JSON object with all information on this job,
@@ -95,7 +112,7 @@ namespace YACCP::Utility {
95112
}
96113

97114
// Save JSON to a file.
98-
std::ofstream file(jobPath / "job_data.json");
115+
std::ofstream file(jobPath / GlobalVariables::jobDataFileName);
99116
file << j.dump(4);
100117
}
101118

src/utility.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ namespace YACCP::Utility {
7373

7474
[[nodiscard]] std::ifstream openFile(const std::filesystem::path& path, const std::string& fileName);
7575

76-
[[nodiscard]] nlohmann::json loadJsonFromFile(const std::filesystem::path& path, const std::string& fileName);
76+
void checkJobPath(const std::filesystem::path& dataPath, const std::string& jobId);
7777

78-
void saveJsonToFile(const std::filesystem::path& jobPath, Config::FileConfig& fileConfig, std::vector<CamData>& camDatas);
78+
void checkJobDataAvailable(const std::filesystem::path& jobPath);
79+
80+
[[nodiscard]] nlohmann::json loadJobDataFromFile(const std::filesystem::path& path);
81+
82+
void saveJobDataToFile(const std::filesystem::path& jobPath, Config::FileConfig& fileConfig, std::vector<CamData>& camDatas);
7983

8084
[[nodiscard]] nlohmann::json parseJsonFromFile(std::ifstream & file);
8185

src/yaccp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int main(int argc, char** argv) {
2222

2323
// Try to initialise GLFW
2424
if (!glfwInit()) {
25-
std::cerr << "Failed to initialize GLFW." << std::endl;
25+
std::cerr << "Failed to initialize GLFW.\n";
2626
raise(SIGABRT);
2727
}
2828

0 commit comments

Comments
 (0)