424 lines
12 KiB
C++
424 lines
12 KiB
C++
#pragma once
|
|
/**
|
|
* @file eqpalg/define/public.h
|
|
* @brief eqpalg项目下常用公共定义
|
|
* @author Cat (null.null.null@qq.com)
|
|
* @version 0.1
|
|
* @date 2021-07-08
|
|
*
|
|
* Company: Baosight Co. Ltd.
|
|
* DO NOT COPY/USE WITHOUT PERMISSION
|
|
*
|
|
*/
|
|
#include "mix_cc/json.h"
|
|
#include <chrono>
|
|
#include <string>
|
|
|
|
#define EVENT_NO_MAX 2019
|
|
#define EVENT_NO_MIN 2000
|
|
#define STASTIC_DAYS 7
|
|
/**
|
|
* @brief 进程类型
|
|
*/
|
|
enum class ProcessType {
|
|
kNull = 0, ///< 空进程,由错误的初始化方法得到的进程
|
|
kMon, ///< 监控进程
|
|
kCron, ///< 定时任务进程
|
|
kTask ///< 任务进程
|
|
};
|
|
/**
|
|
* @brief 其他算法与机器学习类算法的临界算法号
|
|
*/
|
|
struct AlgId_ML {
|
|
static const int AlgIdMLMin = 110;
|
|
};
|
|
/**
|
|
* @brief 表达式类型
|
|
*/
|
|
struct ExpType {
|
|
static const int Logic = 1; ///< 逻辑判断
|
|
static const int Bound = 2; ///< 上下限
|
|
static const int ActionFeedBack = 3; ///< 动作反馈-逻辑判断
|
|
static const int CondBound = 4; ///< 条件-上下限
|
|
static const int BoundHoldTime = 5; ///< 上下限-保持时间
|
|
static const int HoldTimeAcc = 6; ///<保持时间累计
|
|
static const int OccTimesAcc = 7; ///<出现次数累计
|
|
static const int PolyFit = 12; ///< 多项式拟合
|
|
static const int PEAR = 13; ///< 皮尔逊相关系数监控
|
|
static const int Trend = -1; ///< Trend 趋势监控
|
|
static const int OuterPer = 16; ///< 离群百分比阈值
|
|
static const int OuterAct = 18; ///< 离群实际值阈值
|
|
};
|
|
/**
|
|
* @brief 区间模式
|
|
*/
|
|
struct DistMode {
|
|
static const int Manual = 0; ///< 手动
|
|
static const int Online = 1; ///< 在线
|
|
static const int Offline = 2; ///< 离线
|
|
};
|
|
/**
|
|
* @brief 检测模式
|
|
*/
|
|
struct DetectMode {
|
|
static const int Default = 0; ///<默认 双侧
|
|
static const int OnlyLeft = 1; ///<只检测左区间
|
|
static const int OnlyRight = 2; //<只检测右区间
|
|
static const int ErrorMode = 3; ///<错误模式
|
|
};
|
|
/**
|
|
* @brief 规则运行前提条件
|
|
*/
|
|
struct PRR {
|
|
static const int None = 0; ///<无
|
|
static const int Exp = 1; ///<表达式
|
|
// static const int EntRunning = 1; ///< 酸洗入口运行
|
|
// static const int ACDRunning = 2; ///< 酸洗中央运行
|
|
// static const int AXTRunning = 3; ///<酸洗出口运行
|
|
// static const int TCMRunning = 4; ///<轧机段运行
|
|
};
|
|
|
|
/**
|
|
* @brief
|
|
*/
|
|
struct StatConst {
|
|
static const int64_t CronUpdateDelay = 24; ///< 更新统计结果的时间间隔
|
|
static const int StatClassCount = 50; ///< 样本基本组数
|
|
};
|
|
|
|
/**
|
|
* @brief 样本类型
|
|
*/
|
|
struct SampleType {
|
|
static constexpr char T_SAMPLE_FIT[] = "T_SAMPLE_FIT"; ///< 2D 线性相关性/拟合
|
|
static constexpr char T_SAMPLE_STAT[] = "T_SAMPLE_STAT"; ///< 1D 统计
|
|
};
|
|
|
|
/**
|
|
* @brief 导入模型标记位
|
|
*/
|
|
struct CreateModels {
|
|
bool create_new_models = false;
|
|
};
|
|
|
|
/**
|
|
* @brief 对应控制过程的事件号
|
|
*/
|
|
struct EventCase {
|
|
static const int kDelete = 0; ///< 删除任务
|
|
static const int kCreate = 1; ///< 创建任务
|
|
static const int kUpdate = 2; ///< 更新任务
|
|
static const int kEnable = 3; ///< 启用任务
|
|
static const int kReset = 4; ///< 重置任务
|
|
static const int kExec = 10; ///< 单次执行任务
|
|
};
|
|
|
|
/**
|
|
* @brief 数据来源
|
|
* 包含IHDB和共享内存
|
|
*/
|
|
struct DataSource {
|
|
static const int16_t IHDB = 0; ///< IHDB
|
|
static const int16_t MEMORY = 1; ///< 共享内存
|
|
};
|
|
|
|
/**
|
|
* @brief 存储消息类型
|
|
*/
|
|
struct MsgLevel {
|
|
static constexpr char INFO[] = "INFO";
|
|
static constexpr char WARN[] = "WARN";
|
|
static constexpr char ERROR[] = "ERROR";
|
|
};
|
|
|
|
/**
|
|
* @brief 报警信息结构体
|
|
*/
|
|
struct AlarmInfo {
|
|
/**
|
|
* @brief Construct a new Alarm Info object
|
|
*/
|
|
AlarmInfo() {
|
|
alarmed = false;
|
|
auto tmp = std::chrono::system_clock::now();
|
|
alarm_start_time = tmp;
|
|
alarm_end_time = tmp;
|
|
}
|
|
|
|
~AlarmInfo() {}
|
|
|
|
bool alarmed; ///< 是否报警
|
|
mix_cc::json content; ///<报警内容
|
|
std::chrono::system_clock::time_point alarm_start_time; ///< 报警开始时间
|
|
std::chrono::system_clock::time_point alarm_end_time; ///< 报警结束时间
|
|
/**
|
|
* @brief 规则配置信息
|
|
*/
|
|
struct ConfigInfo {
|
|
std::string id; ///< 规则id
|
|
std::string name; ///< 规则名
|
|
std::string group; ///< 规则组
|
|
std::string remark; ///< 标记
|
|
std::string descName;
|
|
};
|
|
ConfigInfo cfg_info;
|
|
};
|
|
|
|
// /**
|
|
// * @brief 报表数据
|
|
// * 弃用
|
|
// */
|
|
// struct TableData {
|
|
// std::string group_name_; ///< cfg 分组名
|
|
// int rule_nums_; ///<规则数量
|
|
// std::vector<std::string> rule_ids_; ///<报表涉及的模型id 运行时间
|
|
// std::vector<std::string> rule_names_; ///<模型名
|
|
// std::vector<double> running_time_; ///<运行时间 0
|
|
// std::vector<int> alrm_times_; ///<报警次数 2
|
|
// std::vector<int> fault_times_; ///<故障次数 1
|
|
// std::vector<unsigned long> used_times_; ///<使用次数 3
|
|
// /**
|
|
// * @brief init_id 初始化
|
|
// * @param rule_ids My Param doc
|
|
// * @param rule_names My Param doc
|
|
// */
|
|
// void init_id(std::vector<std::string> rule_ids,
|
|
// std::vector<std::string> rule_names) {
|
|
// this->rule_ids_ = rule_ids;
|
|
// this->rule_names_ = rule_names;
|
|
// this->rule_nums_ = this->rule_ids_.size();
|
|
// std::vector<std::pair<std::string, std::string>> zipped;
|
|
// for (size_t i = 0; i < this->rule_nums_; i++) {
|
|
// zipped.push_back(std::make_pair(rule_ids_[i], rule_names_[i]));
|
|
// } // Sort the vector of pairs
|
|
// std::sort(zipped.begin(), zipped.end(),
|
|
// [](std::pair<std::string, std::string> a,
|
|
// std::pair<std::string, std::string> b) {
|
|
// return a.second < b.second;
|
|
// });
|
|
// for (size_t i = 0; i < this->rule_nums_; i++) {
|
|
// this->rule_ids_[i] = zipped[i].first;
|
|
// this->rule_names_[i] = zipped[i].second;
|
|
// }
|
|
// this->running_time_ = std::vector<double>(rule_ids_.size(), 0);
|
|
// this->alrm_times_ = std::vector<int>(rule_ids_.size(), 0);
|
|
// this->fault_times_ = std::vector<int>(rule_ids_.size(), 0);
|
|
// this->used_times_ = std::vector<unsigned long>(rule_ids_.size(), 0);
|
|
// }
|
|
|
|
// void clear_running_time() {
|
|
// if (!running_time_.empty()) {
|
|
// running_time_.clear();
|
|
// }
|
|
// }
|
|
// void clear_alrm_times() {
|
|
// if (!alrm_times_.empty()) {
|
|
// alrm_times_.clear();
|
|
// }
|
|
// }
|
|
// void clear_fault_times() {
|
|
// if (!fault_times_.empty()) {
|
|
// fault_times_.clear();
|
|
// }
|
|
// }
|
|
// void clear_used_times() {
|
|
// if (!used_times_.empty()) {
|
|
// used_times_.clear();
|
|
// }
|
|
// }
|
|
// nlohmann::json invert2json() {
|
|
// nlohmann::json js1;
|
|
// js1["rule_ids"] = rule_ids_;
|
|
// js1["rule_names"] = rule_names_;
|
|
// if (!running_time_.empty()) {
|
|
// js1["running_time"] = running_time_;
|
|
// }
|
|
// if (!alrm_times_.empty()) {
|
|
// js1["alarm_times"] = alrm_times_;
|
|
// }
|
|
// if (!fault_times_.empty()) {
|
|
// js1["fault_times"] = fault_times_;
|
|
// }
|
|
// if (!used_times_.empty()) {
|
|
// js1["fault_times"] = fault_times_;
|
|
// }
|
|
|
|
// return js1;
|
|
// }
|
|
// };
|
|
|
|
// /**
|
|
// * @brief 仪表数据项
|
|
// *弃用
|
|
// */
|
|
// struct MeterData {
|
|
// std::string group_name_; ///< cfg 分组名
|
|
// int rule_nums_; ///<规则数量
|
|
// std::vector<std::string> rule_ids_; ///<报表涉及的模型id 运行时间
|
|
// std::vector<std::string> rule_names_; ///<模型名
|
|
// std::vector<double> upL_; ///<报警上限
|
|
// std::vector<double> downL_; ///<报警下限
|
|
// std::vector<std::string> latest_alarm_time_; ///<最新一次报警时间
|
|
|
|
// /**
|
|
// * @brief init_id 初始化
|
|
// * @param rule_ids My Param doc
|
|
// * @param rule_names My Param doc
|
|
// */
|
|
// void init_sort_by_name() {
|
|
// this->rule_nums_ = this->rule_ids_.size();
|
|
// std::vector<std::pair<std::string, std::string>> zipped;
|
|
// for (size_t i = 0; i < this->rule_nums_; i++) {
|
|
// zipped.push_back(std::make_pair(rule_ids_[i], rule_names_[i]));
|
|
// } // Sort the vector of pairs
|
|
// std::sort(zipped.begin(), zipped.end(),
|
|
// [](std::pair<std::string, std::string> a,
|
|
// std::pair<std::string, std::string> b) {
|
|
// return a.second < b.second;
|
|
// });
|
|
// for (size_t i = 0; i < this->rule_nums_; i++) {
|
|
// this->rule_ids_[i] = zipped[i].first;
|
|
// this->rule_names_[i] = zipped[i].second;
|
|
// }
|
|
// this->upL_ = std::vector<double>(rule_ids_.size(), 0);
|
|
// this->downL_ = std::vector<double>(rule_ids_.size(), 0);
|
|
// this->latest_alarm_time_ =
|
|
// std::vector<std::string>(rule_ids_.size(), "无报警");
|
|
// }
|
|
|
|
// void clear_upL() {
|
|
// if (!this->upL_.empty()) {
|
|
// this->upL_.clear();
|
|
// }
|
|
// }
|
|
// void clear_downL() {
|
|
// if (!this->downL_.empty()) {
|
|
// this->downL_.clear();
|
|
// }
|
|
// }
|
|
// void clear_latest_alarm_time() {
|
|
// if (!this->latest_alarm_time_.empty()) {
|
|
// this->latest_alarm_time_.clear();
|
|
// }
|
|
// }
|
|
|
|
// nlohmann::json invert2json() {
|
|
// nlohmann::json js1;
|
|
// js1["rule_ids"] = rule_ids_;
|
|
// js1["rule_names"] = rule_names_;
|
|
// if (!upL_.empty()) {
|
|
// js1["upL"] = upL_;
|
|
// }
|
|
// if (!downL_.empty()) {
|
|
// js1["downL"] = downL_;
|
|
// }
|
|
// if (!latest_alarm_time_.empty()) {
|
|
// js1["latest_alarm_time"] = latest_alarm_time_;
|
|
// }
|
|
// return js1;
|
|
// }
|
|
// };
|
|
|
|
// /**
|
|
// * @brief RuleStat 内存版
|
|
// * 弃用
|
|
// */
|
|
// struct RuleStat {
|
|
// double alarm_value;
|
|
// double limit_up;
|
|
// double limit_down;
|
|
// double running_time;
|
|
// double running_time_th;
|
|
// double motion_time;
|
|
// double motion_time_th;
|
|
// int64_t shear_times;
|
|
// int64_t alarm_times;
|
|
// int64_t last_alarm_time;
|
|
// std::vector<std::string> items;
|
|
// std::string dev_coder;
|
|
// nlohmann::json invert2json() {
|
|
// nlohmann::json js1;
|
|
// js1["alarm_value"] = alarm_value; ///<实时报警值
|
|
// js1["limit_up"] = limit_up; ///<上限阈值
|
|
// js1["limit_down"] = limit_down; ///<下限阈值
|
|
// js1["running_time"] = running_time; ///<统计的运行时间
|
|
// js1["running_time_th"] = running_time_th; ///<运行时间阈值
|
|
// js1["motion_time"] = motion_time; ///<动作时间
|
|
// js1["motion_time_th"] = motion_time_th; ///<动作时间阈值
|
|
// js1["shear_times"] = shear_times; ///<剪切次数
|
|
// js1["alarm_times"] = alarm_times; ///<报警次数
|
|
// js1["last_alarm_time"] = last_alarm_time; ///<上次报警时间
|
|
// js1["items"] = items; ///< tag点
|
|
// js1["dev_coder"] = dev_coder; ///<九位码
|
|
// return js1;
|
|
// }
|
|
// };
|
|
|
|
/**
|
|
* @brief sample2D二维数据 数据结构
|
|
*
|
|
*/
|
|
struct sample2D {
|
|
int orders = 1;
|
|
std::vector<std::vector<double>> fit_coefs;
|
|
std::vector<double> scores;
|
|
double pear_coefs;
|
|
std::string method;
|
|
|
|
bool init() {
|
|
fit_coefs.clear();
|
|
scores.clear();
|
|
return true;
|
|
}
|
|
|
|
nlohmann::json invert2json() {
|
|
nlohmann::json js1;
|
|
js1["orders"] = orders;
|
|
js1["fit_coefs"] = fit_coefs;
|
|
js1["scores"] = scores;
|
|
js1["pear_coefs"] = pear_coefs;
|
|
js1["method"] = method;
|
|
return js1;
|
|
}
|
|
};
|
|
/**
|
|
* @brief sample_stat统计数据 数据结构
|
|
*/
|
|
struct SampleStat {
|
|
double ci_left = 0;
|
|
double ci_right = 0;
|
|
double mean = 0;
|
|
double stddev = 0;
|
|
double skewness = 0;
|
|
double kurtosis = 0;
|
|
double variance = 0;
|
|
double range = 0;
|
|
double init_value = 0;
|
|
double min = 0;
|
|
double max = 0;
|
|
nlohmann::json invert2json() {
|
|
nlohmann::json js1;
|
|
js1["ci_left"] = limit_precision(ci_left);
|
|
js1["ci_right"] = limit_precision(ci_right);
|
|
js1["mean"] = limit_precision(mean, 3);
|
|
js1["min"] = limit_precision(min, 3);
|
|
js1["max"] = limit_precision(max, 3);
|
|
js1["stddev"] = stddev;
|
|
js1["kurtosis"] = kurtosis;
|
|
js1["variance"] = variance;
|
|
js1["skewness"] = skewness;
|
|
js1["range"] = limit_precision(range, 3);
|
|
js1["init_value"] = limit_precision(init_value, 3);
|
|
return js1;
|
|
}
|
|
double limit_precision(double data, int precision = 2) {
|
|
double factor = std::pow(10, precision);
|
|
return std::round(data * factor) / factor;
|
|
}
|
|
};
|
|
|
|
struct TaskReturnType {
|
|
bool is_valid = false;
|
|
double value = 0;
|
|
};
|