eis/src/zad/DataAlarm.cpp

314 lines
9.7 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 <zad/DataAlarm.h>
#include <log4cplus/LOG.h>
#include <common/L2Event.h>
#include <glob/ProxyMag.h>
#include <glob/GlobMem.h>
#include <zlib/DB2Mem.hpp>
#include <dao/DbStandardDBAX.h>
#include <json/json.h>
#include <glob/ConfigMag.h>
#include <algorithm>
//#include <zpub/MonitorI.h>
#include "T_LOV_JUDGE.h"
#include <string.h>
#include <iDA/iDA.h>
#include <zlib/MemVar.h>
#include <sstream>
using namespace log4cplus;
using namespace std;
extern iDA::Connection cn;
DataAlarm::DataAlarm()
{
LOG d("DataAlarm::DataAlarm");
mqp_proxy = ProxyMag::GetAppICEPrx("baosight/zmqp");
mp_restapi = new RestAPI();
pt_pdo = new CMemTable<PDO>("TRKPDO",PDO_LIST_MAX);
mp_coil = new CMemCoil();
mp_trk = new CMemTrk();
AlarmMsg = new T_ALARM_MSG();
db_order = " ORDER BY id DESC";
p_tele = new BinaryTele(2000, "T_LOV_FDAAITEM");
ConfigMag configmag("CRGS");
string server = configmag.ReadProperty("activemq", "server");
judge.clear();
isBA(0);
d.Info() << "END" << std::endl;
}
DataAlarm::~DataAlarm()
{
LOG d("DataAlarm::~DataAlarm");
delete pt_pdo;
delete mp_restapi;
delete mp_coil;
delete mp_trk;
delete AlarmMsg;
delete p_tele;
}
string DataAlarm::isBA(int zone)
{
LOG d("DataAlarm::SteelGrade");
string sql = "select * from T_LOV_JUDGE";
iDA::Command cmd;
cmd.SetConnection(&cn);
//d.Info() << sql << endl;
cmd.SetCommandText(sql);
cmd.Execute();
string setstr;
T_LOV_JUDGE::STR_T_LOV_JUDGE singlejudge;
while (cmd.FetchNext())
{
strcpy(singlejudge.CheckItem, cmd.Field("CheckItem").AsString().c_str());
//d.Info() << singlejudge.CheckItem << endl;
strcpy(singlejudge.CheckMode, cmd.Field("CheckMode").AsString().c_str());
strcpy(singlejudge.checkItemNameMax, cmd.Field("checkItemNameMax").AsString().c_str());
strcpy(singlejudge.checkItemNameMin, cmd.Field("checkItemNameMin").AsString().c_str());
strcpy(singlejudge.AlarmMessage, cmd.Field("AlarmMessage").AsString().c_str());
strcpy(singlejudge.AlarmTerm, cmd.Field("AlarmTerm").AsString().c_str());
strcpy(singlejudge.AlarmPos, cmd.Field("AlarmPos").AsString().c_str());
singlejudge.LowerLimit = cmd.Field("LowerLimit").AsDouble();
singlejudge.UpperLimit = cmd.Field("UpperLimit").AsDouble();
singlejudge.ZoneName = cmd.Field("ZoneName").AsDouble();
for (int i = 0; i < 5; i++)
{
string name = "BoolQueryName_IND" + to_string(i);
strcpy(singlejudge.BoolQueryName[i], cmd.Field(name.c_str()).AsString().c_str());
string source = "BoolQuerySource_IND" + to_string(i);
strcpy(singlejudge.BoolQuerySource[i], cmd.Field(source.c_str()).AsString().c_str());
string valuemax = "DigitalQueryValueMax_IND" + to_string(i);
string valuemin = "DigitalQueryValueMin_IND" + to_string(i);
strcpy(singlejudge.DigitalQueryValueMax[i], cmd.Field(valuemax.c_str()).AsString().c_str());
strcpy(singlejudge.DigitalQueryValueMin[i], cmd.Field(valuemin.c_str()).AsString().c_str());
}
judge.push_back(singlejudge);
}
d.Info() << "END" << std::endl;
}
string DataAlarm::SteelGrade(int zone)
{
}
string DataAlarm::WorkDeal(int zone)
{
}
int DataAlarm::baseAlarm()
{
LOG d("DataAlarm::baseAlarm");
Json::Value jsonData;
//中央段速度为0停止报警
float MainSpd=getItemValue("speed_tm_ext");
if(MainSpd==0)
return 0;
for (int i = 0; i < judge.size(); i++)
{
Json::Value singledata;
string item = string(judge[i].CheckItem);
string ItemSet = string(judge[i].BoolQueryName[0]);
// d.Info()<<"报警项数据"<<item<<endl;
int zone = judge[i].ZoneName, flag = 0;
singledata["CheckItem"] = judge[i].CheckItem;
singledata["ZONE"] = zone;
singledata["AlarmMessage"] = string(judge[i].AlarmMessage);
singledata["AlarmPos"]=string(judge[i].AlarmPos);
singledata["AlarmTerm"]=string(judge[i].AlarmTerm);
double value = 0;
double valueset = 0;
string checkmode = judge[i].CheckMode;
//string checknamemax = judge[i].checkItemNameMax;
//string checknamemin = judge[i].checkItemNameMin;
double lowvalue = judge[i].LowerLimit;
double upvalue = judge[i].UpperLimit;
value = getItemValue(item);
singledata["level"] = 0;
singledata["minvalue"] = lowvalue;
singledata["maxvalue"] = upvalue;
singledata["value"] = value;
ostringstream os;
//按照给出的上下限直接判断
if (checkmode == "0")
{
if (value < lowvalue || value > upvalue)
{
singledata["level"] = 1;
if(value < lowvalue)
{
os<<"低于下限--实时值: "<<value<<"--下限: "<<lowvalue;
strcpy(judge[i].AlarmMessage,os.str().c_str());
os.clear();
}
if(value > upvalue)
{
os<<"高于上限--实时值: "<<value<<"--上限: "<<upvalue;
strcpy(judge[i].AlarmMessage,os.str().c_str());
os.clear();
}
}
}
//判断实际值是否在设定值的误差值以内
if (checkmode == "1" )
{
TRK_UNIT *trk_unit = (*mp_trk)(zone);
// string sql = "select * from T_PDI_DATA where ENTID='" + string(trk_unit->entId) + "'";
// iDA::Command cmd;
// cmd.SetConnection(&cn);
// cmd.SetCommandText(sql);
// cmd.Execute();
string ItemSet = string(judge[i].BoolQueryName[0]);
valueset =getItemValue(ItemSet);
double minset = 0, maxset = 0;
singledata["minvalue"] = valueset + lowvalue;
singledata["maxvalue"] = valueset + upvalue;
//d.Info()<<"item: "<<ItemSet<<" value: "<<valueset<<" min: "<<valueset + lowvalue<<" max: "<<valueset + upvalue<<endl;
if (value < valueset + lowvalue || value > valueset + upvalue)
{
singledata["level"] = 1;
if(value < valueset + lowvalue)
{
os<<"低于下限--实时值: "<<value<<"--下限: "<<valueset - lowvalue;
strcpy(judge[i].AlarmMessage,os.str().c_str());
os.clear();
}
if(value > valueset + upvalue)
{
os<<"高于上限--实时值: "<<value<<"--上限: "<<valueset + upvalue;
strcpy(judge[i].AlarmMessage,os.str().c_str());
os.clear();
}
}
}
jsonData.append(singledata);
//报警数据写入数据库
if (singledata["level"] == 1)
{
db_where = " WHERE id is not null ";
char *dbMsg = AlarmMsg->openSetDB(db_where.c_str(), db_order.c_str());
if (dbMsg != NULL)
{
d.Error() << "Can not open T_AlARM_MSG dbMsg= " << dbMsg << endl;
AlarmMsg->closeSetDB();
}
else
{
int seq = 1;
dbMsg = AlarmMsg->getSetDB();
if (dbMsg == NULL)
{
seq = AlarmMsg->Id() + 1;
}
else
{
d.Error() << "填入序号报错, dbMsg=" << dbMsg << endl;
}
string coild = (*mp_trk)(judge[i].ZoneName)->entId;
if(coild == "") continue;
// d.Info()<<"ZoneName: "<<judge[i].ZoneName<<"entid: "<<coild<<endl;
db_where = "where coilid = '" + coild + "' and MSG_ITEM like '" + judge[i].AlarmTerm + "%'";
// d.Debug()<<"db_where:"<<db_where<<endl;
dbMsg = AlarmMsg->openSetDB(db_where.c_str(), db_order.c_str());
if (dbMsg != NULL)
{
d.Error() << "Can not open T_AlARM_MSG dbMsg= " << dbMsg << endl;
AlarmMsg->closeSetDB();
}
else
{
dbMsg = AlarmMsg->getSetDB();
if(dbMsg != NULL)
{
d.Info()<<"dbmsg: "<<dbMsg<<endl;
}
TCMTime ptime(AlarmMsg->Time());
time_t now = time(0);
//d.Debug()<<"now:"<<now<<" ptime:"<<ptime.GetSec70()<<endl;
if(dbMsg != NULL || now - ptime.GetSec70() > 60*4 )
{
AlarmMsg->initInsertKeys(seq);
AlarmMsg->set_ZoneNo(judge[i].ZoneName);
AlarmMsg->set_Text(judge[i].AlarmMessage);
AlarmMsg->set_msg_item(judge[i].AlarmTerm);
AlarmMsg->set_ZoneName(judge[i].AlarmPos);
AlarmMsg->set_CoilId((*mp_trk)(judge[i].ZoneName)->entId);
AlarmMsg->set_Spare1(judge[i].CheckItem);
AlarmMsg->set_Time((char *)TCMTime::Now().ToString().c_str());
AlarmMsg->set_wplenstart((*mp_trk)(judge[i].ZoneName)->wplen/1000);
AlarmMsg->set_wplenstop((*mp_trk)(judge[i].ZoneName)->wplen/1000);
dbMsg = AlarmMsg->insertDB();
if (dbMsg != NULL)
{
d.Error() << "Insert T_AlARM_MSG failed, dbMsg=" << dbMsg << endl;
AlarmMsg->closeSetDB();
AlarmMsg->dbRollback();
}
else
{
AlarmMsg->closeSetDB();
AlarmMsg->dbCommit();
}
}
else
{
AlarmMsg->closeSetDB();
}
}
}
} //报警项数据库处理结束
}
stringstream strs;
strs << jsonData;
// d.Debug() << strs.str() << std::endl;
this->Proxy(EV_MQP_RULEMSG,"baosight/zmqp",strs.str().c_str(),strs.str().length());
}
double DataAlarm::getItemValue(string item)
{
LOG d("DataAlarm::getItemValue");
p_tele->ReBuild(2000);
int index = p_tele->GetItemIndex(item);
// d.Info()<<"index: "<<index<<endl;
if(index != -1)
{
CMemTable<FDA_DATA> tele(StringHelper::ToString<int>(2000), 2);
FDA_DATA *mp_data = tele();
double value = (*mp_data)[index];
//d.Debug() << "Item = " << item << ", value = " << value << std::endl;
return value;
}
else
{
d.Debug()<<"Don't find item"<<endl;
return -1;
}
}
short DataAlarm::Proxy(int eventNo,const string& proxyName, const char* keyValue,int length)
{
LOG d("DataAlarm::Proxy");
if (keyValue == NULL)
{
d.Error()<<"NULL POINTER"<<endl;
return -1;
}
::Ice::ByteSeq sendData(keyValue,keyValue+length);
try
{
ProxyMag::GetAppICEPrx(proxyName)->SendDataShort(eventNo,sendData,sendData.size());
d.Info("send to %s ok",proxyName.c_str());
}
catch( ::Ice::LocalException& e)
{
d.Error()<< "SendSeq error:"<<e<<endl;
}
return 0;
}