Blame view

src/ParamGetImpl/SpeasyProxyInterface/ParamGetSpeasyProxy.cc 8.9 KB
ac860015   Erdogan Furkan   For now - TBC
1
2
3
4
5
6
7
8
9
10
11
/*
 * ParamGetSpeasyProxy.cc
 *
 *  Created on: April 25, 2024
 *  Author: AKKODIS - Furkan
 */

#include "SpeasyProxyInterfaceConfig.hh"
#include "ParamGetSpeasyProxy.hh"
// #include "VirtualInstrumentManager.hh"

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
12
13
#include "FileReaderCDF.hh"

ac860015   Erdogan Furkan   For now - TBC
14
15
16
17
18
19
20
21
#include <stdlib.h>

#include "Parameter.hh"
#include "ParamData.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "DicError.hh"
#include "TimeUtil.hh"
f9eb7c3a   Erdogan Furkan   For now
22
#include <fstream>
ac860015   Erdogan Furkan   For now - TBC
23
24
25
26
27
28
29
30
31
32
33
#include "Helper.hh"
#define CURL_STATICLIB
#include <curl/curl.h>
#include <curl/easy.h>

namespace AMDA {

namespace SpeasyProxyInterface {

ParamGetSpeasyProxy::ParamGetSpeasyProxy(Parameter &parameter) :
		ParamGet_CRTP<ParamGetSpeasyProxy>(parameter),
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
34
		_paramId(""),_type(TYPE_FLOAT),_dim1(1), _dim2(1), _minSampling(1), _container(CONTAINER_SCALAR),_timeStamp(0) 
ac860015   Erdogan Furkan   For now - TBC
35
36
37
38
39
40
{

}

ParamGetSpeasyProxy::ParamGetSpeasyProxy(const ParamGetSpeasyProxy &pParamGetSpeasyProxy, Parameter &parameter) :
		ParamGet_CRTP<ParamGetSpeasyProxy>(pParamGetSpeasyProxy, parameter),
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
41
42
43
44
45
		_paramId(pParamGetSpeasyProxy._paramId), _type(pParamGetSpeasyProxy._type),
		_dim1(pParamGetSpeasyProxy._dim1), _dim2(pParamGetSpeasyProxy._dim2),
		_minSampling(pParamGetSpeasyProxy._minSampling), _container(pParamGetSpeasyProxy._container),
		_timeStamp(pParamGetSpeasyProxy._timeStamp)
		 
ac860015   Erdogan Furkan   For now - TBC
46
47
48
49
50
51
52
53
54
55
56
{

}

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

f9eb7c3a   Erdogan Furkan   For now
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
std::string ParamGetSpeasyProxy::readFile(const std::string& filename) {
    // Create an input file stream
    std::ifstream file(filename);

    // Check if the file stream was successfully opened
    if (!file.is_open()) {
        throw std::runtime_error("Could not open file");
    }

    // Use a stringstream to read the entire file into a string
    std::stringstream buffer;
    buffer << file.rdbuf();

    // Close the file stream
    file.close();

    // Return the contents of the file as a string
    return buffer.str();
}


ac860015   Erdogan Furkan   For now - TBC
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Downloads timetable file in tmp directory.
 */
std::string ParamGetSpeasyProxy::download(const std::string& pPath) {
	std::string localPath;
	std::string tmpPath(pPath);
	std::transform(tmpPath.begin(), tmpPath.end(), tmpPath.begin(), ::tolower);
	if (!boost::starts_with(tmpPath, "http:")
			&& !boost::starts_with(tmpPath, "https:")) {
		return pPath;
	}
	// download file
	CURL *pCurl;
	CURLcode codes;
	const char *url = pPath.c_str();
	// get tt name to create temp file as tmp_<ttdistantfilename>
	size_t endOfPath = pPath.find_last_of("/");
	if (endOfPath == std::string::npos) {
		endOfPath = pPath.find_last_of("=/\\");
	}
	std::string tmpFile = "./tmp_" + pPath.substr(endOfPath + 1);
	// do download
	pCurl = curl_easy_init();
	if (pCurl) {
		FILE *fptr = fopen(tmpFile.c_str(), "wb");
		if (fptr) {
			curl_easy_setopt(pCurl, CURLOPT_URL, url);
			curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, write_data);
			curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, false);
			curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, false);
			curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, fptr);
			// create buffer to get potential error string
			std::vector<char> errBuf(1024);
			curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, &errBuf[0]);
			codes = curl_easy_perform(pCurl);
			curl_easy_cleanup(pCurl);
			fclose(fptr);
			if (codes == CURLE_OK) {
				localPath = tmpFile;
			} else {
f9eb7c3a   Erdogan Furkan   For now
118
119
				
				std::string str(errBuf.begin(), errBuf.end());
ac860015   Erdogan Furkan   For now - TBC
120
				LOG4CXX_ERROR(_logger,
f9eb7c3a   Erdogan Furkan   For now
121
						"Unable to download " + pPath + " : "  << str );// errBuf[0]);
ac860015   Erdogan Furkan   For now - TBC
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
			}
		} else {
			LOG4CXX_ERROR(_logger,
					"Unable to download " + pPath + " : not found.");
		}

	} else {
		LOG4CXX_ERROR(_logger,
				"Unable to download " + pPath
						+ " : cUrl cannot be initialized.");
	}
	// else, do nothing
	// return local file or empty string if not downloaded
	return localPath;
}


