From b6b202951280108ff09b4af86ec79fdd79edbb2c Mon Sep 17 00:00:00 2001 From: zgj Date: Wed, 12 Mar 2025 17:57:31 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=90=88=E5=B9=B6=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 11 +- com/ToolUtils.cpp | 245 --------------------------------------- com/ToolUtils.h | 29 ----- common/inc/ToolUtils.h | 12 +- common/src/ToolUtils.cpp | 145 ++++++++++++++++++++++- 5 files changed, 159 insertions(+), 283 deletions(-) delete mode 100644 com/ToolUtils.cpp delete mode 100644 com/ToolUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5110343..f0b249d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,13 +15,12 @@ aux_source_directory(./dataservice DS_SRC) aux_source_directory(${PROJECT_SOURCE_DIR}/datacenter/bizService DS_BIZSRC) -add_executable(dcapp - ${PROJECT_SOURCE_DIR}/main.cpp - ${PROJECT_SOURCE_DIR}/common/src/datacenter.cpp +add_executable(dcapp ${PROJECT_SOURCE_DIR}/main.cpp + ${PROJECT_SOURCE_DIR}/datacenter/src/datacenter.cpp + ${PROJECT_SOURCE_DIR}/datacenter/src/mqttClient.cpp ${PROJECT_SOURCE_DIR}/common/src/queue.cpp - ${PROJECT_SOURCE_DIR}/common/src/mqttClient.cpp - ${PROJECT_SOURCE_DIR}/common/ToolUtils.cpp - ${PROJECT_SOURCE_DIR}/datacenter/src/thread_pool.cpp + ${PROJECT_SOURCE_DIR}/common/src/ToolUtils.cpp + ${PROJECT_SOURCE_DIR}/common/src/thread_pool.cpp ${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetVersion.cpp ${PROJECT_SOURCE_DIR}/datacenter/bizService/F_GetVersion.cpp ${PROJECT_SOURCE_DIR}/datacenter/bizService/F_InsertRecord.cpp diff --git a/com/ToolUtils.cpp b/com/ToolUtils.cpp deleted file mode 100644 index 3003854..0000000 --- a/com/ToolUtils.cpp +++ /dev/null @@ -1,245 +0,0 @@ -// -// Created by zgj on 2024/4/29. -// - -#include -#include -#include -#include -#include -#include -#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<= 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& 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::vectortopicV){ - 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( - 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); - } -} diff --git a/com/ToolUtils.h b/com/ToolUtils.h deleted file mode 100644 index fc4b335..0000000 --- a/com/ToolUtils.h +++ /dev/null @@ -1,29 +0,0 @@ -// -// Created by zgj on 2024/4/29. -// - -#ifndef METERMQTT_TOOLUTILS_H -#define METERMQTT_TOOLUTILS_H - -#include -#include - -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& topic); - std::string makeTpopicStr(std::vectortopicV); - std::string now_to_string(); - time_t string_to_now(std::string stamp); -} -#endif //METERMQTT_TOOLUTILS_H diff --git a/common/inc/ToolUtils.h b/common/inc/ToolUtils.h index d11ee38..fc4b335 100644 --- a/common/inc/ToolUtils.h +++ b/common/inc/ToolUtils.h @@ -6,14 +6,24 @@ #define METERMQTT_TOOLUTILS_H #include +#include namespace ToolUtils{ std::string getTimeString(bool isLocalTime); std::string getUtcTimeString(bool isLocalTime); - std::string getTimeStringWithFomat(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& topic); + std::string makeTpopicStr(std::vectortopicV); + std::string now_to_string(); + time_t string_to_now(std::string stamp); } #endif //METERMQTT_TOOLUTILS_H diff --git a/common/src/ToolUtils.cpp b/common/src/ToolUtils.cpp index 639555c..244f07c 100644 --- a/common/src/ToolUtils.cpp +++ b/common/src/ToolUtils.cpp @@ -4,7 +4,11 @@ #include #include -#include "common/inc/ToolUtils.h" +#include +#include +#include +#include +#include "../inc/ToolUtils.h" namespace ToolUtils { /** @@ -41,7 +45,7 @@ namespace ToolUtils { return std::to_string(isLocalTime ? time(NULL) + 8 * 360: time(NULL)); } - std::string getTimeStringWithFomat(bool isLocalTime) { + std::string getCurTimeStringWithFomat(bool isLocalTime) { char buf[128] = {0}; time_t now = time(NULL); @@ -101,4 +105,141 @@ namespace ToolUtils { 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<= 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& 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::vectortopicV){ + 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( + 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); + } }