GetLocalFileNode.cc 6.56 KB
/*
 * GetLocalFileNode.cc
 *
 *  Created on: Nov 22, 2014
 *      Author: AKKA
 */

#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>

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

using namespace AMDA::Parameters;

#include "Constant.hh"

#include "Config.hh"
#include "GetLocalFileNode.hh"
#include "LocalParamData.hh"

using namespace AMDA::XMLConfigurator;

namespace AMDA {
namespace XMLParameterConfigurator {

class LocalCalibrationNode: public AMDA::XMLConfigurator::NodeCfg {
public:
	/**
	 * @brief read Calibration info name localvi/param/clb@name.
	 */
	void proceed(xmlNodePtr pNode,
			const AMDA::Parameters::CfgContext& context) {
		LOG4CXX_DEBUG(gLogger, "LocalCalibrationNode::proceed");
		AMDA::LocalFileInterface::ParamGetLocalFile* lParamGet = context.get<AMDA::LocalFileInterface::ParamGetLocalFile*>();
		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 LocalFileParamNode: public AMDA::XMLConfigurator::NodeGrpCfg {
public:
	LocalFileParamNode() : AMDA::XMLConfigurator::NodeGrpCfg() {
		getChildList()["clb"]=NodeCfgSPtr(new LocalCalibrationNode());
	}

	void proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext)
	{
		LOG4CXX_DEBUG(gLogger,	"LocalFileParamNode::proceed: '" << pNode->name << "' node")

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

		// Attributes list

		try {
			xmlChar* lParamId = xmlGetProp(pNode, (const xmlChar*)"id");
			std::string paramIdStr;
			if (lParamId) {
				paramIdStr = std::string((const char*) lParamId);
				xmlFree(lParamId);
			}
			else {
				ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@id")
			}

			//build unique param id
			std::string paramId = "localfile_";
			paramId += std::string((const char*) lVIId);
			paramId += "_";
			paramId += paramIdStr;

			lParameterManager->applyParamIdCorrection(paramId);

			ParameterSPtr lParameter;
			if ( lParameterManager->addParameter(lParentParameter,paramId,lParameter)) {
				const char *lSouceParamGet = "LOCALFILE";
				AMDA::LocalFileInterface::ParamGetLocalFileSPtr lParamGet(dynamic_cast<AMDA::LocalFileInterface::ParamGetLocalFile*>(lServicesServer->getParamGet( lSouceParamGet, *lParameter)));
				DataWriterSPtr lDataWriter( lParamGet);

				std::string* xmlFileName = pContext.get<std::string*>();
				lDataWriter->setSignatureTrigger(*xmlFileName);
				lParamGet->setParamId(paramIdStr.c_str());
				lParameter->setDataWriter(lDataWriter);
				lParamGet->setVIId((const char*) lVIId);

				//type
				xmlChar* value = NULL;
				if ((value = xmlGetProp(pNode, (const xmlChar*)"type")) != NULL)
				{
					if (strcmp((const char*)value,"float") == 0)
						lParamGet->setParamType(AMDA::LocalFileInterface::LocalParamType::TYPE_FLOAT);
					else if (strcmp((const char*)value,"double") == 0)
						lParamGet->setParamType(AMDA::LocalFileInterface::LocalParamType::TYPE_DOUBLE);
					else if (strcmp((const char*)value,"short") == 0)
						lParamGet->setParamType(AMDA::LocalFileInterface::LocalParamType::TYPE_SHORT);
					else if (strcmp((const char*)value,"int") == 0)
						lParamGet->setParamType(AMDA::LocalFileInterface::LocalParamType::TYPE_INT);
					else
					{
						LOG4CXX_ERROR(gLogger, "LocalFileParamNode::proceed - Unknown data type " << ((const char*)value))
					}
					xmlFree(value);
				}

				//size
				if ((value = xmlGetProp(pNode, (const xmlChar*)"size")) != NULL)
				{
					std::vector<std::string> dims;
					std::string dimsDef = (const char*)value;
					boost::split(dims, dimsDef, boost::is_any_of("*"));

					if (dims.size() == 1)
					{
						lParamGet->setParamDim1Size(std::stoi(dims[0]));
					}
					else if (dims.size() == 2)
					{
						lParamGet->setParamDim1Size(std::stoi(dims[0]));
						lParamGet->setParamDim2Size(std::stoi(dims[1]));
					}
					xmlFree(value);
				}

				//minSampling
				if ((value = xmlGetProp(pNode, (const xmlChar*)"minSampling")) != NULL)
				{
					lParamGet->setParamMinSampling(atof((const char*)value));
					xmlFree(value);
				}

				//maxSampling
				if ((value = xmlGetProp(pNode, (const xmlChar*)"maxSampling")) != NULL)
				{
					lParamGet->setParamMaxSampling(atof((const char*)value));
					xmlFree(value);
				}

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

				//timeFormat
				if ((value = xmlGetProp(pNode, (const xmlChar*)"timeFormat")) != NULL)
				{
					if (std::strcmp((const char*)value, "ISO") == 0)
						lParamGet->setTimeFormat(AMDA::LocalFileInterface::LocalTimeFormat::ISO);
					else
					{
						LOG4CXX_WARN(gLogger, "LocalFileParamNode::proceed - Unknown time format " << (const char*)value);
						lParamGet->setTimeFormat(AMDA::LocalFileInterface::LocalTimeFormat::DOUBLE);
					}
					xmlFree(value);
				}
				else
					lParamGet->setTimeFormat(AMDA::LocalFileInterface::LocalTimeFormat::DOUBLE);

				AMDA::Parameters::CfgContext lContext(pContext);
				lContext.push<AMDA::LocalFileInterface::ParamGetLocalFile*>(lParamGet.get());
				NodeGrpCfg::proceed(pNode, lContext);
			}
		} catch (...) {
			throw;
		}
	}
};

GetLocalFileNode::GetLocalFileNode() : NodeGrpCfg()
{
	getChildList()["param"] = NodeCfgSPtr(new XMLParameterConfigurator::LocalFileParamNode());
}

void GetLocalFileNode::proceed(xmlNodePtr pNode, const AMDA::Parameters::CfgContext& pContext)
{
	LOG4CXX_DEBUG(gLogger, "GetLocalFileNode::proceed")
	xmlChar* lVIId = NULL;
	try {
		if (!(lVIId = xmlGetProp(pNode, (const xmlChar *) "id"))) {
			ERROR_EXCEPTION(ERROR_MANDATORY_ATTRIBUTE_MISSING << pNode->name << "@" << "id")
		}
		AMDA::Parameters::CfgContext lContext(pContext);
		lContext.push<xmlChar*>(lVIId);
		NodeGrpCfg::proceed(pNode, lContext);
	} catch (...) {
		if (lVIId) {
			xmlFree(lVIId);
		}
		throw;
	}
	if (lVIId) {
		xmlFree(lVIId);
	}
}

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