#include #include // #include /*测试用*/ #include "mix_cc/exception.h" #include #include #include #include #include #include #include namespace { // std::mutex local_mutext{}; // mutable std::shared_mutex local_mutext; // std::unordered_map map_data{}; // std::unordered_map map_rule_stat{}; } // namespace // 初始化静态成员变量单例 // 在类外定义并初始化静态成员变量(必须) int GlobaltemSharedMemory::instanceCount = 0; int GlobaltemSharedMemory::get_instanceCount() { return instanceCount; } GlobaltemSharedMemory::GlobaltemSharedMemory() { ++instanceCount; logger_ = std::make_unique("GlobaltemSharedMemory"); logger_->Debug() << "GlobaltemSharedMemory::GlobaltemSharedMemory()" << endl; } // 获取指定tag点数据 double GlobaltemSharedMemory::operator[](std::string tag_name) { // 获取数据前会对map加锁,防止出现map更新一半的情况,这样会出现新值旧值互相交替 // std::lock_guard guard(local_mutext); std::shared_lock lock(local_mutext); return map_data[tag_name]; } // 检查tag点是否存在 // bool find_tag(std::string tag_name) { // // std::lock_guard guard(local_mutext); // std::shared_lock lock(local_mutext); // return (map_data.find(tag_name) != map_data.end()); // } // 从共享内存缓存数据到该程序的map中 int GlobaltemSharedMemory::cache_data() { try { // 增加全局锁,防止map更新出错 // std::lock_guard guard(local_mutext); std::unique_lock 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 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(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:"<index_cur()<<",size:"<size()< guard(local_mutext); // // 为共享内存中的数据添加机组号前缀 // const auto prefix = string(CMemVar::Const()->UnitNo) + "_"; // for (int i = 2012; i <= 2012; i++) { // CMemTable 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()); // } // } // } 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 guard(local_mutext); // // 为共享内存中的数据添加机组号前缀 // const auto prefix = string(CMemVar::Const()->UnitNo) + "_"; // CMemTable 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 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; // }