GetDDBaseNode.cc 8.1 KB
/*
 * GetDDBaseNode.cc
 *
 *  Created on: Oct 31, 2012
 *      Author: f.casimir
 */
//#include <boost/scope_exit.hpp>
//#include "ScopeExit.hh"
#include "ServicesServer.hh"
#include "Parameter.hh"
#include "FileConfigurator.hh"
#include "ParamGet.hh"
//#include "ProcessNode.hh"
#include "ParamGetDDBase.hh"
#include "ParameterManager.hh"

using namespace AMDA::Parameters;

#include "Constant.hh"

#include "Config.hh"
#include "GetDDBaseNode.hh"

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace XMLParameterConfigurator {

class DDCalibrationNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	/**
	 * @brief read Calibration info name vi/baseParam/clb@name.
	 */
	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "CalibrationNode::proceed")
		ParamGetDDBase* lParamGet = context.get<ParamGetDDBase*>();
		xmlChar* lParamName = NULL;

		try {

			if ((lParamName = xmlGetProp(pNode, (const xmlChar *) "name"))) {
				lParamGet->createInfoRequest((const char*) lParamName);
			} else {
				ERROR_EXCEPTION(
						ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "name")
			}

		} catch (...) {
			if (lParamName) {
				xmlFree(lParamName);
			}
			throw;
		}
		if (lParamName) {
			xmlFree(lParamName);
		}
	}
};
class BaseParamNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	BaseParamNode() : AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["clb"]=NodeCfgSPtr(new DDCalibrationNode());
	}

	/**
	 * @brief read Parameter name vi/baseParam@name and child.
	 */
	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext) {
		LOG4CXX_DEBUG(gLogger,	"NodeGetDDBase::proceed: '" << pNode->name << "' node")

		// Context setting
		ServicesServer* lServicesServer = pContext.get<ServicesServer*>();
		Parameter* lParentParameter = pContext.get<Parameter*>();
		ParameterManager* lParameterManager =  pContext.get<ParameterManager*>();
		xmlChar*lVIName = pContext.get<xmlChar*>();

		// Attributes list
		try {

			xmlChar* lParamName = xmlGetProp(pNode, (const xmlChar *) PARAMNAME);
			std::string paramNameStr;
			if (lParamName) {
				paramNameStr = std::string((const char*) lParamName);
				xmlFree(lParamName);
			}
			else {
				ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << PARAMNAME)
			}

			xmlChar* lDim3NumValue = xmlGetProp(pNode, (const xmlChar *) "dim3Num");
			std::string paramDim3NumStr;
			if (lDim3NumValue != NULL) {
				paramDim3NumStr = std::string((const char*) lDim3NumValue);
				xmlFree(lDim3NumValue);
 			}

			xmlChar* lDim3CutIndexValue = xmlGetProp(pNode, (const xmlChar *) "dim3CutIndex");
			std::string paramDim3CutIndexStr;
			if (lDim3CutIndexValue != NULL) {
				paramDim3CutIndexStr = std::string((const char*) lDim3CutIndexValue);
				xmlFree(lDim3CutIndexValue);
			}
                        
                        		xmlChar* lMinSumIndexValue = xmlGetProp(pNode, (const xmlChar *) "minSumIndex");
			std::string paramMinSumIndexStr;
			if (lMinSumIndexValue != NULL) {
				paramMinSumIndexStr = std::string((const char*) lMinSumIndexValue);
				xmlFree(lMinSumIndexValue);
			}
                        
                                                	xmlChar* lMaxSumIndexValue = xmlGetProp(pNode, (const xmlChar *) "maxSumIndex");
			std::string paramMaxSumIndexStr;
			if (lMaxSumIndexValue != NULL) {
				paramMaxSumIndexStr = std::string((const char*) lMaxSumIndexValue);
				xmlFree(lMaxSumIndexValue);
			}

			//build unique param id
			std::string paramId = std::string((const char*) lVIName);
			std::replace( paramId.begin(), paramId.end(), ':', '_');
			paramId += "_";
			paramId += paramNameStr;
                                                            if( ( !paramDim3CutIndexStr.empty()  &&  !paramMinSumIndexStr.empty() ) ||
                                                                 ( !paramDim3CutIndexStr.empty()  &&  !paramMaxSumIndexStr.empty() ) ){
                                                                              ERROR_EXCEPTION( "Too much attributes: CUT and SUM")
                                                            }
			if (!paramDim3NumStr.empty()) {
                                                                       if (!paramDim3CutIndexStr.empty()) {
				paramId += "_CUT_";
				paramId += paramDim3NumStr;
				paramId += "_";
				paramId += paramDim3CutIndexStr;
		                               }else if( !paramMinSumIndexStr.empty() && !paramMaxSumIndexStr.empty() ){
                                                   		paramId += "_SUM_";
				paramId += paramDim3NumStr;
                                                                                paramId += "_";
				paramId += paramMinSumIndexStr;
                                                                                paramId += "_";
				paramId += paramMaxSumIndexStr;
                                                                                }
                                                                     }
			
			lParameterManager->applyParamIdCorrection(paramId);

			ParameterSPtr lParameter;
			if ( lParameterManager->addParameter(lParentParameter,paramId,lParameter)) {
				const char *lSouceParamGet = "DDBASE";
				ParamGetDDBaseSPtr lParamGet(dynamic_cast<ParamGetDDBase*>(lServicesServer->getParamGet( lSouceParamGet, *lParameter)));
				DataWriterSPtr lDataWriter( lParamGet);

				std::string* xmlFileName = pContext.get<std::string*>();
				lDataWriter->setSignatureTrigger(*xmlFileName);
				lParamGet->setParName(paramNameStr.c_str());
				lParameter->setDataWriter(lDataWriter);
				lParamGet->setViName((const char*) lVIName);
				AMDA::Parameters::CfgContext lContext(pContext);
				lContext.push<ParamGetDDBase*>(lParamGet.get());

				// useNearestValue
				xmlChar* lUseNearestValue = xmlGetProp(pNode, (const xmlChar *) "useNearestValue");
				if (lUseNearestValue != NULL)
				{
					lParamGet->setUseNearestValue(strcmp((const char*)lUseNearestValue,"true") == 0);
					xmlFree(lUseNearestValue);
				}

				// maxDim1Size
				xmlChar* lmaxDim1Size = xmlGetProp(pNode, (const xmlChar *) "maxDim1Size");
				if (lmaxDim1Size != NULL)
				{
					lParamGet->setMaxDim1Size(atoi((const char*)lmaxDim1Size));
					xmlFree(lmaxDim1Size);
				}

				// maxDim2Size
				xmlChar* lmaxDim2Size = xmlGetProp(pNode, (const xmlChar *) "maxDim2Size");
				if (lmaxDim2Size != NULL)
				{
					lParamGet->setMaxDim2Size(atoi((const char*)lmaxDim2Size));
					xmlFree(lmaxDim2Size);
				}

				// maxDim3Size
				xmlChar* lmaxDim3Size = xmlGetProp(pNode, (const xmlChar *) "maxDim3Size");
				if (lmaxDim3Size != NULL)
				{
					lParamGet->setMaxDim3Size(atoi((const char*)lmaxDim3Size));
					xmlFree(lmaxDim3Size);
				}

				// dim3Num
				if (!paramDim3NumStr.empty())
				{
					lParamGet->setDim3Num(atoi(paramDim3NumStr.c_str()));
				}

				// dim3CutIndex
				if (!paramDim3CutIndexStr.empty())
				{
					lParamGet->setDim3CutIndex(atoi(paramDim3CutIndexStr.c_str()));
				}
                                
 				// minSumIndex
				if (!paramMinSumIndexStr.empty())
				{
					lParamGet->setMinSumIndex(atoi(paramMinSumIndexStr.c_str()));
				}
                                		                     // maxSumIndex
				if (!paramMaxSumIndexStr.empty())
				{
					lParamGet->setMaxSumIndex(atoi(paramMaxSumIndexStr.c_str()));
				}
                                

				NodeGrpCfg::proceed(pNode, lContext);
			}


		} catch (...) {
			throw;
		}
	}
};

GetDDBaseNode::GetDDBaseNode() : NodeGrpCfg() {
	getChildList()["baseParam"]=NodeCfgSPtr(new XMLParameterConfigurator::BaseParamNode());
}

void GetDDBaseNode::proceed(xmlNodePtr pNode,	const AMDA::Parameters::CfgContext& pContext) {
	LOG4CXX_DEBUG(gLogger, "ViNode::proceed")
	xmlChar* lVIName = NULL;
	try {
		if (!(lVIName = xmlGetProp(pNode, (const xmlChar *) "name"))) {
			ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "name")
		}

		AMDA::Parameters::CfgContext lContext(pContext);
		lContext.push<xmlChar*>(lVIName);

		NodeGrpCfg::proceed(pNode, lContext);
	} catch (...) {
		if (lVIName) {
			xmlFree(lVIName);
		}
		throw;
	}
	if (lVIName) {
		xmlFree(lVIName);
	}
}

}/* namespace XMLParameterConfigurator */
} /* namespace AMDA */