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