ParamGetConstant.cc 5.83 KB
/*
 * ParamGetConstant.cc
 *
 *  Created on: 18 sep. 2018
 *      Author: AKKA
 */

#include "ParamGetConstant.hh"

#include "ConstantInterfaceConfig.hh"

#include <stdlib.h>

#include "Parameter.hh"
#include "ParamData.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "DicError.hh"
#include "TimeUtil.hh"
#include "Helper.hh"

namespace AMDA {

namespace ConstantInterface {

ParamGetConstant::ParamGetConstant(Parameter &parameter) :
		ParamGet_CRTP<ParamGetConstant>(parameter), _sampling(0.), _type(TYPE_FLOAT), _container(CONTAINER_SCALAR), _value(0.), _dim1(1), _dim2(1), _pusher(NULL)
{

}

ParamGetConstant::ParamGetConstant(const ParamGetConstant &pParamGetConstant, Parameter &parameter) :
		ParamGet_CRTP<ParamGetConstant>(pParamGetConstant, parameter),
		_sampling(pParamGetConstant._sampling), _type(pParamGetConstant._type), _container(pParamGetConstant._container), _value(pParamGetConstant._value), _dim1(pParamGetConstant._dim1), _dim2(pParamGetConstant._dim2),
		_pusher(pParamGetConstant._pusher)
{

}

ParamGetConstant::~ParamGetConstant()
{
	//delete the pusher if needed
	if (_pusher != NULL)
		delete _pusher;
}

TimeStamp ParamGetConstant::init()
{
	LOG4CXX_DEBUG(gLogger, "ParamGetConstant::init");

	if (_dim1 * _dim2 == 1) {
		_container = CONTAINER_SCALAR;
	}
	else if ((_dim1 > 1) && (_dim2 > 1)) {
		_container = CONTAINER_MATRIX;
	}
	else {
		_container = CONTAINER_VECTOR;
	}

	_currentInterval = _timeIntervalList->begin();

	//create pusher
	switch (_container) {
		case CONTAINER_SCALAR :
			switch (_type) {
				case TYPE_FLOAT :
					_pusher = new Pusher<TYPE_FLOAT, CONTAINER_SCALAR>(_sampling, _value);
					break;
				case TYPE_DOUBLE :
					_pusher = new Pusher<TYPE_DOUBLE, CONTAINER_SCALAR>(_sampling, _value);
					break;
				case TYPE_SHORT :
					_pusher = new Pusher<TYPE_SHORT, CONTAINER_SCALAR>(_sampling, _value);
					break;
				case TYPE_INT :
					_pusher = new Pusher<TYPE_INT, CONTAINER_SCALAR>(_sampling, _value);
					break;
			}
			break;
		case CONTAINER_VECTOR :
			switch (_type) {
				case TYPE_FLOAT :
					_pusher = new Pusher<TYPE_FLOAT, CONTAINER_VECTOR>(_sampling, _value, _dim1);
					break;
				case TYPE_DOUBLE :
					_pusher = new Pusher<TYPE_DOUBLE, CONTAINER_VECTOR>(_sampling, _value, _dim1);
					break;
				case TYPE_SHORT :
					_pusher = new Pusher<TYPE_SHORT, CONTAINER_VECTOR>(_sampling, _value, _dim1);
					break;
				case TYPE_INT :
					_pusher = new Pusher<TYPE_INT, CONTAINER_VECTOR>(_sampling, _value, _dim1);
					break;
			}
			break;
		case CONTAINER_MATRIX :
			switch (_type) {
				case TYPE_FLOAT :
					_pusher = new Pusher<TYPE_FLOAT, CONTAINER_MATRIX>(_sampling, _value, _dim1, _dim2);
					break;
                                case TYPE_DOUBLE :
					_pusher = new Pusher<TYPE_DOUBLE, CONTAINER_MATRIX>(_sampling, _value, _dim1, _dim2);
					break;
                                case TYPE_SHORT :
					_pusher = new Pusher<TYPE_SHORT, CONTAINER_MATRIX>(_sampling, _value, _dim1, _dim2);
					break;
                                case TYPE_INT :
					_pusher = new Pusher<TYPE_INT, CONTAINER_MATRIX>(_sampling, _value, _dim1, _dim2);
					break;
			}
			break;
	}

	if (_pusher == NULL)
	{
		BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_TYPE_DATA_UNKNOWN) << AMDA::ex_msg(std::string("Cannot create param data")));
	}

	//set link to the param data
	_paramData = ParamDataSPtr(_pusher->getParamData());
	_paramData->setMinSampling(_sampling);

	TimeStamp timeStamp = 0;
	if (_signatureTrigger != "") {
		// _signatureTrigger must be a name of xml parameter file
		timeStamp = AMDA::Helpers::Helper::dateOfFile(
				_signatureTrigger.c_str());
	}
        
        setGlobalStart(0.);
	// 01/01/2038
        setGlobalStop(2145913200.);

	return timeStamp;
}


unsigned int ParamGetConstant::write()
{
	unsigned int result = 0;

	if (_currentInterval == _timeIntervalList->end()) {
		_paramData->getIndexInfo()._noMoreTimeInt = true;
		return result;
	}

	//get packet and push it in ParamData
	LOG4CXX_DEBUG(gLogger, "ParamGetConstant::write ");
        int crtRefIndex = 0;
        if (!_paramData->getIndexInfo()._endTimeIntIndexList.empty()) {
            crtRefIndex = _paramData->getIndexInfo()._endTimeIntIndexList.back();
        }
        
	result += _pusher->put(_currentInterval->_startTime, _currentInterval->_stopTime, _paramData->getDataNumber() - crtRefIndex);

	// Push up the information if all time interval was processed.
	_paramData->getIndexInfo()._timeIntToProcessChanged = (result == 0);
	if (result == 0) {
		++_currentInterval;
		_paramData->getIndexInfo()._noMoreTimeInt =  (_currentInterval == _timeIntervalList->end());
	}

	_paramData->getIndexInfo()._nbDataToProcess = result;

	// if time interval changed store index which delimit the end of the time interval.
	if (_paramData->getIndexInfo()._timeIntToProcessChanged) {
		unsigned int lEndTimeIntIndex = _paramData->getIndexInfo()._nbDataToProcess;
		_paramData->getIndexInfo()._endTimeIntIndexList.push_back(lEndTimeIntIndex);
	}
	else {
		// Nothing to do.
	}

	return result;
}

/*
 * @overload DataWriter::getMinSampling
 */
double ParamGetConstant::getMinSampling()
{
	return _sampling;
}

void ParamGetConstant::updateInfo(Parameter & parameter)
{
	LOG4CXX_DEBUG(gLogger, "ParamGetConstant::updateInfo - " << parameter.getId());
	if (parameter.getInfoId().empty())
		parameter.setInfoId(parameter.getId());

	//Param info
	AMDA::Info::ParamInfoSPtr paramInfo = AMDA::Info::ParamMgr::getInstance()->getParamInfoFromId(parameter.getInfoId(),true);

	if (paramInfo == nullptr)
		return;

	paramInfo->setFillValue(NAN);

	//Add parameter info id as parameter name if no exist
	//if (paramInfo->getName().empty())
	//	paramInfo->setName(_paramId);

	//Add parameter info id as parameter short name if no exist
	//if (paramInfo->getShortName().empty())
	//	paramInfo->setShortName(_paramId);
}

} /* namespace ConstantInterface */
} /* namespace AMDA */