ParamGetSpeasyProxy.cc
10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* ParamGetSpeasyProxy.cc
*
* Created on: April 25, 2024
* Author: AKKODIS - Furkan
*/
#include "SpeasyProxyInterfaceConfig.hh"
#include "ParamGetSpeasyProxy.hh"
// #include "VirtualInstrumentManager.hh"
#include "FileReaderCDF.hh"
#include <stdlib.h>
#include "Parameter.hh"
#include "ParamData.hh"
#include "ParamMgr.hh"
#include "DataSetMgr.hh"
#include "DicError.hh"
#include "TimeUtil.hh"
#include <fstream>
#include "Helper.hh"
#include <iostream>
#define CURL_STATICLIB
#include <curl/curl.h>
#include <curl/easy.h>
#include <cstdio>
namespace AMDA {
namespace SpeasyProxyInterface {
ParamGetSpeasyProxy::ParamGetSpeasyProxy(Parameter ¶meter) :
ParamGet_CRTP<ParamGetSpeasyProxy>(parameter),
_paramId(""),_type(TYPE_FLOAT),_dim1(1), _dim2(1), _minSampling(1), _container(CONTAINER_SCALAR),_timeStamp(0)
{
}
ParamGetSpeasyProxy::ParamGetSpeasyProxy(const ParamGetSpeasyProxy &pParamGetSpeasyProxy, Parameter ¶meter) :
ParamGet_CRTP<ParamGetSpeasyProxy>(pParamGetSpeasyProxy, parameter),
_paramId(pParamGetSpeasyProxy._paramId), _type(pParamGetSpeasyProxy._type),
_dim1(pParamGetSpeasyProxy._dim1), _dim2(pParamGetSpeasyProxy._dim2),
_minSampling(pParamGetSpeasyProxy._minSampling), _container(pParamGetSpeasyProxy._container),
_timeStamp(pParamGetSpeasyProxy._timeStamp)
{
}
ParamGetSpeasyProxy::~ParamGetSpeasyProxy()
{
//delete the pusher if needed
// if (_pusher != NULL)
// delete _pusher;
}
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();
}
/**
* 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 {
std::string str(errBuf.begin(), errBuf.end());
LOG4CXX_ERROR(_logger,
"Unable to download " + pPath + " : " << str );// errBuf[0]);
}
} 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");
// 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);
}
//set link to the param data
_paramData = ParamDataSPtr(_pusher->getParamData());
_paramData->setMinSampling(_minSampling);
if (_timeStamp == 0 && _signatureTrigger != "") {
// _signatureTrigger must be a name of xml parameter file
_timeStamp = AMDA::Helpers::Helper::dateOfFile(
_signatureTrigger.c_str());
}
return _timeStamp;
}
std::string ParamGetSpeasyProxy::getURLTime(double time){
std::string newTime = TimeUtil::DD2ISO_TimeDate(TimeUtil::double2DD_TimeDate(time));
std::string delim = ":";
std::string toReplaceWith = "%3A";
size_t pos = 0;
while ((pos = newTime.find(delim, pos)) != std::string::npos) {
newTime.replace(pos, delim.length(), toReplaceWith);
pos += toReplaceWith.length();
}
// delim = ".000";
// newTime.erase(newTime.find(delim),newTime.find(delim) + delim.length());
return newTime;
}
std::string ParamGetSpeasyProxy::getSpeasyProxyLink(){
// http://172.200.0.14:6543/get_data?path=amda%2Fc1_b_gsm&start_time=2018-10-24T00%3A00%3A00&stop_time=2018-10-24T02%3A00%3A00&format=cdf&zstd_compression=false&output_format=CDF_ISTP&pickle_proto=3
std::string path = "http://172.200.0.14:6543/get_data?path=";
std::string delim = "/";
std::string toReplaceWith = "%2F";
std::string paramId = _paramId;
size_t pos = 0;
while ((pos = paramId.find(delim, pos)) != std::string::npos) {
paramId.replace(pos, delim.length(), toReplaceWith);
pos += toReplaceWith.length();
}
path += paramId + "&start_time=" + getURLTime(_currentInterval->_startTime);
path += "&stop_time=" + getURLTime(_currentInterval->_stopTime);
path += "&format=cdf&zstd_compression=false&output_format=CDF_ISTP&pickle_proto=3";
return path;
}
unsigned int ParamGetSpeasyProxy::write()
{
unsigned int result = 0;
// Getting parameter ids
std::string timeParamId = "time";
std::string croptedParamId = _paramId;
std::string delim = "/";
croptedParamId.erase(0, croptedParamId.find(delim) + delim.length());
// Creation of the data packet
SpeasyProxyParamDataPacket *packet= new SpeasyProxyParamDataPacket();
packet->init(_container,_type,_dim1,_dim2);
// Downloading and loading FileReader
std::string localPath = download(getSpeasyProxyLink());
FileReaderCDF* fileReaderPtr = new FileReaderCDF();
bool isOpen = fileReaderPtr->open(localPath);
if(isOpen)
{
// Call to getParamPacketData to get CDF data
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.
}
if (result != 0)
remove(localPath.c_str());
return result;
}
//close the file
if (!fileReaderPtr->close())
{
LOG4CXX_ERROR(gLogger, "ParamGetSpeasy::init - Cannot close file " << localPath);
}
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;
}
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;
}
} /* namespace SpeasyProxyInterface */
} /* namespace AMDA */