2026-05-09 11:23:45 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @file eqp_stat.h
|
2026-05-12 15:46:01 +08:00
|
|
|
|
* @brief 处理与页面交互的展示数据 + mon↔cron 冷数据交换
|
|
|
|
|
|
*
|
|
|
|
|
|
* 展示数据:本地缓存 → get_stat_json() 拼 JSON(无共享内存锁)
|
|
|
|
|
|
* 冷数据:共享内存 map,boost::interprocess::interprocess_mutex 同步
|
|
|
|
|
|
*
|
2026-05-09 11:23:45 +08:00
|
|
|
|
* @author your name (you@domain.com)
|
2026-05-12 15:46:01 +08:00
|
|
|
|
* @version 0.2
|
2026-05-09 11:23:45 +08:00
|
|
|
|
* @date 2023-12-21
|
|
|
|
|
|
*
|
|
|
|
|
|
* Copyright: Baosight Co. Ltd.
|
|
|
|
|
|
* DO NOT COPY/USE WITHOUT PERMISSION
|
|
|
|
|
|
*/
|
|
|
|
|
|
#include <log4cplus/LOG.h>
|
|
|
|
|
|
#include <shm/RuleStatShm.h>
|
2026-05-12 15:46:01 +08:00
|
|
|
|
#include <map>
|
2026-05-09 11:23:45 +08:00
|
|
|
|
#include <memory>
|
2026-05-12 15:46:01 +08:00
|
|
|
|
#include <mutex>
|
|
|
|
|
|
#include <string>
|
2026-05-09 11:23:45 +08:00
|
|
|
|
#include <vector>
|
2026-05-12 15:46:01 +08:00
|
|
|
|
|
|
|
|
|
|
/// 展示数据条目(本地缓存用)
|
|
|
|
|
|
struct DisplayEntry {
|
|
|
|
|
|
double alarm_value = 0;
|
|
|
|
|
|
double current_value = 0;
|
|
|
|
|
|
double limit_up = 0;
|
|
|
|
|
|
double limit_down = 0;
|
|
|
|
|
|
std::string unit;
|
|
|
|
|
|
std::vector<std::string> items;
|
|
|
|
|
|
|
|
|
|
|
|
double limit_precision(double data, int precision = 2) const {
|
|
|
|
|
|
double factor = std::pow(10, precision);
|
|
|
|
|
|
return std::round(data * factor) / factor;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mix_cc::json to_json() const {
|
|
|
|
|
|
mix_cc::json js;
|
|
|
|
|
|
js["alarm_value"] = limit_precision(alarm_value);
|
|
|
|
|
|
js["current_value"] = limit_precision(current_value);
|
|
|
|
|
|
js["limit_up"] = limit_precision(limit_up);
|
|
|
|
|
|
js["limit_down"] = limit_precision(limit_down);
|
|
|
|
|
|
js["unit"] = unit;
|
|
|
|
|
|
js["items"] = items;
|
|
|
|
|
|
return js;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class DisplayCache {
|
|
|
|
|
|
public:
|
|
|
|
|
|
void update(const std::string &ruleid, const RuleStatShm::RuleStatLocal &stat);
|
|
|
|
|
|
void update_static(const std::string &ruleid, const RuleStatShm::RuleStatCold &cold);
|
|
|
|
|
|
void remove(const std::string &ruleid);
|
|
|
|
|
|
std::string get_json();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
std::map<std::string, DisplayEntry> cache_;
|
|
|
|
|
|
std::map<std::string, std::string> static_fields_;
|
|
|
|
|
|
std::mutex mtx_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-05-09 11:23:45 +08:00
|
|
|
|
class EqpStat {
|
2026-05-12 15:46:01 +08:00
|
|
|
|
public:
|
2026-05-09 11:23:45 +08:00
|
|
|
|
EqpStat();
|
|
|
|
|
|
~EqpStat();
|
|
|
|
|
|
|
2026-05-12 15:46:01 +08:00
|
|
|
|
public:
|
2026-05-09 11:23:45 +08:00
|
|
|
|
void init();
|
|
|
|
|
|
|
2026-05-12 15:46:01 +08:00
|
|
|
|
/// 写展示数据到本地缓存(mon 高频调用,线程锁,无共享内存操作)
|
|
|
|
|
|
bool update_display(const std::string &ruleid,
|
|
|
|
|
|
const RuleStatShm::RuleStatLocal &rule_stat);
|
2026-05-09 11:23:45 +08:00
|
|
|
|
|
2026-05-12 15:46:01 +08:00
|
|
|
|
/// 写冷数据到共享内存(mon 高频调用,进程间锁)
|
|
|
|
|
|
bool update_cold(const std::string &ruleid,
|
|
|
|
|
|
const RuleStatShm::RuleStatLocal &rule_stat);
|
|
|
|
|
|
|
|
|
|
|
|
/// 写静态数据到共享内存 + 更新本地展示缓存(cron/ExpTimes 调用)
|
2026-05-09 11:23:45 +08:00
|
|
|
|
bool update_static(std::string ruleid, bool is_times);
|
|
|
|
|
|
bool update_static();
|
2026-05-12 15:46:01 +08:00
|
|
|
|
|
|
|
|
|
|
/// mon 攒样本到共享内存
|
|
|
|
|
|
bool add_stat_values(std::string ruleid, const double &value);
|
|
|
|
|
|
|
|
|
|
|
|
/// cron 从共享内存取样本
|
|
|
|
|
|
bool get_stat_values(std::string ruleid, RuleStatShm::RuleStatLocal &local);
|
|
|
|
|
|
|
2026-05-09 11:23:45 +08:00
|
|
|
|
bool delete_stat(std::string ruleid);
|
|
|
|
|
|
std::string get_stat_json();
|
|
|
|
|
|
std::vector<std::string> stat_find_no_ruleid();
|
|
|
|
|
|
int get_stat_size();
|
|
|
|
|
|
void get_cfg_rules();
|
2026-05-12 15:46:01 +08:00
|
|
|
|
std::string get_ruleid_json();
|
2026-05-09 11:23:45 +08:00
|
|
|
|
|
2026-05-12 15:46:01 +08:00
|
|
|
|
private:
|
2026-05-09 11:23:45 +08:00
|
|
|
|
std::unique_ptr<LOG> logger_;
|
2026-05-12 15:46:01 +08:00
|
|
|
|
DisplayCache display_cache_;
|
|
|
|
|
|
std::vector<std::string> cfg_rules_;
|
2026-05-09 13:30:09 +08:00
|
|
|
|
bool cfg_flag = false;
|
2026-05-12 15:46:01 +08:00
|
|
|
|
std::chrono::system_clock::time_point last_update_static_time_;
|
|
|
|
|
|
|
|
|
|
|
|
double select_running_by_ruleid(std::string ruleid);
|
|
|
|
|
|
unsigned long select_times_by_ruleid(std::string ruleid);
|
|
|
|
|
|
int select_alarm_by_ruleid(std::string ruleid);
|
|
|
|
|
|
std::string select_dev_coder_by_ruleid(std::string ruleid);
|
|
|
|
|
|
std::string select_latest_alarm_by_ruleid(std::string ruleid);
|
|
|
|
|
|
};
|