eis/eqpalg/algs/fault_code.cc

131 lines
4.1 KiB
C++
Raw Normal View History

#include <eqpalg/algs/fault_code.h>
#include <eqpalg/gb_item_memory.h>
#include <eqpalg/table_struct/t_lov_fcode.h>
#include <eqpalg/utility/build_alarm_info.h>
#include <glob/SingletonTemplate.h>
#include <mix_cc/sql.h>
#include <mix_cc/sql/database/db2_t.h>
FaultCode::FaultCode(const string name, const mix_cc::json& rule_json,
const string ruleId, size_t code_type)
: AlgBase(name, rule_json, ruleId), code_type_(code_type) {
logger_.reset(new LOG("FaultCode:" + rule_name_, AUTO_CATCH_PID));
}
FaultCode::~FaultCode() {
}
int FaultCode::init() {
int ret = 0;
try {
ret += AlgBase::init();
ret += this->select_t_lov_fcode();
if (ret != 0) {
logger_->Debug() << "ret:" << ret << endl;
return ret;
}
is_valid_ = true;
} catch (const std::exception& e) {
logger_->Error() << "FaultCode::init():" << e.what()
<< ",location:" << BOOST_CURRENT_LOCATION << endl;
return -1;
}
return ret;
}
AlarmInfo FaultCode::exec_mon() {
if (!is_valid_) {
logger_->Debug() << "未查询到故障代码表T_LOV_FCODE" << endl;
return AlarmInfo{};
}
this->refresh_now_time();
/*故障代码解析*/
int fault_code_now =
int(SingletonTemplate<GlobaltemSharedMemory>::GetInstance()[m_tags[0]]);
this->rule_stat_.current_value = fault_code_now;
query_time_range_.set_right(now_time_);
query_time_range_.set_left(now_time_ - this->delay_time_);
if (fault_code_now != 0) {
std::string msg = "";
msg += "故障代码:" + std::to_string(fault_code_now);
/*整体解析*/
if (this->code_type_ == 0) {
if (this->map2fcode_.find(fault_code_now) != this->map2fcode_.end()) {
if (this->map2fcode_[fault_code_now].is_usable) {
msg += ",故障名称:";
msg += this->map2fcode_[fault_code_now].name + "," +
map2fcode_[fault_code_now].content;
return utility::build_alarm_info(MsgLevel::ERROR, rule_id_,
rule_name_, "FaultCode", msg,
query_time_range_);
} else {
msg += "无需报警提醒";
}
} else {
msg += "无,不报警提示!";
}
}
/**按位解析*/
else {
bool is_alarm = false;
for (int i = 0; i < 16; i++) {
if (this->gitbit(fault_code_now, i)) {
if (map2fcode_.find(i) != map2fcode_.end()) {
if (map2fcode_[i].is_usable) {
msg += ",bit:" + std::to_string(i) + ",故障名称:";
msg += map2fcode_[i].content;
}
is_alarm = is_alarm || map2fcode_[i].is_usable;
}
}
}
if (is_alarm) {
return utility::build_alarm_info(MsgLevel::ERROR, rule_id_, rule_name_,
"FaultCode", msg, query_time_range_);
} else {
msg += "无需报警提醒";
}
}
logger_->Debug() << msg << endl;
}
return AlarmInfo{};
}
std::vector<AlarmInfo> FaultCode::exec_task(mix_cc::time_range_t time_range) {
std::vector<AlarmInfo> result;
for (auto now_time = time_range.get_left();
now_time <= time_range.get_right(); now_time += delay_time_) {
this->now_time_ = now_time;
auto rr1 = this->exec_mon();
if (rr1.alarmed) {
result.push_back(rr1);
}
}
return result;
}
int FaultCode::select_t_lov_fcode() {
T_LOV_FCODE tlf;
auto sql_statement =
select(tlf.code(), tlf.content(), tlf.name(), tlf.usable())
.from(tlf)
.where(tlf.type() == this->code_type_);
auto sample_list_maybe = exec<db2_t, T_LOV_FCODE>(sql_statement);
if (sample_list_maybe.is_just()) {
auto sample_list = sample_list_maybe.unsafe_get_just();
if (sample_list.empty()) {
logger_->Info() << "无有故障代码表" << std::endl;
return -1;
} else {
auto size = sample_list.size();
for (int i = 0; i < size; i++) {
this->map2fcode_[sample_list[i].code] = {
sample_list[i].usable, sample_list[i].name, sample_list[i].content};
}
}
return 0;
} else {
logger_->Error() << "请检查数据库连接" << std::endl;
return -1;
}
}