ParamOutputIntervalTrueFile.cc 3.67 KB
/**
 * ParamOutputIntervalTrueFile.cc
 *
 *  Created on: 18 oct. 2012
 *      Author: AKKA IS
 */

#include <string>
#include <iostream>

#include <iomanip>
#include <string.h>

#include "PrintIntervalTrue.hh"
#include "ParamOutputIntervalTrueFile.hh"

#include "DicError.hh"

#include "ParameterManager.hh"
#include "Parameter.hh"
#include "ParamData.hh"
#include "TimeUtil.hh"
#include "Helper.hh"

// Download module include
#include "Config.hh"
#include "DD_time.hh"

#include "TimeInterval.hh"

using namespace AMDA::Parameters;

namespace AMDA {
	namespace ParamOutputImpl {
		namespace Download {

		 log4cxx::LoggerPtr gLogger = log4cxx::Logger::getLogger("AMDA-Kernel.XMLConfigurator");

			ParamOutputIntervalTrueFile::ParamOutputIntervalTrueFile(ParameterManager& pParameterManager) :
					ParamOutput(pParameterManager),
					_typeFormatTime(FORMAT_OUTPUT_TIME_DOYTIME),
					_firstVisit(true){
				_lFileName.str("");
			}

			ParamOutputIntervalTrueFile::~ParamOutputIntervalTrueFile() {
			}

			void ParamOutputIntervalTrueFile::establishConnection() {
				_parameter = _parameterManager.getSampledParameter(_paramName, _samplingMode, _samplingValue, _gapThreshold, true);
				if(_parameter == NULL)  {
					LOG4CXX_ERROR(_logger,"ParamOutput::init parameter : \""<< _paramName <<"\" Not Exist" );
					BOOST_THROW_EXCEPTION( ParamOutput_exception());
				}
				getParameter()->openConnection(this);
			}

			void ParamOutputIntervalTrueFile::init() {
				getParameter()->init(this, _timeIntervalList);
			}

			void ParamOutputIntervalTrueFile::apply() {


				_paramDataIndexInfo = _parameter->getAsync(this).get();

				initOutput();
				_printIntervalTrue->setParamDataIndexInfo(_paramDataIndexInfo);

				while ((!_paramDataIndexInfo._noMoreTimeInt && !_paramDataIndexInfo._timeIntToProcessChanged) || (_paramDataIndexInfo._nbDataToProcess > 0)) {
					try {
						_parameter->getParamData(this)->accept(*_printIntervalTrue);
						if (_paramDataIndexInfo._timeIntToProcessChanged || _paramDataIndexInfo._noMoreTimeInt)
							break;
						_paramDataIndexInfo = _parameter->getAsync(this).get();
						_printIntervalTrue->setParamDataIndexInfo(_paramDataIndexInfo);
					} catch(AMDA::AMDA_exception & e) {
						e << AMDA::errno_code(AMDA_PARAM_OUTPUT_ERR);
						throw;
					}
				}
				endOutput();
			}



			void ParamOutputIntervalTrueFile::initOutput() {

				LOG4CXX_DEBUG(_logger, "ParamOutput init  ASCII file creation");
				createCurrentfile();
				_printIntervalTrue = PrintIntervalTrueSPtr(new PrintIntervalTrue(_paramDataIndexInfo, _currentfile));
			}

			void ParamOutputIntervalTrueFile::endOutput() {
				_currentfile << std::endl;
				_currentfile.close();
				LOG4CXX_INFO(_logger, "file " << _lFileName.str() << " is created");
			}

			void ParamOutputIntervalTrueFile::createCurrentfile() {
				char buffer[TIMELENGTH];
				Double2DD_Time(buffer, _timeIntervalList->front()._startTime);

				_lFileName << "outputInt-" << _paramName;
				if (!_indexList.empty()) {
					for (unsigned int i = 0; i < _indexList.size(); ++i) {
						_lFileName << "_" << _indexList[i];
					}
				}
				int ret = AMDA::Helpers::Helper::mkdir(_workPath.c_str());
				if(ret) {
					const size_t buflen = 250;
					char buf[250];
					LOG4CXX_ERROR(_logger, "Create working directory error : " << strerror_r(ret, buf,  buflen));
					BOOST_THROW_EXCEPTION( ParamOutput_exception());
				}
				_lFileName << "_" << buffer << ".txt";
				std::string lComputedFileName = _workPath + "/" +_lFileName.str();
				LOG4CXX_INFO(_logger, "Create outputInt-paramid_starttime.txt file: " << lComputedFileName);
				_currentfile.open(lComputedFileName.c_str());
			}

		} // namespace Download
	} // namespace ParamOutputImpl
} /* namespace AMDA */