00001 #include "Test.h"
00002 #include "Sct/SctNames.h"
00003 #include "Sct/IoExceptions.h"
00004 #include "Sct/LogicErrors.h"
00005 #include "Sct/ISUtilities.h"
00006 #include <sstream>
00007 #include <boost/date_time/posix_time/posix_time.hpp>
00008 #include <is/info.h>
00009 #include <is/infodictionary.h>
00010 #include <is/infoiterator.h>
00011 #include <is/inforeceiver.h>
00012 #include <is/infoT.h>
00013
00014 using namespace std;
00015 using namespace Sct;
00016 using namespace boost::posix_time;
00017
00018 namespace SctCalibrationController {
00019
00020 Test::Test(const TestData& d, const list<string>& l) {
00021
00022 if (d.testPoints_size != d.nScans)
00023 throw InvalidArgumentError("testPoints size not the same as the number of Scans in Test:" + (string)getUniqueID(), __FILE__, __LINE__);
00024
00025 data = d;
00026 data.testPoints = new double[data.testPoints_size];
00027 for (unsigned int i=0; i<data.testPoints_size; ++i) {
00028 data.testPoints[i] = d.testPoints[i];
00029 }
00030 string startTime = to_iso_string(second_clock::universal_time());
00031 data.startTime = startTime.substr(0, 15);
00032
00033 data.modules = new string[l.size()];
00034 data.modules_size = l.size();
00035 unsigned int count = 0;
00036 for (list<string>::const_iterator i=l.begin(); i!=l.end(); ++i, ++count) {
00037 data.modules[count] = *i;
00038 }
00039
00040
00041
00042 publish();
00043 }
00044
00045 Test::~Test() {
00046 withdraw();
00047 }
00048
00049 void Test::addScan(Sct_SctApi::Scan_ptr scan) {
00050 if (scans.size() == data.nScans)
00051 throw InvalidArgumentError("Tried to add too many Scans to Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00052 scans.push_back(scan);
00053 }
00054
00055 const TestData& Test::getData() const {
00056 return data;
00057 }
00058
00059 Sct::UniqueID Test::getUniqueID() const {
00060 ostringstream s;
00061 s << "TestData." << data.runNumber
00062 << "." << data.startScanNumber
00063 << "." << data.testName;
00064 return Sct::UniqueID(s.str());
00065 }
00066
00067 Sct_SctApi::Scan_ptr Test::getScan(unsigned int index) const {
00068 if (index >= scans.size()) {
00069 ostringstream s;
00070 s << "Tried to getScan with index: " << index << " but only " << scans.size() << " available in Test: " << getUniqueID();
00071 throw InvalidArgumentError(s.str(), __FILE__, __LINE__);
00072 }
00073 return scans[index];
00074 }
00075
00076 list<string> Test::getModuleList() const{
00077 list<string> list;
00078 for (unsigned int i=0; i<data.modules_size; ++i) {
00079 list.push_back(data.modules[i]);
00080 }
00081 return list;
00082 }
00083
00084 void Test::setOptions(const std::string& options_in){
00085 data.options=options_in;
00086 publish();
00087 }
00088
00089 void Test::setStatus(TestData::status_E status) {
00090
00091 if (data.status == TestData::ABORTED && status != TestData::ABORTED) {
00092 throw InvalidArgumentError("Can't change status from ABORTED in Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00093 }
00094 if (data.status == TestData::EXECUTING && status == TestData::COMPLETED && scans.size() != data.nScans) {
00095 throw InvalidArgumentError("Can't enter COMPLETED state when not all Scans have been added in Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00096 }
00097 if (data.status == TestData::COMPLETED && status != TestData::COMPLETED) {
00098 throw InvalidArgumentError("Can't change status from COMPLETED in Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00099 }
00100 if (data.status != status && (status == TestData::COMPLETED || status == TestData::ABORTED)) {
00101 string endTime = to_iso_string(second_clock::universal_time());
00102 data.endTime = endTime.substr(0, 15);
00103 }
00104
00105 data.status = status;
00106 publish();
00107 }
00108
00109 void Test::publish() {
00110 string name = SctNames::getControlDataName();
00111 name += ".";
00112 name += (string)getUniqueID();
00113 std::cout << "Publishing " << name << std::endl;
00114 Sct::ISUtilities::addOrUpdateOrMessage(name, data, __FILE__, __LINE__, MRS_WARNING);
00115 }
00116
00117 void Test::withdraw() {
00118 string name = SctNames::getControlDataName();
00119 name += ".";
00120 name += (string)getUniqueID();
00121 ISInfoDictionary& is = SctNames::getISDictionary();
00122 ISInfo::Status s;
00123
00124 s = is.remove(name.c_str());
00125 if (s == ISInfo::CommFailure) {
00126 IsException error(s, "Test::withdraw failed", __FILE__, __LINE__);
00127 error.sendToMrs(MRS_WARNING);
00128 }
00129 }
00130
00131 }