eis/eqpm/eqp_status.cc

371 lines
14 KiB
C++
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 <eqpm/eqp_status.h>
#include <eqpm/jsonReader.hpp>
#include <eqpm/table_struct/FV_DSC_PDI_DATA.h>
#include <eqpm/table_struct/FV_RESULT_JOIN_EQPID.h>
#include <mix_cc/sql.h>
#include <mix_cc/sql/database/db2_t.h>
namespace {
typedef char CoilID[32];
double limit_precision(double data, int precision) {
double factor = std::pow(10, precision);
return std::round(data * factor) / factor;
}
float limit_precision(float data, int precision) {
float factor = std::pow(10, precision);
return std::round(data * factor) / factor;
}
string double2str(double data, int precision) {
std::stringstream ss;
ss << std::fixed << std::setprecision(precision) << data;
return ss.str();
}
string float2str(float data, int precision) {
std::stringstream ss;
ss << std::fixed << std::setprecision(precision) << data;
return ss.str();
}
} // namespace
EqpStatus::EqpStatus() {
logger_ = std::make_unique<LOG>("EqpStatus");
init();
p_btel = new BinaryTele(1002, "T_LOV_TELEITEM");
update_alarm_info();
update_coil_info();
}
EqpStatus::~EqpStatus() {}
void EqpStatus::init() {
/*数据模板*/
string file_path = "/users/dsc/code/eqpm/json/eqp_status.json";
if (!JsonHelper::jsonReader(file_path, eqp_status_json_)) {
logger_->Error() << "文件读取失败" << endl;
}
string config_file_path = "/users/dsc/code/eqpm/json/eqp_status_config.json";
if (!JsonHelper::jsonReader(config_file_path, eqp_status_config_json_)) {
logger_->Error() << "文件读取失败" << endl;
}
}
string EqpStatus::get_eqp_status() { return eqp_status_json_.dump(); }
void EqpStatus::dispose(int event_no, const ::Ice::ByteSeq &seq,
::Ice::Int length) {
p_btel->ReBuild(event_no, (char *)&seq[0]);
int p_btel_size = p_btel->size();
// p_btel->Print();
// logger_->Debug() << eqp_status_config_json_.dump() << std::endl;
/*------status_data------*/
mix_cc::json &status_data = eqp_status_json_["status_data"];
mix_cc::json &status_data_conf = eqp_status_config_json_["status_data"];
for (auto & [ key, value ] : status_data_conf.items()) {
float now_value = p_btel->operator[]((int)(value.get<int>() - 1));
status_data[key] = float2str(
(int)now_value * p_btel->operator[]((int)(value.get<int>() - 1)).factor,
1);
if (tolower(p_btel->operator[]((int)(value.get<int>() - 1)).type[0]) ==
'b') {
bool bv = p_btel->operator[]((int)(value.get<int>() - 1));
status_data[key] = std::to_string(bv);
}
}
/*-----tension-----*/
if (eqp_status_json_.contains("tension") &&
eqp_status_json_["tension"].is_array() &&
eqp_status_config_json_.contains("tension") &&
eqp_status_config_json_["tension"].is_array()) {
mix_cc::json &tension_data = eqp_status_json_["tension"];
mix_cc::json &tension_data_conf = eqp_status_config_json_["tension"];
int td_size = tension_data.size();
int tdc_size = tension_data_conf.size();
if (td_size == tdc_size && td_size == 2) {
for (int i = 0; i < td_size; i++) {
mix_cc::json &tdi = tension_data[i];
mix_cc::json &tdci = tension_data_conf[i];
int tdi_size = tdi.size();
int tdci_size = tdci.size();
if (tdi_size == tdci_size) {
for (int j = 0; j < tdi_size; j++) {
float now_value = p_btel->operator[]((int)(tdci[j].get<int>() - 1));
tdi[j] = float2str(
now_value *
p_btel->operator[]((int)(tdci[j].get<int>() - 1)).factor,
2);
// logger_->Debug() << "i:" << i << ",j:" << j
// << ",value:" << now_value << std::endl;
}
} else {
logger_->Error() << "tdi_size == tdci ERROR"
<< ",tdi_szie:" << tdi_size
<< ",tdci_size:" << tdci_size << std::endl;
}
}
} else {
logger_->Error() << "tdi_size == tdci ERROR"
<< ",td_size:" << td_size << ",tdc_size:" << tdc_size
<< std::endl;
}
}
/*-----cpc----*/
mix_cc::json &cpc_data = eqp_status_json_["cpc"];
mix_cc::json &cpc_data_conf = eqp_status_config_json_["cpc"];
int cpc_size = cpc_data.size();
int cpcc_size = cpc_data_conf.size();
if (cpc_size == cpcc_size && cpcc_size == 3) {
for (int i = 0; i < cpc_size; i++) {
mix_cc::json &cpci = cpc_data[i];
mix_cc::json &cpcci = cpc_data_conf[i];
int cpci_size = cpci.size();
int cpcci_size = cpcci.size();
if (cpci_size == cpcci_size && cpci_size == 10) {
for (int j = 0; j < cpci_size; j++) {
int indexj_cpc = (int)(cpcci[j].get<int>() - 1);
if (p_btel->operator[](indexj_cpc).length == 2) {
float now_value = p_btel->operator[](indexj_cpc);
// if (indexj_cpc == 117) {
if (indexj_cpc <= 117 && indexj_cpc >= 110) {
now_value = (short)now_value & 1;
logger_->Debug() << "now_value:" << now_value << endl;
}
cpci[j] = float2str(
(float)now_value *
p_btel->operator[]((int)(cpcci[j].get<int>() - 1)).factor,
1);
} else {
bool now_value = p_btel->operator[](indexj_cpc);
cpci[j] = (int)now_value;
}
}
} else {
logger_->Debug() << " cpci_size :" << cpci_size
<< ", cpcci_size:" << cpcci_size << std::endl;
}
}
} else {
logger_->Debug() << " cpc_size :" << cpc_size << ",cpcc_size:" << cpcc_size
<< std::endl;
}
/*---------TR-----------*/
mix_cc::json &TR_data = eqp_status_json_["TR"];
mix_cc::json &TR_data_conf = eqp_status_config_json_["TR"];
int TR_size = TR_data.size();
int TRc_size = TR_data_conf.size();
if (TR_size == TRc_size && TR_size == 6) {
/*-----coildid-----*/
int start = TR_data_conf[0][0].get<int>() - 1;
int end = TR_data_conf[0][1].get<int>() - 1;
int char_len = (end - start + 1) * 2;
CoilID tr_coild_id;
int bg = 0;
for (int i = 0; i < start; i++) {
bg += p_btel->operator[](i).length;
}
memset(&tr_coild_id, 0, sizeof(CoilID));
memcpy(&tr_coild_id, (char *)&seq[0] + bg, char_len);
// logger_->Debug() << "bg:" << bg << ",char_len:" << char_len
// << ",tr_coild_id:" << tr_coild_id << std::endl;
TR_data[0] = std::string(tr_coild_id);
/*-----其他数据*/
for (int j = 1; j < TR_size; j++) {
int indexj = (int)(TR_data_conf[j].get<int>() - 1);
float now_value = p_btel->operator[](indexj);
if (j < 3) {
TR_data[j] =
float2str(now_value * p_btel->operator[](indexj).factor, 2);
} else {
TR_data[j] =
float2str(now_value * p_btel->operator[](indexj).factor, 0);
}
// logger_->Debug() << "indexj:" << indexj << ",value:" << now_value
// << std::endl;
}
}
/*-------POR-------*/
mix_cc::json &por_data = eqp_status_json_["POR"];
mix_cc::json &por_data_conf = eqp_status_config_json_["POR"];
int POR_size = por_data.size();
int PORc_size = por_data_conf.size();
if (POR_size == PORc_size && POR_size == 2) {
for (int i = 0; i < POR_size; i++) {
mix_cc::json &pori_data = por_data[i];
mix_cc::json &pori_data_conf = por_data_conf[i];
int pori_size = pori_data.size();
int porci_size = pori_data_conf.size();
if (pori_size == porci_size && pori_size == 5) {
/*---------coild_id--*/
int start = pori_data_conf[0][0].get<int>() - 1;
int end = pori_data_conf[0][1].get<int>() - 1;
int char_len = (end - start + 1) * 2;
CoilID por_coild_id;
int bg = 0;
for (int j = 0; j < start; j++) {
bg += p_btel->operator[](j).length;
}
// for (int k = 0; k < char_len / 2; k++) {
// short vl = p_btel->operator[](start + k);
// logger_->Debug() << "start:" << start + 1
// << ",now index:" << start + 1 + k
// << ",coidid int data:" << vl << std::endl;
// }
memset(&por_coild_id, 0, sizeof(CoilID));
memcpy(&por_coild_id, (char *)&seq[0] + bg , char_len);
// logger_->Debug() << "bg:" << bg << ",char_len:" << char_len
// << ",por_coild_id:" << por_coild_id << std::endl;
pori_data[0] = std::string(por_coild_id);
/*-----其他数据*/
for (int k = 1; k < pori_size; k++) {
float now_value =
p_btel->operator[]((int)(pori_data_conf[k].get<int>() - 1));
pori_data[k] = (int)now_value;
}
int mt = pori_data[pori_size - 1].get<int>() / 60;
int sed = pori_data[pori_size - 1].get<int>() % 60;
string rm = std::to_string(mt) + ":" + std::to_string(sed);
pori_data[pori_size - 1] = rm;
} else {
logger_->Debug() << "pori_size:" << pori_size
<< ",porci_size:" << porci_size << std::endl;
}
}
} else {
logger_->Debug() << "POR_size:" << POR_size << ",PORc_size:" << PORc_size
<< std::endl;
}
}
void EqpStatus::update_alarm_info() {
logger_->Debug() << "update_alarm_info()" << std::endl;
mix_cc::json &eqp_status = eqp_status_json_["eqp_status"];
mix_cc::json &eqpid = eqp_status["eqpid"];
mix_cc::json &isAlarm = eqp_status["isAlarm"];
int epqid_size = eqpid.size();
short statuscode1 = 100;
short statuscode2 = 200;
for (int i = 0; i < epqid_size; i++) {
string eqpidi = eqpid[i].get<string>();
// int res = select_alarm_count(eqpidi, 0);
int res1 = select_alarm_count(eqpidi, statuscode1);
logger_->Debug() << "first---eqpid:" << eqpidi << ",未处理报警数量:"
<< res1 << std::endl;
int res2 = select_alarm_count(eqpidi, statuscode2);
logger_->Debug() << "second--eqpid:" << eqpidi << ",未处理报警跟踪数量:"
<< res2 << std::endl;
int isAlarmNow = res2 == 0 ? 0 : 2;
isAlarm[i] = res1 == 0 ? isAlarmNow : 1;
}
// for (int i = 0; i < epqid_size; i++) {
// string eqpidi = eqpid[i].get<string>();
// int res = select_alarm_count(eqpidi, 0);
// logger_->Debug() << "eqpid:" << eqpidi << ",报警数量:" << res <<
// std::endl; isAlarm[i] = res == 0 ? res : 1;
// }
}
int EqpStatus::select_alarm_count(string eqpid, int dealresult) {
FV_RESULT_JOIN_EQPID frje;
auto min_time =
mix_cc::mix_time_t(chrono::system_clock::now() - chrono::days(3));
auto sql_statement =
select(frje.ruleid())
.from(frje)
.where(frje.eqpid().like(eqpid + "%"), frje.alarmtime() > min_time,
frje.dealresult() == dealresult, frje.rank() == 1);
logger_->Debug() << "sql_str:" << sql_statement.get_command() << std::endl;
auto sql_rsult_maybe = exec<db2_t, FV_RESULT_JOIN_EQPID>(sql_statement);
if (sql_rsult_maybe.is_just()) {
auto sql_rsult = sql_rsult_maybe.unsafe_get_just();
if (sql_rsult.empty()) {
logger_->Debug() << "FV_RESULT_JOIN_EQPID 未查到数据设备id:" << eqpid
<< ",dealresult:" << dealresult << std::endl;
return 0;
} else {
logger_->Debug() << "FV_RESULT_JOIN_EQPID 查询到数据数量:"
<< sql_rsult.size() << "设备id:" << eqpid
<< ",dealresult:" << dealresult << std::endl;
return sql_rsult.size();
}
} else {
logger_->Error() << "EqpStatus::update_alarm_info()查询失败!" << std::endl;
return 0;
}
return 0;
}
int EqpStatus::select_alarm_count(string eqpid, short statuscode) {
FV_RESULT_JOIN_EQPID frje;
auto min_time =
mix_cc::mix_time_t(chrono::system_clock::now() - chrono::days(3));
auto sql_statement =
select(frje.ruleid())
.from(frje)
.where(frje.eqpid().like(eqpid + "%"), frje.alarmtime() > min_time,
frje.statuscode() == statuscode, frje.rank() == 1);
logger_->Debug() << "sql_str:" << sql_statement.get_command() << std::endl;
auto sql_rsult_maybe = exec<db2_t, FV_RESULT_JOIN_EQPID>(sql_statement);
if (sql_rsult_maybe.is_just()) {
auto sql_rsult = sql_rsult_maybe.unsafe_get_just();
if (sql_rsult.empty()) {
logger_->Debug() << "FV_RESULT_JOIN_EQPID 未查到数据设备id:" << eqpid
<< ",statuscode:" << statuscode << std::endl;
return 0;
} else {
logger_->Debug() << "FV_RESULT_JOIN_EQPID 查询到数据数量:"
<< sql_rsult.size() << "设备id:" << eqpid
<< ",statuscode:" << statuscode << std::endl;
return sql_rsult.size();
}
} else {
logger_->Error() << "EqpStatus::update_alarm_info()查询失败!" << std::endl;
return 0;
}
return 0;
}
void EqpStatus::update_coil_info() {
FV_DSC_PDI_DATA fdpd;
auto sql_statement =
select(fdpd.entid(), fdpd.extid(), fdpd.hentry(), fdpd.exthickave(),
fdpd.width(), fdpd.weightcoil(), fdpd.diameterinside(),
fdpd.diameteroutside(), fdpd.steelgrade())
.from(fdpd);
auto sql_rsult_maybe = exec<db2_t, FV_DSC_PDI_DATA>(sql_statement);
if (sql_rsult_maybe.is_just()) {
auto sql_rsult = sql_rsult_maybe.unsafe_get_just();
if (sql_rsult.empty()) {
logger_->Debug() << "FV_DSC_PDI_DATA 未查到数据设备id:" << std::endl;
return;
} else {
mix_cc::json &js1 = this->eqp_status_json_["steel_coil_info"]["data"];
int rt_size = sql_rsult.size();
logger_->Debug() << "FV_DSC_PDI_DATA 查询到数据数量:" << rt_size
<< std::endl;
for (int i = 0; i < rt_size; i++) {
js1[i][0] = sql_rsult[i].entid;
js1[i][1] = sql_rsult[i].extid;
js1[i][2] = sql_rsult[i].hentry;
js1[i][3] = sql_rsult[i].exthickave;
js1[i][4] = sql_rsult[i].width;
js1[i][5] = sql_rsult[i].weightcoil;
js1[i][6] = sql_rsult[i].diameterinside;
js1[i][7] = sql_rsult[i].diameteroutside;
js1[i][8] = sql_rsult[i].steelgrade;
}
}
} else {
logger_->Error() << "EqpStatus::update_coil_info()查询失败!" << std::endl;
return;
}
}