Clean up Zeo loader using techniques from the Dreem loader.

Also speed up skipping previously imported sessions by bailing
earlier. No other change in functionality.
This commit is contained in:
sawinglogz 2020-01-30 12:20:50 -05:00
parent 32ffcc4f94
commit 156215b729
2 changed files with 48 additions and 64 deletions

View File

@ -172,110 +172,73 @@ Session* ZEOLoader::readNextSession()
QStringList SG, DSG; QStringList SG, DSG;
bool ok; bool ok;
bool dodgy;
QHash<QString,QString> row; QHash<QString,QString> row;
while (csv->readRow(row)) { while (csv->readRow(row)) {
dodgy = false; invalid_fields = false;
ZQ = row["ZQ"].toInt(&ok); start_of_night = readDateTime(row["Start of Night"]);
if (!ok) { dodgy = true; } if (start_of_night.isValid()) {
sid = start_of_night.toTime_t();
if (mach->SessionExists(sid)) {
continue;
}
}
ZQ = readInt(row["ZQ"]);
// TotalZ = linecomp[idxTotalZ].toInt(&ok); // TotalZ = linecomp[idxTotalZ].toInt(&ok);
// if (!ok) { dodgy = true; } // if (!ok) { dodgy = true; }
TimeToZ = row["Time to Z"].toInt(&ok); TimeToZ = readInt(row["Time to Z"]);
if (!ok) { dodgy = true; }
TimeInWake = row["Time in Wake"].toInt(&ok); TimeInWake = readInt(row["Time in Wake"]);
if (!ok) { dodgy = true; }
TimeInREM = row["Time in REM"].toInt(&ok); TimeInREM = readInt(row["Time in REM"]);
if (!ok) { dodgy = true; }
TimeInLight = row["Time in Light"].toInt(&ok); TimeInLight = readInt(row["Time in Light"]);
if (!ok) { dodgy = true; }
TimeInDeep = row["Time in Deep"].toInt(&ok); TimeInDeep = readInt(row["Time in Deep"]);
if (!ok) { dodgy = true; }
Awakenings = row["Awakenings"].toInt(&ok); Awakenings = readInt(row["Awakenings"]);
if (!ok) { dodgy = true; }
start_of_night = readDateTime(row["Start of Night"]);
if (!start_of_night.isValid()) { dodgy = true; }
end_of_night = readDateTime(row["End of Night"]); end_of_night = readDateTime(row["End of Night"]);
if (!end_of_night.isValid()) { dodgy = true; }
rise_time = readDateTime(row["Rise Time"]); rise_time = readDateTime(row["Rise Time"]);
if (!rise_time.isValid()) { dodgy = true; }
// AlarmReason = linecomp[idxAlarmReason].toInt(&ok); // AlarmReason = linecomp[idxAlarmReason].toInt(&ok);
// if (!ok) { dodgy = true; }
// SnoozeTime = linecomp[idxSnoozeTime].toInt(&ok); // SnoozeTime = linecomp[idxSnoozeTime].toInt(&ok);
// if (!ok) { dodgy = true; }
// WakeTone = linecomp[idxWakeTone].toInt(&ok); // WakeTone = linecomp[idxWakeTone].toInt(&ok);
// if (!ok) { dodgy = true; }
// WakeWindow = linecomp[idxWakeWindow].toInt(&ok); // WakeWindow = linecomp[idxWakeWindow].toInt(&ok);
// if (!ok) { dodgy = true; }
// AlarmType = linecomp[idxAlarmType].toInt(&ok); // AlarmType = linecomp[idxAlarmType].toInt(&ok);
// if (!ok) { dodgy = true; }
if (!row["First Alarm Ring"].isEmpty()) { FirstAlarmRing = readDateTime(row["First Alarm Ring"], false);
FirstAlarmRing = readDateTime(row["First Alarm Ring"]);
if (!FirstAlarmRing.isValid()) { dodgy = true; }
}
if (!row["Last Alarm Ring"].isEmpty()) { LastAlarmRing = readDateTime(row["Last Alarm Ring"], false);
LastAlarmRing = readDateTime(row["Last Alarm Ring"]);
if (!LastAlarmRing.isValid()) { dodgy = true; }
}
if (!row["First Snooze Time"].isEmpty()) { FirstSnoozeTime = readDateTime(row["First Snooze Time"], false);
FirstSnoozeTime = readDateTime(row["First Snooze Time"]);
if (!FirstSnoozeTime.isValid()) { dodgy = true; } LastSnoozeTime = readDateTime(row["Last Snooze Time"], false);
}
if (!row["Last Snooze Time"].isEmpty()) { SetAlarmTime = readDateTime(row["Set Alarm Time"], false);
LastSnoozeTime = readDateTime(row["Last Snooze Time"]);
if (!LastSnoozeTime.isValid()) { dodgy = true; }
}
if (!row["Set Alarm Time"].isEmpty()) { MorningFeel = readInt(row["Morning Feel"], false);
SetAlarmTime = readDateTime(row["Set Alarm Time"]);
if (!SetAlarmTime.isValid()) { dodgy = true; }
}
MorningFeel = row["Morning Feel"].toInt(&ok);
if (!ok) { MorningFeel = 0; }
FirmwareVersion = row["Firmware Version"]; FirmwareVersion = row["Firmware Version"];
MyZeoVersion = row["My ZEO Version"]; MyZeoVersion = row["My ZEO Version"];
if (dodgy) { if (invalid_fields) {
continue; continue;
} }
SG = row["Sleep Graph"].split(" "); SG = row["Sleep Graph"].split(" ");
DSG = row["Detailed Sleep Graph"].split(" "); DSG = row["Detailed Sleep Graph"].split(" ");
sid = start_of_night.toTime_t();
if (DSG.size() == 0) { if (DSG.size() == 0) {
continue; continue;
} }
if (mach->SessionExists(sid)) {
continue;
}
sess = new Session(mach, sid); sess = new Session(mach, sid);
break; break;
}; };
@ -313,15 +276,34 @@ Session* ZEOLoader::readNextSession()
return sess; return sess;
} }
QDateTime ZEOLoader::readDateTime(const QString & text) QDateTime ZEOLoader::readDateTime(const QString & text, bool required)
{ {
QDateTime dt = QDateTime::fromString(text, "MM/dd/yyyy HH:mm"); QDateTime dt = QDateTime::fromString(text, "MM/dd/yyyy HH:mm");
if (!dt.isValid()) { if (required || !text.isEmpty()) {
dt = QDateTime::fromString(text, "yyyy-MM-dd HH:mm:ss"); if (!dt.isValid()) {
dt = QDateTime::fromString(text, "yyyy-MM-dd HH:mm:ss");
if (!dt.isValid()) {
invalid_fields = true;
}
}
} }
return dt; return dt;
} }
int ZEOLoader::readInt(const QString & text, bool required)
{
bool ok;
int value = text.toInt(&ok);
if (!ok) {
if (required) {
invalid_fields = true;
} else {
value = 0;
}
}
return value;
}
static bool zeo_initialized = false; static bool zeo_initialized = false;
void ZEOLoader::Register() void ZEOLoader::Register()

View File

@ -44,12 +44,14 @@ class ZEOLoader : public MachineLoader
Session* readNextSession(); Session* readNextSession();
protected: protected:
QDateTime readDateTime(const QString & text); QDateTime readDateTime(const QString & text, bool required=true);
int readInt(const QString & text, bool required=true);
private: private:
QFile file; QFile file;
class CSVReader* csv; class CSVReader* csv;
Machine *mach; Machine *mach;
bool invalid_fields;
}; };
#endif // ZEOLOADER_H #endif // ZEOLOADER_H