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