160 lines
5.2 KiB
C++
160 lines
5.2 KiB
C++
#include <eqpalg/algs/AlgExpRunStatic.h>
|
||
|
||
extern std::map<std::string, ITEM> glob_items;
|
||
|
||
int AlgExpRunStatic::StorageToDB(int action) {
|
||
mix_cc::mix_time_t now_time(time(0));
|
||
LOG d("AlgExpSample::calculate|" + rule_name_, AUTO_CATCH_PID);
|
||
// if begin state
|
||
if (action == 1) {
|
||
// 0 eqp stop
|
||
d.Debug() << "Inserting" << std::endl;
|
||
db_com_.insert_value(
|
||
"T_EQP_RUN_HISTORY",
|
||
{{"RuleId", (rule_id_)}, {"TimeStart", now_time.to_db2_str()}});
|
||
} else if (action == 0) {
|
||
// 1 eqp start
|
||
d.Debug() << "Updating" << std::endl;
|
||
db_com_.SetValue("T_EQP_RUN_HISTORY", {{"TimeEnd", now_time.to_db2_str()}},
|
||
{{"RuleId", (rule_id_), "="},
|
||
{"TimeStart", (this->prev_start_time), "="}});
|
||
}
|
||
|
||
// end
|
||
}
|
||
|
||
int AlgExpRunStatic::calculate(string &outjson) {
|
||
LOG d("AlgExpSample::calculate|" + rule_name_, AUTO_CATCH_PID);
|
||
int ret = 0;
|
||
outjson = "";
|
||
|
||
try {
|
||
if (m_modest == DATA_STAT::ACTUAL) {
|
||
for (unsigned int i = 0; i < m_tags.size(); i++) {
|
||
mm_vars["p" + std::to_string(i + 1)] =
|
||
mm_vars["tag" + std::to_string(i + 1)];
|
||
if (data_source_ == DataSource::MEMORY) {
|
||
mm_vars["tag" + std::to_string(i + 1)] =
|
||
glob_items[m_tags[i]].value;
|
||
} else {
|
||
if (ihd_tools_->QuerySnapshot(m_tags[i], &mp_hdRec[i]) == NULL) {
|
||
mm_vars["tag" + std::to_string(i + 1)] =
|
||
mp_hdRec[i].NumberValue();
|
||
}
|
||
}
|
||
}
|
||
ret = this->mon_proc(outjson, mp_hdRec);
|
||
} else {
|
||
ret = this->QueryDB3Record();
|
||
if (ret != NULL) {
|
||
return 0;
|
||
}
|
||
|
||
for (int i = 0; i < tag_count_; i++) {
|
||
for (int j = 0; j < m_tags.size(); j++) {
|
||
mm_vars["p" + std::to_string(j + 1)] =
|
||
mm_vars["tag" + std::to_string(j + 1)];
|
||
mm_vars["tag" + std::to_string(j + 1)] =
|
||
records_queried_[j][i].NumberValue();
|
||
}
|
||
ret = this->mon_proc(outjson, &records_queried_[0][i]);
|
||
if (ret == 0 && outjson != "") {
|
||
break;
|
||
}
|
||
}
|
||
if (this->m_btime == 0) {
|
||
this->SetHDTime(&query_time_region_.right, this->mstime());
|
||
}
|
||
this->free_ihd_mem();
|
||
}
|
||
|
||
} catch (const std::exception &e) {
|
||
d.Error() << mix_cc::get_nested_exception(e) << std::endl;
|
||
ret = -1;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
int AlgExpRunStatic::mon_proc(string &outjson, HD3Record *hdRec) {
|
||
LOG d("AlgExpSample::mon_proc|" + rule_name_, AUTO_CATCH_PID);
|
||
int ret = 0;
|
||
outjson = "";
|
||
bool current_running_state = false;
|
||
long long timediff = 0;
|
||
try {
|
||
current_running_state = (bool)exp_act_->evaluate();
|
||
this->print_exp_vars(m_expstr);
|
||
if (current_running_state == 1 && prev_running_state == 0) {
|
||
d.Debug() << "case run " << endl;
|
||
this->StorageToDB(1);
|
||
this->prev_running_state = 1;
|
||
this->prev_start_time = mix_cc::mix_time_t(time(0)).to_formatted_time();
|
||
} else if (current_running_state == 0 && prev_running_state == 1) {
|
||
d.Debug() << "case stop" << endl;
|
||
this->StorageToDB(0);
|
||
this->prev_running_state = 0;
|
||
}
|
||
|
||
} catch (const std::exception &e) {
|
||
d.Error() << mix_cc::get_nested_exception(e) << std::endl;
|
||
ret = -1;
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
AlgExpRunStatic::AlgExpRunStatic(const string &name,
|
||
const Json::Value &rulejson,
|
||
const string &ruleId)
|
||
: AlgBase(name, rulejson, ruleId),
|
||
AlgExp(name, rulejson, ruleId) {
|
||
LOG d("AlgExpRunStatic::AlgExpRunStatic", AUTO_CATCH_PID);
|
||
|
||
data_source_ = m_json_param["datasource"]["value"][1].asInt();
|
||
d.Debug() << "data source[0:iHyerDB,1:memory]:" << data_source_ << endl;
|
||
m_modest = 1;
|
||
keep_mode_ = 1; // m_json_param["action_condition"]["action_hold"][1].asInt();
|
||
|
||
d.Debug() << "keep:" << keep_mode_ << " mode stat_tools:" << m_modest << endl;
|
||
|
||
this->init();
|
||
|
||
d.Debug() << "Init: "
|
||
<< string(m_json_param["action_condition"]["value"][int(1)].asString())
|
||
<< std::endl;
|
||
d.Debug() << "reloading"
|
||
<< "wait" << m_modest << endl;
|
||
|
||
m_expstr = string(m_json_param["action_condition"]["value"][1].asString());
|
||
d.Debug() << "Init"
|
||
<< string(m_json_param["action_condition"]["value"][int(1)].asString())
|
||
<< std::endl;
|
||
this->print_exp_vars(m_expstr);
|
||
|
||
exp_act_ = new MathExpression(m_expstr.c_str(), mm_vars);
|
||
d.Debug() << "act_triggered_:" << m_expstr << "=" << exp_act_->evaluate() << endl;
|
||
|
||
mix_cc::mix_time_t now_time(time(0));
|
||
auto data =
|
||
db_com_.query_value("T_EQP_RUN_HISTORY", {},
|
||
{{"RULEID", (ruleId), "="},
|
||
{"TIMESTART", now_time.to_formatted_time(), "<="},
|
||
{"TIMEEND", "NULL", "IS"}});
|
||
d.Debug() << data << std::endl;
|
||
if (data.empty()) {
|
||
this->prev_running_state = 0;
|
||
d.Debug() << "prev is not running" << std::endl;
|
||
} else {
|
||
d.Debug() << "prev is running" << std::endl;
|
||
this->prev_start_time = data[0].at("TIMESTART").as_string().c_str();
|
||
this->prev_running_state = 1;
|
||
d.Debug() << "prev is running, start time: " << prev_start_time
|
||
<< std::endl;
|
||
}
|
||
if (!m_modest) {
|
||
this->malloc_ihd_mem();
|
||
}
|
||
d.Debug() << "Init Complete" << std::endl;
|
||
}
|
||
|
||
AlgExpRunStatic::~AlgExpRunStatic() {}
|