Index: sources/StateController.cpp =================================================================== diff -u -r1cfa9a2d55e3bf954e5d02f12b73d3a8deda9e5c -r260229a756a3ee29c681132bdc4d32204c215ceb --- sources/StateController.cpp (.../StateController.cpp) (revision 1cfa9a2d55e3bf954e5d02f12b73d3a8deda9e5c) +++ sources/StateController.cpp (.../StateController.cpp) (revision 260229a756a3ee29c681132bdc4d32204c215ceb) @@ -6,13 +6,17 @@ void StateController::init() { + startTimer(1000); connect(&_ApplicationController, SIGNAL(didUnhandledMsgAppController(const QVariantList &)), - this , SLOT(onStateControllerUnhandledMsgReceived(const QVariantList &))); + this , SLOT(doStateControllerUnhandledMsgReceived(const QVariantList &))); - _dryDemo.connectToState("Idle" , this, &StateController::onIdleStateChange ); - _dryDemo.connectToState("Treatment" , this, &StateController::onTreatmentStateChange ); - _dryDemo.connectToState("Disinfect" , this, &StateController::onDisinfectStateChange ); + _dryDemo.connectToState("Idle" , this, &StateController::onIdleStateChange ); + _dryDemo.connectToState("Tx_Params" , this, &StateController::onTreatmentParamsStateChange ); + _dryDemo.connectToState("Disinfect" , this, &StateController::onDisinfectStateChange ); + + initReceivedMessagesHash(); // Todo move this? + initBroadcastMessagesHash(); } void StateController::quit() @@ -21,71 +25,106 @@ } -void StateController::onStateControllerUnhandledMsgReceived(const QVariantList &msg) +void StateController::doStateControllerUnhandledMsgReceived(const QVariantList &msg) { - qDebug() << "Message With new" << msg[0]; - - quint16 receivedMsgID = msg[0].toUInt(); - - if (!_dryDemo.isRunning()) { _dryDemo.start(); // TODO why cannot start the state machine somewhere else? } - qDebug() << "Active states" << _dryDemo.activeStateNames(); + MessageID receivedMsgID = static_cast(msg[0].toUInt()); - switch(receivedMsgID) - { - case 0x2500: // TODO enum for this - //qDebug() << "Right here"; - //_dryDemo.connectToState("Idle", _dryDemo.onEntry([&]() { - // qDebug() << "In onEntry" << _dryDemo.activeStateNames();})); - //qDebug() << "New state" << _dryDemo.activeStateNames(); - //QVariantList list; - //list.append(static_cast(0x2A00)); - //list.append(Can_Id::eChlid_HD_UI); - //list.append(68); - //list.append(34.56); - //emit _ApplicationController.didActionTransmit(list); - break; - case 0x2700: - if ( _dryDemo.activeStateNames()[0] == "Idle" ) { - qDebug() << "Current active"; - _dryDemo.submitEvent("Tx_Start_Rqst"); - } + if (_dryDemoMsgTable.contains(receivedMsgID)) { + _activeMessageID = receivedMsgID; - qDebug() << "Active state in 0x2700" << _dryDemo.activeStateNames()[0]; - - if (_dryDemo.activeStateNames()[0] == "Treatment") - { - qDebug() << "Active state in treatment" << _dryDemo.activeStateNames()[0]; - _dryDemo.submitEvent("Transition_back_2_idle"); - } - break; + _dryDemo.submitEvent("Transition_back_2_idle"); } +} - //qDebug() << "Msg Received" << msg; // TODO remove +void StateController::timerEvent(QTimerEvent *) +{ + if (_dryDemo.activeStateNames().size() > 0) { + QString currentState = _dryDemo.activeStateNames()[0]; + + if (_dryDemoBroadcast.contains(currentState)) { + //_dryDemoBroadcast[currentState][0].toUInt() += 1; + qDebug() << "In timer event" << currentState << _dryDemoBroadcast[currentState][0]; + QVariantList msg = _dryDemoBroadcast[currentState].mid(RESP_MSG_START_INDEX); + emit _ApplicationController.didActionTransmit(msg); + } + } } // ---------------------- Private methods ------------------ // +void StateController::initReceivedMessagesHash() +{ + // TODO these should be config + _dryDemoMsgTable[ID_UI_RQST_TX].append("Transition_2_Tx_Parms"); + _dryDemoMsgTable[ID_UI_RQST_TX].append(static_cast(ID_HD_OP_MODE)); + _dryDemoMsgTable[ID_UI_RQST_TX].append(Can_Id::eChlid_HD_UI); + _dryDemoMsgTable[ID_UI_RQST_TX].append(MODE_TPAR); + _dryDemoMsgTable[ID_UI_RQST_TX].append(0); +} + +void StateController::initBroadcastMessagesHash() +{ + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(ID_HD_NEW_PARAMS_RESP); + _dryDemoBroadcast["Tx_Params"].append(Can_Id::eChlid_HD_UI); + _dryDemoBroadcast["Tx_Params"].append(1); + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(275); + _dryDemoBroadcast["Tx_Params"].append(250); + _dryDemoBroadcast["Tx_Params"].append(60); + + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(100); + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(0); + _dryDemoBroadcast["Tx_Params"].append(0); + + _dryDemoBroadcast["Tx_Params"].append(250); + _dryDemoBroadcast["Tx_Params"].append(120); + _dryDemoBroadcast["Tx_Params"].append(100); + _dryDemoBroadcast["Tx_Params"].append(20); + _dryDemoBroadcast["Tx_Params"].append(0.000); + _dryDemoBroadcast["Tx_Params"].append(0.000); + _dryDemoBroadcast["Tx_Params"].append(36.00); +} + +void StateController::transitionToStandBy() +{ + QVariantList list; + list.append(static_cast(ID_HD_OP_MODE)); + list.append(Can_Id::eChlid_HD_UI); + list.append(MODE_STAN); + list.append(0); + emit _ApplicationController.didActionTransmit(list); +} + void StateController::onIdleStateChange(bool active) { if (active) { - REMOVE_THIS++; - qDebug() << "Idle from function" << REMOVE_THIS << active; + qDebug() << "Idle on entry"; + transitionToStandBy(); - if (REMOVE_THIS == 2) { - _dryDemo.submitEvent("Disinfect_Rqst"); - } + if (_activeMessageID != ID_NONE) { + QString transitionEvent = _dryDemoMsgTable[_activeMessageID][TRANSITION_EVENT_INDEX].toString(); + _dryDemo.submitEvent(transitionEvent); + } } + else { + QVariantList msg = _dryDemoMsgTable[_activeMessageID].mid(RESP_MSG_START_INDEX); + emit _ApplicationController.didActionTransmit(msg); + } } -void StateController::onTreatmentStateChange(bool active) +void StateController::onTreatmentParamsStateChange(bool active) { - qDebug() << "Calling treatment function"; if (active) { - qDebug() << "Treatment active"; + qDebug() << "In treatment params on entry"; } else { qDebug() << "Treatment inactive";