tdaq-develop-2025-02-12
otsdaq_load_json_document.cc
1 #include "otsdaq/MessageFacility/MessageFacility.h"
2 
3 #include <dirent.h>
4 #include <cassert>
5 #include <iostream>
6 #include <memory>
7 #include <string>
8 
9 #include "otsdaq/ConfigurationInterface/ConfigurationInterface.h"
10 #include "otsdaq/ConfigurationInterface/ConfigurationManagerRW.h"
11 // #include "artdaq-database/StorageProviders/FileSystemDB/provider_filedb_index.h"
12 // #include "artdaq-database/JsonDocument/JSONDocument.h"
13 
17 #define TRACE_NAME "LoadJSON_Document"
18 
19 #undef __COUT__
20 #define __COUT__ \
21  std::cout \
22  << __MF_DECOR__ \
23  << __COUT_HDR_FL__ //TLOG(TLVL_DEBUG) //std::cout << __MF_DECOR__ << __COUT_HDR_FL__
24 
25 using namespace ots;
26 
27 void LoadJSON_Document(int argc, char* argv[])
28 {
29  // The configuration uses __ENV__("SERVICE_DATA_PATH") in init() so define it if it is not defined
30  if(getenv("SERVICE_DATA_PATH") == NULL)
31  setenv("SERVICE_DATA_PATH",
32  (std::string(__ENV__("USER_DATA")) + "/ServiceData").c_str(),
33  1);
34 
35  __COUT__ << "=================================================\n";
36  __COUT__ << "=================================================\n";
37  __COUT__ << "=================================================\n";
38  __COUT__ << "\nLoading Trigger Document!" << std::endl;
39 
40  __COUT__ << "\n\nusage: Two arguments:\n\t <document_name_to_load> "
41  "<document_version_to_load> <path_to_save_JSON>"
42  << std::endl
43  << std::endl;
44 
45  __COUT__ << "argc = " << argc << std::endl;
46  for(int i = 0; i < argc; i++)
47  __COUT__ << "argv[" << i << "] = " << argv[i] << std::endl;
48 
49  if(argc != 4)
50  {
51  __COUT__ << "\n\nError! Must provide 3 parameters.\n\n" << std::endl;
52  return;
53  }
54 
55  //==============================================================================
56  // Define environment variables
57  // Note: normally these environment variables are set by StartOTS.sh
58 
59  // These are needed by
60  // otsdaq/otsdaq/ConfigurationDataFormats/ConfigurationInfoReader.cc [207]
61  // setenv("CONFIGURATION_TYPE", "File", 1); // Can be File, Database, DatabaseTest
62  // setenv("CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/ConfigurationDataExamples").c_str(), 1);
63  // setenv("TABLE_INFO_PATH", (std::string(getenv("USER_DATA")) + "/TableInfo").c_str(), 1);
64  // ////////////////////////////////////////////////////
65 
66  // // Some configuration plug-ins use __ENV__("OTSDAQ_LIB") and
67  // // __ENV__("OTSDAQ_UTILITIES_LIB") in init() so define it to a non-sense place is ok
68  // setenv("OTSDAQ_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1);
69  // setenv("OTSDAQ_UTILITIES_LIB", (std::string(getenv("USER_DATA")) + "/").c_str(), 1);
70 
71  // // Some configuration plug-ins use __ENV__("OTS_MAIN_PORT") in init() so define it
72  // setenv("OTS_MAIN_PORT", "2015", 1);
73 
74  // // also xdaq envs for XDAQContextTable
75  // setenv("XDAQ_CONFIGURATION_DATA_PATH", (std::string(getenv("USER_DATA")) + "/XDAQConfigurations").c_str(), 1);
76  // setenv("XDAQ_CONFIGURATION_XML", "otsConfigurationNoRU_CMake", 1);
78 
79  //==============================================================================
80  // get prepared with initial source db
81 
82  // ConfigurationManager instance immediately loads active groups
83  __COUT__ << "Loading JSON Document..." << std::endl;
84  ConfigurationManagerRW cfgMgrInst("load_admin");
85  ConfigurationManagerRW* cfgMgr = &cfgMgrInst;
86 
87  ConfigurationInterface* theInterface_ = cfgMgr->getConfigurationInterface();
88  std::string json =
89  theInterface_->loadCustomJSON(argv[1], TableVersion(atoi(argv[2])));
90  __COUTV__(json);
91  FILE* fp = std::fopen(argv[3], "w");
92  if(!fp)
93  {
94  __COUT__ << "\n\nERROR! Could not open file at " << argv[1]
95  << ". Error: " << errno << " - " << strerror(errno) << __E__;
96  return;
97  }
98  fputs(json.c_str(), fp);
99  fclose(fp);
100  return;
101 } //end LoadJSON_Document()
102 
103 int main(int argc, char* argv[])
104 {
105  if(getenv("OTSDAQ_LOG_FHICL") == NULL)
106  setenv("OTSDAQ_LOG_FHICL",
107  (std::string(__ENV__("USER_DATA")) +
108  "/MessageFacilityConfigurations/MessageFacilityWithCout.fcl")
109  .c_str(),
110  1);
111 
112  if(getenv("OTSDAQ_LOG_ROOT") == NULL)
113  setenv(
114  "OTSDAQ_LOG_ROOT", (std::string(__ENV__("USER_DATA")) + "/Logs").c_str(), 1);
115 
116  // INIT_MF("LoadJSON_Document");
117  LoadJSON_Document(argc, argv);
118  return 0;
119 }
120 // BOOST_AUTO_TEST_SUITE_END()