127 lines
2.9 KiB
C
127 lines
2.9 KiB
C
|
|
#pragma once
|
|||
|
|
/**
|
|||
|
|
* @file public.h
|
|||
|
|
* @brief data save handle
|
|||
|
|
* 保存正常数据的处理类
|
|||
|
|
* @author your name (you@domain.com)
|
|||
|
|
* @version 0.1
|
|||
|
|
* @date 2026-01-29
|
|||
|
|
*
|
|||
|
|
* Copyright: Baosight Co. Ltd.
|
|||
|
|
* DO NOT COPY/USE WITHOUT PERMISSION
|
|||
|
|
*
|
|||
|
|
*/
|
|||
|
|
#include "mix_cc/json.h"
|
|||
|
|
#include <chrono>
|
|||
|
|
#include <map>
|
|||
|
|
#include <memory>
|
|||
|
|
#include <string>
|
|||
|
|
#include <vector>
|
|||
|
|
#include <zlib/MemMap.hpp>
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* @brief dsm用MemMap的配置
|
|||
|
|
*/
|
|||
|
|
struct DsmMapConfig {
|
|||
|
|
static inline constexpr char TableName[] = "ZONE10"; ///<表名
|
|||
|
|
static constexpr int MapMaxSize = 5000; ///< map 最大容量
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
typedef char TagName[50]; ///<数据项(tag点英文item,长度限制50字节以内)
|
|||
|
|
typedef TagName Tags[30]; ///<单个规则tag点,个数限制30个以内
|
|||
|
|
using std::vector;
|
|||
|
|
/**
|
|||
|
|
* @brief 保存正常数据,数据信息
|
|||
|
|
*/
|
|||
|
|
struct DataInfo {
|
|||
|
|
int64_t start;
|
|||
|
|
int64_t end;
|
|||
|
|
Tags tags;
|
|||
|
|
int flag = 0; ///< 0--RuleNormData;1----RuleAlertData
|
|||
|
|
int tag_size = 0;
|
|||
|
|
|
|||
|
|
void init(vector<std::string> tag_s) {
|
|||
|
|
tag_size = 0;
|
|||
|
|
int size_n = tag_s.size();
|
|||
|
|
if (size_n == 0) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
for (int i = 0; i < size_n; i++) {
|
|||
|
|
std::strcpy(tags[i], tag_s[i].c_str());
|
|||
|
|
tag_size++;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* @brief
|
|||
|
|
* @param _start 开始时间 时间戳
|
|||
|
|
* @param _end 结束时间 时间戳
|
|||
|
|
* @param _flag 数据类型 0--正常数据;1--报警数据,
|
|||
|
|
* default 0 正常数据
|
|||
|
|
*/
|
|||
|
|
void update(int64_t _start, int64_t _end, int _flag = 0) {
|
|||
|
|
start = _start;
|
|||
|
|
end = _end;
|
|||
|
|
flag = _flag;
|
|||
|
|
}
|
|||
|
|
DataInfo() {
|
|||
|
|
tag_size = 0;
|
|||
|
|
start = 0;
|
|||
|
|
end = 0;
|
|||
|
|
flag = 0;
|
|||
|
|
}
|
|||
|
|
DataInfo(nlohmann::json js1) {
|
|||
|
|
tag_size = 0;
|
|||
|
|
start = js1["start"];
|
|||
|
|
end = js1["end"];
|
|||
|
|
vector<std::string> tag_s = js1["tags"];
|
|||
|
|
for (int i = 0; i < tag_s.size(); i++) {
|
|||
|
|
std::strcpy(tags[i], tag_s[i].c_str());
|
|||
|
|
tag_size++;
|
|||
|
|
}
|
|||
|
|
if (js1.contains("data_type")) {
|
|||
|
|
std::string data_type = js1["data_type"].get<std::string>();
|
|||
|
|
flag = 0;
|
|||
|
|
if (data_type == "RuleAlertData") {
|
|||
|
|
flag = 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
nlohmann::json invert2json() {
|
|||
|
|
nlohmann::json js1;
|
|||
|
|
js1["start"] = start;
|
|||
|
|
js1["end"] = end;
|
|||
|
|
vector<std::string> tag_s;
|
|||
|
|
for (int i = 0; i < tag_size; i++) {
|
|||
|
|
tag_s.push_back(std::string(tags[i]));
|
|||
|
|
}
|
|||
|
|
js1["tags"] = tag_s;
|
|||
|
|
js1["data_type"] = "RuleNormData";
|
|||
|
|
if (flag == 1) {
|
|||
|
|
js1["data_type"] = "RuleAlertData";
|
|||
|
|
}
|
|||
|
|
return js1;
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
class DsmHandle {
|
|||
|
|
private:
|
|||
|
|
std::unique_ptr<CMemMap<std::string, DataInfo>> ruleid_CMemMap_;
|
|||
|
|
|
|||
|
|
public:
|
|||
|
|
DsmHandle() {
|
|||
|
|
if (ruleid_CMemMap_ == nullptr) {
|
|||
|
|
ruleid_CMemMap_ = std::make_unique<CMemMap<std::string, DataInfo>>(
|
|||
|
|
DsmMapConfig::TableName, DsmMapConfig::MapMaxSize);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
~DsmHandle() {}
|
|||
|
|
bool insert(const std::string& ruleid, DataInfo *data_info) {
|
|||
|
|
try {
|
|||
|
|
ruleid_CMemMap_->insert(ruleid, data_info);
|
|||
|
|
} catch (const std::exception &e) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
};
|