ParamNode.cc 3.38 KB
/**
 * ParamNode.cc
 *
 *  Created on: 18 oct. 2012
 *      Author: AKKA IS
 */

#include <sstream>

#include <boost/shared_ptr.hpp>

#include "Config.hh"

#include "ParameterManager.hh"
#include "Parameter.hh"
#include "ParamGet.hh"
#include "ServicesServer.hh"
#include "Parameter.hh"

#include "ParamNode.hh"
#include "ProcessNode.hh"
#include "CalibrationNode.hh"
#include "ManualCalibrationNode.hh"
#include "GetAMDAParamNode.hh"
#include "Constant.hh"

using namespace log4cxx;

#define strXMLFree(str)  if(str) { xmlFree(str); str=NULL; }

using namespace AMDA::Parameters;
using namespace AMDA::XMLConfigurator;

namespace AMDA {
	namespace XMLParameterConfigurator {

		class NodeTimeResolution: public AMDA::XMLConfigurator::NodeCfg {
		public:
			void proceed( xmlNodePtr pNode, const AMDA::Parameters::CfgContext& context) {
				LOG4CXX_DEBUG(gLogger, "NodeTimeResolution::proceed")
				Parameter* p = context.get<Parameter*>();
				if ( pNode->children && pNode->children->content && pNode->children->content[0] != '0') {
					p->setTimeResolution(atof((char*)pNode->children->content));
				}
			}
		};

		class NodeGapThreshold: public AMDA::XMLConfigurator::NodeCfg {
		public:
			void proceed( xmlNodePtr pNode, const AMDA::Parameters::CfgContext& context) {
				LOG4CXX_DEBUG(gLogger, "NodeGapThreshold::proceed")
				Parameter* p = context.get<Parameter*>();
				if ( pNode->children && pNode->children->content && pNode->children->content[0] != '0') {
					p->setGapThreshold(atof((char*)pNode->children->content));
				}
			}
		};


		  ParamNode::ParamNode() : AMDA::XMLConfigurator::NodeGrpCfg()
			{
				LOG4CXX_DEBUG(gLogger, "ParamNode Constructor")
				getChildList()["time_resolution"]=NodeCfgSPtr(new NodeTimeResolution);
				getChildList()["gap_threshold"]=NodeCfgSPtr(new NodeGapThreshold);
				getChildList()["process"]=NodeCfgSPtr(new ProcessNode());
				getChildList()["clbManual"]=NodeCfgSPtr(new ManualCalibrationNode());
				getChildList()["clbProcess"]=NodeCfgSPtr(new CalibrationNode());
				NodeCfgSPtr getNode = NodeCfgSPtr(new NodeGrpCfg);
				getChildList()["get"]=getNode;
				addNodeParser("get/amdaParam",NodeCfgSPtr(new GetAMDAParamNode));

				// Output group node
				NodeGrpCfg* lOutputNode = new NodeGrpCfg();
				getChildList()["output"] = NodeCfgSPtr(lOutputNode);
			}

		  ParamNode::~ParamNode()
			{
				LOG4CXX_DEBUG(gLogger, "~ParamNode")
			}

		void ParamNode::proceed(xmlNodePtr pNode,		const AMDA::Parameters::CfgContext& pContext)
		{
			LOG4CXX_DEBUG(gLogger, "ParamNode::proceed")
			//ServicesServer* lServicesServer = pContext.get<ServicesServer*>();
			ParameterManager* lParameterManager =  pContext.get<ParameterManager*>();
			xmlChar* lParamName = NULL;

			try {
				if (!(lParamName = xmlGetProp(pNode, (const xmlChar *) "id"))) {
					ERROR_EXCEPTION(
							ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << PARAMNAME)
				}
				ParameterSPtr parameter;
				if ( lParameterManager->addParameter(NULL, std::string((char*)lParamName), parameter)) {
					parameter->setInfoId(std::string((char*)lParamName));
					AMDA::Parameters::CfgContext lContext(pContext);
					lContext.push<Parameter*>(parameter.get());
					NodeGrpCfg::proceed(pNode, lContext);
				}

			} catch (...) {
				if (lParamName) {
					xmlFree(lParamName);
				}
				throw;
			}
			if (lParamName) {
				xmlFree(lParamName);
			}
		}

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