eis/eqpalg/gb_item_memory.ccBK2

198 lines
6.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <eqpalg/gb_item_memory.h>
#include <eqpalg/table_struct/t_rule_cfg.h>
// #include <eqpalg/utility/RandT.h> /*测试用*/
#include "mix_cc/exception.h"
#include <chrono>
#include <mix_cc/sql.h>
#include <mix_cc/sql/database/db2_t.h>
#include <mutex>
#include <thread>
#include <tuple>
#include <vector>
namespace {
// std::mutex local_mutext{};
// mutable std::shared_mutex local_mutext;
// std::unordered_map<std::string, double> map_data{};
// std::unordered_map<std::string, RuleStat> map_rule_stat{};
} // namespace
// 初始化静态成员变量单例
// 在类外定义并初始化静态成员变量(必须)
int GlobaltemSharedMemory::instanceCount = 0;
int GlobaltemSharedMemory::get_instanceCount() { return instanceCount; }
GlobaltemSharedMemory::GlobaltemSharedMemory() {
++instanceCount;
logger_ = std::make_unique<LOG>("GlobaltemSharedMemory");
logger_->Debug() << "GlobaltemSharedMemory::GlobaltemSharedMemory()" << endl;
}
// 获取指定tag点数据
double GlobaltemSharedMemory::operator[](std::string tag_name) {
// 获取数据前会对map加锁防止出现map更新一半的情况这样会出现新值旧值互相交替
// std::lock_guard<std::mutex> guard(local_mutext);
std::shared_lock<std::shared_mutex> lock(local_mutext);
return map_data[tag_name];
}
// 检查tag点是否存在
// bool find_tag(std::string tag_name) {
// // std::lock_guard<std::mutex> guard(local_mutext);
// std::shared_lock<std::shared_mutex> lock(local_mutext);
// return (map_data.find(tag_name) != map_data.end());
// }
// 从共享内存缓存数据到该程序的map中
int GlobaltemSharedMemory::cache_data() {
try {
// 增加全局锁防止map更新出错
// std::lock_guard<std::mutex> guard(local_mutext);
std::unique_lock<std::shared_mutex> lock(local_mutext);
// 为共享内存中的数据添加机组号前缀
const auto prefix = string(CMemVar::Const()->UnitNo) + "_";
for (int i = CMemVar::Const()->event_eis_start;
i <= CMemVar::Const()->event_eis_end; i++) {
if (true) {
// if ((i != 2012)) {
// CMemTable<PLC_DATA> tele(std::to_string(i));
// binary_tele.ReBuild(i, *tele());
// logger_->Debug() << "eventNo:" << i << std::endl;
if (m_mapfix.find(i) == m_mapfix.end()) {
m_mapfix.insert(
make_pair(i, new CMemFix<PLC_DATA>(std::to_string(i))));
}
// if (i == 2001) {
// logger_->Debug() << "m_getnext:" << m_mapfix[i]->get_next() <<
// endl;
// }
// PLC_DATA* pdata = m_mapfix[i]->getNext();
PLC_DATA *pdata = m_mapfix[i]->getCur();
// logger_->Debug()<<"CurIndex:"<<m_mapfix[i]->index_cur()<<",size:"<<m_mapfix[i]->size()<<endl;
if (pdata == nullptr) {
return -1 * i;
}
binary_tele.ReBuild(i, (char *)(pdata));
// binary_tele.Print();
const auto size = binary_tele.size();
for (int j = 0; j < size; j++) {
float value = binary_tele[j];
// std::string item_name = binary_tele[j].item;
std::string name_without_plant_code = binary_tele[j].item;
// 添加前缀
auto item_name = prefix + name_without_plant_code;
map_data[item_name] = value;
//无前缀
// map_data[name_without_plant_code] = value;
}
}
}
} catch (const std::exception &e) {
std::throw_with_nested(
mix_cc::Exception(-1, "shared mem load error", BOOST_CURRENT_LOCATION));
}
return 0;
}
/**
* @brief 读取memcache至共享内存 -----弃用
* @param Jkey My Param doc
* @return int
*/
// int GlobaltemSharedMemory::memcache_data(std::string Jkey) {
// // LOG log("memcache_data() ");
// try {
// // 增加全局锁防止map更新出错
// auto CS_itemtag = m_memclient_.Get(Jkey.c_str()); ///<指定key的数据
// auto js1 = mix_cc::json::parse(CS_itemtag);
// std::lock_guard<std::mutex> guard(local_mutext);
// // 为共享内存中的数据添加机组号前缀
// const auto prefix = string(CMemVar::Const()->UnitNo) + "_";
// for (int i = 2012; i <= 2012; i++) {
// CMemTable<PLC_DATA> tele(std::to_string(i), 1);
// binary_tele.ReBuild(i, *tele());
// const auto size = binary_tele.size();
// for (int j = 0; j < size; j++) {
// // 添加前缀
// std::string name_without_plant_code = binary_tele[j].item;
// auto item_name = prefix + name_without_plant_code;
// map_data[item_name] =
// std::stod(js1[name_without_plant_code].get<string>());
// }
// }
// } catch (const std::exception& e) {
// std::throw_with_nested(
// mix_cc::Exception(-1, "memcache_data() error",
// BOOST_CURRENT_LOCATION));
// }
// return 0;
// }
// int GlobaltemSharedMemory::memcache_data(int eventno) {
// try {
// std::lock_guard<std::mutex> guard(local_mutext);
// // 为共享内存中的数据添加机组号前缀
// const auto prefix = string(CMemVar::Const()->UnitNo) + "_";
// CMemTable<PLC_DATA> tele(std::to_string(eventno), 1);
// binary_tele.ReBuild(eventno, *tele());
// const auto size = binary_tele.size();
// for (int j = 0; j < size; j++) {
// // 添加前缀
// std::string name_without_plant_code = binary_tele[j].item;
// auto item_name = prefix + name_without_plant_code;
// // log.Info() << "item_name:" << item_name << endl;
// map_data[item_name] = RandT::GuassRand(5, 20);
// }
// } catch (const std::exception& e) {
// std::throw_with_nested(
// mix_cc::Exception(-1, "memcache_data() error",
// BOOST_CURRENT_LOCATION));
// }
// return 0;
// }
std::string GlobaltemSharedMemory::get_data_json() {
// std::shared_lock<std::shared_mutex> lock(local_mutext);
mix_cc::json js1;
try {
if (!map_data.empty()) {
js1 = map_data;
return js1.dump();
} else {
return "empty";
}
} catch (std::exception &e) {
return "ERROR";
}
}
// std::string GlobaltemSharedMemory::get_rule_stat_json() {
// mix_cc::json js1;
// try {
// mix_cc::json js1;
// if (!map_rule_stat.empty()) {
// for (auto item : map_rule_stat) {
// js1[item.first] = item.second.invert2json();
// }
// } else {
// return "empty";
// }
// return js1.dump();
// } catch (std::exception& e) {
// return "ERROR";
// }
// }
// bool GlobaltemSharedMemory::insert_stat_data(std::string ruleid,
// RuleStat& rule_stat) {
// map_rule_stat[ruleid] = rule_stat;
// return true;
// }