#include "VCreateTreatment.h" // Qt // Project #include "filehandler.h" #include "MsgDefs.h" using namespace Gui; using namespace View; using namespace Storage; VCreateTreatment::VCreateTreatment(QObject *parent) : QObject(parent) { connect(&_FileSaver, SIGNAL(fileSaved(bool)), this, SLOT(onFinishedSaveNewTreatment(bool))); connect(this, SIGNAL(validateParamsWithFW(GuiActionType,QVariantList)), &_GuiController, SLOT(doActionTransmit(GuiActionType,QVariantList))); connect(&_GuiController, SIGNAL(didActionReceive(GuiActionType,QVariantList)), this, SLOT(onFWValidationResponse(GuiActionType,QVariantList))); loadTreatmentParameterRanges(); } QJsonObject VCreateTreatment::loadTreatmentParameterRanges() { QJsonObject obj; if (!FileHandler::readJSON(Treatment_Parameter_Ranges_Path, obj)) { // TODO: handle error } foreach (const QString& key, obj.keys()) { QJsonValue value = obj.value(key); if (key == GET_VARIABLE_NAME(bloodFlowRateMin)) _bloodFlowRateMin = value.toInt(); else if (key == GET_VARIABLE_NAME(bloodFlowRateMax)) _bloodFlowRateMax = value.toInt(); else if (key == GET_VARIABLE_NAME(dialysateFlowRateMin)) _dialysateFlowRateMin = value.toInt(); else if (key == GET_VARIABLE_NAME(dialysateFlowRateMax)) _dialysateFlowRateMax = value.toInt(); else if (key == GET_VARIABLE_NAME(durationMin)) _durationMin = value.toInt(); else if (key == GET_VARIABLE_NAME(durationMax)) _durationMax = value.toInt(); else if (key == GET_VARIABLE_NAME(heparinDispensingRateMin)) _heparinDispensingRateMin = value.toInt(); else if (key == GET_VARIABLE_NAME(heparinDispensingRateMax)) _heparinDispensingRateMax = value.toInt(); else if (key == GET_VARIABLE_NAME(heparinBolusVolumeMin)) _heparinBolusVolumeMin = value.toInt(); else if (key == GET_VARIABLE_NAME(heparinBolusVolumeMax)) _heparinBolusVolumeMax = value.toInt(); else if (key == GET_VARIABLE_NAME(heparinStopTimeMin)) _heparinStopTimeMin = value.toInt(); else if (key == GET_VARIABLE_NAME(heparinStopTimeMax)) _heparinStopTimeMax = value.toInt(); //options else if (key == GET_VARIABLE_NAME(salineBolusOptions)) _salineBolusOptions = jsonArrayToStringList(value.toArray()); else if (key == GET_VARIABLE_NAME(acidConcentrateOptions)) _acidConcentrateOptions = jsonArrayToStringList(value.toArray()); else if (key == GET_VARIABLE_NAME(acidConcentrateOptions)) _acidConcentrateOptions = jsonArrayToStringList(value.toArray()); else if (key == GET_VARIABLE_NAME(bicarbonateConcentrateOptions)) _bicarbonateConcentrateOptions = jsonArrayToStringList(value.toArray()); else if (key == GET_VARIABLE_NAME(dialyzerTypeOptions)) _dialyzerTypeOptions = jsonArrayToStringList(value.toArray()); else if (key == GET_VARIABLE_NAME(dialysateTempMin)) _dialysateTempMin = value.toInt(); else if (key == GET_VARIABLE_NAME(dialysateTempMax)) _dialysateTempMax = value.toInt(); // arterial else if (key == GET_VARIABLE_NAME(arterialPressureLimitLowMin)) _arterialPressureLimitLowMin = value.toInt(); else if (key == GET_VARIABLE_NAME(arterialPressureLimitLowMax)) _arterialPressureLimitLowMax = value.toInt(); else if (key == GET_VARIABLE_NAME(arterialPressureLimitHighMin)) _arterialPressureLimitHighMin = value.toInt(); else if (key == GET_VARIABLE_NAME(arterialPressureLimitHighMax)) _arterialPressureLimitHighMax = value.toInt(); //venous else if (key == GET_VARIABLE_NAME(venousPressureLimitLowMin)) _venousPressureLimitLowMin = value.toInt(); else if (key == GET_VARIABLE_NAME(venousPressureLimitLowMax)) _venousPressureLimitLowMax = value.toInt(); else if (key == GET_VARIABLE_NAME(venousPressureLimitHighMin)) _venousPressureLimitHighMin = value.toInt(); else if (key == GET_VARIABLE_NAME(venousPressureLimitHighMax)) _venousPressureLimitHighMax = value.toInt(); else if (key == GET_VARIABLE_NAME(bloodPressureMeasureIntervalMin)) _bloodPressureMeasureIntervalMin = value.toInt(); else if (key == GET_VARIABLE_NAME(bloodPressureMeasureIntervalMax)) _bloodPressureMeasureIntervalMax = value.toInt(); else if (key == GET_VARIABLE_NAME(rinsebackFlowRateMin)) _rinsebackFlowRateMin = value.toInt(); else if (key == GET_VARIABLE_NAME(rinsebackFlowRateMax)) _rinsebackFlowRateMax = value.toInt(); else { qDebug() << "Invalid treatment parameter range: " << key; LOG_EVENT(tr("Invalid treatment parameter found: %0").arg(key)); } } return obj; } /** * @brief VCreateTreatment::onResetCreateTreatment * Resets the create treatment page to the default values. * Disables the continue button. */ void VCreateTreatment::onResetCreateTreatment() { reset_bloodFlowRate(); reset_dialysateFlowRate(); reset_duration(); reset_heparinDispensingRate(); reset_heparinBolusVolume(); reset_heparinStopTime(); reset_salineBolusVolume(); reset_acidConcentrate(); reset_bicarbonateConcentrate(); reset_dialyzerType(); reset_arterialPressureLimitLow(); reset_arterialPressureLimitHigh(); reset_venousPressureLimitLow(); reset_venousPressureLimitHigh(); reset_bloodPressureMeasureInterval(); reset_rinsebackFlowRate(); continueEnabled(false); emit resetCreateTreatment(); } /** * @brief VCreateTreatment::jsonArrayToStringList * Converts a jsonarray of strings to a QStringList * @param arr (QJsonArray) to convert * @return (QStringList) The list of strings in the array */ QStringList VCreateTreatment::jsonArrayToStringList(const QJsonArray &arr) { QStringList result; foreach (const QJsonValue &val, arr) { result.append(val.toString()); } return result; } /** * @brief VCreateTreatment::setTreatmentData * Assigns treatment parameters to the treatment data structure. */ void VCreateTreatment::setTreatmentData() { treatmentData.bloodFlowRate = _bloodFlowRate; treatmentData.dialysateFlowRate = _dialysateFlowRate; treatmentData.duration = _duration; treatmentData.heparinDispensingRate = _heparinDispensingRate; treatmentData.heparinBolusVolume = _heparinBolusVolume; treatmentData.heparinStopTime = _heparinStopTime; treatmentData.salineBolus = _salineBolusVolume; treatmentData.acidConcentrate = _acidConcentrate; treatmentData.bicarbonateConcentrate = _bicarbonateConcentrate; treatmentData.dialyzerType = _dialyzerType; treatmentData.dialysateTemp = _dialysateTemp; treatmentData.arterialPressureLimitLow = _arterialPressureLimitLow; treatmentData.arterialPressureLimitHigh = _arterialPressureLimitHigh; treatmentData.venousPressureLimitLow = _venousPressureLimitLow; treatmentData.venousPressureLimitHigh = _venousPressureLimitHigh; treatmentData.bloodPressureMeasureInterval = _bloodPressureMeasureInterval; treatmentData.rinsebackFlowRate = _rinsebackFlowRate; } /** * @brief VCreateTreatment::onFinishedCreate * Validates the treatment profile locally, then requests validation of it with FW */ void VCreateTreatment::onFinishedCreate() { setTreatmentData(); qDebug() << treatmentData.bloodFlowRate; qDebug() << treatmentData.dialysateFlowRate; qDebug() << treatmentData.duration; qDebug() << treatmentData.heparinDispensingRate; qDebug() << treatmentData.heparinBolusVolume; qDebug() << treatmentData.heparinStopTime; qDebug() << treatmentData.salineBolus; qDebug() << treatmentData.acidConcentrate; qDebug() << treatmentData.bicarbonateConcentrate; qDebug() << treatmentData.dialyzerType; qDebug() << treatmentData.dialysateTemp; qDebug() << treatmentData.arterialPressureLimitLow; qDebug() << treatmentData.arterialPressureLimitHigh; qDebug() << treatmentData.venousPressureLimitLow; qDebug() << treatmentData.venousPressureLimitHigh; qDebug() << treatmentData.bloodPressureMeasureInterval; qDebug() << treatmentData.rinsebackFlowRate; if (!validate(treatmentData)) { qDebug() << "Local create treatment validation failed."; LOG_EVENT("Local create treatment validation failed."); return; } #ifdef DEBUG emit showConfirm(); #else // TODO: Ask FW to validate selected parameters QVariantList messageData = { treatmentData.bloodFlowRate, treatmentData.dialysateFlowRate, treatmentData.duration, treatmentData.heparinDispensingRate, treatmentData.heparinBolusVolume, treatmentData.heparinStopTime, treatmentData.salineBolus, treatmentData.acidConcentrate, treatmentData.bicarbonateConcentrate, treatmentData.dialyzerType, treatmentData.dialysateTemp, treatmentData.arterialPressureLimitLow, treatmentData.arterialPressureLimitHigh, treatmentData.venousPressureLimitLow, treatmentData.venousPressureLimitHigh, treatmentData.bloodPressureMeasureInterval, treatmentData.rinsebackFlowRate }; GuiActionType messageAction = GuiActions::CreateTreatmentReq; emit validateParamsWithFW(messageAction, messageData); qDebug() << "Requesting FW validation of new treatment parameters"; LOG_EVENT("Requesting FW validation of new treatment parameters"); #endif } QString VCreateTreatment::enumToString(GuiRequestReasons vEnum) { const QMetaObject *mo = qt_getEnumMetaObject(vEnum); int enumIdx = mo->indexOfEnumerator(qt_getEnumName(vEnum)); QString mText = mo->enumerator(enumIdx).valueToKey(vEnum); if ( ! mText.isEmpty() ) { return mText; } return QString("[%1] Unknown Rejection Reason").arg(vEnum); } void VCreateTreatment::onFWValidationResponse(GuiActionType actionType, QVariantList messageData) { Q_UNUSED(actionType) if (actionType != GuiActionType::CreateTreatmentRsp) { QString logMsg = tr("Invalid action type received in %0").arg(__FUNCTION__); qDebug() << logMsg; LOG_ERROR(logMsg); return; } Model::TreatmentParametersResp response; TreatmentParametersRespData data = response.fromVariantList(messageData); bool success = true; if (data.requestValid != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit fwValidationFailed(enumToString(static_cast(data.requestValid))); } if (data.bloodFlowRate != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit bloodFlowRate_ValidationFailed(enumToString(static_cast(data.bloodFlowRate))); } if (data.dialysateFlowRate != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit dialysateFlowRate_ValidationFailed(enumToString(static_cast(data.dialysateFlowRate))); } if (data.duration != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit duration_ValidationFailed(enumToString(static_cast(data.duration))); } if (data.heparinDispensingRate != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit heparinBolusVolume_ValidationFailed(enumToString(static_cast(data.heparinDispensingRate))); } if (data.heparinBolusVolume != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit heparinBolusVolume_ValidationFailed(enumToString(static_cast(data.heparinBolusVolume))); } if (data.heparinStopTime != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit heparinStopTime_ValidationFailed(enumToString(static_cast(data.heparinStopTime))); } if (data.salineBolus != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit salineBolusVolume_ValidationFailed(enumToString(static_cast(data.salineBolus))); } if (data.acidConcentrate != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit acidConcentrate_ValidationFailed(enumToString(static_cast(data.acidConcentrate))); } if (data.bicarbonateConcentrate != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit bicarbonateConcentrate_ValidationFailed(enumToString(static_cast(data.bicarbonateConcentrate))); } if (data.dialyzerType != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit dialysateFlowRate_ValidationFailed(enumToString(static_cast(data.dialyzerType))); } if (data.dialysateTemp != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit dialysateTemp_ValidationFailed(enumToString(static_cast(data.dialysateTemp))); } if (data.arterialPressureLimitLow != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit arterialPressureLimitLow_ValidationFailed(enumToString(static_cast(data.arterialPressureLimitLow))); } if (data.arterialPressureLimitHigh != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit arterialPressureLimitHigh_ValidationFailed(enumToString(static_cast(data.arterialPressureLimitHigh))); } if (data.venousPressureLimitLow != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit venousPressureLimitLow_ValidationFailed(enumToString(static_cast(data.venousPressureLimitLow))); } if (data.venousPressureLimitHigh != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit venousPressureLimitHigh_ValidationFailed(enumToString(static_cast(data.venousPressureLimitHigh))); } if (data.bloodPressureMeasureInterval != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit bloodFlowRate_ValidationFailed(enumToString(static_cast(data.bloodPressureMeasureInterval))); } if (data.rinsebackFlowRate != Gui::GuiRequestReasons::REQUEST_REJECT_REASON_NONE) { success = false; emit rinsebackFlowRate_ValidationFailed(enumToString(static_cast(data.rinsebackFlowRate))); } qDebug() << "fw validation success = " << success; if (success) emit showConfirm(); } /** * @brief VCreateTreatment::onFinishedConfirm * Emits treatment data to gui controller, * who emits to application controller and the * application controller will save the data to disk. */ void VCreateTreatment::onFinishedConfirm() { qDebug() << "Finished create treatment confirm..."; QJsonObject obj { {"bloodFlowRate", QString::number(treatmentData.bloodFlowRate)}, {"dialysateFlowRate", QString::number(treatmentData.dialysateFlowRate)}, {"duration", QString::number(treatmentData.duration)}, {"heparinDispensingRate", QString::number(treatmentData.heparinDispensingRate)}, {"heparinBolusVolume", QString::number(treatmentData.heparinBolusVolume)}, {"heparinStopTime", QString::number(treatmentData.heparinStopTime)}, {"acidConcentrate", QString::number(treatmentData.acidConcentrate)}, {"bicarbonateConcentrate", QString::number(treatmentData.bicarbonateConcentrate)}, {"dialyzerType", QString::number(treatmentData.dialyzerType)}, {"dialysateTemp", QString::number(treatmentData.dialysateTemp)}, {"arterialPressureLimitLow", QString::number(treatmentData.arterialPressureLimitLow)}, {"arterialPressureLimitHigh", QString::number(treatmentData.arterialPressureLimitHigh)}, {"venousPressureLimitLow", QString::number(treatmentData.venousPressureLimitLow)}, {"venousPressureLimitHigh", QString::number(treatmentData.venousPressureLimitHigh)}, {"bloodPressureMeasureInterval",QString::number(treatmentData.bloodPressureMeasureInterval)}, {"rinsebackFlowRate", QString::number(treatmentData.rinsebackFlowRate)} }; saveNewTreatment(obj); emit didCreateTreatment(treatmentData); emit showPrime(); } /** * @brief ApplicationController::saveNewTreatment * Saves a new treatment to the filesystem. * @param doc - QJsonDocument containing the new treatment parameters. */ void VCreateTreatment::saveNewTreatment(QJsonObject obj) { QJsonDocument document(obj); int i = 0; while (QFile(QString("%0treatment%1.json").arg(Treatment_Profiles_Dir).arg(i)).exists()) { i++; } QString filename = QString("%0treatment%1.json").arg(Treatment_Profiles_Dir).arg(i); _FileSaver.concurrentSave(filename, document.toJson(), false); } void VCreateTreatment::onFinishedSaveNewTreatment(bool success) { if (success) { qDebug() << "------------------> Saved a new treatment."; } else { qDebug() << "------------------> Failed to save new treatment."; } } /** * @brief VCreateTreatment::onFnishedPrime * Called when priming qml selections are complete. * TODO: Placed here for now. Likely will be moved eventually. */ void VCreateTreatment::onFinishedPrime() { qDebug() << "Finished create treatment prime..."; emit showBegin(); } /** * @brief VCreateTreatment::start * Called when user is ready to begin a new treatment. * TODO: Placed here for now. Likely will be moved eventually. */ void VCreateTreatment::onStart() { qDebug() << "Starting Begin Treatment..."; } /** * @brief VCreateTreatment::validate * Validates the create new treatment input. * @param vData - the selected TreatmentData * @return true on success, false otherwise. */ bool VCreateTreatment::validate(const TreatmentData &vData) { bool success = true; if (!isbloodFlowRateSet) success = false; if (!isdialysateFlowRateSet) success = false; if (!isdurationSet) success = false; if (!isheparinDispensingRateSet) success = false; if (!isheparinBolusVolumeSet) success = false; if (!issalineBolusVolumeSet) success = false; if (!isacidConcentrateSet) success = false; if (!isbicarbonateConcentrateSet) success = false; if (!isdialyzerTypeSet) success = false; if (!isdialysateTempSet) success = false; if (!isarterialPressureLimitLowSet) success = false; if (!isarterialPressureLimitHighSet) success = false; if (!isvenousPressureLimitLowSet) success = false; if (!isvenousPressureLimitHighSet) success = false; if (!isbloodPressureMeasureIntervalSet) success = false; if (!isrinsebackFlowRateSet) success = false; if (!success) { qDebug() << "Not all values are set yet."; return false; } if (vData.bloodFlowRate < _bloodFlowRateMin || vData.bloodFlowRate > _bloodFlowRateMax) { emit bloodFlowRate_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.dialysateFlowRate < _dialysateFlowRateMin || vData.dialysateFlowRate > _dialysateFlowRateMax ) { emit dialysateFlowRate_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.duration < _durationMin || vData.duration > _durationMax) { emit duration_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.heparinDispensingRate < _heparinDispensingRateMin || vData.heparinDispensingRate > _heparinDispensingRateMax) { emit heparinDispensingRate_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.heparinBolusVolume < _heparinBolusVolumeMin || vData.heparinBolusVolume > _heparinBolusVolumeMax) { emit heparinBolusVolume_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.heparinStopTime < _heparinStopTimeMin || vData.heparinStopTime > _heparinStopTimeMax) { emit heparinStopTime_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (!indexInItems(vData.salineBolus, _salineBolusOptions)) { emit salineBolusVolume_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (!indexInItems(vData.acidConcentrate, _acidConcentrateOptions)) { emit acidConcentrate_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (!indexInItems(vData.bicarbonateConcentrate, _bicarbonateConcentrateOptions)) { emit bicarbonateConcentrate_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (!indexInItems(vData.dialyzerType, _dialyzerTypeOptions)) { emit dialyzerType_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.dialysateTemp < _dialysateTempMin || vData.dialysateTemp > _dialysateTempMax) { emit dialysateTemp_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.arterialPressureLimitLow < _arterialPressureLimitLowMin || vData.arterialPressureLimitLow > _arterialPressureLimitLowMax || vData.arterialPressureLimitLow > vData.arterialPressureLimitHigh) { emit arterialPressureLimitLow_ValidationFailed(uiRejections.value(LOW_HIGH_INCOMPATIBLE)); success = false; } if (vData.arterialPressureLimitHigh < _arterialPressureLimitHighMin || vData.arterialPressureLimitHigh > _arterialPressureLimitHighMax || vData.arterialPressureLimitHigh < vData.arterialPressureLimitLow) { emit arterialPressureLimitHigh_ValidationFailed(uiRejections.value(LOW_HIGH_INCOMPATIBLE)); success = false; } if (vData.venousPressureLimitLow < _venousPressureLimitLowMin || vData.venousPressureLimitLow > _venousPressureLimitLowMax || vData.venousPressureLimitLow > vData.venousPressureLimitHigh) { emit venousPressureLimitLow_ValidationFailed(uiRejections.value(LOW_HIGH_INCOMPATIBLE)); success = false; } if (vData.venousPressureLimitHigh < _venousPressureLimitHighMin || vData.venousPressureLimitHigh > _venousPressureLimitHighMax || vData.venousPressureLimitHigh < vData.venousPressureLimitLow) { emit venousPressureLimitHigh_ValidationFailed(uiRejections.value(LOW_HIGH_INCOMPATIBLE)); success = false; } if (vData.bloodPressureMeasureInterval < _bloodPressureMeasureIntervalMin || vData.bloodPressureMeasureInterval > _bloodPressureMeasureIntervalMax) { emit bloodPressureMeasureInterval_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (vData.rinsebackFlowRate < _rinsebackFlowRateMin || vData.rinsebackFlowRate > _rinsebackFlowRateMax) { emit rinsebackFlowRate_ValidationFailed(uiRejections.value(OUT_OF_RANGE)); success = false; } if (!success) { qDebug() << "Initial treatment parameter validation failed."; } return success; } /** * @brief VCreateTreatment::indexInItems * Checks if the index is in the QStringList * @param idx (int) the index * @param items (QStringList) The list of strings to check * @return True if it is in the list, false otherwise */ bool VCreateTreatment::indexInItems(quint32 idx, const QStringList &items) { if (idx > (quint32)items.size() - 1) return false; else return true; } /** * @brief VCreateTreatment::onUserModifiedParameters * Manages enabling / disabling the continue button */ void VCreateTreatment::onUserModifiedParameters() { setTreatmentData(); continueEnabled(validate(treatmentData)); emit continueEnabledChanged(continueEnabled()); }