TimeStamp ParamGetSpeasyProxy::init()
{
	LOG4CXX_DEBUG(gLogger, "ParamGetSpeasyProxy::init");
ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
	
	// Ici, instanciation du Pusher (en fonction du type et des dim définis dans le fichier XML)

	_currentInterval = _timeIntervalList->begin();

	// get the right container

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

	//create pusher
	switch (_container) {
		case CONTAINER_SCALAR :
			switch (_type) {
				case TYPE_FLOAT :
					_pusher = new Pusher<TYPE_FLOAT, CONTAINER_SCALAR>();
					break;
				case TYPE_DOUBLE :
					_pusher = new Pusher<TYPE_DOUBLE, CONTAINER_SCALAR>();
					break;
				case TYPE_SHORT :
					_pusher = new Pusher<TYPE_SHORT, CONTAINER_SCALAR>();
					break;
				case TYPE_INT :
					_pusher = new Pusher<TYPE_INT, CONTAINER_SCALAR>();
					break;
				default:
					LOG4CXX_ERROR(gLogger, "ParamGetSpeasyProxy::init() - Unknown type" << (const char*)_type);

			}
			break;
		case CONTAINER_VECTOR :
			switch (_type) {
				case TYPE_FLOAT :
					_pusher = new Pusher<TYPE_FLOAT, CONTAINER_VECTOR>(_dim1);
					break;
				case TYPE_DOUBLE :
					_pusher = new Pusher<TYPE_DOUBLE, CONTAINER_VECTOR>(_dim1);
					break;
				case TYPE_SHORT :
					_pusher = new Pusher<TYPE_SHORT, CONTAINER_VECTOR>(_dim1);
					break;
				case TYPE_INT :
					_pusher = new Pusher<TYPE_INT, CONTAINER_VECTOR>(_dim1);
					break;
				default:
					LOG4CXX_ERROR(gLogger, "ParamGetSpeasyProxy::init() - Unknown type" << (const char*)_type);
			}
			break;
		case CONTAINER_MATRIX :
			switch (_type) {
				case TYPE_FLOAT :
					_pusher = new Pusher<TYPE_FLOAT, CONTAINER_MATRIX>(_dim1, _dim2);
					break;
                                case TYPE_DOUBLE :
					_pusher = new Pusher<TYPE_DOUBLE, CONTAINER_MATRIX>(_dim1, _dim2);
					break;
                                case TYPE_SHORT :
					_pusher = new Pusher<TYPE_SHORT, CONTAINER_MATRIX>(_dim1, _dim2);
					break;
                                case TYPE_INT :
					_pusher = new Pusher<TYPE_INT, CONTAINER_MATRIX>(_dim1, _dim2);
					break;
				default:
					LOG4CXX_ERROR(gLogger, "ParamGetSpeasyProxy::init() - Unknown type" << (const char*)_type);
			}
			break;
		default:
			LOG4CXX_ERROR(gLogger, "ParamGetSpeasyProxy::init() - Unknown container format " << (const char*)_container);

	}
f9eb7c3a   Erdogan Furkan   For now
220

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
221
222
223
	//set link to the param data
	_paramData = ParamDataSPtr(_pusher->getParamData());
	_paramData->setMinSampling(_minSampling);
f9eb7c3a   Erdogan Furkan   For now
224

f9eb7c3a   Erdogan Furkan   For now
225

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
226
227
228
229
230
	if (_timeStamp == 0 && _signatureTrigger != "") {
		// _signatureTrigger must be a name of xml parameter file
		_timeStamp = AMDA::Helpers::Helper::dateOfFile(
				_signatureTrigger.c_str());
	}
f9eb7c3a   Erdogan Furkan   For now
231

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
232
	
ac860015   Erdogan Furkan   For now - TBC
233
234
235
236
237
238
239
240
	return _timeStamp;
}


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

ae3e8cdf   Erdogan Furkan   ParamGet of Speas...
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
	//std::string link = "http://172.200.0.14:6543/get_data?path=amda%2Fimf&start_time=2008-01-01T00%3A00%3A00&stop_time=2008-01-02T00%3A00%3A00&format=json&zstd_compression=false&pickle_proto=3";
	std::string localPath = "/home/amda_admin/AMDA/AMDA_Kernel/cdf_speasy.cdf";
	FileReaderCDF* fileReaderPtr = new FileReaderCDF();
	bool isOpen = fileReaderPtr->open(localPath);
	if(isOpen)
	{	
		std::string timeParamId  = "time";
		std::string croptedParamId = _paramId;
		std::string delim = "/";
		croptedParamId.erase(0, croptedParamId.find(delim) + delim.length());

		// Call to getParamPacketData to get CDF data

		SpeasyProxyParamDataPacket *packet= new SpeasyProxyParamDataPacket();
		packet->init(_container,_type,_dim1,_dim2);

		fileReaderPtr->getParamPacketData(timeParamId, croptedParamId, packet);

		
		// Call to the put function of pusher to initiate the ParamData
		// result == nb of data in ParamData
		result += _pusher->put(packet);

		// Push up the information if all time interval was processed.
		_paramData->getIndexInfo()._timeIntToProcessChanged = true; // ATTENTION - valeur forcée pour les tests!!!!!
		if (true) {
			++_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;
	}

	//close the file
	if (!fileReaderPtr->close())
	{
		LOG4CXX_ERROR(gLogger, "ParamGetSpeasy::init - Cannot close file " << localPath);
	}

ac860015   Erdogan Furkan   For now - TBC
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	return result;
}

void ParamGetSpeasyProxy::updateInfo(Parameter & parameter)
{
	LOG4CXX_DEBUG(gLogger, "ParamGetSpeasyProxy::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;
}

f9eb7c3a   Erdogan Furkan   For now
307
308
309
310
311
312
313
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
	size_t written;
	written = fwrite(ptr, size, nmemb, stream);
	return written;
}


ac860015   Erdogan Furkan   For now - TBC
314
315
} /* namespace SpeasyProxyInterface */
} /* namespace AMDA */