/* SleepLib PRS1 Loader Implementation Author: Mark Watkins License: GPL */ #include #include #include #include #include #include #include #include #include #include "prs1_loader.h" extern wxProgressDialog *loader_progress; map ModelMap= { {34,wxT("RemStar Pro with C-Flex+")}, {35,wxT("RemStar Auto with A-Flex")} }; PRS1::PRS1(Profile *p,MachineID id):CPAP(p,id) { m_class=wxT("PRS1"); properties[wxT("Brand")]=wxT("Philips Respironics"); properties[wxT("Model")]=wxT("System One"); SleepFlags= { CPAP_RERA, PRS1_VSnore2, CPAP_FlowLimit, CPAP_Hypopnea, CPAP_Obstructive, CPAP_ClearAirway, CPAP_CSR }; } PRS1::~PRS1() { } PRS1Loader::PRS1Loader() { } PRS1Loader::~PRS1Loader() { for (auto i=PRS1List.begin(); i!=PRS1List.end(); i++) { delete i->second; } } Machine *PRS1Loader::CreateMachine(wxString serial,Profile *profile) { wxLogMessage(wxT("Create Machine")+serial); if (!profile) { wxLogMessage(wxT("No Profile!")); return NULL; } vector ml=profile->GetMachines(MT_CPAP); bool found=false; for (auto i=ml.begin(); i!=ml.end(); i++) { if (((*i)->GetClass()==wxT("PRS1")) && ((*i)->properties[wxT("Serial")]==serial)) { PRS1List[serial]=*i; //static_cast(*i); found=true; break; } } if (found) return PRS1List[serial]; //assert(PRS1List.find(serial)==PRS1List.end()) //wxPuts(wxT("Creating CPAP Machine ")+serial); Machine *m=new PRS1(profile,0); PRS1List[serial]=m; profile->AddMachine(m); m->properties[wxT("Serial")]=serial; return m; } bool PRS1Loader::Open(wxString & path,Profile *profile) { wxString newpath; wxString sep=wxFileName::GetPathSeparator(); wxString pseries=wxT("P-Series"); if (path.Right(pseries.Len()+sep.Len())==pseries) { newpath=path; } else { newpath=path+sep+pseries; } wxDir dir; dir.Open(newpath); if (!dir.IsOpened()) return 0; list SerialNumbers; list::iterator sn; wxString filename; bool cont=dir.GetFirst(&filename); int c=0; while (cont) { if ((filename[0]=='P') && (isdigit(filename[1])) && (isdigit(filename[2]))) { SerialNumbers.push_back(filename); } else if (filename.Lower()==wxT("last.txt")) { // last.txt points to the current serial number wxTextFile f(newpath+sep+filename); f.Open(); last=f.GetFirstLine(); last.Strip(); f.Close(); } cont=dir.GetNext(&filename); } if (SerialNumbers.empty()) return 0; Machine *m; for (sn=SerialNumbers.begin(); sn!=SerialNumbers.end(); sn++) { wxString s=*sn; m=CreateMachine(s,profile); if (m) OpenMachine(m,newpath+wxFileName::GetPathSeparator()+(*sn)); } return PRS1List.size(); return c; } bool PRS1Loader::ParseProperties(Machine *m,wxString filename) { wxTextFile f(filename); f.Open(); if (!f.IsOpened()) return false; wxString line; map prop; wxString s=f.GetFirstLine(); wxChar sep=wxChar('='); wxString key,value; while (!f.Eof()) { key=s.BeforeFirst(sep); if (key==s) continue; value=s.AfterFirst(sep).Strip(); if (value==s) continue; prop[key]=value; s=f.GetNextLine(); } if (prop[wxT("ProductType")].IsNumber()) { long i; prop[wxT("ProductType")].ToLong(&i); if (ModelMap.find(i)!=ModelMap.end()) { m->properties[wxT("SubModel")]=ModelMap[i]; } } if (prop[wxT("SerialNumber")]!=m->properties[wxT("Serial")]) { wxLogWarning(wxT("Serial Number in PRS1 properties.txt doesn't match directory structure")); } else prop.erase(wxT("SerialNumber")); // already got it stored. for (auto i=prop.begin(); i!=prop.end(); i++) { m->properties[i->first]=i->second; } f.Close(); return true; } int PRS1Loader::OpenMachine(Machine *m,wxString path) { wxLogDebug(wxT("Opening PRS1 ")+path); //wxPuts(wxT("opening "+path)); wxDir dir; dir.Open(path); if (!dir.IsOpened()) return false; wxString pathname,filename; bool cont=dir.GetFirst(&filename); list paths; if(loader_progress) { loader_progress->Update(0); } while (cont) { pathname=path+wxFileName::GetPathSeparator()+filename; if ((filename[0]==wxChar('p')) && (isdigit(filename[1]))) { paths.push_back(pathname); } else if (filename.Lower()==wxT("properties.txt")) { ParseProperties(m,pathname); } else if (filename.Lower()==wxT("e")) { // don't really give a crap about .004 files yet. } if (loader_progress) loader_progress->Pulse(); cont=dir.GetNext(&filename); } SessionID session; long ext; typedef vector StringList; map sessfiles; int size=paths.size(); int cnt=0; for (auto p=paths.begin(); p!=paths.end(); p++) { dir.Open(*p); if (!dir.IsOpened()) continue;; bool cont=dir.GetFirst(&filename); while (cont) { wxString ext_s=filename.AfterLast(wxChar('.')); wxString session_s=filename.BeforeLast(wxChar('.')); if (!ext_s.IsNumber()) continue; if (!session_s.IsNumber()) continue; session_s.ToLong(&session); ext_s.ToLong(&ext); if (sessfiles[session].capacity()==0) sessfiles[session].resize(3); wxString fullname=*p+wxFileName::GetPathSeparator()+filename; if (ext==1) { sessfiles[session][0]=fullname; } else if (ext==2) { sessfiles[session][1]=fullname; } else if (ext==5) { sessfiles[session][2]=fullname; } cnt++; if (loader_progress) loader_progress->Pulse(); //Update((float(cnt)/float(size)*25)); //if (loader_progress) loader_progress->Update((float(cnt)/float(size)*25.0)); cont=dir.GetNext(&filename); } } if (sessfiles.size()==0) return 0; size=sessfiles.size(); cnt=0; for (auto s=sessfiles.begin(); s!=sessfiles.end(); s++) { session=s->first; cnt++; if (loader_progress) loader_progress->Update(25.0+(float(cnt)/float(size)*25.0)); if (m->SessionExists(session)) continue; if (!s->second[0]) continue; Session *sess=new Session(m,session); if (!OpenSummary(sess,s->second[0])) { wxLogWarning(wxT("PRS1Loader: Could'nt open summary file ")+s->second[0]); delete sess; continue; } const double ignore_thresh=300.0/3600.0;// Ignore useless sessions under 5 minute if (sess->hours()SetSessionID(sess->start().GetTicks()); m->AddSession(sess); if (!s->second[1].IsEmpty()) { if (!OpenEvents(sess,s->second[1])) { wxLogWarning(wxT("PRS1Loader: Couldn't open event file ")+s->second[1]); } } if (!s->second[2].IsEmpty()) { if (!OpenWaveforms(sess,s->second[2])) { wxLogWarning(wxT("PRS1Loader: Couldn't open event file ")+s->second[2]); } } sess->summary[CPAP_CSR]=sess->sum_event_field(CPAP_CSR,0); sess->summary[CPAP_VSnore]=(long)sess->count_events(CPAP_VSnore); sess->summary[PRS1_VSnore2]=sess->sum_event_field(PRS1_VSnore2,0); sess->summary[CPAP_PressureMedian]=sess->avg_event_field(CPAP_Pressure,0); sess->summary[CPAP_LeakMinimum]=sess->min_event_field(CPAP_Leak,0); sess->summary[CPAP_LeakMaximum]=sess->max_event_field(CPAP_Leak,0); // should be merged.. sess->summary[CPAP_LeakMedian]=sess->avg_event_field(CPAP_Leak,0); sess->summary[CPAP_LeakAverage]=sess->weighted_avg_event_field(CPAP_Leak,0); //wxPrintf(sess->start().Format()+wxT(" avgsummary=%.3f avgmine=%.3f\n"),sess->summary[CPAP_PressureAverage].GetDouble(),sess->weighted_avg_event_field(CPAP_Pressure,0)); sess->SetChanged(true); } m->Save(); // Save any new sessions to disk in our format if (loader_progress) loader_progress->Update(100); return true; } bool PRS1Loader::OpenSummary(Session *session,wxString filename) { int size,sequence,seconds,br; time_t timestamp; unsigned char header[24]; unsigned char ext; //wxLogMessage(wxT("Opening PRS1 Summary ")+filename); wxFFile f(filename,wxT("rb")); if (!f.IsOpened()) return false; int hl=16; br=f.Read(header,hl); if (header[0]!=header[5]) return false; sequence=size=timestamp=seconds=ext=0; sequence=(header[10] << 24) | (header[9] << 16) | (header[8] << 8) | header[7]; timestamp=(header[14] << 24) | (header[13] << 16) | (header[12] << 8) | header[11]; size=(header[2] << 8) | header[1]; ext=header[6]; if (ext!=1) return false; //size|=(header[3]<<16) | (header[4]<<24); // the jury is still out on the 32bitness of one. doesn't matter here anyway. size-=(hl+2); unsigned char sum=0; for (int i=0; i wavedata; vector wavesize; int samples=0; int duration=0; while (true) { br=f.Read(header,hl); if (brc) min=c; if (maxAddWaveform(w); //wxLogMessage(wxT("Done PRS1 Waveforms ")+filename); return true; } bool initialized=false; void PRS1Loader::Register() { if (initialized) return; wxLogVerbose(wxT("Registering PRS1Loader")); RegisterLoader(new PRS1Loader()); initialized=true; }