2011-07-20 18:32:47 +00:00
|
|
|
/*
|
2011-06-26 08:30:44 +00:00
|
|
|
SleepLib Session Implementation
|
|
|
|
This stuff contains the base calculation smarts
|
|
|
|
Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
|
|
|
|
License: GPL
|
2011-07-20 18:32:47 +00:00
|
|
|
*/
|
2011-06-26 08:30:44 +00:00
|
|
|
|
|
|
|
#include "session.h"
|
|
|
|
#include "math.h"
|
|
|
|
#include <QDir>
|
2011-07-01 10:10:44 +00:00
|
|
|
#include <QDebug>
|
2011-07-27 09:21:53 +00:00
|
|
|
#include <QMetaType>
|
2011-06-26 08:30:44 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
Session::Session(Machine * m,SessionID session)
|
|
|
|
{
|
|
|
|
if (!session) {
|
|
|
|
session=m->CreateSessionID();
|
|
|
|
}
|
|
|
|
s_machine=m;
|
|
|
|
s_session=session;
|
|
|
|
s_changed=false;
|
|
|
|
s_events_loaded=false;
|
|
|
|
_first_session=true;
|
|
|
|
|
2011-07-02 14:35:50 +00:00
|
|
|
s_first=s_last=0;
|
2011-06-26 08:30:44 +00:00
|
|
|
s_eventfile="";
|
|
|
|
}
|
|
|
|
Session::~Session()
|
|
|
|
{
|
|
|
|
TrashEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::TrashEvents()
|
|
|
|
// Trash this sessions Events and release memory.
|
|
|
|
{
|
2011-07-27 09:21:53 +00:00
|
|
|
map<MachineCode,vector<EventList *> >::iterator i;
|
|
|
|
vector<EventList *>::iterator j;
|
|
|
|
for (i=eventlist.begin(); i!=eventlist.end(); i++) {
|
2011-06-26 08:30:44 +00:00
|
|
|
for (j=i->second.begin(); j!=i->second.end(); j++) {
|
|
|
|
delete *j;
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 10:47:50 +00:00
|
|
|
s_events_loaded=false;
|
2011-07-27 09:21:53 +00:00
|
|
|
eventlist.clear();
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 02:43:50 +00:00
|
|
|
//const int max_pack_size=128;
|
2011-07-03 11:49:47 +00:00
|
|
|
bool Session::OpenEvents() {
|
2011-07-27 09:21:53 +00:00
|
|
|
if (s_events_loaded)
|
2011-07-03 11:49:47 +00:00
|
|
|
return true;
|
|
|
|
bool b;
|
2011-07-20 18:32:47 +00:00
|
|
|
b=LoadEvents(s_eventfile);
|
|
|
|
if (!b) {
|
2011-07-03 11:49:47 +00:00
|
|
|
qWarning() << "Error Unkpacking Events" << s_eventfile;
|
2011-07-27 09:21:53 +00:00
|
|
|
return false;
|
2011-07-03 11:49:47 +00:00
|
|
|
}
|
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
|
|
|
|
return s_events_loaded=true;
|
2011-07-03 11:49:47 +00:00
|
|
|
};
|
|
|
|
|
2011-06-26 08:30:44 +00:00
|
|
|
bool Session::Store(QString path)
|
|
|
|
// Storing Session Data in our format
|
|
|
|
// {DataDir}/{MachineID}/{SessionID}.{ext}
|
|
|
|
{
|
|
|
|
QDir dir(path);
|
|
|
|
if (!dir.exists(path))
|
|
|
|
dir.mkpath(path);
|
|
|
|
|
|
|
|
QString base;
|
|
|
|
base.sprintf("%08lx",s_session);
|
|
|
|
base=path+"/"+base;
|
2011-07-22 13:46:17 +00:00
|
|
|
//qDebug() << "Storing Session: " << base;
|
2011-07-24 16:34:53 +00:00
|
|
|
bool a;
|
2011-06-26 08:30:44 +00:00
|
|
|
a=StoreSummary(base+".000"); // if actually has events
|
2011-07-22 13:46:17 +00:00
|
|
|
//qDebug() << " Summary done";
|
2011-07-27 10:47:50 +00:00
|
|
|
s_eventfile=base+".001";
|
2011-07-27 09:21:53 +00:00
|
|
|
if (eventlist.size()>0) StoreEvents(base+".001");
|
2011-07-22 13:46:17 +00:00
|
|
|
//qDebug() << " Events done";
|
2011-06-26 08:30:44 +00:00
|
|
|
if (a) {
|
|
|
|
s_changed=false;
|
2011-07-27 10:47:50 +00:00
|
|
|
s_events_loaded=true;
|
|
|
|
//TrashEvents();
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 10:47:50 +00:00
|
|
|
|
2011-06-26 08:30:44 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
const quint32 magic=0xC73216AB;
|
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
bool IsPlatformLittleEndian()
|
|
|
|
{
|
|
|
|
quint32 j=1;
|
|
|
|
*((char*)&j) = 0;
|
|
|
|
return j!=1;
|
|
|
|
}
|
|
|
|
|
2011-06-26 08:30:44 +00:00
|
|
|
bool Session::StoreSummary(QString filename)
|
|
|
|
{
|
2011-07-27 09:21:53 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
|
|
|
|
QDataStream out(&file);
|
|
|
|
out.setVersion(QDataStream::Qt_4_6);
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
out << (quint32)magic;
|
|
|
|
out << (quint32)s_machine->id();
|
|
|
|
out << (quint32)s_session;
|
|
|
|
out << (quint16)0 << (quint16)0;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-03 02:43:50 +00:00
|
|
|
quint32 starttime=s_first/1000L;
|
|
|
|
quint32 duration=(s_last-s_first)/1000L;
|
2011-07-02 14:35:50 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
out << (quint32)starttime; // Session Start Time
|
|
|
|
out << (quint32)duration; // Duration of sesion in seconds.
|
|
|
|
out << (quint16)summary.size();
|
2011-06-26 08:30:44 +00:00
|
|
|
|
|
|
|
map<MachineCode,MCDataType> mctype;
|
|
|
|
|
|
|
|
// First output the Machine Code and type for each summary record
|
|
|
|
map<MachineCode,QVariant>::iterator i;
|
|
|
|
for (i=summary.begin(); i!=summary.end(); i++) {
|
|
|
|
MachineCode mc=i->first;
|
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
QMetaType::Type type=(QMetaType::Type)i->second.type(); // Urkk.. this is a mess.
|
|
|
|
if (type==QMetaType::Bool) {
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=MC_bool;
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (type==QMetaType::Int) {
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=MC_int;
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (type==QMetaType::LongLong) {
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=MC_long;
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (type==QMetaType::Double) {
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=MC_double;
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (type==QMetaType::Float) {
|
|
|
|
mctype[mc]=MC_float;
|
|
|
|
} else if (type==QMetaType::QString) {
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=MC_string;
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (type==QMetaType::QDateTime) {
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=MC_datetime;
|
|
|
|
} else {
|
2011-07-27 09:21:53 +00:00
|
|
|
QString t=i->second.typeToName((QVariant::Type)type);
|
2011-07-01 10:10:44 +00:00
|
|
|
qWarning() << "Error in Session->StoreSummary: Can't pack variant type " << t;
|
2011-07-20 18:32:47 +00:00
|
|
|
return false;
|
|
|
|
//exit(1);
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-20 18:32:47 +00:00
|
|
|
out << (qint16)mc;
|
|
|
|
out << (qint8)mctype[mc];
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
// Then dump out the actual data, according to format.
|
|
|
|
for (i=summary.begin(); i!=summary.end(); i++) {
|
|
|
|
MachineCode mc=i->first;
|
|
|
|
if (mctype[mc]==MC_bool) {
|
2011-07-20 18:32:47 +00:00
|
|
|
out << i->second.toBool();
|
2011-06-26 08:30:44 +00:00
|
|
|
} else if (mctype[mc]==MC_int) {
|
2011-07-20 18:32:47 +00:00
|
|
|
out << (qint32)i->second.toInt();
|
2011-06-26 08:30:44 +00:00
|
|
|
} else if (mctype[mc]==MC_long) {
|
2011-07-20 18:32:47 +00:00
|
|
|
out << (qint64)i->second.toLongLong();
|
2011-06-26 08:30:44 +00:00
|
|
|
} else if (mctype[mc]==MC_double) {
|
2011-07-20 18:32:47 +00:00
|
|
|
out << (double)i->second.toDouble();
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (mctype[mc]==MC_float) {
|
|
|
|
float f=(float)i->second.toDouble(); // check me.
|
|
|
|
out << f;
|
2011-06-26 08:30:44 +00:00
|
|
|
} else if (mctype[mc]==MC_string) {
|
2011-07-20 18:32:47 +00:00
|
|
|
out << i->second.toString();
|
2011-06-26 08:30:44 +00:00
|
|
|
} else if (mctype[mc]==MC_datetime) {
|
2011-07-20 18:32:47 +00:00
|
|
|
out << i->second.toDateTime();
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-20 18:32:47 +00:00
|
|
|
file.close();
|
2011-06-26 08:30:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-07-02 14:35:50 +00:00
|
|
|
|
2011-06-26 08:30:44 +00:00
|
|
|
bool Session::LoadSummary(QString filename)
|
|
|
|
{
|
2011-06-28 01:51:21 +00:00
|
|
|
if (filename.isEmpty()) return false;
|
2011-07-24 17:18:10 +00:00
|
|
|
//qDebug() << "Loading Summary " << filename;
|
2011-07-02 14:35:50 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
2011-07-01 10:10:44 +00:00
|
|
|
qDebug() << "Couldn't open file" << filename;
|
2011-06-26 08:30:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
QDataStream in(&file);
|
|
|
|
in.setVersion(QDataStream::Qt_4_6);
|
|
|
|
|
2011-06-26 08:30:44 +00:00
|
|
|
quint64 t64;
|
|
|
|
quint32 t32;
|
|
|
|
quint16 t16;
|
|
|
|
quint8 t8;
|
|
|
|
|
|
|
|
qint16 sumsize;
|
|
|
|
map<MachineCode,MCDataType> mctype;
|
|
|
|
vector<MachineCode> mcorder;
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32;
|
|
|
|
if (t32!=magic) {
|
|
|
|
qDebug() << "Wrong magic number in " << filename;
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32; // MachineID (dont need this result)
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32; // Sessionid;
|
2011-06-26 08:30:44 +00:00
|
|
|
s_session=t32;
|
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t16; // File Type
|
|
|
|
if (t16!=0) {
|
|
|
|
qDebug() << "Wrong file type"; //wrong file type
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t16; // File Version
|
2011-06-26 08:30:44 +00:00
|
|
|
// dont care yet
|
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32; // Start time
|
2011-07-03 02:43:50 +00:00
|
|
|
s_first=qint64(t32)*1000L;
|
2011-07-02 14:35:50 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32; // Duration // (16bit==Limited to 18 hours)
|
2011-07-03 02:43:50 +00:00
|
|
|
s_last=s_first+qint64(t32)*1000L;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> sumsize; // Summary size (number of Machine Code lists)
|
2011-06-26 08:30:44 +00:00
|
|
|
|
|
|
|
for (int i=0; i<sumsize; i++) {
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t16; // Machine Code
|
2011-06-26 08:30:44 +00:00
|
|
|
MachineCode mc=(MachineCode)t16;
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t8; // Data Type
|
2011-06-26 08:30:44 +00:00
|
|
|
mctype[mc]=(MCDataType)t8;
|
|
|
|
mcorder.push_back(mc);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<sumsize; i++) { // Load each summary entry according to type
|
|
|
|
MachineCode mc=mcorder[i];
|
|
|
|
if (mctype[mc]==MC_bool) {
|
|
|
|
bool b;
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> b;
|
2011-06-26 08:30:44 +00:00
|
|
|
summary[mc]=b;
|
|
|
|
} else if (mctype[mc]==MC_int) {
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32;
|
2011-06-26 08:30:44 +00:00
|
|
|
summary[mc]=(qint32)t32;
|
|
|
|
} else if (mctype[mc]==MC_long) {
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t64;
|
2011-06-26 08:30:44 +00:00
|
|
|
summary[mc]=(qint64)t64;
|
|
|
|
} else if (mctype[mc]==MC_double) {
|
|
|
|
double dl;
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> dl;
|
2011-06-26 08:30:44 +00:00
|
|
|
summary[mc]=(double)dl;
|
2011-07-27 09:21:53 +00:00
|
|
|
} else if (mctype[mc]==MC_float) {
|
|
|
|
float fl;
|
|
|
|
in >> fl;
|
|
|
|
summary[mc]=(float)fl;
|
2011-06-26 08:30:44 +00:00
|
|
|
} else if (mctype[mc]==MC_string) {
|
|
|
|
QString s;
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> s;
|
2011-06-26 08:30:44 +00:00
|
|
|
summary[mc]=s;
|
|
|
|
} else if (mctype[mc]==MC_datetime) {
|
|
|
|
QDateTime dt;
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> dt;
|
2011-06-26 08:30:44 +00:00
|
|
|
summary[mc]=dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Session::StoreEvents(QString filename)
|
|
|
|
{
|
2011-07-20 18:32:47 +00:00
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::WriteOnly);
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
QDataStream out(&file);
|
|
|
|
out.setVersion(QDataStream::Qt_4_6);
|
|
|
|
|
|
|
|
out << (quint32)magic; // Magic Number
|
|
|
|
out << (quint32)s_machine->id(); // Machine ID
|
|
|
|
out << (quint32)s_session; // This session's ID
|
|
|
|
out << (quint16)1; // File type 1 == Event
|
|
|
|
out << (quint16)0; // File Version
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
out << s_first;
|
|
|
|
out << s_last;
|
|
|
|
|
|
|
|
out << (qint16)eventlist.size(); // Number of event categories
|
|
|
|
|
|
|
|
|
|
|
|
map<MachineCode,vector<EventList *> >::iterator i;
|
|
|
|
vector<EventList *>::iterator j;
|
|
|
|
|
|
|
|
for (i=eventlist.begin(); i!=eventlist.end(); i++) {
|
|
|
|
out << (qint16)i->first; // MachineCode
|
|
|
|
out << (qint16)i->second.size();
|
|
|
|
for (unsigned j=0;j<i->second.size();j++) {
|
|
|
|
EventList &e=*i->second[j];
|
|
|
|
out << e.first();
|
|
|
|
out << e.last();
|
|
|
|
out << (qint32)e.count();
|
|
|
|
out << (qint8)e.type();
|
|
|
|
out << e.rate();
|
|
|
|
out << e.gain();
|
|
|
|
out << e.offset();
|
|
|
|
//if (!e.update_minmax()) {
|
|
|
|
out << e.min();
|
|
|
|
out << e.max();
|
|
|
|
//} else {
|
|
|
|
// out << (EventDataType)0;
|
|
|
|
// out << (EventDataType)0;
|
|
|
|
//}
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
qint64 t,last;
|
|
|
|
quint32 x;
|
|
|
|
for (i=eventlist.begin(); i!=eventlist.end(); i++) {
|
|
|
|
for (unsigned j=0;j<i->second.size();j++) {
|
|
|
|
EventList &e=*i->second[j];
|
|
|
|
for (int c=0;c<e.count();c++) {
|
|
|
|
out << e.raw(c);
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
if (e.type()!=EVL_Waveform) {
|
|
|
|
last=e.first();
|
|
|
|
for (int c=0;c<e.count();c++) {
|
|
|
|
t=e.time(c);
|
|
|
|
x=(t-last);
|
|
|
|
out << x;
|
|
|
|
last=e.time(c);
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
//file.close();
|
2011-06-26 08:30:44 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool Session::LoadEvents(QString filename)
|
|
|
|
{
|
2011-06-28 01:51:21 +00:00
|
|
|
if (filename.isEmpty()) return false;
|
2011-07-20 18:32:47 +00:00
|
|
|
|
|
|
|
QFile file(filename);
|
|
|
|
if (!file.open(QIODevice::ReadOnly)) {
|
|
|
|
qDebug() << "Couldn't open file" << filename;
|
2011-06-26 08:30:44 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-07-20 18:32:47 +00:00
|
|
|
QDataStream in(&file);
|
|
|
|
in.setVersion(QDataStream::Qt_4_6);
|
2011-06-26 08:30:44 +00:00
|
|
|
|
|
|
|
quint32 t32;
|
|
|
|
quint16 t16;
|
|
|
|
quint8 t8;
|
2011-07-01 10:10:44 +00:00
|
|
|
//qint16 i16;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32; // Magic Number
|
|
|
|
if (t32!=magic) {
|
|
|
|
qWarning() << "Wrong Magic number in " << filename;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
in >> t32; // MachineID
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t32; // Sessionid;
|
2011-06-26 08:30:44 +00:00
|
|
|
s_session=t32;
|
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t16; // File Type
|
|
|
|
if (t16!=1) {
|
|
|
|
qDebug() << "Wrong File Type in " << filename;
|
|
|
|
return false;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-20 18:32:47 +00:00
|
|
|
in >> t16; // File Version
|
2011-06-26 08:30:44 +00:00
|
|
|
// dont give a crap yet..
|
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
in >> s_first;
|
|
|
|
in >> s_last;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
qint16 mcsize;
|
|
|
|
in >> mcsize; // Summary size (number of Machine Code lists)
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
MachineCode code;
|
|
|
|
qint64 ts1,ts2;
|
|
|
|
qint32 evcount;
|
|
|
|
EventListType elt;
|
|
|
|
EventDataType rate,gain,offset,mn,mx;
|
|
|
|
qint16 size2;
|
2011-06-26 08:30:44 +00:00
|
|
|
vector<MachineCode> mcorder;
|
2011-07-27 09:21:53 +00:00
|
|
|
vector<qint16> sizevec;
|
|
|
|
for (int i=0;i<mcsize;i++) {
|
|
|
|
in >> t16;
|
|
|
|
code=(MachineCode)t16;
|
|
|
|
mcorder.push_back(code);
|
|
|
|
in >> size2;
|
|
|
|
sizevec.push_back(size2);
|
|
|
|
for (int j=0;j<size2;j++) {
|
|
|
|
in >> ts1;
|
|
|
|
//UpdateFirst(ts1);
|
|
|
|
in >> ts2;
|
|
|
|
//UpdateLast(ts2);
|
|
|
|
in >> evcount;
|
|
|
|
in >> t8;
|
|
|
|
elt=(EventListType)t8;
|
|
|
|
in >> rate;
|
|
|
|
in >> gain;
|
|
|
|
in >> offset;
|
|
|
|
in >> mn;
|
|
|
|
in >> mx;
|
|
|
|
EventList *elist=new EventList(code,elt,gain,offset,mn,mx,rate);
|
|
|
|
|
|
|
|
eventlist[code].push_back(elist);
|
|
|
|
elist->m_count=evcount;
|
|
|
|
elist->m_first=ts1;
|
|
|
|
elist->m_last=ts2;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
|
|
|
|
EventStoreType t;
|
|
|
|
qint64 last;
|
|
|
|
quint32 x;
|
|
|
|
for (int i=0;i<mcsize;i++) {
|
|
|
|
code=mcorder[i];
|
|
|
|
size2=sizevec[i];
|
|
|
|
for (int j=0;j<size2;j++) {
|
|
|
|
EventList &evec=*eventlist[code][j];
|
|
|
|
evec.m_data.resize(evec.m_count);
|
|
|
|
for (int c=0;c<evec.m_count;c++) {
|
|
|
|
in >> t;
|
|
|
|
evec.m_data[c]=t;
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
last=evec.first();
|
|
|
|
if (evec.type()!=EVL_Waveform) {
|
|
|
|
evec.m_time.resize(evec.m_count);
|
|
|
|
for (int c=0;c<evec.m_count;c++) {
|
|
|
|
in >> x;
|
|
|
|
last+=x;
|
|
|
|
evec.m_time[c]=last;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
EventDataType Session::min(MachineCode code)
|
|
|
|
{
|
|
|
|
if (eventlist.find(code)==eventlist.end())
|
|
|
|
return 0;
|
|
|
|
vector<EventList *> & evec=eventlist[code];
|
|
|
|
bool first=true;
|
|
|
|
EventDataType min,t1;
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
t1=evec[i]->min();
|
|
|
|
if (t1==evec[i]->max()) continue;
|
|
|
|
if (first) {
|
|
|
|
min=t1;
|
|
|
|
first=false;
|
|
|
|
} else {
|
|
|
|
if (min>t1) min=t1;
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
return min;
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
EventDataType Session::max(MachineCode code)
|
2011-06-26 08:30:44 +00:00
|
|
|
{
|
2011-07-27 09:21:53 +00:00
|
|
|
if (eventlist.find(code)==eventlist.end())
|
|
|
|
return 0;
|
|
|
|
vector<EventList *> & evec=eventlist[code];
|
|
|
|
bool first=true;
|
|
|
|
EventDataType max,t1;
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
t1=evec[i]->max();
|
|
|
|
if (t1==evec[i]->min()) continue;
|
|
|
|
if (first) {
|
|
|
|
max=t1;
|
|
|
|
first=false;
|
|
|
|
} else {
|
|
|
|
if (max<t1) max=t1;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
return max;
|
|
|
|
}
|
|
|
|
qint64 Session::first(MachineCode code)
|
|
|
|
{
|
|
|
|
if (eventlist.find(code)==eventlist.end())
|
|
|
|
return 0;
|
|
|
|
vector<EventList *> & evec=eventlist[code];
|
|
|
|
bool first=true;
|
|
|
|
qint64 min,t1;
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
t1=evec[i]->first();
|
|
|
|
if (first) {
|
|
|
|
min=t1;
|
|
|
|
first=false;
|
|
|
|
} else {
|
|
|
|
if (min>t1) min=t1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return min;
|
|
|
|
}
|
|
|
|
qint64 Session::last(MachineCode code)
|
|
|
|
{
|
|
|
|
if (eventlist.find(code)==eventlist.end())
|
|
|
|
return 0;
|
|
|
|
vector<EventList *> & evec=eventlist[code];
|
|
|
|
bool first=true;
|
|
|
|
qint64 max,t1;
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
t1=evec[i]->last();
|
|
|
|
if (first) {
|
|
|
|
max=t1;
|
|
|
|
first=false;
|
|
|
|
} else {
|
|
|
|
if (max<t1) max=t1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
int Session::count(MachineCode code)
|
|
|
|
{
|
|
|
|
if (eventlist.find(code)==eventlist.end())
|
|
|
|
return 0;
|
|
|
|
vector<EventList *> & evec=eventlist[code];
|
|
|
|
int sum=0;
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
sum+=evec[i]->count();
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
double Session::sum(MachineCode mc)
|
|
|
|
{
|
|
|
|
if (eventlist.find(mc)==eventlist.end()) return 0;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
vector<EventList *> & evec=eventlist[mc];
|
|
|
|
double sum=0;
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
for (int j=0;j<evec[i]->count();j++) {
|
|
|
|
sum+=evec[i]->data(j);
|
|
|
|
}
|
2011-07-20 18:32:47 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
EventDataType Session::avg(MachineCode mc)
|
|
|
|
{
|
|
|
|
if (eventlist.find(mc)==eventlist.end()) return 0;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
double cnt=count(mc);
|
|
|
|
if (cnt==0) return 0;
|
|
|
|
EventDataType val=sum(mc)/cnt;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
return val;
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
bool sortfunction (double i,double j) { return (i<j); }
|
|
|
|
|
|
|
|
EventDataType Session::percentile(MachineCode mc,double percent)
|
|
|
|
{
|
|
|
|
if (eventlist.find(mc)==eventlist.end()) return 0;
|
|
|
|
|
|
|
|
vector<EventDataType> array;
|
|
|
|
|
|
|
|
vector<EventList *> & evec=eventlist[mc];
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
for (int j=0;j<evec[i]->count();j++) {
|
|
|
|
array.push_back(evec[i]->data(j));
|
|
|
|
}
|
2011-07-20 18:32:47 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
std::sort(array.begin(),array.end(),sortfunction);
|
|
|
|
int size=array.size();
|
|
|
|
if (!size) return 0;
|
|
|
|
double i=size*percent;
|
|
|
|
double t;
|
|
|
|
modf(i,&t);
|
|
|
|
int j=t;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
//if (j>=size-1) return array[j];
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
return array[j];
|
|
|
|
}
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
EventDataType Session::weighted_avg(MachineCode mc)
|
|
|
|
{
|
|
|
|
if (eventlist.find(mc)==eventlist.end()) return 0;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
int cnt=0;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
bool first=true;
|
|
|
|
qint64 last;
|
|
|
|
int lastval=0,val;
|
|
|
|
const int max_slots=4096;
|
|
|
|
qint64 vtime[max_slots]={0};
|
2011-06-26 08:30:44 +00:00
|
|
|
|
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
double mult=10.0;
|
|
|
|
//if ((mc==CPAP_Pressure) || (mc==CPAP_EAP) || (mc==CPAP_IAP) | (mc==CPAP_PS)) {
|
|
|
|
// mult=10.0;
|
|
|
|
//} else mult=10.0;
|
2011-06-26 08:30:44 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
// vector<Event *>::iterator i;
|
2011-07-20 18:32:47 +00:00
|
|
|
|
2011-07-27 09:21:53 +00:00
|
|
|
vector<EventList *> & evec=eventlist[mc];
|
|
|
|
for (unsigned i=0;i<evec.size();i++) {
|
|
|
|
for (int j=0;j<evec[i]->count();j++) {
|
|
|
|
val=evec[i]->data(j)*mult;
|
|
|
|
if (first) {
|
|
|
|
first=false;
|
|
|
|
} else {
|
|
|
|
int d=(evec[i]->time(j)-last)/1000L;
|
|
|
|
if (lastval>max_slots) {
|
|
|
|
qWarning("max_slots to small in Session::weighted_avg()");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
vtime[lastval]+=d;
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
cnt++;
|
|
|
|
last=evec[i]->time(j);
|
|
|
|
lastval=val;
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
|
|
|
|
qint64 total=0;
|
|
|
|
for (int i=0; i<max_slots; i++) total+=vtime[i];
|
|
|
|
//double hours=total.GetSeconds().GetLo()/3600.0;
|
|
|
|
|
|
|
|
qint64 s0=0,s1=0,s2=0;
|
|
|
|
if (total==0) return 0;
|
|
|
|
for (int i=0; i<max_slots; i++) {
|
|
|
|
if (vtime[i] > 0) {
|
|
|
|
s0=vtime[i];
|
|
|
|
s1+=i*s0;
|
|
|
|
s2+=s0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double j=double(s1)/double(total);
|
|
|
|
return j/mult;
|
2011-06-26 08:30:44 +00:00
|
|
|
}
|
2011-07-27 09:21:53 +00:00
|
|
|
|