#include "UpperCommManager.h" #include "PowerManger.h" #include "driver.h" #include "fsm/PowerManagerFsm.hpp" UpperCommManager::UpperCommManager(PowerManger* pm) : m_pm(pm), m_secdistStateMsg(), m_setOpConditionMsg(), m_depthMsg(), m_taskStartMsg(), m_equInsStateMsg(), m_equStateMsg(), m_setPMSysMsg(), m_disSysStateMsg(), m_currentStateMsg(), m_pmStateMsg(), m_fcsStateMsg(), m_liBatStateMsg(), m_setOpModeMsg(), m_taskStopMsg(), m_setDevSwitchMsg(), m_sysStateMsg(), m_msCmd(), m_driver() { } bool UpperCommManager::processUpperMsg(const string& key, const string& strJson) { if (key == m_sysStateMsg.key) { #ifdef _DEBUG m_sysStateMsg_st_debug = std::make_pair(key, strJson); #endif m_pm->m_msCmd = m_sysStateMsg.getMasterControlCmd(key, strJson); MasterCommandEvent event; PowerManagerFsm::dispatch(event); return true; } // 设备状态处理 else if(key == m_equStateMsg.key_cmd) { #ifdef _DEBUG m_equStateMsg_cmd_debug = std::make_pair(key, strJson); #endif m_pm->m_MSDriverCmd = m_equStateMsg.getEquipmentCmd(key, strJson); vector devCmdExt = m_pm->m_driver.loadDriverIdFromJson(strJson); if (devCmdExt.empty()) { LOG_F(WARNING, "控制指令为空"); } else { for (auto& cmd : devCmdExt) { DevCmdExt event(cmd.id, cmd.cmd); PowerManagerFsm::dispatch(event); } } return true; } // 次级配电状态处理 else if(key == m_secdistStateMsg.key_fb) { #ifdef _DEBUG m_secdistStateMsg_fb_debug = std::make_pair(key, strJson); #endif m_secdistStateMsg.parseMsg(strJson, m_pm->m_subDisSysState); //debug 接受到次级配电状态,自动更新到设备状态 //TODO:自动更新到设备状态 //m_pm->m_subDisSysState = m_pm->m_deviceState; return true; } // 深度获取 else if(key == m_depthMsg.key) { #ifdef _DEBUG m_depthMsg_st_debug = std::make_pair(key, strJson); #endif m_pm->m_depth = m_depthMsg.getDepth(key, strJson); return true; } else if(key == m_taskStartMsg.key) { #ifdef _DEBUG m_taskStartMsg_st_debug = std::make_pair(key, strJson); #endif m_pm->m_missionState = m_taskStartMsg.getMission(key, strJson); TaskStartEvent event; event.type = m_pm->m_missionState.type; event.enableBowRudder = m_pm->m_missionState.enableBowRudder; event.enableBuoyancyAdjustment = m_pm->m_missionState.enableBuoyancyAdjustment; PowerManagerFsm::dispatch(event); return true; } else if(key == m_setOpModeMsg.key) { #ifdef _DEBUG m_setOpModeMsg_st_debug = std::make_pair(key, strJson); #endif m_pm->m_msCmd.modCMD = m_setOpModeMsg.getSetOpMode(key, strJson); MasterCommandEvent event; PowerManagerFsm::dispatch(event); return true; } else if(key == m_setOpConditionMsg.key) { #ifdef _DEBUG m_setOpConditionMsg_st_debug = std::make_pair(key, strJson); #endif m_pm->m_msCmd.workCMD = m_setOpConditionMsg.getSetOpCondition(key, strJson); MasterCommandEvent event; PowerManagerFsm::dispatch(event); } else if(key == m_setPMSysMsg.key) { #ifdef _DEBUG m_setPMSysMsg_st_debug = std::make_pair(key, strJson); #endif m_pm->m_msCmd.powerCMD = m_setPMSysMsg.getSetPMSys(key, strJson); MasterCommandEvent event; PowerManagerFsm::dispatch(event); return true; } else if(key == m_setDevSwitchMsg.key) { #ifdef _DEBUG m_setDevSwitchMsg_st_debug = std::make_pair(key, strJson); #endif DeviceSwitchCmd cmd; cmd = m_setDevSwitchMsg.getSetDevSwitch(key, strJson); if (cmd.id < 0) { LOG_F(WARNING, "parse device switch error:%s", strJson.c_str()); } else { DevCmdExt devCmdExt; devCmdExt.id = cmd.id; devCmdExt.cmd = cmd.cmd; PowerManagerFsm::dispatch(devCmdExt); LOG_F(INFO, "set dev switch success"); return true; } } else if(key == m_taskStopMsg.key) { #ifdef _DEBUG m_taskStopMsg_st_debug = std::make_pair(key, strJson); #endif bool stop = m_taskStopMsg.getTaskStop(key, strJson); if (stop) { LOG_F(INFO, "task stop success"); return true; } else { LOG_F(WARNING, "task stop error:%s", strJson.c_str()); return false; } }; LOG_F(WARNING, "Unknown key: %s", key.c_str()); return false; } void UpperCommManager::processPeriodicTasks() { buildSystemReport(); std::string strJson; //发送当前设备状态 strJson = m_equStateMsg.buildMsg(m_pm->m_currentDriverState); m_pm->sendNotification(m_equStateMsg.key, strJson); //发送次级配电指令 修改后的代码 // strJson = getSubDisCmd(m_pm->m_subDisSysCmd); // TODO : 修改指令逻辑,当状态一致时发送保持指令 // strJson = m_secdistStateMsg.buildMsg(m_pm->m_subDisSysCmd); strJson = m_secdistStateMsg.buildMsg(m_pm->m_subDisSysCmd,m_pm->m_subDisSysState,2); m_pm->sendNotification(m_secdistStateMsg.key_cmd, strJson); //发送仪表电状态 m_pm->sendNotification(m_equInsStateMsg.key,m_equInsStateMsg.buildMsg(m_pm->m_subDisSysState)); #ifdef _DEBUG m_equInsStateMsg_fb_degub = std::make_pair(m_equInsStateMsg.key,m_equInsStateMsg.buildMsg(m_pm->m_subDisSysState)); m_secdistStateMsg_cmd_debug = std::make_pair(m_secdistStateMsg.key_cmd,strJson); m_equStateMsg_fb_debug = std::make_pair(m_equStateMsg.key_cmd,m_equStateMsg.buildMsg(m_pm->m_currentDriverState)); #endif } void UpperCommManager::buildSystemReport() { m_pmStateMsg.state.workCondition = m_pm->workCondition; m_pmStateMsg.state.currentMod = m_pm->currentMod; m_pmStateMsg.state.powerState = m_pm->powerState; m_pmStateMsg.state.faultLevel = m_pm->faultLevel; m_pmStateMsg.state.faultCode = m_pm->faultCode; // cout << "m_pmStateMsg.state.faultCode.size():" << m_pmStateMsg.state.faultCode.size() << endl; m_pmStateMsg.state.soc = m_pm->soc; m_pmStateMsg.state.sustainableTime = m_pm->sustainableTime; m_pmStateMsg.state.m_leakage = m_pm->m_leakage; m_pmStateMsg.state.m_faultNum = m_pm->m_faultNum; m_pm->sendNotification(m_pmStateMsg.key, m_pmStateMsg.buildMsg()); m_pm->sendNotification(m_fcsStateMsg.key, m_fcsStateMsg.buildMsg(*(m_pm->m_systemData))); m_pm->sendNotification(m_liBatStateMsg.key, m_liBatStateMsg.buildMsg(*(m_pm->m_systemData))); m_pm->sendNotification(m_disSysStateMsg.key, m_disSysStateMsg.buildMsg(*(m_pm->m_systemData),m_pm->m_currentDisBreakerState)); m_pm->sendNotification(m_currentStateMsg.key, m_currentStateMsg.buildMsg(m_pm->m_pmCurrentState)); #ifdef _DEBUG m_pmStateMsg_fb_debug = std::make_pair(m_pmStateMsg.key, m_pmStateMsg.buildMsg()); m_fcsStateMsg_st_debug = std::make_pair(m_fcsStateMsg.key, m_fcsStateMsg.buildMsg(*(m_pm->m_systemData))); m_liBatStateMsg_st_debug = std::make_pair(m_liBatStateMsg.key, m_liBatStateMsg.buildMsg(*(m_pm->m_systemData))); m_disSysStateMsg_st_debug = std::make_pair(m_disSysStateMsg.key, m_disSysStateMsg.buildMsg(*(m_pm->m_systemData),m_pm->m_currentDisBreakerState)); m_currentStateMsg_st_debug = std::make_pair(m_currentStateMsg.key, m_currentStateMsg.buildMsg(m_pm->m_pmCurrentState)); #endif }