1、调整目录结构
2、调整公共接口
This commit is contained in:
+7
-7
@@ -4,8 +4,9 @@ project(dcapp)
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_FLAGS "-std=c++11")
|
||||
|
||||
include_directories(./
|
||||
./datacenter/inc)
|
||||
include_directories(${PROJECT_SOURCE_DIR}/
|
||||
${PROJECT_SOURCE_DIR}/datacenter/inc
|
||||
${PROJECT_SOURCE_DIR}/com)
|
||||
|
||||
|
||||
LINK_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib/x86)
|
||||
@@ -18,23 +19,22 @@ aux_source_directory(${PROJECT_SOURCE_DIR}/datacenter/bizService DS_BIZSRC)
|
||||
add_executable(dcapp
|
||||
${PROJECT_SOURCE_DIR}/main.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/datacenter.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
|
||||
${PROJECT_SOURCE_DIR}/com/ToolUtils.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/src/thread_pool.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetVersion.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_InsertRecord.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_UpdateRecord.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetSaveCount.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_Tool.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetSupportNum.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_SetSupportNum.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_ReadData.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_WriteData.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_Online.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/F_SetSubscribe.cpp
|
||||
${PROJECT_SOURCE_DIR}/datacenter/bizService/f-readdata/normalDataDB.cpp
|
||||
${DC_SRC} ${DS_SRC} ${DQ_SRC} ${DS_BIZSRC}
|
||||
)
|
||||
${DC_SRC} ${DS_SRC} ${DQ_SRC} ${DS_BIZSRC})
|
||||
|
||||
target_link_libraries(dcapp
|
||||
paho-mqtt3c
|
||||
|
||||
+2
-2
@@ -22,10 +22,10 @@
|
||||
"F-DelUserDic"
|
||||
],
|
||||
"subTopics": [
|
||||
"/sys.dbc/sys.brd/S-datacenter/F-GetVersion",
|
||||
"/sys.dbc/+/S-datacenter/+"
|
||||
],
|
||||
"pubTopics": [
|
||||
"/sys.brd/sys.dbc/S-datacenter/F-Online"
|
||||
"/sys.brd/sys.dbc/S-datacenter/F-Online",
|
||||
"/sys.brd/sys.dbc/S-datacenter/F-NotifyInit"
|
||||
|
||||
]
|
||||
|
||||
@@ -0,0 +1,245 @@
|
||||
//
|
||||
// Created by zgj on 2024/4/29.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <uuid/uuid.h>
|
||||
#include <cstring>
|
||||
#include <sstream>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include "ToolUtils.h"
|
||||
|
||||
namespace ToolUtils {
|
||||
/**
|
||||
* 获取当前时间的字符串表示。
|
||||
*
|
||||
* @param isLocalTime 如果为true,则返回本地时间的字符串表示,否则返回UTC时间的字符串表示。
|
||||
* @return 返回当前时间(根据isLocalTime参数决定是本地时间还是UTC时间)的字符串表示,格式为"YYYY-MM-DD HH:MM:SS"。
|
||||
*/
|
||||
std::string getTimeString(bool isLocalTime) {
|
||||
char buf[128] = {0};
|
||||
|
||||
time_t now = time(NULL);
|
||||
if (isLocalTime) {
|
||||
now = now + 8 * 3600;
|
||||
}
|
||||
struct tm ttm;
|
||||
struct tm *pTm = NULL;
|
||||
pTm = localtime_r(&now, &ttm);
|
||||
|
||||
snprintf(buf, sizeof(buf), "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
|
||||
pTm->tm_year + 1900,
|
||||
pTm->tm_mon + 1,
|
||||
pTm->tm_mday,
|
||||
pTm->tm_hour,
|
||||
pTm->tm_min,
|
||||
pTm->tm_sec
|
||||
);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
|
||||
std::string getUtcTimeString(bool isLocalTime) {
|
||||
return std::to_string(isLocalTime ? time(NULL) + 8 * 360: time(NULL));
|
||||
}
|
||||
|
||||
std::string getCurTimeStringWithFomat(bool isLocalTime) {
|
||||
char buf[128] = {0};
|
||||
|
||||
time_t now = time(NULL);
|
||||
if (isLocalTime) {
|
||||
now = now + 8 * 3600;
|
||||
}
|
||||
struct tm ttm;
|
||||
struct tm *pTm = NULL;
|
||||
pTm = localtime_r(&now, &ttm);
|
||||
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
|
||||
snprintf(buf, sizeof(buf), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%03ld+0800",
|
||||
pTm->tm_year + 1900,
|
||||
pTm->tm_mon + 1,
|
||||
pTm->tm_mday,
|
||||
pTm->tm_hour,
|
||||
pTm->tm_min,
|
||||
pTm->tm_sec,
|
||||
ts.tv_nsec/1000000
|
||||
);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
uint64_t getUtcTime(bool isLocalTime) {
|
||||
return isLocalTime ? time(NULL) + 8 * 3600: time(NULL);
|
||||
}
|
||||
|
||||
bool isCSTZone(){
|
||||
std::string zoneStr = "CST";
|
||||
struct tm ttm;
|
||||
time_t tt = time(NULL);
|
||||
struct tm *local_time = localtime_r(&tt, &ttm);
|
||||
if(zoneStr.compare(local_time->tm_zone) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool subString(std::string srcStr, std::string subStr,std::string &resultStr) {
|
||||
int pos = -1;
|
||||
pos = srcStr.find(subStr);
|
||||
if(pos != std::string::npos){
|
||||
resultStr = srcStr.substr(pos + subStr.length());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 生成UUID
|
||||
std::string genUUID(){
|
||||
uuid_t bin_uuid;
|
||||
char uuid_str[37];
|
||||
uuid_generate_random(bin_uuid);
|
||||
uuid_unparse_lower(bin_uuid, uuid_str);
|
||||
return std::string(uuid_str);
|
||||
}
|
||||
|
||||
void hexstr_to_str(void *dst, const void *src, int src_len) {
|
||||
int i;
|
||||
uint8_t low, high, ch;
|
||||
uint8_t *dst_ptr = (uint8_t *) dst;
|
||||
const uint8_t *src_ptr = (const uint8_t *) src;
|
||||
|
||||
for (i = 0; i < src_len / 2; i++) {
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
high = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
high = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
high = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
low = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
low = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
low = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
*dst_ptr++ = high * 16 + low; //hex
|
||||
}
|
||||
}
|
||||
|
||||
void str_to_hexstr(void *dst, const void *src, int src_len) {
|
||||
int i;
|
||||
uint8_t low, high, ch;
|
||||
char *dst_ptr = (char *) dst;
|
||||
const char *src_ptr = (const char *) src;
|
||||
const char *str = "0123456789abcdef";
|
||||
|
||||
for (i = 0; i < src_len; i++) {
|
||||
ch = *src_ptr++;
|
||||
high = (ch >> 4) & 0x0f;
|
||||
low = ch & 0x0f;
|
||||
*dst_ptr++ = str[high];
|
||||
*dst_ptr++ = str[low];
|
||||
}
|
||||
}
|
||||
|
||||
int encode_blob(void *dst, int max_len, const void *src, int src_len)
|
||||
{
|
||||
int dst_len = 0;
|
||||
|
||||
// may be changed to base64 encode
|
||||
if (max_len >= src_len * 2 + 1)
|
||||
{
|
||||
dst_len = src_len * 2;
|
||||
str_to_hexstr(dst, src, src_len);
|
||||
}
|
||||
((uint8_t *) dst)[dst_len] = 0x00;
|
||||
return dst_len;
|
||||
}
|
||||
|
||||
int decode_blob(void *dst, int max_len, const void *src)
|
||||
{
|
||||
// may be changed to base64 decode
|
||||
int src_len, dst_len = 0;
|
||||
|
||||
src_len = strlen((char *) src);
|
||||
std::cout << "decode_blob src len "<< src_len<<std::endl;
|
||||
if (max_len >= src_len / 2)
|
||||
{
|
||||
dst_len = src_len / 2;
|
||||
hexstr_to_str(dst, src, src_len);
|
||||
}
|
||||
return dst_len;
|
||||
}
|
||||
void topic_split(const std::string& str, char delimiter,std::vector<std::string>& topic) {
|
||||
std::string token; // 临时存储每个子字符串
|
||||
std::istringstream tokenStream(str); // 使用字符串流处理输入字符串
|
||||
// 按分隔符逐个读取子字符串
|
||||
while (std::getline(tokenStream, token, delimiter)) {
|
||||
if(!token.empty())
|
||||
topic.push_back(token); // 将子字符串添加到结果中
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::string makeTpopicStr(std::vector<std::string>topicV){
|
||||
std::string topicStr = "/" + topicV[1] + "/" + topicV[0] + "/" + topicV[2] + "/" + topicV[3];
|
||||
return topicStr;
|
||||
}
|
||||
std::string now_to_string() {
|
||||
// 获取系统时钟时间点
|
||||
auto time_now = std::chrono::system_clock::now();
|
||||
|
||||
// 将时间点转成time_t类型的值 (秒)
|
||||
std::time_t tt = std::chrono::system_clock::to_time_t(time_now);
|
||||
|
||||
// 转换成tm结构体形式
|
||||
struct tm* ptm = gmtime(&tt); // 或者 localtime(&tt) 如果想要本地时间
|
||||
ptm->tm_hour += 8; //utc -> cst
|
||||
// 计算出毫秒部分
|
||||
long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
time_now.time_since_epoch()
|
||||
).count() % 1000;
|
||||
|
||||
char buffer[40];
|
||||
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", ptm);
|
||||
|
||||
// 添加上毫秒和时区信息
|
||||
sprintf(buffer + strlen(buffer), ".%03ld+0800", milliseconds);
|
||||
|
||||
return std::string(buffer);
|
||||
}
|
||||
std::string getTimeStringWithFomat(time_t stamp) {
|
||||
struct timeval tmptime;
|
||||
struct tm tm_time;
|
||||
char buf[50]={0};
|
||||
char tempbuf[200]={0};
|
||||
gettimeofday(&tmptime, NULL);
|
||||
localtime_r(&tmptime.tv_sec, &tm_time);
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.:03d%z", &tm_time);
|
||||
char *ptr = strstr(buf, ":03d");
|
||||
if (ptr){
|
||||
ptr[0] = '%';
|
||||
}
|
||||
sprintf(tempbuf, buf, tmptime.tv_usec/1000);
|
||||
std::string timestr = tempbuf;
|
||||
return timestr;
|
||||
}
|
||||
time_t string_to_now(std::string stamp) {
|
||||
struct tm tm={};
|
||||
std::istringstream ss(stamp);
|
||||
ss >> std::get_time(&tm,"%Y-%m-%dT%H:%M:%S");
|
||||
if(ss.fail()){
|
||||
return -1;
|
||||
}
|
||||
return mktime(&tm);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// Created by zgj on 2024/4/29.
|
||||
//
|
||||
|
||||
#ifndef METERMQTT_TOOLUTILS_H
|
||||
#define METERMQTT_TOOLUTILS_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace ToolUtils{
|
||||
std::string getTimeString(bool isLocalTime);
|
||||
std::string getUtcTimeString(bool isLocalTime);
|
||||
std::string getCurTimeStringWithFomat(bool isLocalTime);
|
||||
std::string getTimeStringWithFomat(time_t stamp);
|
||||
uint64_t getUtcTime(bool isLocalTime);
|
||||
bool isCSTZone();
|
||||
bool subString(std::string srcStr, std::string subStr,std::string &resultStr);
|
||||
std::string genUUID();
|
||||
void hexstr_to_str(void *dst, const void *src, int src_len);
|
||||
void str_to_hexstr(void *dst, const void *src, int src_len);
|
||||
int encode_blob(void *dst, int max_len, const void *src, int src_len);
|
||||
int decode_blob(void *dst, int max_len, const void *src);
|
||||
void topic_split(const std::string& str, char delimiter,std::vector<std::string>& topic);
|
||||
std::string makeTpopicStr(std::vector<std::string>topicV);
|
||||
std::string now_to_string();
|
||||
time_t string_to_now(std::string stamp);
|
||||
}
|
||||
#endif //METERMQTT_TOOLUTILS_H
|
||||
@@ -10,8 +10,8 @@
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "F_Tool.h"
|
||||
class DeleteData:public IBizService,public Tool{
|
||||
|
||||
class DeleteData:public IBizService{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param = NULL);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "json.hpp"
|
||||
#include "F_DeleteData.h"
|
||||
#include "bizservice.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
@@ -98,7 +99,7 @@ int DeleteData::SQLBatch_exc(sqlite3 *db,std::vector<std::string> &sql_list,std
|
||||
int DeleteData::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/',topic);
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0, update_flag = 0;;
|
||||
@@ -121,7 +122,7 @@ int DeleteData::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
if(value.contains("time")){
|
||||
;// value["time"]
|
||||
}
|
||||
|
||||
|
||||
std::stringstream ss;
|
||||
//table_name =search_table_db(afn,dataid);
|
||||
table_name = "frozen";
|
||||
@@ -131,12 +132,11 @@ int DeleteData::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
ss.clear();
|
||||
ret = SQLBatch_exc((sqlite3*)handpara,sql_list,result);
|
||||
resp_process:
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
resp["ack"]="true";
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
outMsg.timestamp = time(NULL);;
|
||||
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp DeleteData payload:"<<resp.dump().c_str()<<std::endl;
|
||||
return 1;
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "json.hpp"
|
||||
#include "F_FormatData.h"
|
||||
#include "bizservice.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
@@ -98,7 +99,7 @@ int FormatData::SQLBatch_exc(sqlite3 *db,std::vector<std::string> &sql_list,std
|
||||
int FormatData::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/',topic);
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0, update_flag = 0;;
|
||||
@@ -131,12 +132,10 @@ int FormatData::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
ss.clear();
|
||||
ret = SQLBatch_exc((sqlite3*)handpara,sql_list,result);
|
||||
resp_process:
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
resp["ack"]="true";
|
||||
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);;
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp FormatData payload:"<<resp.dump().c_str()<<std::endl;
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "F_Tool.h"
|
||||
class FormatData:public IBizService,public Tool{
|
||||
|
||||
class FormatData:public IBizService{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param = NULL);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "json.hpp"
|
||||
#include "F_GetSaveCount.h"
|
||||
#include "bizservice.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
@@ -95,7 +96,7 @@ std::string GetSaveCount::GetFID(){
|
||||
int GetSaveCount::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/',topic);
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0, update_flag = 0;;
|
||||
@@ -122,8 +123,8 @@ int GetSaveCount::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
json subvalue = value["data"];
|
||||
int afn = 0;
|
||||
int dataid = 0;
|
||||
decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
||||
decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
||||
ToolUtils::decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
||||
ToolUtils::decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
||||
std::stringstream ss;
|
||||
//table_name =search_table_db(afn,dataid);
|
||||
table_name = "frozen";
|
||||
@@ -157,13 +158,10 @@ int GetSaveCount::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
}
|
||||
ret = SQLBatch_exc((sqlite3*)handpara,sql_list,result);
|
||||
resp_process:
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
resp["data"]["savecount"]=result[0];
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);;
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp UpdateRecord payload:"<<resp.dump().c_str()<<std::endl;
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "F_Tool.h"
|
||||
class GetSaveCount : public IBizService,public Tool
|
||||
|
||||
class GetSaveCount : public IBizService
|
||||
{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "json.hpp"
|
||||
#include "F_GetSupportNum.h"
|
||||
#include "bizservice.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
@@ -101,7 +102,7 @@ int GetSupportNum::SQLBatch_exc(sqlite3 *db,std::vector<std::string> &sql_list,
|
||||
int GetSupportNum::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/',topic);
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0, update_flag = 0;;
|
||||
@@ -128,8 +129,8 @@ int GetSupportNum::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
json subvalue = value["data"];
|
||||
int afn = 0;
|
||||
int dataid = 0;
|
||||
decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
||||
decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
||||
ToolUtils::decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
||||
ToolUtils::decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
||||
std::stringstream ss;
|
||||
//table_name =search_table_db(afn,dataid);
|
||||
table_name = "frozen";
|
||||
@@ -142,13 +143,11 @@ int GetSupportNum::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
}
|
||||
ret = SQLBatch_exc((sqlite3*)handpara,sql_list,result);
|
||||
resp_process:
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
resp["data"]["support"]=62;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);;
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp UpdateRecord payload:"<<resp.dump().c_str()<<std::endl;
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "F_Tool.h"
|
||||
class GetSupportNum:public IBizService,public Tool{
|
||||
|
||||
class GetSupportNum:public IBizService{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param = NULL);
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <unistd.h>
|
||||
#include <ToolUtils.h>
|
||||
#include "ToolUtils.h"
|
||||
|
||||
#include "bizservice.h"
|
||||
#include "msg.h"
|
||||
@@ -49,11 +49,9 @@ int GetVersion::HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param) {
|
||||
std::cerr << "Parsing error: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
//是否操作数据库
|
||||
//todo............操作数据库读写
|
||||
|
||||
//封装json响应
|
||||
makeGetVersionReponse(tokenStr,outMsg);
|
||||
makeGetVersionReponse(tokenStr,msg.subTopicV,outMsg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -66,13 +64,13 @@ std::string GetVersion::GetFID(void) {
|
||||
return "F-GetVersion";
|
||||
}
|
||||
|
||||
void GetVersion::makeGetVersionReponse(std::string tokenStr,msg_t &outMsg) {
|
||||
void GetVersion::makeGetVersionReponse(std::string tokenStr,const std::vector<std::string>topicV,msg_t &outMsg) {
|
||||
nlohmann::json js;
|
||||
js["token"] = tokenStr;
|
||||
js["time"] = ToolUtils::getTimeStringWithFomat(false);
|
||||
js["time"] = ToolUtils::getCurTimeStringWithFomat(false);
|
||||
js["data"] = {{"version",DATA_CENTER_VERSION}};
|
||||
|
||||
outMsg.topic = "/sys.dbc/sys.brd/S-datacenter/F-GetVersion";
|
||||
outMsg.topic = "/" + topicV[1] + "/" + topicV[0] + "/" + topicV[2] + "/" + topicV[3];
|
||||
outMsg.payload = js.dump();
|
||||
outMsg.timestamp = ToolUtils::getUtcTime(false);
|
||||
}
|
||||
@@ -50,7 +50,7 @@ public:
|
||||
std::string GetFID(void);
|
||||
|
||||
private:
|
||||
void makeGetVersionReponse(std::string token,msg_t &outMsg);
|
||||
void makeGetVersionReponse(std::string token,const std::vector<std::string>,msg_t &outMsg);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "F_InsertRecord.h"
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
@@ -42,113 +43,6 @@ InsertRecord::~InsertRecord(void){
|
||||
void InsertRecord::Init(initparam_t *param){
|
||||
initpara = param;
|
||||
}
|
||||
void hexstr_to_str(void *dst, const void *src, int src_len)
|
||||
{
|
||||
int i;
|
||||
BYTE low, high, ch;
|
||||
BYTE *dst_ptr = (BYTE *) dst;
|
||||
const BYTE *src_ptr = (const BYTE *) src;
|
||||
|
||||
for (i = 0; i < src_len / 2; i++)
|
||||
{
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
high = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
high = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
high = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
low = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
low = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
low = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
*dst_ptr++ = high * 16 + low; //hex
|
||||
}
|
||||
}
|
||||
|
||||
void str_to_hexstr(void *dst, const void *src, int src_len)
|
||||
{
|
||||
int i;
|
||||
BYTE low, high, ch;
|
||||
char *dst_ptr = (char *) dst;
|
||||
const char *src_ptr = (const char *) src;
|
||||
const char *str = "0123456789abcdef";
|
||||
|
||||
for (i = 0; i < src_len; i++)
|
||||
{
|
||||
ch = *src_ptr++;
|
||||
high = (ch >> 4) & 0x0f;
|
||||
low = ch & 0x0f;
|
||||
*dst_ptr++ = str[high];
|
||||
*dst_ptr++ = str[low];
|
||||
}
|
||||
}
|
||||
int encode_blob(void *dst, int max_len, const void *src, int src_len)
|
||||
{
|
||||
int dst_len = 0;
|
||||
|
||||
// may be changed to base64 encode
|
||||
if (max_len >= src_len * 2 + 1)
|
||||
{
|
||||
dst_len = src_len * 2;
|
||||
str_to_hexstr(dst, src, src_len);
|
||||
}
|
||||
((BYTE *) dst)[dst_len] = 0x00;
|
||||
return dst_len;
|
||||
}
|
||||
|
||||
int decode_blob(void *dst, int max_len, const void *src)
|
||||
{
|
||||
// may be changed to base64 decode
|
||||
int src_len, dst_len = 0;
|
||||
|
||||
src_len = strlen((char *) src);
|
||||
if (max_len >= src_len / 2)
|
||||
{
|
||||
dst_len = src_len / 2;
|
||||
hexstr_to_str(dst, src, src_len);
|
||||
}
|
||||
return dst_len;
|
||||
}
|
||||
std::string InsertRecord::now_to_string() {
|
||||
// 获取系统时钟时间点
|
||||
auto time_now = std::chrono::system_clock::now();
|
||||
|
||||
// 将时间点转成time_t类型的值 (秒)
|
||||
std::time_t tt = std::chrono::system_clock::to_time_t(time_now);
|
||||
|
||||
// 转换成tm结构体形式
|
||||
struct tm* ptm = gmtime(&tt); // 或者 localtime(&tt) 如果想要本地时间
|
||||
ptm->tm_hour += 8; //utc -> cst
|
||||
// 计算出毫秒部分
|
||||
long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
time_now.time_since_epoch()
|
||||
).count() % 1000;
|
||||
|
||||
char buffer[40];
|
||||
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", ptm);
|
||||
|
||||
// 添加上毫秒和时区信息
|
||||
sprintf(buffer + strlen(buffer), ".%03ld+0800", milliseconds);
|
||||
|
||||
return std::string(buffer);
|
||||
}
|
||||
time_t InsertRecord::string_to_now(std::string stamp) {
|
||||
struct tm tm={};
|
||||
std::istringstream ss(stamp);
|
||||
ss >> std::get_time(&tm,"%Y-%m-%dT%H:%M:%S");
|
||||
if(ss.fail()){
|
||||
return -1;
|
||||
}
|
||||
return mktime(&tm);
|
||||
}
|
||||
|
||||
int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result){
|
||||
sqlite3 *db = (sqlite3 *)handpara;
|
||||
@@ -192,7 +86,7 @@ int InsertRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<s
|
||||
int InsertRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/');
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0;
|
||||
@@ -231,7 +125,7 @@ int InsertRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
<< 0 << ","\
|
||||
<<std::stoi(k["metid"].get<std::string>())<<","\
|
||||
<<k["dataid"].get<std::string>().c_str()<<","\
|
||||
<<string_to_now(k["frozentime"].get<std::string>())<<","\
|
||||
<<ToolUtils::string_to_now(k["frozentime"].get<std::string>())<<","\
|
||||
<<time(NULL)<<","\
|
||||
<<std::stoi(k["crcaux"].get<std::string>())<<","\
|
||||
<<k["databuff"].get<std::string>().c_str()<<");";
|
||||
@@ -242,17 +136,15 @@ int InsertRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
}
|
||||
}
|
||||
// ret = SQLBatch_exc(sql_list,result);
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
if(ret == -1 || afn_flag == -1)
|
||||
resp["ack"]="false";
|
||||
else if(ret == 0)
|
||||
resp["ack"]="true";
|
||||
|
||||
resp["data"]["dataidex"]=result;
|
||||
std::stringstream ss;
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp InsertRecord payload:"<<resp.dump().c_str()<<std::endl;
|
||||
@@ -261,16 +153,6 @@ int InsertRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
std::string InsertRecord::GetFID(){
|
||||
std::string value("F-InsertRecord");
|
||||
return value;
|
||||
|
||||
}
|
||||
void InsertRecord::topic_split(const std::string& str, char delimiter) {
|
||||
std::string token; // 临时存储每个子字符串
|
||||
std::istringstream tokenStream(str); // 使用字符串流处理输入字符串
|
||||
// 按分隔符逐个读取子字符串
|
||||
while (std::getline(tokenStream, token, delimiter)) {
|
||||
topic.push_back(token); // 将子字符串添加到结果中
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -21,8 +21,5 @@ class InsertRecord : public IBizService
|
||||
handparam_t * handpara;
|
||||
initparam_t * initpara;
|
||||
std::vector<std::string> topic;//0:dst 1:src 2:sid 3:fid
|
||||
void topic_split(const std::string& str, char delimiter);
|
||||
int SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result);
|
||||
std::string now_to_string();
|
||||
time_t string_to_now(std::string stamp);
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <ToolUtils.h>
|
||||
#include "ToolUtils.h"
|
||||
|
||||
#include "bizservice.h"
|
||||
#include "json.hpp"
|
||||
|
||||
@@ -6,48 +6,11 @@
|
||||
#include "f-readdata/normalDataDB.h"
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
#include "ToolUtils.h"
|
||||
|
||||
|
||||
REGISTER_BIZSERVICE("F-ReadData",readDataSrv);
|
||||
|
||||
//时间格式转换,时间戳转成 "2021-05-10T08:18:28.666+0800"
|
||||
static std::string time2jsontime(time_t timestamp)
|
||||
{
|
||||
struct timeval tmptime;
|
||||
struct tm tm_time;
|
||||
char buf[50]={0};
|
||||
char tempbuf[200]={0};
|
||||
gettimeofday(&tmptime, NULL);
|
||||
localtime_r(&tmptime.tv_sec, &tm_time);
|
||||
strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.:03d%z", &tm_time);
|
||||
char *ptr = strstr(buf, ":03d");
|
||||
if (ptr){
|
||||
ptr[0] = '%';
|
||||
}
|
||||
sprintf(tempbuf, buf, tmptime.tv_usec/1000);
|
||||
std::string timestr = tempbuf;
|
||||
return timestr;
|
||||
}
|
||||
|
||||
static bool parse_topic(const std::string &topic, std::vector<std::string> &strarray)
|
||||
{
|
||||
std::size_t pos_h, pos_t;
|
||||
bool fRet=false;
|
||||
|
||||
for (int i=0; i<4; i++){
|
||||
pos_h = topic.find("/");
|
||||
pos_t = topic.find("/", pos_h+1);
|
||||
if (pos_h!=std::string::npos && pos_t!=std::string::npos){
|
||||
strarray.push_back(topic.substr(pos_h,pos_t));
|
||||
fRet=true;
|
||||
}else{
|
||||
fRet=false;
|
||||
}
|
||||
}
|
||||
|
||||
return fRet;
|
||||
}
|
||||
|
||||
static bool readDataDb(void *db_handle, const json &cdtjson, json &outjson)
|
||||
{
|
||||
return true;
|
||||
@@ -58,70 +21,59 @@ void readDataSrv::Init(initparam_t *param)
|
||||
//check data table
|
||||
}
|
||||
|
||||
int readDataSrv::HandleMsg(msg_t msg, msg_t &outMsg, handparam_t *param)
|
||||
{
|
||||
//check param table
|
||||
json tmjson, repjson, datajson, dataarray, cdtjson, repjson1;
|
||||
int iRet=0;
|
||||
|
||||
try {
|
||||
tmjson = json::parse(msg.payload);
|
||||
int readDataSrv::HandleMsg(msg_t msg, msg_t &outMsg, handparam_t *param) {
|
||||
//check param table
|
||||
json tmjson, repjson, datajson, dataarray, cdtjson, repjson1;
|
||||
int iRet = 0;
|
||||
|
||||
cdtjson = tmjson["data"]["reqdata"] ;
|
||||
bool fRet = readDataDb(param->pDbHandle, cdtjson, datajson);
|
||||
if (fRet){
|
||||
for (auto vljson:datajson){
|
||||
vljson["metid"]= tmjson["metid"];
|
||||
vljson["afn"]="04";
|
||||
vljson["dataid"]= tmjson["dataid"];
|
||||
vljson["createdtime"] = time2jsontime(datajson["ctime"]);
|
||||
vljson["source"] = datajson["source"];
|
||||
vljson["databuff"] = datajson["databuff"];
|
||||
dataarray.push_back(vljson);
|
||||
}
|
||||
repjson["dataarray"] = dataarray;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = time2jsontime(time(NULL));
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}else{
|
||||
repjson["dataarray"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = time2jsontime(time(NULL));
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}
|
||||
try {
|
||||
tmjson = json::parse(msg.payload);
|
||||
|
||||
std::vector<std::string> str_array;
|
||||
if (parse_topic(msg.topic, str_array)){
|
||||
msg_t msg_rep;
|
||||
msg_rep.payload = repjson1.dump();
|
||||
std::string tempstr = str_array[0];
|
||||
str_array[0] = str_array[1];
|
||||
str_array[1] = tempstr;
|
||||
for (auto j:str_array){
|
||||
msg_rep.topic += "/";
|
||||
msg_rep.topic += j;
|
||||
}
|
||||
cdtjson = tmjson["data"]["reqdata"];
|
||||
bool fRet = readDataDb(param->pDbHandle, cdtjson, datajson);
|
||||
if (fRet) {
|
||||
for (auto vljson:datajson) {
|
||||
vljson["metid"] = tmjson["metid"];
|
||||
vljson["afn"] = "04";
|
||||
vljson["dataid"] = tmjson["dataid"];
|
||||
vljson["createdtime"] = ToolUtils::getTimeStringWithFomat(datajson["ctime"]);
|
||||
vljson["source"] = datajson["source"];
|
||||
vljson["databuff"] = datajson["databuff"];
|
||||
dataarray.push_back(vljson);
|
||||
}
|
||||
repjson["dataarray"] = dataarray;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
} else {
|
||||
repjson["dataarray"] = NULL;
|
||||
repjson["complete"] = "1";
|
||||
repjson1["data"] = repjson;
|
||||
repjson1["time"] = ToolUtils::now_to_string();
|
||||
repjson1["token"] = tmjson["token"];
|
||||
}
|
||||
|
||||
outMsg = msg_rep;
|
||||
iRet=1;
|
||||
/*
|
||||
if (!WriteResponseMsg(msg_rep)){
|
||||
std::cerr << " WriteResponseMsg error " << std::endl;
|
||||
iRet=0;
|
||||
}else{
|
||||
iRet=1;
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
catch(json::parse_error& ex){
|
||||
std::cerr << "parse error at byte " << ex.byte << std::endl;
|
||||
return 0;
|
||||
}
|
||||
msg_t msg_rep;
|
||||
msg_rep.topic = ToolUtils::makeTpopicStr(msg.subTopicV);
|
||||
msg_rep.payload = repjson1.dump();
|
||||
outMsg = msg_rep;
|
||||
iRet = 1;
|
||||
/*
|
||||
if (!WriteResponseMsg(msg_rep)){
|
||||
std::cerr << " WriteResponseMsg error " << std::endl;
|
||||
iRet=0;
|
||||
}else{
|
||||
iRet=1;
|
||||
}*/
|
||||
|
||||
return iRet;
|
||||
}
|
||||
catch (json::parse_error &ex) {
|
||||
std::cerr << "parse error at byte " << ex.byte << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return iRet;
|
||||
}
|
||||
|
||||
std::string readDataSrv::GetFID(void) /* 如"F-ReadData" */
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
//
|
||||
// Created by Thinkpad on 2025/3/12.
|
||||
//
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include "json.hpp"
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "ToolUtils.h"
|
||||
#include "F_SetSubscribe.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
|
||||
REGISTER_BIZSERVICE("F-SetSubscribe",SetSubscribe);
|
||||
SetSubscribe::SetSubscribe() {
|
||||
handpara = nullptr;
|
||||
initpara = nullptr;
|
||||
}
|
||||
|
||||
SetSubscribe::~SetSubscribe() {
|
||||
|
||||
}
|
||||
|
||||
void SetSubscribe::Init(initparam_t *param) {
|
||||
initpara = param;
|
||||
}
|
||||
|
||||
int SetSubscribe::SQLBatch_exc(std::vector<std::string> &sql_list, std::vector<std::string> &result) {
|
||||
sqlite3 *db = (sqlite3 *)handpara;
|
||||
char *errMsg = nullptr;
|
||||
const char *sql;
|
||||
int rc;
|
||||
int resp = 0;
|
||||
// 开启事务
|
||||
if (sqlite3_exec(db, "BEGIN TRANSACTION;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
||||
std::cerr << "事务开启失败: " << errMsg << std::endl;
|
||||
sqlite3_free(errMsg);
|
||||
return -1;
|
||||
}
|
||||
// 执行批量插入操作
|
||||
sqlite3_stmt *stmt;
|
||||
int idx = 0;
|
||||
for(const auto &k:sql_list){
|
||||
std::cout << "cmd:"<<k.c_str()<<std::endl;
|
||||
if (sqlite3_prepare_v2(db, k.c_str(), -1, &stmt, nullptr) != SQLITE_OK) {
|
||||
std::cerr << "准备语句失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
continue;
|
||||
}
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
std::cerr << "插入失败: " << sqlite3_errmsg(db) << std::endl;
|
||||
result[idx] = "-1";
|
||||
}else
|
||||
result[idx] = "0"; //SUCCESS
|
||||
sqlite3_reset(stmt); // 重置语句
|
||||
}
|
||||
// 提交事务
|
||||
if (sqlite3_exec(db, "COMMIT;", nullptr, nullptr, &errMsg) != SQLITE_OK) {
|
||||
std::cerr << "事务提交失败: " << errMsg << std::endl;
|
||||
sqlite3_free(errMsg);
|
||||
resp = -1;
|
||||
}
|
||||
// 清理
|
||||
sqlite3_finalize(stmt);
|
||||
return resp;
|
||||
}
|
||||
|
||||
int SetSubscribe::HandleMsg(const msg_t msg, msg_t &outMsg, handparam_t *param) {
|
||||
topic.clear();
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
std::vector<std::string> sql_list;
|
||||
std::vector<std::string> result;
|
||||
sql_list.clear();
|
||||
result.clear();
|
||||
|
||||
std::string table_name("xxxxxx");
|
||||
try{
|
||||
value = json::parse(msg.payload);
|
||||
}
|
||||
catch(const std::exception &e){
|
||||
std::cout << "topic "<<msg.topic.c_str()<<" parse error:"<<e.what();
|
||||
return -1;//format fail
|
||||
}
|
||||
printf("insert request:%s \n",msg.payload.c_str());
|
||||
if(!value.is_object())return -1;
|
||||
|
||||
if(value.contains("token")){
|
||||
resp["token"] = value["token"];
|
||||
}
|
||||
if(value.contains("time")){
|
||||
;// value["time"]
|
||||
}
|
||||
|
||||
if(value.contains("data") && value["data"].contains("subparam")){
|
||||
json subvalue = value["data"]["subparam"];
|
||||
if(subvalue.is_array()){
|
||||
for(const auto &k:subvalue){
|
||||
std::string metidStr = k["metid"].get<std::string>();
|
||||
std::cout << "metid:"<<metidStr.c_str()<<std::endl;
|
||||
std::vector<std::string>idclassV = k["idclass"];
|
||||
for(const auto &k1:idclassV){
|
||||
std::cout << "idclass:"<<k1.c_str()<<std::endl;
|
||||
}
|
||||
|
||||
//todo 保存到数据库
|
||||
}
|
||||
}
|
||||
}
|
||||
// ret = SQLBatch_exc(sql_list,result);
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
if(ret == -1)
|
||||
resp["ack"]="false";
|
||||
else if(ret == 0)
|
||||
resp["ack"]="true";
|
||||
|
||||
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp SetSubscribe payload:"<<resp.dump().c_str()<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string SetSubscribe::GetFID(){
|
||||
std::string value("F-SetSubscribe");
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Thinkpad on 2025/3/12.
|
||||
//
|
||||
|
||||
#ifndef DCAPP_F_SETSUBSCRIBE_H
|
||||
#define DCAPP_F_SETSUBSCRIBE_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
|
||||
class SetSubscribe : public IBizService{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param);
|
||||
std::string GetFID();
|
||||
SetSubscribe();
|
||||
~SetSubscribe();
|
||||
private:
|
||||
handparam_t * handpara;
|
||||
initparam_t * initpara;
|
||||
std::vector<std::string> topic;//0:dst 1:src 2:sid 3:fid
|
||||
int SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result);
|
||||
};
|
||||
|
||||
|
||||
#endif //DCAPP_F_SETSUBSCRIBE_H
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "json.hpp"
|
||||
#include "F_SetSupportNum.h"
|
||||
#include "bizservice.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json ;// for convenience
|
||||
@@ -101,7 +102,7 @@ int SetSupportNum::SQLBatch_exc(sqlite3 *db,std::vector<std::string> &sql_list,
|
||||
int SetSupportNum::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/',topic);
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0, update_flag = 0;;
|
||||
@@ -144,13 +145,10 @@ int SetSupportNum::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
ret = SQLBatch_exc((sqlite3*)handpara,sql_list,result);
|
||||
*/
|
||||
resp_process:
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
resp["ack"]="true";
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);;
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp UpdateRecord payload:"<<resp.dump().c_str()<<std::endl;
|
||||
|
||||
@@ -10,8 +10,8 @@
|
||||
#include <vector>
|
||||
#include "bizservice.h"
|
||||
#include "sqlite3.h"
|
||||
#include "F_Tool.h"
|
||||
class SetSupportNum:public IBizService,public Tool{
|
||||
|
||||
class SetSupportNum:public IBizService{
|
||||
public:
|
||||
void Init(initparam_t *param);
|
||||
int HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param = NULL);
|
||||
|
||||
@@ -1,126 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <ctime>
|
||||
#include "json.hpp"
|
||||
#include "datacenter/bizService/F_Tool.h"
|
||||
typedef unsigned char BYTE;
|
||||
void Tool::hexstr_to_str(void *dst, const void *src, int src_len)
|
||||
{
|
||||
int i;
|
||||
BYTE low, high, ch;
|
||||
BYTE *dst_ptr = (BYTE *) dst;
|
||||
const BYTE *src_ptr = (const BYTE *) src;
|
||||
|
||||
for (i = 0; i < src_len / 2; i++)
|
||||
{
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
high = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
high = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
high = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
low = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
low = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
low = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
*dst_ptr++ = high * 16 + low; //hex
|
||||
}
|
||||
}
|
||||
|
||||
void Tool::str_to_hexstr(void *dst, const void *src, int src_len)
|
||||
{
|
||||
int i;
|
||||
BYTE low, high, ch;
|
||||
char *dst_ptr = (char *) dst;
|
||||
const char *src_ptr = (const char *) src;
|
||||
const char *str = "0123456789abcdef";
|
||||
|
||||
for (i = 0; i < src_len; i++)
|
||||
{
|
||||
ch = *src_ptr++;
|
||||
high = (ch >> 4) & 0x0f;
|
||||
low = ch & 0x0f;
|
||||
*dst_ptr++ = str[high];
|
||||
*dst_ptr++ = str[low];
|
||||
}
|
||||
}
|
||||
int Tool::encode_blob(void *dst, int max_len, const void *src, int src_len)
|
||||
{
|
||||
int dst_len = 0;
|
||||
|
||||
// may be changed to base64 encode
|
||||
if (max_len >= src_len * 2 + 1)
|
||||
{
|
||||
dst_len = src_len * 2;
|
||||
str_to_hexstr(dst, src, src_len);
|
||||
}
|
||||
((BYTE *) dst)[dst_len] = 0x00;
|
||||
return dst_len;
|
||||
}
|
||||
|
||||
int Tool::decode_blob(void *dst, int max_len, const void *src)
|
||||
{
|
||||
// may be changed to base64 decode
|
||||
int src_len, dst_len = 0;
|
||||
|
||||
src_len = strlen((char *) src);
|
||||
std::cout << "decode_blob src len "<< src_len<<std::endl;
|
||||
if (max_len >= src_len / 2)
|
||||
{
|
||||
dst_len = src_len / 2;
|
||||
hexstr_to_str(dst, src, src_len);
|
||||
}
|
||||
return dst_len;
|
||||
}
|
||||
void Tool::topic_split(const std::string& str, char delimiter,std::vector<std::string>& topic) {
|
||||
std::string token; // 临时存储每个子字符串
|
||||
std::istringstream tokenStream(str); // 使用字符串流处理输入字符串
|
||||
// 按分隔符逐个读取子字符串
|
||||
while (std::getline(tokenStream, token, delimiter)) {
|
||||
topic.push_back(token); // 将子字符串添加到结果中
|
||||
}
|
||||
|
||||
}
|
||||
std::string Tool::now_to_string() {
|
||||
// 获取系统时钟时间点
|
||||
auto time_now = std::chrono::system_clock::now();
|
||||
|
||||
// 将时间点转成time_t类型的值 (秒)
|
||||
std::time_t tt = std::chrono::system_clock::to_time_t(time_now);
|
||||
|
||||
// 转换成tm结构体形式
|
||||
struct tm* ptm = gmtime(&tt); // 或者 localtime(&tt) 如果想要本地时间
|
||||
ptm->tm_hour += 8; //utc -> cst
|
||||
// 计算出毫秒部分
|
||||
long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
time_now.time_since_epoch()
|
||||
).count() % 1000;
|
||||
|
||||
char buffer[40];
|
||||
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", ptm);
|
||||
|
||||
// 添加上毫秒和时区信息
|
||||
sprintf(buffer + strlen(buffer), ".%03ld+0800", milliseconds);
|
||||
|
||||
return std::string(buffer);
|
||||
}
|
||||
time_t Tool::string_to_now(std::string stamp) {
|
||||
struct tm tm={};
|
||||
std::istringstream ss(stamp);
|
||||
ss >> std::get_time(&tm,"%Y-%m-%dT%H:%M:%S");
|
||||
if(ss.fail()){
|
||||
return -1;
|
||||
}
|
||||
return mktime(&tm);
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
/*
|
||||
* @Description: file content
|
||||
* @Author: why
|
||||
* @version:
|
||||
* @Date: 2025-02-28 14:32:55
|
||||
* @LastEditors: why
|
||||
* @LastEditTime: 2025-02-28 16:28:59
|
||||
*/
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
class Tool{
|
||||
public :
|
||||
void topic_split(const std::string& str, char delimiter,std::vector<std::string>&topic);//0:dst 1:src 2:sid 3:fid
|
||||
std::string now_to_string();
|
||||
time_t string_to_now(std::string stamp);
|
||||
void hexstr_to_str(void *dst, const void *src, int src_len);
|
||||
void str_to_hexstr(void *dst, const void *src, int src_len);
|
||||
int encode_blob(void *dst, int max_len, const void *src, int src_len);
|
||||
int decode_blob(void *dst, int max_len, const void *src);
|
||||
//std::map<usigned int ,std::map<int,std::string>> Directionary;
|
||||
Tool(){};
|
||||
~Tool(){};
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "json.hpp"
|
||||
#include "F_UpdateRecord.h"
|
||||
#include "sqlite3.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
using namespace std;
|
||||
using json = nlohmann::json; // for convenience
|
||||
@@ -31,82 +32,7 @@ UpdateRecord::UpdateRecord(void){
|
||||
handpara = nullptr;
|
||||
initpara = nullptr;
|
||||
}
|
||||
static void hexstr_to_str(void *dst, const void *src, int src_len)
|
||||
{
|
||||
int i;
|
||||
BYTE low, high, ch;
|
||||
BYTE *dst_ptr = (BYTE *) dst;
|
||||
const BYTE *src_ptr = (const BYTE *) src;
|
||||
|
||||
for (i = 0; i < src_len / 2; i++)
|
||||
{
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
high = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
high = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
high = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
ch = *src_ptr++;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
low = ch - '0';
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
low = ch - 'A' + 10;
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
low = ch - 'a' + 10;
|
||||
else
|
||||
break;
|
||||
*dst_ptr++ = high * 16 + low; //hex
|
||||
}
|
||||
}
|
||||
|
||||
static void str_to_hexstr(void *dst, const void *src, int src_len)
|
||||
{
|
||||
int i;
|
||||
BYTE low, high, ch;
|
||||
char *dst_ptr = (char *) dst;
|
||||
const char *src_ptr = (const char *) src;
|
||||
const char *str = "0123456789abcdef";
|
||||
|
||||
for (i = 0; i < src_len; i++)
|
||||
{
|
||||
ch = *src_ptr++;
|
||||
high = (ch >> 4) & 0x0f;
|
||||
low = ch & 0x0f;
|
||||
*dst_ptr++ = str[high];
|
||||
*dst_ptr++ = str[low];
|
||||
}
|
||||
}
|
||||
static int encode_blob(void *dst, int max_len, const void *src, int src_len)
|
||||
{
|
||||
int dst_len = 0;
|
||||
|
||||
// may be changed to base64 encode
|
||||
if (max_len >= src_len * 2 + 1)
|
||||
{
|
||||
dst_len = src_len * 2;
|
||||
str_to_hexstr(dst, src, src_len);
|
||||
}
|
||||
((BYTE *) dst)[dst_len] = 0x00;
|
||||
return dst_len;
|
||||
}
|
||||
|
||||
static int decode_blob(void *dst, int max_len, const void *src)
|
||||
{
|
||||
// may be changed to base64 decode
|
||||
int src_len, dst_len = 0;
|
||||
|
||||
src_len = strlen((char *) src);
|
||||
std::cout << "decode_blob src len "<< src_len<<std::endl;
|
||||
if (max_len >= src_len / 2)
|
||||
{
|
||||
dst_len = src_len / 2;
|
||||
hexstr_to_str(dst, src, src_len);
|
||||
}
|
||||
return dst_len;
|
||||
}
|
||||
UpdateRecord::~UpdateRecord(void){
|
||||
}
|
||||
std::string search_table_db(int afn,unsigned int dataid){
|
||||
@@ -116,36 +42,6 @@ std::string search_table_db(int afn,unsigned int dataid){
|
||||
void UpdateRecord::Init(initparam_t *param){
|
||||
initpara = param;
|
||||
}
|
||||
std::string UpdateRecord::now_to_string() {
|
||||
// 获取系统时钟时间点
|
||||
auto time_now = std::chrono::system_clock::now();
|
||||
|
||||
// 将时间点转成time_t类型的值 (秒)
|
||||
std::time_t tt = std::chrono::system_clock::to_time_t(time_now);
|
||||
|
||||
// 转换成tm结构体形式
|
||||
struct tm* ptm = gmtime(&tt); // 或者 localtime(&tt) 如果想要本地时间
|
||||
ptm->tm_hour += 8; //utc -> cst
|
||||
// 计算出毫秒部分
|
||||
long milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
time_now.time_since_epoch()
|
||||
).count() % 1000;
|
||||
|
||||
char buffer[40];
|
||||
strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", ptm);
|
||||
|
||||
// 添加上毫秒和时区信息
|
||||
sprintf(buffer + strlen(buffer), ".%03ld+0800", milliseconds);
|
||||
|
||||
return std::string(buffer);
|
||||
}
|
||||
time_t UpdateRecord::string_to_now(std::string stamp) {
|
||||
struct tm t;
|
||||
std::istringstream ss(stamp);
|
||||
ss >> std::get_time(&t,"%Y-%m-%dT%H:%M:%S");
|
||||
if(ss.fail())return -1;
|
||||
return mktime(&t);
|
||||
}
|
||||
|
||||
int UpdateRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result){
|
||||
|
||||
@@ -191,7 +87,7 @@ int UpdateRecord::SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<st
|
||||
int UpdateRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
|
||||
topic.clear();
|
||||
topic_split(msg.topic,'/');
|
||||
topic = msg.subTopicV;
|
||||
json value,resp;
|
||||
int ret = 0;
|
||||
int afn_flag = 0, update_flag = 0;;
|
||||
@@ -218,8 +114,8 @@ int UpdateRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
json subvalue = value["data"];
|
||||
int afn = 0;
|
||||
int dataid = 0;
|
||||
decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
||||
decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
||||
ToolUtils::decode_blob(&dataid,4, subvalue["dataid"].get<std::string>().c_str());
|
||||
ToolUtils::decode_blob(&afn,4,subvalue["afn"].get<std::string>().c_str());
|
||||
std::stringstream ss;
|
||||
//table_name =search_table_db(afn,dataid);
|
||||
table_name = "frozen";
|
||||
@@ -238,7 +134,7 @@ int UpdateRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
<< 0 << ","\
|
||||
<<std::stoi(subvalue["metid"].get<std::string>())<<","\
|
||||
<<subvalue["dataid"].get<std::string>().c_str()<<","\
|
||||
<<string_to_now(subvalue["updatefilter"].get<std::string>())<<","\
|
||||
<<ToolUtils::string_to_now(subvalue["updatefilter"].get<std::string>())<<","\
|
||||
<<time(NULL)<<","\
|
||||
<<std::stoi(subvalue["crcaux"].get<std::string>())<<","\
|
||||
<<subvalue["databuff"].get<std::string>().c_str()<<");";
|
||||
@@ -249,18 +145,15 @@ int UpdateRecord::HandleMsg(const msg_t msg, msg_t &outMsg,handparam_t *param){
|
||||
}
|
||||
ret = SQLBatch_exc(sql_list,result);
|
||||
resp_process:
|
||||
resp["time"]=now_to_string();
|
||||
resp["time"]=ToolUtils::now_to_string();
|
||||
if(ret == -1 || afn_flag == -1 || update_flag == -1)
|
||||
resp["ack"]="false";
|
||||
else if(ret == 0)
|
||||
resp["ack"]="true";
|
||||
|
||||
resp["data"]["dataidex"]=result;
|
||||
|
||||
std::stringstream ss;
|
||||
ss << "/"<<topic[1].c_str()<<"/"<<topic[0].c_str()<<"/"\
|
||||
<<topic[2].c_str()<<"/"<<topic[3].c_str();
|
||||
outMsg.topic = ss.str();
|
||||
|
||||
outMsg.topic = ToolUtils::makeTpopicStr(topic);
|
||||
outMsg.timestamp = time(NULL);;
|
||||
outMsg.payload = resp.dump();
|
||||
std::cout << "resp UpdateRecord payload:"<<resp.dump().c_str()<<std::endl;
|
||||
@@ -270,12 +163,3 @@ std::string UpdateRecord::GetFID(){
|
||||
std::string tmp("F-UpdateRecord");
|
||||
return tmp;
|
||||
}
|
||||
void UpdateRecord::topic_split(const std::string& str, char delimiter) {
|
||||
std::string token; // 临时存储每个子字符串
|
||||
std::istringstream tokenStream(str); // 使用字符串流处理输入字符串
|
||||
// 按分隔符逐个读取子字符串
|
||||
while (std::getline(tokenStream, token, delimiter)) {
|
||||
topic.push_back(token); // 将子字符串添加到结果中
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,8 +20,5 @@ class UpdateRecord:public IBizService {
|
||||
handparam_t * handpara;
|
||||
initparam_t * initpara;
|
||||
std::vector<std::string> topic;//0:dst 1:src 2:sid 3:fid
|
||||
void topic_split(const std::string& str, char delimiter);
|
||||
int SQLBatch_exc(std::vector<std::string> &sql_list,std::vector<std::string> &result);
|
||||
std::string now_to_string();
|
||||
time_t string_to_now(std::string stamp);
|
||||
};
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "F_WriteData.h"
|
||||
#include "bizservice.h"
|
||||
#include "f-readdata/normalDataDB.h"
|
||||
#include "ToolUtils.h"
|
||||
|
||||
|
||||
static bool writeDataDb(void *db_handle, const json &cdtjson, json &outjson)
|
||||
@@ -52,7 +53,7 @@ int writeDataSrv::HandleMsg(const msg_t msg,msg_t &outMsg, handparam_t *param)
|
||||
}
|
||||
msg_t msg_rep;
|
||||
msg_rep.payload = repjson1.dump();
|
||||
msg_rep.topic = msg.topic;
|
||||
msg_rep.topic = ToolUtils::makeTpopicStr(msg.subTopicV);
|
||||
outMsg = msg_rep;
|
||||
iRet=1;
|
||||
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
//
|
||||
// Created by zgj on 2024/4/29.
|
||||
//
|
||||
|
||||
#ifndef METERMQTT_TOOLUTILS_H
|
||||
#define METERMQTT_TOOLUTILS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ToolUtils{
|
||||
std::string getTimeString(bool isLocalTime);
|
||||
std::string getUtcTimeString(bool isLocalTime);
|
||||
std::string getTimeStringWithFomat(bool isLocalTime);
|
||||
uint64_t getUtcTime(bool isLocalTime);
|
||||
bool isCSTZone();
|
||||
bool subString(std::string srcStr, std::string subStr,std::string &resultStr);
|
||||
std::string genUUID();
|
||||
}
|
||||
#endif //METERMQTT_TOOLUTILS_H
|
||||
@@ -8,6 +8,7 @@
|
||||
#define _DCAPP_CENTER_INTERFACE_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
@@ -23,6 +24,7 @@ typedef struct msg
|
||||
{
|
||||
std::string topic; // 消息主题
|
||||
std::string payload; // 消息内容
|
||||
std::vector<std::string>subTopicV;//分段主题
|
||||
long timestamp; // 消息时间戳
|
||||
} msg_t; // 消息体结构
|
||||
|
||||
|
||||
@@ -50,6 +50,7 @@ public:
|
||||
|
||||
std::thread recvThread(&DataCenter::ExecPoll,this);
|
||||
std::thread sendThread(&DataCenter::ExecReponse, this);
|
||||
//todo 构造任务 用于监测数据是否更新
|
||||
recvThread.join();
|
||||
sendThread.join();
|
||||
}
|
||||
@@ -66,6 +67,7 @@ public:
|
||||
private:
|
||||
void datacenterinit(void); // 初始化
|
||||
void datacenterOnline(void);
|
||||
void updateSubTopicsMap(msg_mqtt_t &);
|
||||
private:
|
||||
bool recvThreadFlag = false;
|
||||
bool sendThreadFlag = false;
|
||||
@@ -73,6 +75,7 @@ private:
|
||||
MessageQueue mQueue_Out; // 输出队列
|
||||
CMqttClient *objmqttClient;
|
||||
ThreadPool *objThreadPool;
|
||||
std::map<std::string/*订阅主题*/,std::vector<std::string/*主题字段*/>>tmpSubTopicsMap;
|
||||
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
*/
|
||||
#ifndef MSG_H_
|
||||
#define MSG_H_
|
||||
|
||||
/**
|
||||
* @brief 消息传递格式
|
||||
*
|
||||
@@ -18,6 +17,7 @@ typedef struct msg_mqtt
|
||||
{
|
||||
std::string topic;
|
||||
std::string payload;
|
||||
std::vector<std::string>subTopicV;
|
||||
} msg_mqtt_t;
|
||||
|
||||
#endif
|
||||
@@ -1,104 +0,0 @@
|
||||
//
|
||||
// Created by zgj on 2024/4/29.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include <uuid/uuid.h>
|
||||
#include "ToolUtils.h"
|
||||
|
||||
namespace ToolUtils {
|
||||
/**
|
||||
* 获取当前时间的字符串表示。
|
||||
*
|
||||
* @param isLocalTime 如果为true,则返回本地时间的字符串表示,否则返回UTC时间的字符串表示。
|
||||
* @return 返回当前时间(根据isLocalTime参数决定是本地时间还是UTC时间)的字符串表示,格式为"YYYY-MM-DD HH:MM:SS"。
|
||||
*/
|
||||
std::string getTimeString(bool isLocalTime) {
|
||||
char buf[128] = {0};
|
||||
|
||||
time_t now = time(NULL);
|
||||
if (isLocalTime) {
|
||||
now = now + 8 * 3600;
|
||||
}
|
||||
struct tm ttm;
|
||||
struct tm *pTm = NULL;
|
||||
pTm = localtime_r(&now, &ttm);
|
||||
|
||||
snprintf(buf, sizeof(buf), "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
|
||||
pTm->tm_year + 1900,
|
||||
pTm->tm_mon + 1,
|
||||
pTm->tm_mday,
|
||||
pTm->tm_hour,
|
||||
pTm->tm_min,
|
||||
pTm->tm_sec
|
||||
);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
|
||||
std::string getUtcTimeString(bool isLocalTime) {
|
||||
return std::to_string(isLocalTime ? time(NULL) + 8 * 360: time(NULL));
|
||||
}
|
||||
|
||||
std::string getTimeStringWithFomat(bool isLocalTime) {
|
||||
char buf[128] = {0};
|
||||
|
||||
time_t now = time(NULL);
|
||||
if (isLocalTime) {
|
||||
now = now + 8 * 3600;
|
||||
}
|
||||
struct tm ttm;
|
||||
struct tm *pTm = NULL;
|
||||
pTm = localtime_r(&now, &ttm);
|
||||
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_REALTIME, &ts);
|
||||
|
||||
snprintf(buf, sizeof(buf), "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d.%03ld+0800",
|
||||
pTm->tm_year + 1900,
|
||||
pTm->tm_mon + 1,
|
||||
pTm->tm_mday,
|
||||
pTm->tm_hour,
|
||||
pTm->tm_min,
|
||||
pTm->tm_sec,
|
||||
ts.tv_nsec/1000000
|
||||
);
|
||||
|
||||
return std::string(buf);
|
||||
}
|
||||
|
||||
uint64_t getUtcTime(bool isLocalTime) {
|
||||
return isLocalTime ? time(NULL) + 8 * 3600: time(NULL);
|
||||
}
|
||||
|
||||
bool isCSTZone(){
|
||||
std::string zoneStr = "CST";
|
||||
struct tm ttm;
|
||||
time_t tt = time(NULL);
|
||||
struct tm *local_time = localtime_r(&tt, &ttm);
|
||||
if(zoneStr.compare(local_time->tm_zone) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool subString(std::string srcStr, std::string subStr,std::string &resultStr) {
|
||||
int pos = -1;
|
||||
pos = srcStr.find(subStr);
|
||||
if(pos != std::string::npos){
|
||||
resultStr = srcStr.substr(pos + subStr.length());
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// 生成UUID
|
||||
std::string genUUID(){
|
||||
uuid_t bin_uuid;
|
||||
char uuid_str[37];
|
||||
uuid_generate_random(bin_uuid);
|
||||
uuid_unparse_lower(bin_uuid, uuid_str);
|
||||
return std::string(uuid_str);
|
||||
}
|
||||
}
|
||||
@@ -56,8 +56,9 @@ void DataCenter::datacenterOnline() {
|
||||
msg_mqtt_t outMqttMsg;
|
||||
outMqttMsg.topic = outMsg.topic;
|
||||
outMqttMsg.payload = outMsg.payload;
|
||||
std::cout << "send:" << outMqttMsg.topic << " : " << outMqttMsg.payload << std::endl;
|
||||
mQueue_Out.push(outMqttMsg);
|
||||
|
||||
//mQueue_Out.push(outMqttMsg);
|
||||
this->objmqttClient->publish(outMqttMsg.topic,outMqttMsg.payload);
|
||||
}
|
||||
std::string getPidFromTopic(std::string topic){
|
||||
size_t pos = topic.find_last_of("/");
|
||||
@@ -72,10 +73,9 @@ bool handleMsg(msg_mqtt_t inMqttMsg,MessageQueue &mQueue_Out) {
|
||||
|
||||
intMsg.topic = inMqttMsg.topic;
|
||||
intMsg.payload = inMqttMsg.payload;
|
||||
intMsg.subTopicV = inMqttMsg.subTopicV;
|
||||
intMsg.timestamp = ToolUtils::getUtcTime(true);
|
||||
|
||||
std::cout << "Recv:" << inMqttMsg.topic << " : " << inMqttMsg.payload << std::endl;
|
||||
|
||||
std::string pidStr = getPidFromTopic(inMqttMsg.topic);
|
||||
if(pidStr.empty()){
|
||||
return false;
|
||||
@@ -100,6 +100,17 @@ bool handleMsg(msg_mqtt_t inMqttMsg,MessageQueue &mQueue_Out) {
|
||||
mQueue_Out.push(outMqttMsg);
|
||||
return true;
|
||||
}
|
||||
|
||||
void DataCenter::updateSubTopicsMap(msg_mqtt_t &msgMqtt) {
|
||||
if(this->tmpSubTopicsMap.find(msgMqtt.topic) != this->tmpSubTopicsMap.end()){
|
||||
msgMqtt.subTopicV = this->tmpSubTopicsMap[msgMqtt.topic];
|
||||
return;
|
||||
}
|
||||
std::vector<std::string>subTopicV;
|
||||
ToolUtils::topic_split(msgMqtt.topic, '/', subTopicV);
|
||||
msgMqtt.subTopicV = subTopicV;
|
||||
this->tmpSubTopicsMap.insert(std::make_pair(msgMqtt.topic, subTopicV));
|
||||
}
|
||||
void DataCenter::ExecPoll(void *argc) {
|
||||
DataCenter *that = (DataCenter *)argc;
|
||||
if(NULL == that){
|
||||
@@ -108,6 +119,8 @@ void DataCenter::ExecPoll(void *argc) {
|
||||
|
||||
while (!that->recvThreadFlag) {
|
||||
msg_mqtt_t inMsg = that->mQueue_In.pop();
|
||||
that->updateSubTopicsMap(inMsg);
|
||||
std::cout << "Recv topic :" << inMsg.topic << "\n playload : " << inMsg.payload << std::endl;
|
||||
that->objThreadPool->submitTask(1,handleMsg, inMsg, std::ref(that->mQueue_Out));
|
||||
}
|
||||
|
||||
@@ -121,7 +134,7 @@ void DataCenter::ExecReponse(void *argc) {
|
||||
|
||||
while(!that->sendThreadFlag){
|
||||
msg_mqtt_t outMsg = that->mQueue_Out.pop();
|
||||
std::cout << "send:" << outMsg.topic << " : " << outMsg.payload << std::endl;
|
||||
std::cout << "send topic :" << outMsg.topic << "\n playload : " << outMsg.payload << std::endl;
|
||||
that->objmqttClient->publish(outMsg.topic,outMsg.payload);
|
||||
}
|
||||
}
|
||||
@@ -74,9 +74,10 @@ void CMqttClient::printfMqttCfgInfo(){
|
||||
std::cout << "gatewayID:" << this->strGatewayID_ << std::endl;
|
||||
};
|
||||
void CMqttClient::printfTopicList() {
|
||||
std::cout << "sub topic : " << std::endl;
|
||||
for (std::vector<std::string>::const_iterator c_iter = subTopicsList_.begin();
|
||||
c_iter != subTopicsList_.end(); ++c_iter) {
|
||||
std::cout << "topic:" << c_iter->c_str() << std::endl;
|
||||
std::cout << "\t" << c_iter->c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
void CMqttClient::loadMqttCfgFromFile() {
|
||||
|
||||
Reference in New Issue
Block a user