GetDDBaseNode.cc 4.32 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)
			}

			//build unique param id
			std::string paramId = std::string((const char*) lVIName);
			std::replace( paramId.begin(), paramId.end(), ':', '_');
			paramId += "_";
			paramId += paramNameStr;

			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);
				}

				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 */