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::setStatus(TestData::status_E status) {
00085
00086 if (data.status == TestData::ABORTED && status != TestData::ABORTED) {
00087 throw InvalidArgumentError("Can't change status from ABORTED in Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00088 }
00089 if (data.status == TestData::EXECUTING && status == TestData::COMPLETED && scans.size() != data.nScans) {
00090 throw InvalidArgumentError("Can't enter COMPLETED state when not all Scans have been added in Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00091 }
00092 if (data.status == TestData::COMPLETED && status != TestData::COMPLETED) {
00093 throw InvalidArgumentError("Can't change status from COMPLETED in Test: " + (string)getUniqueID(), __FILE__, __LINE__);
00094 }
00095 if (data.status != status && (status == TestData::COMPLETED || status == TestData::ABORTED)) {
00096 string endTime = to_iso_string(second_clock::universal_time());
00097 data.endTime = endTime.substr(0, 15);
00098 }
00099
00100 data.status = status;
00101 publish();
00102 }
00103
00104 void Test::publish() {
00105 string name = SctNames::getControlDataName();
00106 name += ".";
00107 name += (string)getUniqueID();
00108 Sct::ISUtilities::addOrUpdateOrMessage(name, data, __FILE__, __LINE__, MRS_DIAGNOSTIC);
00109 }
00110
00111 void Test::withdraw() {
00112 string name = SctNames::getControlDataName();
00113 name += ".";
00114 name += (string)getUniqueID();
00115 ISInfoDictionary& is = SctNames::getISDictionary();
00116 ISInfo::Status s;
00117
00118 s = is.remove(name.c_str());
00119 if (s == ISInfo::CommFailure) {
00120 IsException error(s, "Test::withdraw failed", __FILE__, __LINE__);
00121 error.sendToMrs(MRS_DIAGNOSTIC);
00122 }
00123 }
00124
00125 }