eis/eqpalg/.do_not_use/otheralg/exp_cpc.cpp

228 lines
8.3 KiB
C++
Raw Permalink Normal View History

/******************************************************************************************************************
* Action instruction algorithm
*
* arg[0] data determination expression
*
*
*
* 1.0 2020-12-17 zoufuzhou
******************************************************************************************************************/
#include <T_PDO_RULE.h>
#include <base/BitTool.h>
#include <base/TCMTime.h>
#include <eqpalg/algs/exp_cpc.h>
#include <eqpalg/utility/build_alarm_info.h>
#include <map>
#include <memory>
#include <string>
const int CONST_CPC_WPLEN = 50 * 1000;
/**
* @brief Construct a new Exp Cpc:: Exp Cpc object
* @param name My Param doc
* @param rule_json My Param doc
* @param ruleId My Param doc
*/
ExpCpc::ExpCpc(const string name, const mix_cc::json& rule_json,
const string ruleId)
: Exp(name, rule_json, ruleId, 1) {
logger_.reset(new LOG("ExpCpc:" + rule_name_, AUTO_CATCH_PID));
// this->data_source_ = DataSource::MEMORY;
}
/**
* @brief CPC算法配置信息并初始化算法
* @return int
*/
int ExpCpc::init() {
int ret = 0;
Exp::init();
try {
if (exp_result_ == nullptr) {
exp_result_ = std::make_unique<mix_cc::matheval::Expression>(
exp_str_, &tags_value_);
logger_->Debug() << "offset" << exp_str_ << ":" << exp_result_->evaluate()
<< endl;
} else {
logger_->Info() << "指针已经初始化完成" << exp_str_ << ":"
<< exp_result_->evaluate() << endl;
}
} catch (const std::exception& e) {
logger_->Error() << exp_result_ << "计算错误:" << e.what() << endl;
}
try {
m_zone = 1;
m_cpc.offset[0] = 0;
m_cpc.offset[1] = 0;
m_zone = rule_json_.at("action_condition").at("zone").at(1).get<int64>();
logger_->Debug() << "zone:" << m_zone << endl;
mem_trk_ = std::make_unique<CMemTrk>();
m_entId = (*mem_trk_)(m_zone)->entId;
m_startpos = -1;
} catch (const std::exception& e) {
this->logger_->Debug() << this->rule_name_ << "init()ERROR" << std::endl;
std::throw_with_nested(
mix_cc::Exception(-1, "load error", BOOST_CURRENT_LOCATION));
}
return ret;
}
ExpCpc::~ExpCpc() {}
AlarmInfo ExpCpc::mon_proc() {
// this->logger_->Debug() << this->rule_name_ << "mon_proc() test" <<
// std::endl;
AlarmInfo now_alarm;
double act_triggered_ = 0.0;
try {
act_triggered_ = exp_act_->evaluate();
// 得到对应区域的钢卷号
string entId = (*mem_trk_)(m_zone)->entId;
if (entId != m_entId) {
max_deviation_["tag1"] = 0;
max_deviation_["tag2"] = 0;
}
if (static_cast<bool>(act_triggered_)) {
//报警时间 依赖query_time_range_ 设置
this->query_time_range_.set_left(this->query_time_range_.get_right() -
this->delay_time_);
this->print_exp_vars(exp_str_);
//最大跑偏量
max_deviation_["tag1"] =
(abs(mm_vars["tag1"]) > abs(max_deviation_["tag1"])
? mm_vars["tag1"]
: max_deviation_["tag1"]);
max_deviation_["tag2"] =
(abs(mm_vars["tag2"]) > abs(max_deviation_["tag2"])
? mm_vars["tag2"]
: max_deviation_["tag2"]);
// 得到对应区域的钢卷号
// string entId = (*mem_trk_)(m_zone)->entId;
if (m_startpos >= 0 && entId != m_entId) {
// 得到钢卷的长度,从而推出钢卷的位置信息
m_endpos = (*mem_trk_).getHistory(m_zone, CS_TRK::BEFORE)->wplen;
if (m_endpos < 1000) {
logger_->Warn() << " end pos is zero " << endl;
m_endpos = 1500;
}
auto msg = rule_name_ + " 钢卷:" + m_entId + "位置m[" +
std::to_string(m_startpos / 1000) + "," +
std::to_string(m_endpos / 1000) + "T]," + error_str_ +
",最大跑偏量:" + std::to_string(max_deviation_["tag1"]) +
"," + std::to_string(max_deviation_["tag2"]) + ".";
logger_->Debug() << msg << endl;
now_alarm =
utility::build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_,
"EXP_MAT", msg, query_time_range_);
m_startpos = -1;
// max_deviation_["tag1"] = 0;
// max_deviation_["tag2"] = 0;
} else if (m_startpos < 0) {
m_startpos = (*mem_trk_)(m_zone)->wplen;
m_entId = (*mem_trk_)(m_zone)->entId;
auto msg = rule_name_ + "start 钢卷:" + m_entId +
"位置m:" + std::to_string(m_startpos / 1000) + error_str_ +
",最大跑偏量:" + std::to_string(max_deviation_["tag1"]) +
"," + std::to_string(max_deviation_["tag2"]) + ".";
logger_->Debug() << msg << endl;
}
} else if (m_startpos >= 0) {
int m_endpos = (*mem_trk_)(m_zone)->wplen;
auto msg = rule_name_ + " 钢卷:" + m_entId + "位置m[" +
std::to_string(m_startpos / 1000) + "," +
std::to_string(m_endpos / 1000) + "]," + error_str_ +
",最大跑偏量:" + std::to_string(max_deviation_["tag1"]) + "," +
std::to_string(max_deviation_["tag2"]) + ".";
logger_->Debug() << msg << endl;
now_alarm =
utility::build_alarm_info(MsgLevel::WARN, rule_id_, rule_name_,
"EXP_MAT", msg, query_time_range_);
m_startpos = -1;
} else {
m_startpos = -1;
m_entId = (*mem_trk_)(m_zone)->entId;
}
// if(entId != m_entId){
// max_deviation_["tag1"] = 0;
// max_deviation_["tag2"] = 0;
// }
this->proc_cpc(act_triggered_);
} catch (const std::exception& e) {
std::throw_with_nested(
mix_cc::Exception(-1, "calc_once error", BOOST_CURRENT_LOCATION));
}
return now_alarm;
}
int ExpCpc::proc_cpc(const double& act_triggered_) {
int ret = -1;
m_cpc.endId = string(m_entId);
int totallen = (*mem_trk_)(1, m_entId)->wplen;
int wplen = (*mem_trk_)(m_zone)->wplen;
if (m_cpc.endId != m_cpc.endId_saved) {
if (wplen > CONST_CPC_WPLEN && (totallen - wplen) > CONST_CPC_WPLEN) {
for (unsigned int i = 0; i < m_tags.size() && i < 2; i++) {
m_cpc.offset[i] =
abs(m_cpc.offset[i]) > abs(mm_vars["tag" + std::to_string(i + 1)])
? m_cpc.offset[i]
: mm_vars["tag" + std::to_string(i + 1)];
}
} else if (wplen > CONST_CPC_WPLEN && totallen - wplen < CONST_CPC_WPLEN) {
logger_->Debug() << " entId:" << m_cpc.endId
<< " saved entId:" << m_cpc.endId_saved << endl;
logger_->Debug() << "cpc offset[0]:" << m_cpc.offset[0]
<< " offset[1]:" << m_cpc.offset[1] << endl;
logger_->Debug() << "zone:" << m_zone << " totallen:" << totallen
<< " wplen:" << wplen << endl;
string main_speed = std::string(CMemVar::Const()->UnitNo) + "_" +
std::string(CMemVar::Const()->eis_tagname_main_speed);
float speed =
SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[main_speed];
logger_->Debug() << "pdo start" << std::endl;
T_PDO_RULE pdo;
pdo.initInsertKeys(rule_id_.c_str(), const_cast<char*>(m_entId.c_str()));
pdo.set_stime(const_cast<char*>(mix_cc::mix_time_t(now_time_)
.to_formatted_time("%Y%m%d%H%M%S")
.c_str()));
pdo.set_entId(m_entId.c_str());
pdo.set_speed(speed);
pdo.set_start(m_startpos);
pdo.set_end(m_endpos);
pdo.set_nspare1(m_cpc.offset[0]);
pdo.set_nspare2(m_cpc.offset[1]);
// if ((bool)act_triggered_) {
// pdo.set_nspare3(1.0);
// } else {
// pdo.set_nspare3(0.0);
// }
tags_value_["tag1"] = m_cpc.offset[0];
tags_value_["tag2"] = m_cpc.offset[1];
if (this->exp_result_->evaluate()) {
pdo.set_nspare3(1.0);
} else {
pdo.set_nspare3(0.0);
}
if (pdo.storeDB() != NULL) {
pdo.dbRollback();
ret = -1;
logger_->Debug() << "save pdo failure" << std::endl;
} else {
pdo.dbCommit();
m_cpc.offset[0] = 0;
m_cpc.offset[1] = 0;
m_cpc.endId_saved = m_cpc.endId;
logger_->Debug() << "save pdo success" << std::endl;
}
}
}
return ret;
}