96 lines
2.9 KiB
C++
96 lines
2.9 KiB
C++
#include "dataqueue.h"
|
|
|
|
|
|
DataQueue::DataQueue(QObject *parent) :
|
|
QThread(parent), stopped(false)
|
|
{}
|
|
|
|
|
|
DataQueue::~DataQueue(){
|
|
}
|
|
|
|
void DataQueue::run() {
|
|
mutex.lock();
|
|
bool stop = stopped || (requestQueue.size() == 0);
|
|
mutex.unlock();
|
|
while (!stop) {
|
|
mutex.lock();
|
|
if (requestQueue.size() > 0) {
|
|
DataRequest r = requestQueue.front();
|
|
if (r.valid) {
|
|
DataResult1D data = loader->doLoad(requestQueue.front());
|
|
double min = data.ydata[0];
|
|
double max = data.ydata[0];
|
|
for (size_t i = 0; i < data.xdata.size(); ++i) {
|
|
r.buffer->getBuffer()->push_back({data.xdata[i], data.ydata[i]});
|
|
if (data.ydata[i] < min)
|
|
min = data.ydata[i];
|
|
if (data.ydata[i] > max)
|
|
max = data.ydata[i];
|
|
}
|
|
//resultMap.insert(std::pair<std::string, DataResult1D>(data.id, data));
|
|
emit dataReady(QString::fromStdString(data.entityId), min, max);
|
|
}
|
|
else {
|
|
std::cerr << "request is invalid" << std::endl;
|
|
}
|
|
requestQueue.pop_front();
|
|
stop = requestQueue.size() == 0 || stopped;
|
|
}
|
|
mutex.unlock();
|
|
}
|
|
}
|
|
|
|
/*
|
|
void DataQueue::requestData(const nix::DataArray &array, const nix::NDSize &count, const nix::NDSize &offset, std::string &requestId) {
|
|
requestId = nix::util::createId();
|
|
DataRequest dr(requestId, array, count, offset);
|
|
mutex.lock();
|
|
std::deque<DataRequest>::iterator it;
|
|
for (it = requestQueue.begin(); it < requestQueue.end(); it++) {
|
|
std::cerr << it->array.id() << std::endl;
|
|
if (it->array.id() == array.id())
|
|
std::cerr << "updating request" <<std::endl;
|
|
}
|
|
requestQueue.push_back(dr);
|
|
mutex.unlock();
|
|
start();
|
|
}
|
|
*/
|
|
|
|
void DataQueue::requestData(const nix::DataArray &array, DataBuffer *buffer, const ArrayInfo &info, qreal min, qreal max, std::string &requestId) {
|
|
min = min < info.xoffset ? info.xoffset : min;
|
|
if (min < buffer->rangeMinimum()) {
|
|
return; // don't do nothing...
|
|
}
|
|
if (max > buffer->rangeMaximum()) { // need to load new data
|
|
if (min >= buffer->rangeMaximum())
|
|
min = buffer->rangeMaximum() + info.sampleinterval;
|
|
|
|
requestId = nix::util::createId();
|
|
DataRequest dr(requestId, array, buffer, info, min, max);
|
|
mutex.lock();
|
|
requestQueue.push_back(dr);
|
|
mutex.unlock();
|
|
start();
|
|
}
|
|
}
|
|
|
|
void DataQueue::stop() {
|
|
mutex.lock();
|
|
stopped = true;
|
|
mutex.unlock();
|
|
}
|
|
|
|
DataResult1D DataQueue::getData(QString requestId) {
|
|
DataResult1D ret;
|
|
mutex.lock();
|
|
std::map<std::string, DataResult1D>::iterator it = resultMap.find(requestId.toStdString());
|
|
if (it != resultMap.end()) {
|
|
ret = it->second;
|
|
resultMap.erase(it);
|
|
}
|
|
mutex.unlock();
|
|
return ret;
|
|
}
|