2025-03-12 14:17:53 +08:00

134 lines
3.6 KiB
C++

#include "TimetableMsgBusMng.h"
#include "boost/date_time.hpp"
#include "boost/algorithm/string.hpp"
#include "common/Common.h"
#include "pub_utility_api/TimeUtil.h"
#include "pub_utility_api/I18N.h"
#include "pub_utility_api/CharUtil.h"
#include "net_msg_bus_api/CMbCommunicator.h"
#include "operate_server_api/JsonOptCommand.h"
#include "common/MessageChannel.h"
#include "TimetableCommon.h"
using namespace std;
using namespace iot_public;
using namespace kbd_app;
using namespace iot_dbms;
using namespace iot_net;
kbd_app::CTimetableMsgBusMng::CTimetableMsgBusMng(const iot_public::SRunAppInfo &stRunAppInfo,
const iot_public::CSysInfoInterfacePtr &ptrSysInfo)
:m_stRunAppInfo(stRunAppInfo),
m_ptrSysInfo(ptrSysInfo)
{
}
kbd_app::CTimetableMsgBusMng::~CTimetableMsgBusMng()
{
m_ptrSysInfo.reset();
}
/* @brief 订阅消息通道 */
int kbd_app::CTimetableMsgBusMng::subscribeMessage()
{
if (!m_objCommunicator.addSub(m_stRunAppInfo.nAppId, CH_HMI_TO_TIMETABLE))
{
LOGERROR("通道[CH_HMI_TO_TIMETABLE]订阅失败.");
return kbdFailed;
}
else
{
LOGINFO("订阅通道[CH_HMI_TO_TIMETABLE]成功");
}
if (!m_objCommunicator.addSub(m_stRunAppInfo.nAppId, CH_OPT_TO_HMI_OPTCMD_UP))
{
LOGERROR("通道[CH_OPT_TO_HMI_OPTCMD_UP]订阅失败.");
return kbdFailed;
}
else
{
LOGINFO("订阅通道[CH_OPT_TO_HMI_OPTCMD_UP]成功");
}
return kbdSuccess;
}
/* @brief 取消消息订阅 */
int kbd_app::CTimetableMsgBusMng::unsubscribeMessage()
{
std::vector<CMbSubInfo> vecSub = m_objCommunicator.getMySub();
for (size_t i = 0; i < vecSub.size(); ++i)
{
if (!m_objCommunicator.delSub(vecSub[i].getAppID(), vecSub[i].getChannelID()))
{
LOGERROR("取消通道[%d-%d]订阅失败.", vecSub[i].getAppID(), vecSub[i].getChannelID());
return kbdFailed;
}
}
/* @brief 清空消息总线中的数据 */
CMbMessage objMsg;
while (m_objCommunicator.recvMsg(objMsg, 0))
{
}
return kbdSuccess;
}
/* @brief 初始化 */
int kbd_app::CTimetableMsgBusMng::initialize()
{
return kbdSuccess;
}
/* @brief 接收消息 */
bool kbd_app::CTimetableMsgBusMng::recvMsg(iot_net::CMbMessage &objMsg, int nTimeoutMsec /*= 0*/)
{
return m_objCommunicator.recvMsg(objMsg, nTimeoutMsec);
}
/* @brief 发送消息 */
bool kbd_app::CTimetableMsgBusMng::sendMsgToMsgBus(const std::string &strMsg,
const int nChannel,
const int nMsgType,
const int nDstDomain,
const std::string &strNodeName)
{
CMbMessage objMsg(strMsg, m_stRunAppInfo.nAppId, nChannel, nMsgType);
if (!objMsg.isValid())
{
LOGERROR("构造CMbMessage失败.AppId=[%d],CH=[%d],MT=[%d]", m_stRunAppInfo.nAppId, nChannel, nMsgType);
return false;
}
bool bRet = false;
if (!strNodeName.empty())
{
bRet = m_objCommunicator.sendMsgToHost(objMsg);
}
else
{
bRet = m_objCommunicator.sendMsgToDomain(objMsg, nDstDomain);
}
if (bRet) //< 发送给本机
{
LOGINFO("发送消息成功:\n%s", strMsg.c_str());
}
else
{
LOGERROR("发送消息成功:\n%s", strMsg.c_str());
}
return bRet;
}
/* @brief 获取通信器名称 */
std::string kbd_app::CTimetableMsgBusMng::getCommunicatorName()
{
return m_objCommunicator.getName();
}