VirtualInstrumentInterval.cc
6.42 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
/*
* VirtualInstrumentInterval.cc
*
* Created on: Nov 25, 2014
* Author: AKKA
*/
#include <climits>
#include "DicError.hh"
#include "TimeUtil.hh"
#include "LocalFileInterfaceConfig.hh"
#include "VirtualInstrumentManager.hh"
#include "VirtualInstrument.hh"
#include "VirtualInstrumentInterval.hh"
#include "LocalParamData.hh"
#include "TimeInterval.hh"
namespace AMDA {
namespace LocalFileInterface {
VirtualInstrumentInterval::VirtualInstrumentInterval(VirtualInstrument& pVI,
TimeIntervalList* pTimeIntervalList) : Worker(),
_vi(pVI),
_timeIntervalList(*pTimeIntervalList),
_currentTimeIntToProcess(_timeIntervalList.end()),
_timeIntToProcessChanged(false),
_crtFileIndex(0),
_crtRecordIndex(0)
{
_step = &VirtualInstrumentInterval::writeData;
// Initialize file reader to point to the first TimeInterval.
if (!setTimeInterval())
{
LOG4CXX_ERROR(gLogger, "At least one time interval must be specified to send request");
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::ex_msg("At least one time interval must be specified"));
}
active();
}
VirtualInstrumentInterval::~VirtualInstrumentInterval()
{
}
unsigned int VirtualInstrumentInterval::writeData()
{
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval::writeData");
if (_paramFlowSPtr == nullptr)
{
LOG4CXX_ERROR(gLogger, "VirtualInstrumentInterval::writeData - No param flow defined");
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::ex_msg("No param flow defined"));
return 0;
}
unsigned int ret = 0;
if (_currentTimeIntToProcess == _timeIntervalList.end())
return 0;
_timeIntToProcessChanged = false;
double lStopTime = (*_currentTimeIntToProcess)._stopTime;
int crtIntervalRecIndex = _crtRecordIndex;
int crtIntervalFileIndex = _crtFileIndex;
for(auto paramInfo : _paramFlowSPtr->getParamInfoMap())
{
ret = 0;
if (paramInfo.second == nullptr)
continue;
std::string paramId = paramInfo.first;
bool moreData = true;
int crtParamRecIndex = crtIntervalRecIndex;
int crtParamFileIndex = crtIntervalFileIndex;
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval::writeData for " << paramId << " - crtParamRecIndex = " << crtParamRecIndex << " - crtParamFileIndex " << crtParamFileIndex);
while (moreData)
{
LocalParamDataPacket *pPacket= new LocalParamDataPacket();
int paramDim1Size = paramInfo.second->getDim1Size();
int paramDim2Size = paramInfo.second->getDim2Size();
LocalContainerType containerType = CONTAINER_SCALAR;
if ((paramDim1Size > 1) && (paramDim2Size <= 1))
containerType = CONTAINER_VECTOR;
else if ((paramDim1Size > 1) && (paramDim2Size > 1))
containerType = CONTAINER_MATRIX;
if (!pPacket->init(containerType,paramInfo.second->getType(),paramDim1Size,paramDim2Size))
{
delete pPacket;
LOG4CXX_ERROR(gLogger, "VirtualInstrumentInterval::writeData - Cannot init data packet for " << paramId);
moreData = false;
continue;
}
FileReaderStatus status = _vi.getParamPacketData(paramId, crtParamFileIndex,
crtParamRecIndex, lStopTime, pPacket);
switch (status)
{
case FRS_MORE :
_paramFlowSPtr->push(paramId,pPacket);
//there is more data to get
crtParamRecIndex += pPacket->getNbData();
break;
case FRS_EOF :
//end of file
_paramFlowSPtr->push(paramId,pPacket);
//if it's last ParamFlow to proceed => update recordIndex and fileIndex
crtParamRecIndex = 0;
++crtParamFileIndex;
break;
case FRS_FINISH :
//stop time is reached
_paramFlowSPtr->push(paramId,pPacket);
//_paramFlowSPtr->push(paramId,nullptr);
moreData = false;
break;
case FRS_ERROR :
//error detected
delete pPacket;
LOG4CXX_ERROR(gLogger, "VirtualInstrumentInterval::writeData - Error detected to write data for " << paramId);
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::ex_msg("Cannot write data"));
break;
}
}
}
_step = &VirtualInstrumentInterval::writeEmptyData;
return ret;
}
unsigned int VirtualInstrumentInterval::writeEmptyData()
{
LOG4CXX_INFO(gLogger, "VirtualInstrumentInterval::writeEmptyData");
if (setTimeInterval())
{
_timeIntToProcessChanged = true;
_step = &VirtualInstrumentInterval::writeData;
}
for(auto paramInfo : _paramFlowSPtr->getParamInfoMap())
{
std::string paramId = paramInfo.first;
_paramFlowSPtr->push(paramId,nullptr);
}
return 0;
}
bool VirtualInstrumentInterval::setTimeInterval()
{
if (_currentTimeIntToProcess == _timeIntervalList.end()) {
_currentTimeIntToProcess = _timeIntervalList.begin();
} else if (++_currentTimeIntToProcess != _timeIntervalList.end()) {
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval => Get Next TimeInterval");
// Nothing to do.
} else {
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval => Reached all TimeInterval");
return false;
}
double lStartTime = (*_currentTimeIntToProcess)._startTime;
double lStopTime = (*_currentTimeIntToProcess)._stopTime;
if (lStopTime < _vi.getGlobalStartTime() || lStartTime > _vi.getGlobalStopTime())
{
LOG4CXX_ERROR(gLogger, "VirtualInstrumentInterval::setTimeInterval - Cannot get data for this time interval - Outside of global start and global stop definition");
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OUTOFTIME_ERR) << AMDA::ex_msg("Cannot get data for this time interval - Outside of global start and global stop definition"));
}
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval => start time: " << lStartTime << " lStopTime: " << lStopTime);
//
if (!_vi.getDataPosition(lStartTime, _crtFileIndex, _crtRecordIndex))
{
LOG4CXX_ERROR(gLogger, "VirtualInstrumentInterval::setTimeInterval - Cannot get data for this time interval");
BOOST_THROW_EXCEPTION(AMDA::AMDA_exception() << AMDA::errno_code(AMDA_OUTOFTIME_ERR) << AMDA::ex_msg("Cannot get data for this time interval"));
}
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval => _crtFileIndex: " << _crtFileIndex << ", _crtRecordIndex: " << _crtRecordIndex);
return true;
}
unsigned int VirtualInstrumentInterval::getOneDataBloc() {
LOG4CXX_DEBUG(gLogger, "VirtualInstrumentInterval::getOneDataBloc");
return (this->*_step)();
}
ParamFlowSPtr VirtualInstrumentInterval::getParamFlow(const std::string& pParamId,
LocalParamType paramType, int dim1Size, int dim2Size)
{
if (_paramFlowSPtr == nullptr)
_paramFlowSPtr.reset(new ParamFlow(*this));
_paramFlowSPtr->addAssociatedParam(pParamId,paramType,dim1Size,dim2Size);
return _paramFlowSPtr;
}
} /* namespace DDServerInterface */
} /* namespace AMDA */