PythonScript.h
Go to the documentation of this file.
00001 /*
00002  * PythonScript.h
00003  *
00004  *  Created on: Oct 17, 2013
00005  *      Author: blackpc
00006  */
00007 
00008 #ifndef PYTHONSCRIPT_H_
00009 #define PYTHONSCRIPT_H_
00010 
00011 #include <iostream>
00012 #include <boost/date_time.hpp>
00013 
00014 #include <diagnostic_aggregator/status_item.h>
00015 #include <scriptable_monitor/scripts/PythonScriptWrapper.h>
00016 
00017 using namespace std;
00018 
00019 typedef vector<boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> > ScriptOutputType;
00020 
00021 class PythonScript {
00022 public:
00023 
00024         enum FailType {
00025                 OK    = 0,
00026                 WARN  = 1,
00027                 ERROR = 2,
00028                 STALL = 3
00029         };
00030 
00031         PythonScript(string name, string sourceCode, map<string, string> parameters) {
00032                 init(name, sourceCode, parameters);
00033         }
00034 
00035         PythonScript(string name, string sourceCode) {
00036                 init(name, sourceCode, map<string, string>());
00037         }
00038 
00039         PythonScript(const PythonScript& script) { *this = script; }
00040 
00041         void addOutput(boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> status) {
00042                 _output.push_back(status);
00043         }
00044 
00045         string getSourceCode() { return _sourceCode; }
00046 
00047         ScriptOutputType getOutput() { return _output; }
00048 
00049         void addUsedTopic(string topicName) {
00050                 if (_usedTopics.count(topicName) == 0)
00051                         _usedTopics.insert(topicName);
00052         }
00053 
00054         set<string>& getUsedTopics() { return _usedTopics; }
00055 
00056         void addUsedInternalFunction(string functionName) {
00057                 if (_usedInternalFunctions.count(functionName) == 0)
00058                         _usedInternalFunctions.insert(functionName);
00059         }
00060 
00061         set<string>& getUsedInternalFunction() { return _usedInternalFunctions; }
00062 
00063         void setValidationFailed(bool isFailed) { _validationFailed = isFailed; }
00064         void addValidation(string description) { _validations.push_back(description); }
00065         void addFailedValidation(string description) { _failedValidations.push_back(description); }
00066         bool isValidationFailed() { return _validationFailed; }
00067         string getFailedValidation() { return _failedValidations.size() > 0 ? _failedValidations[0] : ""; }
00068         vector<string>& getValidations() { return _validations; }
00069 
00070         void updateExecutionTime() { _executionTime = boost::posix_time::second_clock::local_time(); }
00071         void updateExecutionTime(boost::posix_time::ptime executionTime) { _executionTime = executionTime; }
00072         boost::posix_time::ptime getExecutionTime() { return _executionTime; }
00073 
00074         double getInterval() { return _interval; }
00075 
00076         FailType getFailType() { return _failType; }
00077 
00078         string getParameter(string name) { return _parameters.count(name) > 0 ? _parameters[name] : ""; }
00079 
00080         string getName() { return _name; }
00081 
00082         bool operator<(const PythonScript& script) const {
00083                 return strcmp(script._name.c_str(), this->_name.c_str()) > 0;
00084         }
00085 
00086 private:
00087         string _name;
00088         string _sourceCode;
00089         bool   _validationFailed;
00090 
00091         set<string> _usedTopics;
00092         set<string> _usedInternalFunctions;
00093         vector<string> _validations;
00094         vector<string> _failedValidations;
00095         map<string, string> _parameters;
00096         boost::posix_time::ptime _executionTime;
00097         FailType _failType;
00098 
00102         double _interval;
00103 
00104         ScriptOutputType _output;
00105 
00106         void init(string name, string sourceCode, map<string, string> parameters) {
00107                 _name = name;
00108                 _sourceCode = string(PYTHON_SCRIPT_WRAPPER) + sourceCode;
00109                 _validationFailed = false;
00110                 _parameters = parameters;
00111                 _executionTime = boost::posix_time::microsec_clock::local_time();
00112 
00113                 _failType = PythonScript::ERROR;
00114                 if (parameters.count("fail") > 0) {
00115                         string failType = parameters["fail"];
00116                         boost::to_lower(failType);
00117 
00118                         if (failType == "ok")
00119                                 _failType = PythonScript::OK;
00120 
00121                         if (failType == "warn" || failType == "warning")
00122                                 _failType = PythonScript::WARN;
00123 
00124                         if (failType == "error")
00125                                 _failType = PythonScript::ERROR;
00126 
00127                         if (failType == "stall")
00128                                 _failType = PythonScript::STALL;
00129                 }
00130 
00131                 _interval = 1;
00132                 try { _interval = boost::lexical_cast<double>(parameters.count("interval") > 0 ? parameters["interval"] : "1"); }
00133                 catch(...) { }
00134 
00135         }
00136 };
00137 
00138 typedef boost::shared_ptr<PythonScript> PythonScriptPtr;
00139 
00140 #endif /* PYTHONSCRIPT_H_ */


scriptable_monitor
Author(s):
autogenerated on Wed Aug 26 2015 16:21:30