/*! * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * \copyright * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * \file MessageInterpreter.cpp * \author (last) Peter Lucia * \date (last) 15-Oct-2020 * \author (original) Behrouz NematiPour * \date (original) 26-Aug-2020 * */ #include "MessageInterpreter.h" // Qt #include // #include // Project #include "Logger.h" #include "format.h" using namespace Can; using namespace Model; #define DEBUG_SIGNAL(vID, vMODEL) // qDebug() << vID << vMODEL; // a macro to simplify the transmit message // would be better later to be replaced by a template method // like the notify method of received messages #define INTERPRET_TRANSMIT_MESSAGE(vMODEL) \ if ( ! length ) { logInvalidLength(vActionId); return false; } \ vCanId = vMODEL::canid(); \ vPayload = Format::fromVariant(vData); \ LOG_EVENT(vMODEL::toString(vData)); \ DEBUG_SIGNAL(0, typeid(vMODEL).name()) // another version of the INTERPRET_TRANSMIT_MESSAGE for empty messages // same later improvements apply to this MACRO as well. #define INTERPRET_TRSMT_MT_MESSAGE(vMODEL) \ if ( length ) { logInvalidLength(vActionId); return false; } \ vCanId = vMODEL::canid(); \ vPayload = Format::fromVariant(vData); \ LOG_EVENT(vMODEL::toString(vData)); \ DEBUG_SIGNAL(0, typeid(vMODEL).name()) /*! * \brief MessageInterpreter::MessageInterpreter * \details Constructor * \param parent - QObject parent owner object. * Qt handles the children destruction by their parent objects life-cycle. */ MessageInterpreter::MessageInterpreter(QObject *parent) : QObject(parent) { } /*! * \brief MessageInterpreter::notify * \details Checks and prepares the model with the Message Data * Regarding the type of message logs the message received. * Notifies observers by emitting the didActionReceive( < Data > ) signal * \param vMessage - The Denali message * \param vID - The Message ID to be checked against * \param vModel - The appropriate model for the Message Data * \param vData - A QVariant list of the Message Data which will be used for debugging if needed. * \return true on successful check and prepare. */ template bool MessageInterpreter::notify(const Message &vMessage, QVariantList &vData, Gui::GuiActionType vIdCheck) { bool ok = false; TModel tModel; if ( ! isValidMessage(vMessage, vIdCheck) ) return ok; ok = tModel.fromByteArray(vMessage.data); tModel.toVariantList(vData); // coco begin validated : Tested manually. This code will never go false // because the isValidMessage is catching errors. // only is checking here for developer safety if logic has changed. if ( ! ok ) return false; // coco end emit didActionReceive(tModel.data()); logReceivedMessage(tModel); DEBUG_SIGNAL(vIdCheck, typeid(TModel).name()) return ok; } /*! * \brief MessageInterpreter::isType * \details Checks if this is the message intended to be * \param vMessage - The message * \param vType - The type of the message to be checked against * \return true on correct type */ bool MessageInterpreter::isType(const Message &vMessage, Gui::GuiActionType vType) const { if ( vMessage.actionId != vType ) { LOG_DEBUG(QString("Incorrect expected ID '%1', got '%2'") .arg(vType) .arg(vMessage.actionId) ); return false; } return true; } /*! * \brief MessageInterpreter::isPayloadLenValid * \details Checks if the Data length has been defined for this type of message * if not logs Undefined Data Length error * if defined checks if the correct length of data is provided for this type of message. * if not logs Incorrect Data Length error * otherwise returns true * \param vMessage - The message * \param vType - The type of the message to be checked against * \return true on correct data length for the type vType */ bool MessageInterpreter::isPayloadLenValid(const Message &vMessage, Gui::GuiActionType vType) const { int len = vMessage.data.length(); QString mActionIdHexString = Format::toHexString(vMessage.actionId); if ( ! payloadLen.contains(vType) ) { LOG_DEBUG(QString("Undefined data length for received Message with ID '%1'").arg(mActionIdHexString)); return false; } if ( len < payloadLen[vType] ) { LOG_DEBUG(QString("Incorrect data length (%2 of %3) for received Message with ID '%1'") .arg(mActionIdHexString) .arg(len) .arg(payloadLen[vType]) ); return false; } return true; } /*! * \brief MessageInterpreter::logInvalidLength * \details Logs invalid data length for the message type vActionId * \param vActionId - Message Type */ void MessageInterpreter::logInvalidLength(const Gui::GuiActionType &vActionId) { QString mActionIdHexString = Format::toHexString(vActionId); LOG_DEBUG(QString("Incorrect data length for transmit message with ID '%1'") .arg(mActionIdHexString)); } /*! * \brief MessageInterpreter::validateMessage * \details Validate the message by checking its type and data * \param vMessage - The message * \param vType - The type of the message to be checked against * \return true on valid massage */ bool MessageInterpreter::isValidMessage(const Message &vMessage, Gui::GuiActionType vType) const { if ( ! isType (vMessage, vType) ) return false; if ( ! isPayloadLenValid(vMessage, vType) ) return false; return true; } /*! * \brief MessageInterpreter::printUnhandled * \details Prints out the formatted string of the vMessage of type Message * In case the Message ID of received CANBus message * is known to the interpreter but has not been handled/implemented. * \param vMessage - The message contains Unhandled Message ID */ void MessageInterpreter::printUnhandled(const Message &vMessage) const { if ( logUnhandledMessage(vMessage)) return; if ( gDisableUnhandledReport ) return; QString mActionIdHexString = Format::toHexString(vMessage.actionId, false, eLenMessageIDDigits); QString logMessage = tr("Unhandled Message ID (HD)") + '\n' + QString("%1 # %2 %3") .arg(int(vMessage.can_id), 3, 16, QChar('0')) .arg(mActionIdHexString) .arg(QString(vMessage.data.toHex('.'))).toUpper(); LOG_DEBUG(logMessage); } /*! * \brief MessageInterpreter::logReceived * \details Regarding the type of message logs the message received. * \param vModel - the MAbstract model type */ void MessageInterpreter::logReceivedMessage(const Model::MAbstract &vModel) { switch (vModel.typeText()) { case Model::MAbstract::Type_Enum::eDatum: LOG_DATUM(vModel.toString()); break; case Model::MAbstract::Type_Enum::eEvent: LOG_EVENT(vModel.toString()); break; } } /*! * \brief MessageInterpreter::interpretMessage * \details This method will be called * to interpret messages from UI regarding vActionId. * \param vActionId - The ActionID of type GuiActionType * to be interpreted to hex representation of Message ID. * \param vData - The data which has to be sent over the CANBus. * \param vPayload - The Payload of the frame of Type QByteArray of hex values * Which has been interpreted from vData of Type QVariantList * \return true if the vActionId is valid. * This return value will be used later for error handling. */ bool MessageInterpreter::interpretMessage(const Gui::GuiActionType &vActionId, const QVariantList &vData, QByteArray &vPayload, Can_Id &vCanId) { bool ok = true; if (vCanId == Can::Can_Id::eChlid_NONE ) vCanId = Can::Can_Id::eChlid_UI_HD ; vPayload.clear(); int length = vData.length(); // DEBUG: LOG_EVENT(QString("0x%0").arg(vActionId, 4, 16,QChar('0'))); switch (vActionId) { // notice we are in transmit mode case Gui::GuiActionType::ID_Acknow: // len: 0, can have zero len break; // No data, Just registered case Gui::GuiActionType::ID_KeepAlive: // len: 255, can have any len if ( length ) { // this message has a variable length vPayload = Format::fromVariant(vData[0]); } // DEBUG: LOG_EVENT_UI(QString("CheckIn")); break; case Gui::GuiActionType::ID_RawData: // len: 255, can have any len if ( length ) { // this message has a variable length vPayload = Format::fromVariant(vData[0]); } LOG_EVENT_UI(QString("RawData")); break; case Gui::GuiActionType::ID_PowerOff : INTERPRET_TRANSMIT_MESSAGE(AdjustPowerOffRequestData ); break; // POST case Gui::GuiActionType::ID_UIPostFinalResultData : INTERPRET_TRANSMIT_MESSAGE(AdjustUIPostFinalResultRequestData ); break; // Settings case Gui::GuiActionType::ID_AdjustVersionsUIRsp : INTERPRET_TRANSMIT_MESSAGE(AdjustVersionsResponseData ); break; case Gui::GuiActionType::ID_AdjustVersionsUIReq : INTERPRET_TRANSMIT_MESSAGE(AdjustVersionsRequestData ); break; case Gui::GuiActionType::ID_AdjustHDDateTimeReq : INTERPRET_TRANSMIT_MESSAGE(AdjustHDDateTimeRequestData ); break; case Gui::GuiActionType::ID_AdjustDGDateTimeReq : INTERPRET_TRANSMIT_MESSAGE(AdjustDGDateTimeRequestData ); break; case Gui::GuiActionType::ID_AdjustServiceReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustServiceRequestData ); break; // Pre-Treatment case Gui::GuiActionType::ID_AdjustInitTreatmentReq : INTERPRET_TRANSMIT_MESSAGE(AdjustInitTreatmentRequestData ); break; case Gui::GuiActionType::ID_AdjustParametersValidationReq : INTERPRET_TRANSMIT_MESSAGE(AdjustParametersValidationRequestData ); break; case Gui::GuiActionType::ID_AdjustParametersConfirmReq : INTERPRET_TRANSMIT_MESSAGE(AdjustParametersConfirmRequestData ); break; case Gui::GuiActionType::ID_AdjustWaterSampleReq : INTERPRET_TRANSMIT_MESSAGE(AdjustWaterSampleRequestData ); break; case Gui::GuiActionType::ID_AdjustWaterSampleResultReq : INTERPRET_TRANSMIT_MESSAGE(AdjustWaterSampleResultRequestData ); break; case Gui::GuiActionType::ID_AdjustConsumablesConfirmReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustConsumablesConfirmRequestData ); break; case Gui::GuiActionType::ID_AdjustDisposablesConfirmReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustDisposablesConfirmRequestData ); break; case Gui::GuiActionType::ID_AdjustDisposablesPrimeReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustDisposablesPrimeRequestData ); break; case Gui::GuiActionType::ID_AdjustPatientConnectionBeginReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustPatientConnectionBeginRequestData ); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationInitReq : INTERPRET_TRANSMIT_MESSAGE(AdjustUltrafiltrationInitRequestData ); break; case Gui::GuiActionType::ID_AdjustPatientConnectionConfirmReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustPatientConnectionConfirmRequestData ); break; case Gui::GuiActionType::ID_AdjustStartTreatmentReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustStartTreatmentRequestData ); break; // in-treatment case Gui::GuiActionType::ID_AdjustDurationReq : INTERPRET_TRANSMIT_MESSAGE(AdjustDurationRequestData ); break; case Gui::GuiActionType::ID_AdjustBloodDialysateReq : INTERPRET_TRANSMIT_MESSAGE(AdjustBloodDialysateRequestData ); break; case Gui::GuiActionType::ID_AdjustPressuresLimitsReq : INTERPRET_TRANSMIT_MESSAGE(AdjustPressuresLimitsRequestData ); break; case Gui::GuiActionType::ID_AdjustSalineReq : INTERPRET_TRANSMIT_MESSAGE(AdjustSalineRequestData ); break; case Gui::GuiActionType::ID_AdjustHeparinReq : INTERPRET_TRANSMIT_MESSAGE(AdjustHeparinRequestData ); break; // in-treatment - ultrafiltration case Gui::GuiActionType::ID_AdjustUltrafiltrationStateReq : INTERPRET_TRANSMIT_MESSAGE(AdjustUltrafiltrationStateRequestData ); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationEditReq : INTERPRET_TRANSMIT_MESSAGE(AdjustUltrafiltrationEditRequestData ); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationConfirmReq : INTERPRET_TRANSMIT_MESSAGE(AdjustUltrafiltrationConfirmRequestData ); break; // End-Treatment case Gui::GuiActionType::ID_AdjustRinsebackReq : INTERPRET_TRANSMIT_MESSAGE(AdjustRinsebackRequestData ); break; case Gui::GuiActionType::ID_AdjustRecirculateReq : INTERPRET_TRANSMIT_MESSAGE(AdjustRecirculateRequestData ); break; case Gui::GuiActionType::ID_AdjustTreatmentEndReq : INTERPRET_TRANSMIT_MESSAGE(AdjustTreatmentEndRequestData ); break; // Post-Treatment case Gui::GuiActionType::ID_AdjustPatientDisconnectionConfirmReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustPatientDisconnectionConfirmRequestData ); break; case Gui::GuiActionType::ID_AdjustDisposablesRemovalConfirmReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustDisposablesRemovalConfirmRequestData ); break; case Gui::GuiActionType::ID_AdjustTreatmentLogReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustTreatmentLogRequestData ); break; // Disinfection case Gui::GuiActionType::ID_AdjustDisinfectReq : INTERPRET_TRANSMIT_MESSAGE(AdjustDisinfectRequestData ); break; case Gui::GuiActionType::ID_AdjustChemicalConfirmReq : INTERPRET_TRSMT_MT_MESSAGE(AdjustChemicalConfirmRequestData ); break; // alarms case Gui::GuiActionType::ID_AlarmSilenceReq : INTERPRET_TRANSMIT_MESSAGE(AlarmSilenceRequestData ); break; case Gui::GuiActionType::ID_AlarmUserActionReq : INTERPRET_TRANSMIT_MESSAGE(AlarmUserActionRequestData ); break; case Gui::GuiActionType::ID_AlarmVolumeSetReq : INTERPRET_TRANSMIT_MESSAGE(AdjustHDAlarmVolumeRequestData ); break; // coco begin validated: Manually tested. This model class is a placeholder for the message 63(0x3F00) and there is no use case for this now. case Gui::GuiActionType::ID_AlarmClearedConditionReq : INTERPRET_TRANSMIT_MESSAGE(AlarmClearedConditionRequestData ); break; case Gui::GuiActionType::ID_AlarmActiveListReq : INTERPRET_TRSMT_MT_MESSAGE(AlarmActiveListRequestData ); break; case Gui::GuiActionType::ID_AlarmTriggered : INTERPRET_TRANSMIT_MESSAGE(AlarmTriggeredRequestData ); break; // coco end default: QString mActionIdHexString = Format::toHexString(vActionId); LOG_DEBUG(tr("Unknown transmit Message with ID '%1'").arg(mActionIdHexString)); ok = false; break; } return ok; } /*! * \brief MessageInterpreter::identifySource * \details Identifies the source of the message regarding the channel id * \return the source of the message in enum * \sa can::Can_Source */ Can_Source MessageInterpreter::identifySource(Can_Id vCanId, QString *vText) { switch (vCanId) { case eChlid_HD_DG : // 0x008, ///< HD => DG case eChlid_HD_UI : // 0x020, ///< HD => UI case eChlid_HD_Alarm: // 0x001, ///< HD alarm broadcast case eChlid_HD_Sync : // 0x040, ///< HD sync broadcast if (vText) *vText = "HD"; return Can_Source::eCan_HD; case eChlid_DG_HD : // 0x010, ///< DG => HD case eChlid_DG_UI : // 0x070, ///< DG => UI case eChlid_DG_Alarm: // 0x002, ///< DG alarm broadcast case eChlid_DG_Sync : // 0x080, ///< DG sync broadcast if (vText) *vText = "DG"; return Can_Source::eCan_DG; default: if (vText) *vText = "XX"; return Can_Source::eCan_Unknown; } } /*! * \brief MessageInterpreter::identifyDestination * \details Identifies the destination of the message regarding the channel id for the acknowledges * \return the destination of the message in enum * \sa can::Can_Source * \sa can::Can_Id */ Can_Id MessageInterpreter::identifyDestination(Can_Id vCanId, QString *vText) { switch (vCanId) { case eChlid_HD_UI : // 0x020, ///< HD => UI case eChlid_HD_Alarm: // 0x001, ///< HD alarm broadcast case eChlid_HD_Sync : // 0x040, ///< HD sync broadcast if (vText) *vText = "HD"; return Can_Id::eChlid_UI_HD; case eChlid_DG_UI : // 0x070, ///< DG => UI case eChlid_DG_Alarm: // 0x002, ///< DG alarm broadcast case eChlid_DG_Sync : // 0x080, ///< DG sync broadcast if (vText) *vText = "DG"; return Can_Id::eChlid_UI_DG; default: if (vText) *vText = "XX"; return Can_Id::eChlid_UI_Sync; } } /*! * \brief MessageInterpreter::interpretMessage * \details This method will call appropriate message interpreter * for received messages from HD or DG regarding the Can_Id. * \param vCan_Id - The Channel Id of the CANBus frame. * \param vMessage - The complete message of type Message which needs to be interpreted. * \param vActionId - The ActionId of GuiActionType which will be extracted from vMessage. * \param vData - The values of QVariantList which is understandable for UI * and has been extracted from hex values of the CANBus Message Payload * regarding each Message Id definition. * \return true if the message channel is in the range which can be interpreted, false otherwise. * This return value will be used later to emit MessageDispatcher::didActionReceive signal or not */ bool MessageInterpreter::interpretMessage(const Message &vMessage, QVariantList &vData) { bool ok = false; switch (identifySource(vMessage.can_id)) { case Can_Source::eCan_HD: ok = interpretMessage_HD(vMessage, vData); break; case Can_Source::eCan_DG: ok = interpretMessage_DG(vMessage, vData); break; default : break; } return ok; } /*! * \brief MessageInterpreter::interpretMessage_HD * \details This method will be called * for received messages from HD to interpret the vMessage of type Message * to vData of type QVariantList which UI understands regarding the Can_Id. * \param vMessage - The complete message of type Message which needs to be interpreted. * \param vActionId - The ActionId of GuiActionType which will be extracted from vMessage. * \param vData - The values of QVariantList which is understandable for UI * and has been extracted from hex values of the CANBus Message Payload * in vMessage of type Message regarding each Message Id definition. * \return true if the message CANBus channel is in the range which can be interpreted, false otherwise. * This return value will be used later to emit MessageDispatcher::didActionReceive signal or not */ bool MessageInterpreter::interpretMessage_HD(const Message &vMessage, QVariantList &vData) { bool ok = false; vData.clear(); switch (vMessage.actionId) { // notice we are in receive mode // ----- Debug case Gui::GuiActionType::ID_CANBusFaultCount : ok = canbusFaultCountData (vMessage, vData); break; // TODO : implement notify<>() case Gui::GuiActionType::ID_HDDebugText : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDDebugText ); break; case Gui::GuiActionType::ID_HDGeneralEvent : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDGeneralEvent ); break; // ----- Events case Gui::GuiActionType::ID_Acknow : ok = true; break; // TODO : implement notify<>() case Gui::GuiActionType::ID_PowerOff : ok = notify(vMessage, vData, Gui::GuiActionType::ID_PowerOff ); break; case Gui::GuiActionType::ID_ShuttingDown : ok = true; LOG_EVENT("HD,ShuttingDown"); break; // TODO : implement notify<>() // ----- POST case Gui::GuiActionType::ID_HDPostSingleResultData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDPostSingleResultData ); break; case Gui::GuiActionType::ID_HDPostFinalResultData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDPostFinalResultData ); break; // ----- Settings case Gui::GuiActionType::ID_AdjustVersionsHDReq : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustVersionsHDReq ); break; case Gui::GuiActionType::ID_AdjustVersionsHDRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustVersionsHDRsp ); break; case Gui::GuiActionType::ID_AdjustSerialHDRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustSerialHDRsp ); break; case Gui::GuiActionType::ID_AdjustServiceHDRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustServiceHDRsp ); break; case Gui::GuiActionType::ID_AdjustHDDateTimeRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustHDDateTimeRsp ); break; // ----- States case Gui::GuiActionType::ID_HDOperationModeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDOperationModeData ); break; case Gui::GuiActionType::ID_PreTreatmentStates : ok = notify(vMessage, vData, Gui::GuiActionType::ID_PreTreatmentStates ); break; case Gui::GuiActionType::ID_TreatmentStates : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentStates ); break; case Gui::GuiActionType::ID_PostTreatmentStates : ok = notify(vMessage, vData, Gui::GuiActionType::ID_PostTreatmentStates ); break; // ----- Datum case Gui::GuiActionType::ID_HDAccelerometerData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDAccelerometerData ); break; case Gui::GuiActionType::ID_HDSyringePumpData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_HDSyringePumpData ); break; // ----- Datum - Treatment case Gui::GuiActionType::ID_TreatmentRanges : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentRanges ); break; // ----- Pre-Treatment - Datum - Progress case Gui::GuiActionType::ID_SelfTestNoCartridgeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_SelfTestNoCartridgeData ); break; case Gui::GuiActionType::ID_SelfTestDryData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_SelfTestDryData ); break; case Gui::GuiActionType::ID_DisposablesPrimeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DisposablesPrimeData ); break; // ----- Pre-Treatment - Adjust case Gui::GuiActionType::ID_AdjustInitTreatmentRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustInitTreatmentRsp ); break; case Gui::GuiActionType::ID_AdjustParametersValidationRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustParametersValidationRsp ); break; case Gui::GuiActionType::ID_AdjustWaterSampleRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustWaterSampleRsp ); break; case Gui::GuiActionType::ID_AdjustDisposablesPrimeRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustDisposablesPrimeRsp ); break; case Gui::GuiActionType::ID_AdjustPatientConnectionBeginRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustPatientConnectionBeginRsp ); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationInitRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustUltrafiltrationInitRsp ); break; case Gui::GuiActionType::ID_AdjustPatientConnectionConfirmRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustPatientConnectionConfirmRsp ); break; case Gui::GuiActionType::ID_AdjustStartTreatmentRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustStartTreatmentRsp ); break; // ----- In-Treatment - Datum case Gui::GuiActionType::ID_TreatmentTime : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentTime ); break; case Gui::GuiActionType::ID_BloodFlow : ok = notify(vMessage, vData, Gui::GuiActionType::ID_BloodFlow ); break; case Gui::GuiActionType::ID_DialysateInletFlow : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DialysateInletFlow ); break; case Gui::GuiActionType::ID_DialysateOutletFlow : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DialysateOutletFlow ); break; case Gui::GuiActionType::ID_PressureOcclusion : ok = notify(vMessage, vData, Gui::GuiActionType::ID_PressureOcclusion ); break; case Gui::GuiActionType::ID_Saline : ok = notify(vMessage, vData, Gui::GuiActionType::ID_Saline ); break; case Gui::GuiActionType::ID_Heparin : ok = notify(vMessage, vData, Gui::GuiActionType::ID_Heparin ); break; case Gui::GuiActionType::ID_Rinseback : ok = notify(vMessage, vData, Gui::GuiActionType::ID_Rinseback ); break; case Gui::GuiActionType::ID_Recirculate : ok = notify(vMessage, vData, Gui::GuiActionType::ID_Recirculate ); break; case Gui::GuiActionType::ID_BloodPrime : ok = notify(vMessage, vData, Gui::GuiActionType::ID_BloodPrime ); break; case Gui::GuiActionType::ID_TreatmentStop : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentStop ); break; // ----- In-Treatment Adjust case Gui::GuiActionType::ID_AdjustDurationRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustDurationRsp ); break; case Gui::GuiActionType::ID_AdjustBloodDialysateRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustBloodDialysateRsp ); break; case Gui::GuiActionType::ID_AdjustPressuresLimitsRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustPressuresLimitsRsp ); break; case Gui::GuiActionType::ID_AdjustSalineRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustSalineRsp ); break; case Gui::GuiActionType::ID_AdjustUltrafiltrationStateRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustUltrafiltrationStateRsp ); break; case Gui::GuiActionType::ID_AdjustHeparinRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustHeparinRsp ); break; case Gui::GuiActionType::ID_AdjustRinsebackRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustRinsebackRsp ); break; case Gui::GuiActionType::ID_AdjustRecirculateRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustRecirculateRsp ); break; case Gui::GuiActionType::ID_AdjustTreatmentEndRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustTreatmentEndRsp ); break; // ----- Post-Treatment Adjust case Gui::GuiActionType::ID_AdjustDisposableRemovalConfirmRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustDisposableRemovalConfirmRsp ); break; case Gui::GuiActionType::ID_AdjustTreatmentLogRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustTreatmentLogRsp ); break; // ----- Treatment Log case Gui::GuiActionType::ID_TreatmentLogAvrgeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentLogAvrgeData ); break; case Gui::GuiActionType::ID_TreatmentLogAlarmData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentLogAlarmData ); break; case Gui::GuiActionType::ID_TreatmentLogEventData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_TreatmentLogEventData ); break; // ----- Disinfect - States case Gui::GuiActionType::ID_DisinfectStates : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DisinfectStates ); break; // ----- Disinfection - Adjust case Gui::GuiActionType::ID_AdjustDisinfectRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustDisinfectRsp ); break; case Gui::GuiActionType::ID_AdjustChemicalConfirmRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustChemicalConfirmRsp ); break; // these need to be standard and use notify as well case Gui::GuiActionType::ID_AdjustUltrafiltrationEditRsp : ok = adjustUltrafiltrationEdit (vMessage, vData); break; // TODO : implement notify<>() case Gui::GuiActionType::ID_AdjustUltrafiltrationConfirmRsp : ok = adjustUltrafiltrationConfirm (vMessage, vData); break; // TODO : implement notify<>() // ----- Alarms case Gui::GuiActionType::ID_AlarmStatus : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AlarmStatus ); break; case Gui::GuiActionType::ID_AlarmTriggered : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AlarmTriggered ); break; case Gui::GuiActionType::ID_AlarmCleared : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AlarmCleared ); break; case Gui::GuiActionType::ID_AlarmVolumeSetRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AlarmVolumeSetRsp ); break; // coco begin validated: Manually tested. This model class is a placeholder for the message 63(0x3F00) and there is no use case for this now. case Gui::GuiActionType::ID_AlarmClearedConditionRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AlarmClearedConditionRsp ); break; case Gui::GuiActionType::ID_AlarmActiveListRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AlarmActiveListRsp ); break; // coco end // unhandled messages: these will only be logged as received message // there has nothing been defined for these messages. default : printUnhandled(vMessage); break; } return ok; } /*! * \brief MessageInterpreter::interpretMessage_DG * \details This method will be called * for received messages from DG to interpret the vMessage of type Message * to vData of type QVariantList which UI understands regarding the Can_Id. * \param vMessage - The complete message of type Message which needs to be interpreted. * \param vActionId - The ActionId of GuiActionType which will be extracted from vMessage. * \param vData - The values of QVariantList which is understandable for UI * and has been extracted from hex values of the CANBus Message Payload * in vMessage of type Message regarding each Message Id definition. * \return true if the message CANBus channel is in the range which can be interpreted, false otherwise. * This return value will be used later to emit MessageDispatcher::didActionReceive signal or not */ bool MessageInterpreter::interpretMessage_DG(const Message &vMessage, QVariantList &vData) { bool ok = false; vData.clear(); switch (vMessage.actionId) { // notice we are in receive mode case Gui::GuiActionType::ID_Acknow : ok = true; break; // TODO : implement notify<>() case Gui::GuiActionType::ID_DGCheckIn: // TODO : implement notify<>() ok = true; LOG_EVENT(QString("DG,CheckIn," + QVariant(vData).toStringList().join(','))); break; // ----- POST case Gui::GuiActionType::ID_DGPostSingleResultData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGPostSingleResultData ); break; case Gui::GuiActionType::ID_DGPostFinalResultData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGPostFinalResultData ); break; // DG data messages case Gui::GuiActionType::ID_DGROPumpData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGROPumpData ); break; case Gui::GuiActionType::ID_DGPressuresData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGPressuresData ); break; case Gui::GuiActionType::ID_DGDrainPumpData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGDrainPumpData ); break; case Gui::GuiActionType::ID_DGOperationModeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGOperationModeData ); break; case Gui::GuiActionType::ID_DGReservoirData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGReservoirData ); break; case Gui::GuiActionType::ID_DGValvesStatesData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGValvesStatesData ); break; case Gui::GuiActionType::ID_DGHeatersData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGHeatersData ); break; case Gui::GuiActionType::ID_DGLoadCellReadingsData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGLoadCellReadingsData ); break; case Gui::GuiActionType::ID_DGTemperaturesData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGTemperaturesData ); break; case Gui::GuiActionType::ID_DGDebugText : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGDebugText ); break; case Gui::GuiActionType::ID_DGGeneralEvent : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGGeneralEvent ); break; case Gui::GuiActionType::ID_DGAccelerometerData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGAccelerometerData ); break; // ----- Datum - Pre-Treatment progress case Gui::GuiActionType::ID_DGFilterFlushData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DGFilterFlushData ); break; // ----- Datum - Disinfection case Gui::GuiActionType::ID_DisinfectDGFlushTimeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DisinfectDGFlushTimeData ); break; case Gui::GuiActionType::ID_DisinfectDGHeatTimeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DisinfectDGHeatTimeData ); break; case Gui::GuiActionType::ID_DisinfectDGChemicalTimeData : ok = notify(vMessage, vData, Gui::GuiActionType::ID_DisinfectDGChemicalTimeData ); break; // DG Response Messages case Gui::GuiActionType::ID_AdjustVersionsDGRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustVersionsDGRsp ); break; case Gui::GuiActionType::ID_AdjustSerialDGRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustSerialDGRsp ); break; case Gui::GuiActionType::ID_AdjustServiceDGRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustServiceDGRsp ); break; case Gui::GuiActionType::ID_AdjustDGDateTimeRsp : ok = notify(vMessage, vData, Gui::GuiActionType::ID_AdjustDGDateTimeRsp ); break; // unhandled messages: these will only be logged as received message // there has nothing been defined for these messages. default : printUnhandled(vMessage); break; } return ok; } // ---------- ---------- Message handlers ---------- ---------- // // ---------- ---------- ---------- ---------- ---------- Debug ---------- ---------- ---------- ---------- ---------- // /*! * \brief MessageInterpreter::canbusFaultCountData * \details This method interprets Fault Count message data * in vMessage of type Message. This message is only used for debugging purposes. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Fault Count data * \return true if the data can be extracted as defined for Fault Count Message ID */ bool MessageInterpreter::canbusFaultCountData(const Message &vMessage, QVariantList &vData) { // TODO : review other methods bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_CANBusFaultCount) ) return ok; QVariantList mData; int index = 0; Types::U32 mCanBUSFaultCount; ok = GetValue(vMessage.data, index, mCanBUSFaultCount); // coco begin validated : developer safety if for any reason length of CanBUSFaultCount set to 0 if (ok) { // coco end vData += mCanBUSFaultCount.value; } return ok; } // ---------- ---------- ---------- ---------- ---------- Adjustments ---------- ---------- ---------- ---------- ---------- // /*! * \brief MessageInterpreter::adjustUltrafiltrationEditData * \details This method interprets Treatment Ultrafiltration Volume Adjustment Response message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Treatment Ultrafiltration Volume Adjustment Response data * \return true if the data can be extracted as defined for Treatment Ultrafiltration Volume Adjustment Response Message ID */ bool MessageInterpreter::adjustUltrafiltrationEdit(const Message &vMessage, QVariantList &vData) { bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AdjustUltrafiltrationEditRsp) ) return ok; Model::MAdjustUltrafiltrationEditResponse mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); // TODO : Not Standard mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; } /*! * \brief MessageInterpreter::adjustUltrafiltrationConfirmData * \details This method interprets Treatment Ultrafiltration Volume Adjustment Confirm Response message data * in vMessage of type Message. * \param vMessage - The vMessage of type Message which contains all the data, * require to be interpreted. * \param vData - Treatment Ultrafiltration Volume Adjustment Confirm Response data * \return true if the data can be extracted as defined for Treatment Ultrafiltration Volume Adjustment Confirm Response Message ID */ bool MessageInterpreter::adjustUltrafiltrationConfirm(const Message &vMessage, QVariantList &vData) { bool ok = false; if ( ! isValidMessage(vMessage, Gui::GuiActionType::ID_AdjustUltrafiltrationConfirmRsp) ) return ok; Model::MAdjustUltrafiltrationConfirmResponse mData; ok = mData.fromByteArray(vMessage.data); LOG_EVENT("HD," + mData.toString()); // TODO : Not Standard mData.toVariantList(vData); emit didActionReceive(mData.data()); return ok; } /*! * \brief MessageInterpreter::updateUnhandledMessages * \return this method is converting the general settings messages/unhandled group of settings to the message interpreter specific map structure. * it is done for performance to keep the lookup table shorter and faster. * This method is called by the chain of events when Application controller is done reading the settings and emits it didSettingsDone, * which Message dispatcher is listening to will call this function in its signal handler slot. */ void MessageInterpreter::updateUnhandledMessages() { // DEBUG: qDebug() << _Settings.groups("messages/unhandled"); for(const auto group: _Settings.groups("Messages/Unhandled")) { // DEBUG: qDebug() << _Settings.keys(group); bool ok; quint16 id = QString(group).toUInt(&ok,16); if (!ok) continue; _messageList[ id ] = _Settings.keys(group); } } /*! * \brief MessageInterpreter::logUnhandledMessage * \details Search in the list of the unhandled messages and tries to extract the data and log it. * \param vMessage - the received message. * \return true if the message data can be extracted and logged. */ bool MessageInterpreter::logUnhandledMessage(const Message &vMessage) const { bool ok = false; quint16 id = vMessage.actionId; if (_messageList.contains(id)) { ok = true; QStringList items = _messageList[id]; QString logString; int index = 0; for ( int i = 0; i < items.count(); i++ ) { QString item = items[i]; if (i == 0) { switch (identifySource(vMessage.can_id)) { case Can_Source::eCan_HD: logString = "HD,~" + item; break; case Can_Source::eCan_DG: logString = "DG,~" + item; break; default : logString = "XX,~" + item; break; } } else { if ( item == "F32" ) { Types::F32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "S32" ) { Types::S32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "U32" ) { Types::U32 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "S16" ) { Types::S16 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "U16" ) { Types::U16 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "S08" ) { Types::S08 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } else if ( item == "U08" ) { Types::U08 param; if (! GetValue(vMessage.data, index, param )) logString += ",?"; else logString += "," + QString::number(param.value); } } } LOG_DATUM(logString); } return ok; }