1、调整服务结构,增加服务名
2、调整F-GetVersion测试Demo
This commit is contained in:
+1
-1
@@ -16,7 +16,7 @@ aux_source_directory(./dataservice DS_SRC)
|
||||
add_executable(dcapp
|
||||
${PROJECT_SOURCE_DIR}/main.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/datacenter.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/getVersion.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F-GetVersion.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/queue.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/mqttClient.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/ToolUtils.cpp
|
||||
|
||||
@@ -1,73 +1,77 @@
|
||||
/**
|
||||
* @文件路径: \datacenter\datacenter\src\userinterface.cpp
|
||||
* @作者名称: zjp
|
||||
* @文件版本: V1.0.0
|
||||
* @创建日期: 2025-02-20 10:13:44
|
||||
* @简要说明:
|
||||
* @编辑时间: 2025-02-21 15:06:47
|
||||
* @Copyright: 2025 Shenzhen ShenBao Electrical instruments CO.,LTD. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unistd.h>
|
||||
#include <ToolUtils.h>
|
||||
|
||||
#include "bizservice.h"
|
||||
#include "msg.h"
|
||||
#include "json.hpp"
|
||||
#include "datacenter/bizService/getVersion.h"
|
||||
|
||||
REGISTER_BIZSERVICE(GetVersion);
|
||||
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
void GetVersion::Init(initparam_t *param) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 业务处理
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为1 失败为0
|
||||
*/
|
||||
int GetVersion::HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param) {
|
||||
// 正常业务处理...
|
||||
std::string tokenStr = "";
|
||||
std::string recvTimeStr = "";
|
||||
try {
|
||||
nlohmann::json jsObj = nlohmann::json::parse(msg.payload);
|
||||
tokenStr = jsObj["token"];
|
||||
recvTimeStr = jsObj["time"];
|
||||
}catch (nlohmann::json::parse_error& e) {
|
||||
std::cerr << "Parsing error: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
makeGetVersionReponse(tokenStr,outMsg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取FID
|
||||
*
|
||||
* @return const std::string FID名称
|
||||
*/
|
||||
std::string GetVersion::GetFID(void) {
|
||||
return "F-GetVersion";
|
||||
}
|
||||
|
||||
void GetVersion::makeGetVersionReponse(std::string tokenStr,msg_t &outMsg) {
|
||||
nlohmann::json js;
|
||||
js["token"] = tokenStr;
|
||||
js["time"] = ToolUtils::getTimeStringWithFomat(true);
|
||||
js["data"] = {{"version","shb-xxxx"}};
|
||||
|
||||
outMsg.topic = "/sys.dbc/sys.brd/S-datacenter/F-GetVersion";
|
||||
outMsg.payload = js.dump();
|
||||
outMsg.timestamp = ToolUtils::getUtcTime(true);
|
||||
/**
|
||||
* @文件路径: \datacenter\datacenter\src\userinterface.cpp
|
||||
* @作者名称: zjp
|
||||
* @文件版本: V1.0.0
|
||||
* @创建日期: 2025-02-20 10:13:44
|
||||
* @简要说明:
|
||||
* @编辑时间: 2025-02-21 15:06:47
|
||||
* @Copyright: 2025 Shenzhen ShenBao Electrical instruments CO.,LTD. All Rights Reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unistd.h>
|
||||
#include <ToolUtils.h>
|
||||
|
||||
#include "bizservice.h"
|
||||
#include "msg.h"
|
||||
#include "json.hpp"
|
||||
#include "datacenter/bizService/F-GetVersion.h"
|
||||
|
||||
REGISTER_BIZSERVICE("F-GetVersion",GetVersion);
|
||||
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
void GetVersion::Init(initparam_t *param) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 业务处理
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为1 失败为0
|
||||
*/
|
||||
int GetVersion::HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param) {
|
||||
// 正常业务处理...
|
||||
std::string tokenStr = "";
|
||||
std::string recvTimeStr = "";
|
||||
try {
|
||||
nlohmann::json jsObj = nlohmann::json::parse(msg.payload);
|
||||
tokenStr = jsObj["token"];
|
||||
recvTimeStr = jsObj["time"];
|
||||
}catch (nlohmann::json::parse_error& e) {
|
||||
std::cerr << "Parsing error: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
//是否操作数据库
|
||||
//todo............操作数据库读写
|
||||
|
||||
//封装json响应
|
||||
makeGetVersionReponse(tokenStr,outMsg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 获取FID
|
||||
*
|
||||
* @return const std::string FID名称
|
||||
*/
|
||||
std::string GetVersion::GetFID(void) {
|
||||
return "F-GetVersion";
|
||||
}
|
||||
|
||||
void GetVersion::makeGetVersionReponse(std::string tokenStr,msg_t &outMsg) {
|
||||
nlohmann::json js;
|
||||
js["token"] = tokenStr;
|
||||
js["time"] = ToolUtils::getTimeStringWithFomat(false);
|
||||
js["data"] = {{"version","shb-xxxx"}};
|
||||
|
||||
outMsg.topic = "/sys.dbc/sys.brd/S-datacenter/F-GetVersion";
|
||||
outMsg.payload = js.dump();
|
||||
outMsg.timestamp = ToolUtils::getUtcTime(false);
|
||||
}
|
||||
@@ -1,55 +1,55 @@
|
||||
/**
|
||||
* @文件路径: \datacenter\datacenter\inc\userinterface.h
|
||||
* @作者名称: zjp
|
||||
* @文件版本: V1.0.0
|
||||
* @创建日期: 2025-02-20 14:35:38
|
||||
* @简要说明:
|
||||
* @编辑时间: 2025-02-21 14:28:48
|
||||
* @Copyright: 2025 Shenzhen ShenBao Electrical instruments CO.,LTD. All Rights Reserved.
|
||||
*/
|
||||
#ifndef _DCAPP_DATACENTER_USERFID_H_
|
||||
#define _DCAPP_DATACENTER_USERFID_H_
|
||||
|
||||
#include "msg.h"
|
||||
|
||||
#include "bizservice.h"
|
||||
#include "queue.h"
|
||||
|
||||
class GetVersion : public IBizService
|
||||
{
|
||||
public:
|
||||
|
||||
GetVersion(){
|
||||
|
||||
}
|
||||
~GetVersion(){
|
||||
|
||||
}
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
void Init(initparam_t *param);
|
||||
|
||||
/**
|
||||
* @brief 业务处理
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为0 失败<0 (-1待处理数据格式错误 -2操作数据库失败 -3其它错误)
|
||||
*/
|
||||
int HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param);
|
||||
|
||||
/**
|
||||
* @brief 获取FID服务标识
|
||||
*
|
||||
* @return FID服务标识
|
||||
*/
|
||||
std::string GetFID(void);
|
||||
|
||||
private:
|
||||
void makeGetVersionReponse(std::string token,msg_t &outMsg);
|
||||
};
|
||||
|
||||
/**
|
||||
* @文件路径: \datacenter\datacenter\inc\userinterface.h
|
||||
* @作者名称: zjp
|
||||
* @文件版本: V1.0.0
|
||||
* @创建日期: 2025-02-20 14:35:38
|
||||
* @简要说明:
|
||||
* @编辑时间: 2025-02-21 14:28:48
|
||||
* @Copyright: 2025 Shenzhen ShenBao Electrical instruments CO.,LTD. All Rights Reserved.
|
||||
*/
|
||||
#ifndef _DCAPP_DATACENTER_USERFID_H_
|
||||
#define _DCAPP_DATACENTER_USERFID_H_
|
||||
|
||||
#include "msg.h"
|
||||
|
||||
#include "bizservice.h"
|
||||
#include "queue.h"
|
||||
|
||||
class GetVersion : public IBizService
|
||||
{
|
||||
public:
|
||||
|
||||
GetVersion(){
|
||||
|
||||
}
|
||||
~GetVersion(){
|
||||
|
||||
}
|
||||
/**
|
||||
* @brief 初始化
|
||||
*
|
||||
* @param param 输入初始化参数
|
||||
*/
|
||||
void Init(initparam_t *param);
|
||||
|
||||
/**
|
||||
* @brief 业务处理
|
||||
*
|
||||
* @param msg 输入消息体
|
||||
* @param param 可用数据库连接句柄
|
||||
* @return int 返回值 成功为0 失败<0 (-1待处理数据格式错误 -2操作数据库失败 -3其它错误)
|
||||
*/
|
||||
int HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param);
|
||||
|
||||
/**
|
||||
* @brief 获取FID服务标识
|
||||
*
|
||||
* @return FID服务标识
|
||||
*/
|
||||
std::string GetFID(void);
|
||||
|
||||
private:
|
||||
void makeGetVersionReponse(std::string token,msg_t &outMsg);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,28 +0,0 @@
|
||||
{
|
||||
"fid":[
|
||||
"F-Online",
|
||||
"F-GetVersion",
|
||||
"F-SetSubscribe",
|
||||
"F-NotifyUpdate",
|
||||
"F-NotifyInit",
|
||||
"F-ReadData",
|
||||
"F-WriteData",
|
||||
"F-ReadRecordBegin",
|
||||
"F-ReadRecordStep",
|
||||
"F-ReadRecordEnd",
|
||||
"F-InsertRecord",
|
||||
"F-UpdateRecord",
|
||||
"F-GetSaveCount",
|
||||
"F-GetSupportNum",
|
||||
"F-SetSupportNum",
|
||||
"F-FormatData",
|
||||
"F-DeleteData",
|
||||
"F-FactoryReset",
|
||||
"F-AddUserDic",
|
||||
"F-DelUserDic"
|
||||
],
|
||||
"subTopics":[
|
||||
"/test1-topic",
|
||||
"/test2-topic"
|
||||
]
|
||||
}
|
||||
@@ -71,10 +71,10 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
#define REGISTER_BIZSERVICE(className) \
|
||||
#define REGISTER_BIZSERVICE(pidName,className) \
|
||||
struct className##BizRegister { \
|
||||
className##BizRegister() { \
|
||||
BizServiceFactovy::getBizServiceMap()[#className] = new className(); \
|
||||
BizServiceFactovy::getBizServiceMap()[pidName] = new className(); \
|
||||
} \
|
||||
} className##BizRegisterInstance;
|
||||
|
||||
|
||||
@@ -53,8 +53,8 @@ namespace ToolUtils {
|
||||
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
printf("%ld,%ld\n",ts.tv_sec,ts.tv_nsec);
|
||||
snprintf(buf, sizeof(buf), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%03d+0800",
|
||||
|
||||
snprintf(buf, sizeof(buf), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%03ld+0800",
|
||||
pTm->tm_year + 1900,
|
||||
pTm->tm_mon + 1,
|
||||
pTm->tm_mday,
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
#include "datacenter.h"
|
||||
#include "msg.h"
|
||||
#include "datacenter/bizService/getVersion.h"
|
||||
#include "datacenter/bizService/F-GetVersion.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
REGISTER_SERVICE(DataCenter);
|
||||
@@ -36,7 +36,7 @@ void DataCenter::ExecPoll(void) {
|
||||
}
|
||||
msg_mqtt_t inMsg = this->mQueue_In.pop_nonblock();
|
||||
if(inMsg.topic.empty() && inMsg.payload.empty()){
|
||||
std::cout << "mQueue_In:" << " : " << " is empty " << std::endl;
|
||||
// std::cout << "mQueue_In:" << " : " << " is empty " << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ void DataCenter::ExecReponse() {
|
||||
//消息应答队列无阻赛返回
|
||||
msg_mqtt_t outMsg = this->mQueue_Out.pop_nonblock();
|
||||
if(outMsg.topic.empty() && outMsg.payload.empty()){
|
||||
std::cout << "mQueue_Out:" << " : " << " is empty " << std::endl;
|
||||
// std::cout << "mQueue_Out:" << " : " << " is empty " << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -82,13 +82,11 @@ bool DataCenter::handleMsg(msg_mqtt_t inMqttMsg) {
|
||||
return false;
|
||||
}
|
||||
std::map<std::string, IBizService*> map = BizServiceFactovy::getBizServiceMap();
|
||||
for (std::map<std::string, IBizService*>::iterator itor = map.begin(); itor != map.end(); itor++) {
|
||||
if(itor->second->GetFID().compare(pidStr) == 0){
|
||||
fRet = itor->second->HandleMsg(intMsg,outMsg, NULL);
|
||||
break;
|
||||
}
|
||||
auto itor = map.find(pidStr);
|
||||
if(itor == map.end()){
|
||||
return false;
|
||||
}
|
||||
|
||||
fRet = itor->second->HandleMsg(intMsg,outMsg, NULL);
|
||||
if (fRet == -1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user