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