COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
command_generator_remote.cpp File Reference
#include "support/configCosmos.h"
#include "support/datalib.h"
#include "support/jsonlib.h"
#include "support/timelib.h"
#include "agent/event.h"
#include "agent/scheduler.h"
Include dependency graph for command_generator_remote.cpp:

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)
45 {
46 
47  uint32_t flag = 0;
48  string data = "";
49  string condition = "";
50  string name= "";
51  double utc = 0.;
52 
53  string node = "";
54 
55  switch (argc)
56  {
57  case 7: // set repeat flag
58  {
59  flag |= EVENT_FLAG_REPEAT;
60  }
61  case 6: // set conditions
62  {
63  condition = argv[5];
64  flag |= EVENT_FLAG_CONDITIONAL;
65  }
66  case 5: // add command to the scheduler
67  {
68  node = string(argv[4]);
69  }
70  case 4: // set time utc in mjd
71  {
72  switch (argv[3][0])
73  {
74  // add a few seconds to current time
75  case '+':
76  {
77  double seconds = atof(&argv[3][1]);
78  utc = currentmjd() + seconds / 86400.;
79  break;
80  }
81  default:
82  // use set time
83  {
84  utc = atof(argv[3]);
85  break;
86  }
87  }
88  }
89  case 3: // set command string
90  {
91  data = argv[2];
92  }
93  case 2: // set command name
94  {
95  name = argv[1];
96  break;
97  }
98  default:
99  {
100  cout << "The command generator produces the command string to be fed into the command\n"
101  "queue which is managed by agent_exec. Commands are a subset of events\n"
102  "in COSMOS." << endl << endl;
103 
104  cout << "Usage" << endl << endl;
105  cout << " command_generator [options]" << endl;
106  cout << " command_generator name command [time | +sec] [node] [condition] [repeat_flag]" << endl << endl;
107 
108 
109  cout << "Example" << endl << endl;
110  cout << " $ command_generator myCmd1 \"agent kauaicc_sim execsoh get_queue_size\" +10" << endl << endl;
111  cout << " This will run the command \"agent kauaicc_sim execsoh get_queue_size\" \n"
112  " with name 'myCmd' within 10 seconds from now." << endl << endl;
113 
114  cout << "Options" << endl << endl;
115 
116  cout << " -name \t = name of the command, can be a string or combination of \n "
117  " \t alphanumeric characters (ex: myCmd1)" << endl;
118  cout << " -command\t = the actual command to be executed, if more than one word \n"
119  " \t enclose the command string in quotes \n"
120  " \t (ex: \"agent kauaicc_sim execsoh get_queue_size\"" << endl;
121  cout << " -time \t = optional argument to enter the desired modified julian date\n"
122  " \t (mjd). If not entered it will use the current time. \n"
123  " \t If '+' is used instead then the number of seconds can be\n"
124  " \t inserted. For 10 seconds in the future use +10" << endl;
125  cout << " -node \t = optional argument to add the generated command to the command\n"
126  " \t queue on the specified node (ex: kauaicc_sim) " << endl;
127  return 0;
128  }
129  }
130 
131  Event event;
132  cout << "Command string:" << endl;
133 
134  cout << event.generator(name, data, utc, condition, flag) << endl << endl;
135  // Find agent_mongo
136  Agent *agent = new Agent("", "temp");
137  beatstruc agent_mongo_soh = agent->find_agent("any", "mongo");
138  if (!agent_mongo_soh.exists) {
139  cout << "could not find agent mongo" << endl;
140  }
141 
142  if(node.empty()) {
143  cout << "Could not add command to node [No Node]" << endl;
144  return 0;
145  }
146  else {
147 
148  // create outgoing directory if it doesnt exist
149  std::string outgoing_dir = data_base_path(node, "outgoing", "exec") + "/";
150 
151  cout << "Adding command/event to node directory:" << outgoing_dir << endl;
152 
153  // write command string to file in outgoing_dir
154  std::string outfilename = data_name(node, currentmjd(),"","command");
155  ofstream cmdfile;
156  cmdfile.open(outgoing_dir + outfilename);
157  cmdfile << event;
158  cmdfile.close();
159 
160  // request agent_mongo to insert command
161  if(agent_mongo_soh.exists) {
162  string out;
163  string mongo_request = "insert db " + node + ":command ";
164  mongo_request += event.get_event_string();
165  agent->send_request(agent_mongo_soh, mongo_request, out, 0);
166  cout <<"Request agent mongo: " << mongo_request << endl;
167  cout << out << endl;
168  }
169  }
170 
171 }
Class to manage Event information.
Definition: event.h:57
Definition: jsondef.h:923
int32_t send_request(beatstruc cbeat, string request, string &output, float waitsec=5.)
Send a request over AGENT.
Definition: agentclass.cpp:424
string generator(string name, string data, double utc, string condition, uint32_t flag)
Definition: event.cpp:53
string data_name(string node, double mjd, string extra, string type)
Create data file name.
Definition: datalib.cpp:685
string data_base_path(string node, string location, string agent, string filename)
Create data file path.
Definition: datalib.cpp:767
bool exists
Existence Flag (if beat exists then flag is set to true, false otherwise)
Definition: jsondef.h:950
#define EVENT_FLAG_REPEAT
Repeating event.
Definition: cosmos-defs.h:171
string get_event_string()
Retrieves Event information.
Definition: event.cpp:138
static Agent * agent
ensure the Agent constructor creates only one instance per process
Definition: agent_001.cpp:45
Definition: agentclass.h:139
string name
Definition: cubesat2obj.cpp:6
double currentmjd(double offset)
Current UTC in Modified Julian Days.
Definition: timelib.cpp:65
beatstruc find_agent(string node, string agent, double waitsec=0.)
Find agent.
Definition: agentclass.cpp:559
#define EVENT_FLAG_CONDITIONAL
Conditional event.
Definition: cosmos-defs.h:169
static string node
Definition: agent_monitor.cpp:126
static string outgoing_dir
Definition: agent_exec-2-0.cpp:144