#pragma once #include #include #include #include "DryDemoDefines.h" #include "LeahiDryDemoXMLStates.h" #include "main.h" #include "MessageGlobals.h" #define _StateController StateController::I() // TODO check illegal state transition class StateController : public QObject { Q_OBJECT SINGLETON(StateController) public: void init(); // TODO move these in slots void quit(); public slots: void doStateControllerUnhandledMsgReceived(const QVariantList &msg); protected: void timerEvent(QTimerEvent *) override; private: struct Treatment_Status_Vars { bool hasTxParamsBeenInitialized; quint32 treatmentElapsedTimeMS; User_Command_ID userCmd; QString stateTransitionEvent; float accumFluidBolusDeliveredML; float totalFluidBolusDeliveredML; Fluid_Bolus_States fluidBolusState; Treatment_States txState; Dialysis_States dialysisSubState; // UF variables float presUFRateMLPM; float ufVolDeliveredL; bool validateUFVolReceived; float validateUFVolGoalL; float validateUFRateLHR; // Tx dur variables bool validateTxDurReceived; quint32 validateTxDurMin; quint32 validateHepDurMin; // Alarm variabls bool isAnAlarmActive; }; struct Treatment_Params { bool finalConfirmation; quint32 treatmentModality; quint32 hdfDilution; quint32 bloodFlowRateMLPM; quint32 dialysateFlowRateMLPM; quint32 treatmentDurationMin; quint32 heparinDeliveryDurationMin; quint32 heparinType; quint32 dryBicarbCartSize; quint32 sodiumMEQL; quint32 bicarbobateMEQL; quint32 dialyzerType; quint32 fluidBlousVolumeML; quint32 bloodPressureIntervalMin; quint32 rinsebackVolumeML; quint32 hepatitisBStatus; quint32 acidConcentrate; float substitutionFluidVolumeL; float heparinBolusVolumeML; float heparinDeliveryRateMLHR; float dialysateTemperatureC; float acidKConcentrateConversionFactor; float ufPreWeightKG; float ufEstimatedTargetWeightKG; float ufVolumeL; }; QHash_transitionEventsFromStandby; QList _broadcastMessages; QList _sendMessages; QHash _treatmentRcvdMessages; QRandomGenerator _randNumGenerator; Treatment_Params _treatmentParams; Treatment_Status_Vars _treatmentStatus; bool _isBroadcastListReady; // TODO use a mutex? bool _isSendListReady; // Todo use a mutex? bool _hasUserConfirmedToProceed; // TODO remove? drydemoXMLStates _dryDemo; // CAN function(s) void sendMessages(bool isBroadcast); void handleDryDemoMenuRequestMessage(); // State handlers void onStandbyStateChange(bool active); void onPretreatmentStateChange(bool active); void onBloodPrimeStateChange(bool active); void onTreatmentStateChange(bool active); void onTreatmentEndStateChange(bool active); // Helper methods void handleTDOpModeTransitionBroadcastData(const Can::TD_OP_MODE mode, const quint32 subMode, const Broadcast_Type BCType); void setTreatmentParametersFromUI(const QVariant &payload); // TODO move the implementation to pretx cpp void handleUIResponseMessage(const Can::Message_ID_Enum msgID, const quint32 acceptReject, const QVariantList &rejectReasons = {}); void resetDryDemoVariables(); void handleBackToStandbyEvent(State_Status &status); quint32 extractU32DataFromReceivedMessage(const Can::Message_ID_Enum msgID, qint32 index); float extractF32DataFromReceivedMessage(const Can::Message_ID_Enum msgID, qint32 index); // Standby methods void handleInitiateTreatmentRequestMessage(State_Status &status); // Blood prime methods float getBloodPrimeRampStepML(); void handleBloodPrimeBroadcastData(const float accumulatedVolML, const quint32 primeTimeOutS, const quint32 pauseCountDownS, const Broadcast_Type BCType); void handleBloodPrimeVolML(const bool resume, const float rampStepML, const bool hasFlowChanged, quint32 &currBloodFlowMLPM, float &accumBloodVolML); // Treatment methods void setTreatmentParametersToDefault(); void initializeTreatmentTimeAndUltrafiltration(); void handleTreatmentSetpointsBroadcastData(const quint32 bloodFlowMLPM, const quint32 dialFlowMLPM, const float dialTempC, const Broadcast_Type BCType); void handleTreatmentStatesBroadcastData(const QList &txStates, const Broadcast_Type BCType); void handleFluidBolusRequestMessage(); void handleFluidBolusFluidDelivery(const quint32 bloodFlowMLPM); void handleFluidBolusBroadcastData(const Broadcast_Type BCType); void handleTDPressureProcessAndBroadcastData(const Broadcast_Type BCType); void handleTreatmentTimeProcessAndBroadcastData(const Broadcast_Type BCType); void handleTreatmentPrescriptionEditRequestMessage(); void handleSetPointBloodFlowChange(quint32 &bloodFlowMLPM, bool &hasBloodFlowChanged); void handleDDGenDialysateModeBroadcastData(const Broadcast_Type BCType); void handleSetPointDialysateFlowChange(); void handleDDConductivityBroadcastData(const Broadcast_Type BCType); void handleTDBloodPressureBroadcastData(const Broadcast_Type BCType); void handleUltrafiltrationBroadcastData(const Broadcast_Type BCType); void handleSetPointDialysateTemperatureChange(); void handleUltrafiltrationPauseResume(); void handleUltrafiltrationVolumeChangeValidation(); void handleTreatmentSettingBroadcastData(const Broadcast_Type BCType); void handleUltrafiltrationVolumeChangeConfirm(); void handleTreatmentTimeChangeValidation(); void handleTreatmentTimeChangeConfirm(); // Treatment end methods };