AlarmFSM.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. #include "stdafx.h"
  2. #include "AlarmFSM.h"
  3. #include "Event.h"
  4. #include "CommEntityRestful.hpp"
  5. #include "json/json.h"
  6. namespace Task
  7. {
  8. //发送告警
  9. struct SendAlarmTask : public ITaskSp{
  10. AlarmFSM* m_fsm;
  11. explicit SendAlarmTask(AlarmFSM* f) : m_fsm(f) {}
  12. void Process(){
  13. LOG_FUNCTION();
  14. //改为循环处理告警
  15. int logSum=0;
  16. while(true){
  17. if(m_fsm->m_NewUploading_alarm.empty()&&m_fsm->m_unSendAlarm.IsNullOrEmpty()){
  18. if(logSum>=120){
  19. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("no warn,wait 6min");
  20. logSum=0;
  21. }else{
  22. logSum++;
  23. }
  24. Sleep(3000);//无重发和新的告警则等待
  25. continue;
  26. }
  27. logSum=0;
  28. #ifdef RVC_OS_WIN
  29. IHttpFunc* client;
  30. client = create_http(m_fsm->HttpsLogCallBack);
  31. bool isSendSucc=false;//查询是否成功标志
  32. if(SendAlarmTaskImpl(client)){
  33. isSendSucc = true;
  34. }else{
  35. isSendSucc = false;
  36. }
  37. client->Destory();
  38. #else
  39. bool isSendSucc=false;//查询是否成功标志
  40. if(SendAlarmTaskImpl()){
  41. isSendSucc = true;
  42. }else{
  43. isSendSucc = false;
  44. }
  45. #endif
  46. //m_fsm->PostEventFIFO(new SendAlarmEvent(isSendSucc));//返回处理结果
  47. if(isSendSucc){
  48. //成功
  49. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("send alarm statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSucc=%d , m_iFail=%d",m_fsm->m_iRec,m_fsm->m_iThrow,m_fsm->m_NewUploading_alarm.size(),m_fsm->m_iSucc,m_fsm->m_iFail);
  50. if(!m_fsm->m_NewUploading_alarm.empty()){
  51. Sleep(2000);//赶紧发送
  52. continue;
  53. }else{
  54. Sleep(3000);//等待
  55. continue;
  56. }
  57. }else{
  58. //失败,超过未发送成功的告警时间间隔,丢弃告警信息
  59. CSmallDateTime endTime = CSmallDateTime::GetNow();
  60. if(((DWORD)endTime-(DWORD)m_fsm->m_sendBeginTime)>m_fsm->m_maxResendTime){
  61. m_fsm->m_unSendAlarm="";//置为空
  62. m_fsm->m_iFail = m_fsm->m_iFail+ m_fsm->m_eachSum ;//发送失败
  63. LogWarn(Severity_Low,Error_Exception,WARN_ALARM_SEND_FAIL,CSimpleStringA::Format("throw away alarm num= %d,m_iFail=%d,m_iThrow=%d",m_fsm->m_eachSum,m_fsm->m_iFail,m_fsm->m_iThrow));
  64. m_fsm->m_eachSum =0;//清空批次数量
  65. m_fsm->m_sendBeginTime = CSmallDateTime::GetNow();
  66. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("send alarm statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSucc=%d , m_iFail=%d",m_fsm->m_iRec,m_fsm->m_iThrow,m_fsm->m_NewUploading_alarm.size(),m_fsm->m_iSucc,m_fsm->m_iFail);
  67. }
  68. Sleep(20*1000);//失败重发等待
  69. continue;
  70. }
  71. }
  72. }
  73. #ifdef RVC_OS_WIN
  74. bool SendAlarmTaskImpl(IHttpFunc* client){
  75. HttpStruct::SendAlarmTaskReq qTempReq;
  76. HttpStruct::SendAlarmTaskRet qTempRet;
  77. if(m_fsm->m_unSendAlarm.IsNullOrEmpty()){
  78. //组装新的json告警信息
  79. string strJson = m_fsm->alarmJson();
  80. if(strJson.empty()){
  81. return true;
  82. }else{
  83. qTempReq.m_reqStr = strJson.c_str();//请求参数
  84. m_fsm->m_unSendAlarm = strJson.c_str();
  85. m_fsm->m_sendBeginTime = CSmallDateTime::GetNow();
  86. }
  87. }else{
  88. //重发新的告警信息
  89. qTempReq.m_reqStr = m_fsm->m_unSendAlarm.GetData();//请求参数
  90. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("alarm retry send");
  91. }
  92. qTempReq.m_url=m_fsm->m_sendUrl.GetData();//访问地址
  93. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("alarm send =%s",qTempReq.m_reqStr.c_str());
  94. if(!client->Post(qTempReq, qTempRet)){
  95. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask http req fail,url=%s",qTempReq.m_url.c_str());
  96. return false;//失败
  97. }
  98. Json::Reader reader;
  99. Json::Value rootRet;
  100. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  101. {
  102. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask parse resp is fail");
  103. return false;//失败
  104. }
  105. bool isSucc = rootRet["success"].asBool();
  106. if(isSucc){
  107. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask [success] is succ,m_eachSum=%d",m_fsm->m_eachSum);
  108. m_fsm->m_unSendAlarm="";//置为空
  109. m_fsm->m_iSucc = m_fsm->m_iSucc+ m_fsm->m_eachSum ;//发送成功
  110. m_fsm->m_eachSum = 0;//清空批次数量
  111. return true;//写入成功
  112. }else{
  113. CSimpleStringA errCode = rootRet["code"].asString().c_str();
  114. if(!errCode.IsNullOrEmpty()||errCode == "10101"){
  115. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask [success] is false,code = 10101");
  116. m_fsm->m_unSendAlarm="";//置为空
  117. m_fsm->m_iFail = m_fsm->m_iFail+ m_fsm->m_eachSum ;//发送失败
  118. LogWarn(Severity_Low,Error_Exception,WARN_ALARM_PARSE_FAIL,CSimpleStringA::Format("SendAlarmTask [success] is false,code = 10101,throw away alarm num= %d,m_iFail=%d",m_fsm->m_eachSum,m_fsm->m_iFail));
  119. m_fsm->m_eachSum = 0;//清空批次数量
  120. return true;//中文乱码解析失败,默认成功
  121. }else{
  122. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask [success] is false,code =%s",errCode);
  123. return false;//写入失败
  124. }
  125. }
  126. }
  127. #else
  128. bool SendAlarmTaskImpl() {
  129. HttpClientResponseResult result;
  130. HttpClientRequestConfig config(HttpRequestMethod::POST, m_fsm->m_sendUrl.GetData());
  131. string reqStr = "";
  132. if(m_fsm->m_unSendAlarm.IsNullOrEmpty()){
  133. //组装新的json告警信息
  134. string strJson = m_fsm->alarmJson();
  135. if(strJson.empty()){
  136. return true;
  137. }else{
  138. reqStr = strJson.c_str();//请求参数
  139. m_fsm->m_unSendAlarm = strJson.c_str();
  140. m_fsm->m_sendBeginTime = CSmallDateTime::GetNow();
  141. }
  142. }else{
  143. //重发新的告警信息
  144. reqStr = m_fsm->m_unSendAlarm.GetData();//请求参数
  145. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("alarm retry send");
  146. }
  147. config.SetJsonBody(reqStr);
  148. RestfulClient client = RestfulClient::getInstance();
  149. config.PreDo();
  150. client.Do(&config, &result);
  151. //判断结果
  152. if(result.ResponseOK()){
  153. //解析返回字符串
  154. Json::Reader reader;
  155. Json::Value rootRet;
  156. if (!reader.parse(result.content, rootRet, false))
  157. {
  158. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask parse resp is fail");
  159. return false;//失败
  160. }
  161. bool isSucc = rootRet["success"].asBool();
  162. if(isSucc){
  163. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask [success] is succ,m_eachSum=%d",m_fsm->m_eachSum);
  164. m_fsm->m_unSendAlarm="";//置为空
  165. m_fsm->m_iSucc = m_fsm->m_iSucc+ m_fsm->m_eachSum ;//发送成功
  166. m_fsm->m_eachSum = 0;//清空批次数量
  167. return true;//写入成功
  168. }else{
  169. CSimpleStringA errCode = rootRet["code"].asString().c_str();
  170. if(!errCode.IsNullOrEmpty()||errCode == "10101"){
  171. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask [success] is false,code = 10101");
  172. m_fsm->m_unSendAlarm="";//置为空
  173. m_fsm->m_iFail = m_fsm->m_iFail+ m_fsm->m_eachSum ;//发送失败
  174. LogWarn(Severity_Low,Error_Exception,WARN_ALARM_PARSE_FAIL,CSimpleStringA::Format("SendAlarmTask [success] is false,code = 10101,throw away alarm num= %d,m_iFail=%d",m_fsm->m_eachSum,m_fsm->m_iFail));
  175. m_fsm->m_eachSum = 0;//清空批次数量
  176. return true;//中文乱码解析失败,默认成功
  177. }else{
  178. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask [success] is false,code =%s",errCode);
  179. return false;//写入失败
  180. }
  181. }
  182. }
  183. else {
  184. std::string errDetail(result.WhatError());
  185. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarmTaskImpl")("SendAlarmTask http req fail,error=[%s],url=%s", errDetail.c_str(), m_fsm->m_sendUrl.GetData());
  186. return false;//失败
  187. }
  188. return true;
  189. }
  190. #endif
  191. };
  192. }
  193. AlarmFSM::AlarmFSM() : m_pConnection(NULL),m_b_alarmstate(FALSE)
  194. {
  195. InitializeCriticalSection(&cs);
  196. INIT_LIST_HEAD(&m_uploading_alarm);
  197. INIT_LIST_HEAD(&m_uploaded_alarm);
  198. m_iEachSend=0;//每次连接发送的数量
  199. m_iRec=0;//累计收到的告警数目
  200. m_iSend=0;//累计队列发送总数
  201. m_iThrow=0;//累计队列丢弃总数
  202. m_iFail=0;//累计发送失败总次数
  203. m_iSucc=0;//累计发送成功总次数
  204. m_CenterModel=0;
  205. m_maxResendTime=60;
  206. m_eachSum=0;
  207. m_initAlarm=false;
  208. }
  209. AlarmFSM::~AlarmFSM()
  210. {
  211. DeleteCriticalSection(&cs);
  212. }
  213. void AlarmFSM::OnStateTrans( int iSrcState, int iDstState )
  214. {
  215. //Dbg("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  216. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  217. }
  218. void AlarmFSM::OnSysVarEvent( const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName )
  219. {
  220. }
  221. ErrorCodeEnum AlarmFSM::OnInit()
  222. {
  223. AddStateHooker(this);
  224. //......
  225. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  226. CSmartPointer<IConfigInfo> spConfig;
  227. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  228. if (Error_Succeed == Error)
  229. {
  230. Error = spConfig->ReadConfigValueInt("Alarm", "DealWarningTime", m_nDealWarningTime);
  231. if (Error_Succeed == Error)
  232. {
  233. //Dbg("get DealWarningTime=%d from CenterSetting.ini", m_nDealWarningTime);
  234. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("OnInit")("get DealWarningTime=%d from CenterSetting.ini", m_nDealWarningTime);
  235. }
  236. else
  237. {
  238. //Dbg("get DealWarningTime from CenterSetting.ini failed");
  239. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("get DealWarningTime from CenterSetting.ini failed");
  240. }
  241. int iCenterModel =0;
  242. Error = spConfig->ReadConfigValueInt("Alarm", "CenterModel", iCenterModel);
  243. if(Error==Error_Succeed){
  244. m_CenterModel = iCenterModel;
  245. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini CenterModel=%d",m_CenterModel);
  246. }else{
  247. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini CenterModel failed");
  248. }
  249. Error = spConfig->ReadConfigValueInt("Alarm", "maxResendTime", m_maxResendTime);
  250. if(Error==Error_Succeed){
  251. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini maxResendTime=%d",m_maxResendTime);
  252. }else{
  253. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini maxResendTime failed");
  254. }
  255. CSimpleStringA str="";
  256. Error = spConfig->ReadConfigValue("Alarm", "topic", str);
  257. if(Error==Error_Succeed){
  258. m_topic = str;
  259. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini topic=%s",str.GetData());
  260. }else{
  261. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini topic failed");
  262. }
  263. Error = spConfig->ReadConfigValue("Alarm", "sendUrl", str);
  264. if(Error==Error_Succeed){
  265. m_sendUrl = str;
  266. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini sendUrl=%s",str.GetData());
  267. }else{
  268. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini sendUrl failed");
  269. }
  270. Error = spConfig->ReadConfigValue("Alarm", "branchNo", str);
  271. if(Error==Error_Succeed){
  272. m_branchNo = str;
  273. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini branchNo=%s",str.GetData());
  274. }else{
  275. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("init read CenterSetting.ini branchNo failed");
  276. }
  277. } else {
  278. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI("OnInit")("open CenterSetting.ini failed");
  279. }
  280. if (m_nDealWarningTime == 0)
  281. {
  282. m_nDealWarningTime = 10;
  283. }
  284. if(m_maxResendTime <= 0){
  285. m_maxResendTime ==60;
  286. }
  287. if(m_branchNo.IsNullOrEmpty()){
  288. m_branchNo = "000";//默认是000总行分行号
  289. }
  290. return Error_Succeed;
  291. }
  292. ErrorCodeEnum AlarmFSM::OnExit()
  293. {
  294. return Error_Succeed;
  295. }
  296. void AlarmFSM::s0_on_entry()
  297. {
  298. if(!m_initAlarm){
  299. m_initAlarm=true;
  300. //添加集中配置参数读取和判断走分行还是总行
  301. if(m_CenterModel==0){
  302. LogWarn(Severity_Low,Error_Exception,WARN_ALARM_START_OLD_MODE,CSimpleStringA::Format("init alarm old model,model=%d",m_CenterModel));
  303. }else{
  304. LogWarn(Severity_Low,Error_Exception,WARN_ALARM_START_NEW_MODE,CSimpleStringA::Format("init alarm new model,model=%d",m_CenterModel));
  305. }
  306. }
  307. if(m_CenterModel==0){
  308. //旧模式
  309. //改造为先判断是否不为空,再创建连接,减少空连接数
  310. if(m_NewUploading_alarm.empty()){
  311. //Dbg("no warn,wait 3s");
  312. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("no warn,wait 3s");
  313. ScheduleTimer(1, 3000);// try 3 seconds later
  314. }else{
  315. if (!m_pConnection)
  316. {
  317. m_pConnection = new AlarmConnection(m_pEntity, this);
  318. if (m_pConnection->ConnectFromCentralSetting()&& m_pConnection->IsConnectionOK())
  319. {
  320. //创建成功
  321. }else{
  322. //创建失败
  323. //Dbg("connect server fail,config or Servers Error!");
  324. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("connect server fail,config or Servers Error!");
  325. closeClientConn();//断开连接
  326. }
  327. }
  328. if (!m_pConnection)
  329. {
  330. ScheduleTimer(1, 10000); // try 10 seconds later
  331. }
  332. else
  333. {
  334. PostEventLIFO(new FSMEvent(USER_EVT_JMP_ALARMUP));//跳转S1状态
  335. }
  336. }
  337. }else{
  338. //新模式
  339. PostEventLIFO(new FSMEvent(USER_EVT_JMP_NEW_ALARM));//跳转新模式发送
  340. }
  341. }
  342. void AlarmFSM::s0_on_exit()
  343. {
  344. CancelTimer(1);
  345. CancelTimer(2);
  346. }
  347. unsigned int AlarmFSM::s0_on_event( FSMEvent* event )
  348. {
  349. if (event->iEvt == EVT_TIMER)
  350. {
  351. if(event->param1 == 1){
  352. //改造为先判断是否不为空,再创建连接,减少空连接数
  353. if(m_NewUploading_alarm.empty()){
  354. //Dbg("no warn,wait 3s");
  355. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("no warn,wait 3s");
  356. ScheduleTimer(1, 3000);// try 3 seconds later
  357. }else{
  358. if (!m_pConnection)
  359. {
  360. m_pConnection = new AlarmConnection(m_pEntity, this);
  361. if (m_pConnection->ConnectFromCentralSetting()&& m_pConnection->IsConnectionOK())
  362. {
  363. //创建成功
  364. }else{
  365. //创建失败
  366. Dbg("connect server fail,config or Servers Error!");
  367. closeClientConn();//断开连接
  368. }
  369. }
  370. if (!m_pConnection)
  371. {
  372. ScheduleTimer(1, 10000); // try 10 seconds later
  373. }
  374. else
  375. {
  376. PostEventLIFO(new FSMEvent(USER_EVT_JMP_ALARMUP));//跳转S1状态
  377. }
  378. }
  379. }else if(event->param1 == 2){
  380. //新模式
  381. //if(m_NewUploading_alarm.empty()&&m_unSendAlarm.IsNullOrEmpty()){
  382. // //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("no warn,wait 3s");
  383. // ScheduleTimer(2, 3000);// try 3 seconds later
  384. //}else{
  385. //开始启动调用发送线程
  386. Task::SendAlarmTask* alarmTask = new Task::SendAlarmTask(this);
  387. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(alarmTask);
  388. if(rc!=Error_Succeed){
  389. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s0 create SendAlarmTask is fail,%d",(int)rc);
  390. ScheduleTimer(2, 10*1000);//等待一段时间
  391. }
  392. //}
  393. }
  394. }else if (event->iEvt == USER_EVT_JMP_NEW_ALARM){
  395. //if(m_NewUploading_alarm.empty()&&m_unSendAlarm.IsNullOrEmpty()){
  396. // //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("no warn,wait 3s");
  397. // ScheduleTimer(2, 3000);// try 3 seconds later
  398. //}else{
  399. //开始启动调用发送线程
  400. Task::SendAlarmTask* alarmTask = new Task::SendAlarmTask(this);
  401. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(alarmTask);
  402. if(rc!=Error_Succeed){
  403. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s0 create SendAlarmTask is fail,%d",(int)rc);
  404. ScheduleTimer(2, 10*1000);//等待一段时间
  405. }
  406. //}
  407. }
  408. //else if (event->iEvt == USER_EVT_SEND_ALARM){
  409. // SendAlarmEvent* pEvent = (SendAlarmEvent*)event;
  410. // if(pEvent->isSucc){
  411. // //成功
  412. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("send alarm statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSucc=%d , m_iFail=%d",m_iRec,m_iThrow,m_NewUploading_alarm.size(),m_iSucc,m_iFail);
  413. // if(!m_NewUploading_alarm.empty()){
  414. // ScheduleTimer(2,2000);
  415. // }else{
  416. // ScheduleTimer(2,3000);
  417. // }
  418. // }else{
  419. // //失败
  420. // //超过未发送成功的告警时间间隔,丢弃告警信息
  421. // CSmallDateTime endTime = CSmallDateTime::GetNow();
  422. // if(((DWORD)endTime-(DWORD)m_sendBeginTime)>m_maxResendTime){
  423. // m_unSendAlarm="";//置为空
  424. // m_iFail = m_iFail+ m_eachSum ;//发送失败
  425. // LogWarn(Severity_Low,Error_Exception,WARN_ALARM_SEND_FAIL,CSimpleStringA::Format("throw away alarm num= %d,m_iFail=%d",m_eachSum,m_iFail));
  426. // m_eachSum =0;//清空批次数量
  427. // m_sendBeginTime = CSmallDateTime::GetNow();
  428. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("send alarm statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSucc=%d , m_iFail=%d",m_iRec,m_iThrow,m_NewUploading_alarm.size(),m_iSucc,m_iFail);
  429. // }
  430. // ScheduleTimer(2,10000);
  431. // }
  432. //}
  433. return 0;
  434. }
  435. void AlarmFSM::s1_on_entry()
  436. {
  437. //connect
  438. //if (!m_pConnection)
  439. //{
  440. // m_pConnection = new AlarmConnection(m_pEntity, this);
  441. // if (!m_pConnection->ConnectFromCentralSetting())
  442. // {
  443. // Dbg("connect server fail,config or Servers Error!");
  444. // PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT));
  445. // return;
  446. // }
  447. //}
  448. //if (m_pConnection->IsConnectionOK()) //upload
  449. //{
  450. // EnterCriticalSection(&cs);
  451. // alarm_t*alarm = list_first_entry(&m_uploading_alarm,alarm_t,entry);
  452. // if (alarm == NULL)
  453. // {
  454. // Dbg("get alarm error");
  455. // PostEventLIFO(new FSMEvent(USER_EVT_JMP_RELEASE));
  456. // }
  457. // m_pConnection->SendAlarm(alarm);
  458. // //add by zl 20180306, 添加到历史告警列表
  459. // add_UploadedAlarm(alarm);
  460. // list_del(&alarm->entry);
  461. // //delete alarm->Description;
  462. // //delete alarm->EntityName;
  463. // //delete alarm->SN;
  464. // //delete alarm;
  465. // //因使用malloc创建内存,用free释放
  466. // free(alarm->Description);
  467. // alarm->Description=NULL;
  468. // free(alarm->EntityName);
  469. // alarm->EntityName=NULL;
  470. // free(alarm->SN);
  471. // alarm->SN=NULL;
  472. // delete alarm;
  473. // alarm=NULL;
  474. // Dbg("send alarm success");
  475. // PostEventLIFO(new FSMEvent(USER_EVT_JMP_RELEASE));
  476. // LeaveCriticalSection(&cs);
  477. //}
  478. //else
  479. //{
  480. // //链接失效,需要先释放并清空链接,否一直不会重连
  481. // m_pConnection->Close();
  482. // m_pConnection->DecRefCount();
  483. // m_pConnection = NULL;
  484. // PostEventLIFO(new FSMEvent(USER_EVT_JMP_RELEASE));
  485. //}
  486. if(m_NewUploading_alarm.empty()){
  487. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_FAIL));//为空时,跳回S0状态
  488. }else{
  489. //Dbg("send alarm statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSend=%d , m_iSucc=%d , m_iFail=%d",m_iRec,m_iThrow,m_NewUploading_alarm.size(),m_iSend,m_iSucc,m_iFail);
  490. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("send alarm statistic m_iRec=%d , m_iThrow=%d , UnSend=%d , m_iSend=%d , m_iSucc=%d , m_iFail=%d",m_iRec,m_iThrow,m_NewUploading_alarm.size(),m_iSend,m_iSucc,m_iFail);
  491. m_iEachSend = 0;
  492. //发送告警信息
  493. if(SendAlarm()){
  494. m_iEachSend++;//发送成功
  495. }else{
  496. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT));//发送失败时,跳回S0状态
  497. }
  498. }
  499. }
  500. void AlarmFSM::s1_on_exit()
  501. {
  502. CancelTimer(2);
  503. }
  504. unsigned int AlarmFSM::s1_on_event(FSMEvent* event)
  505. {
  506. //if (event->iEvt == USER_EVT_DISCONNECT)
  507. //{
  508. // Dbg("rx disconnect evt at alarmupload s1");
  509. // if (m_pConnection)
  510. // {
  511. // m_pConnection->Close();
  512. // m_pConnection->DecRefCount();
  513. // m_pConnection = NULL;
  514. // ScheduleTimer(2, 5000);
  515. // }
  516. //}
  517. //else if (event->iEvt == EVT_TIMER)
  518. //{
  519. // if (!m_pConnection)
  520. // {
  521. // m_pConnection = new AlarmConnection(m_pEntity, this);
  522. // if (!m_pConnection->ConnectFromCentralSetting())
  523. // {
  524. // Dbg("connect server fail,config or Servers Error!");
  525. // PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT));
  526. // }
  527. // }
  528. //}
  529. //else if (event->iEvt == EVT_TIMER)
  530. //{
  531. // //继续发送
  532. //}
  533. if (event->iEvt == USER_EVT_DISCONNECT_FAIL)
  534. {
  535. //Dbg("disconnect 10s at alarmupload s1");
  536. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("disconnect 5s at alarmupload s1");
  537. closeClientConn();//断开连接
  538. ScheduleTimer(2, 5000); // try 5 seconds later
  539. }
  540. else if (event->iEvt == USER_EVT_DISCONNECT_SUCC)
  541. {
  542. //Dbg("disconnect 3s at alarmupload s1");
  543. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("disconnect 2s at alarmupload s1");
  544. closeClientConn();//断开连接
  545. ScheduleTimer(2, 2000); // try 2 seconds later
  546. }
  547. else if (event->iEvt == EVT_TIMER)
  548. {
  549. PostEventLIFO(new FSMEvent(USER_EVT_JMP_START));//跳转S0状态
  550. }
  551. else if (event->iEvt ==USER_EVT_UPLOAD_ANS)
  552. {
  553. if(m_iEachSend<=50)
  554. {
  555. if(!m_NewUploading_alarm.empty()){
  556. if(SendAlarm()){
  557. m_iEachSend++;//发送成功
  558. }else{
  559. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_FAIL));//发送失败时,跳回S0状态
  560. }
  561. }else{
  562. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_FAIL));//为空时,跳回S0状态
  563. }
  564. }
  565. else
  566. {
  567. PostEventLIFO(new FSMEvent(USER_EVT_DISCONNECT_SUCC));//达到一定发送数量,主动断开,跳回S0状态
  568. }
  569. }
  570. return 0;
  571. }
  572. void AlarmFSM::s2_on_entry()
  573. {
  574. //connect
  575. if (!m_pConnection)
  576. {
  577. m_pConnection = new AlarmConnection(m_pEntity, this);
  578. if (!m_pConnection->ConnectFromCentralSetting())
  579. {
  580. //Dbg("connect server fail,config or Servers Error!");
  581. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("connect server fail,config or Servers Error!");
  582. }
  583. }
  584. if (m_pConnection->IsConnectionOK()) //upload alarm state
  585. {
  586. if (m_b_alarmstate)
  587. {
  588. m_pConnection->SendAlarmState(&m_uploading_alarmstate);
  589. //Dbg("send alarmstate success");
  590. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("send alarmstate success");
  591. delete m_uploading_alarmstate.Description;
  592. PostEventLIFO(new FSMEvent(USER_EVT_JMP_RELEASE));
  593. }
  594. }
  595. else
  596. {
  597. //链接失效,需要先释放并清空链接,否则一直不会重连
  598. m_pConnection->Close();
  599. m_pConnection->DecRefCount();
  600. m_pConnection = NULL;
  601. PostEventLIFO(new FSMEvent(USER_EVT_JMP_RELEASE));
  602. }
  603. }
  604. void AlarmFSM::s2_on_exit()
  605. {
  606. m_b_alarmstate = FALSE;
  607. CancelTimer(3);
  608. }
  609. unsigned int AlarmFSM::s2_on_event(FSMEvent* event)
  610. {
  611. if (event->iEvt == USER_EVT_DISCONNECT)
  612. {
  613. //Dbg("rx disconnect evt at alarm upload s2");
  614. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("rx disconnect evt at alarm upload s2");
  615. if (m_pConnection)
  616. {
  617. m_pConnection->Close();
  618. m_pConnection->DecRefCount();
  619. m_pConnection = NULL;
  620. }
  621. ScheduleTimer(3, 10000);
  622. }
  623. return 0;
  624. }
  625. void AlarmFSM::s3_on_entry()
  626. {
  627. //modify by zl 20180314,发送完,马上断开链接可能会导致服务端无法接受到请求,改用长链接
  628. /*if (m_pConnection)
  629. {
  630. m_pConnection->Close();
  631. m_pConnection->DecRefCount();
  632. m_pConnection = NULL;
  633. LogEvent(Severity_Low,EVENT_MOD_ALARM_INITIATIVE_DISCONNECT,"Connecting Closed");
  634. }*/
  635. PostEventFIFO(new FSMEvent(USER_EVT_DISCONNECT));
  636. if (!m_pConnection)
  637. {
  638. ScheduleTimer(4, 1000); // try 30 seconds later
  639. }
  640. }
  641. void AlarmFSM::s3_on_exit()
  642. {
  643. CancelTimer(4);
  644. }
  645. unsigned int AlarmFSM::s3_on_event(FSMEvent* event)
  646. {
  647. if (event->iEvt == USER_EVT_DISCONNECT)
  648. {
  649. //Dbg(" disconnect evt at alarm upload s3");
  650. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("disconnect evt at alarm upload s3");
  651. /*if (m_pConnection)
  652. {
  653. m_pConnection->Close();
  654. m_pConnection->DecRefCount();
  655. m_pConnection = NULL;
  656. }*/
  657. }
  658. return 0;
  659. }
  660. //增加告警
  661. void AlarmFSM::add_alarm(alarm_t*alarm)
  662. {
  663. EnterCriticalSection(&cs);
  664. add_new_alarm(alarm,&m_uploading_alarm);
  665. LeaveCriticalSection(&cs);
  666. }
  667. //设置告警状态
  668. void AlarmFSM::set_alarm_state(const CUUID nLogID,DWORD LifeId,const SeverityLevelEnum eLevel,
  669. DWORD Item,const char *pszMessage)
  670. {
  671. m_uploading_alarmstate.SN = nLogID;
  672. _itoa((int)eLevel,&m_uploading_alarmstate.Level,16);
  673. m_uploading_alarmstate.LifeID = LifeId;
  674. m_uploading_alarmstate.Item = Item;
  675. char cDescription[512];
  676. strcpy(cDescription,pszMessage);
  677. m_uploading_alarmstate.Description = _strdup(cDescription);
  678. m_b_alarmstate = TRUE;
  679. }
  680. //获取告警数量
  681. bool AlarmFSM::is_alarm_empty()
  682. {
  683. EnterCriticalSection(&cs);
  684. bool i = list_empty(&m_uploading_alarm);
  685. LeaveCriticalSection(&cs);
  686. return i;
  687. }
  688. bool AlarmFSM::is_UploadedAlarm_empty()
  689. {
  690. EnterCriticalSection(&cs);
  691. bool i = list_empty(&m_uploaded_alarm);
  692. LeaveCriticalSection(&cs);
  693. return i;
  694. }
  695. static time_t NowTime()
  696. {
  697. time_t t_Now = time(0);
  698. struct tm* tm_Now = localtime(&t_Now);
  699. /*tm_Now->tm_hour =0;
  700. tm_Now->tm_min = 0;
  701. tm_Now->tm_sec = 0;*/
  702. return mktime(tm_Now);
  703. }
  704. void AlarmFSM::add_UploadedAlarm(alarm_t*alarm)
  705. {
  706. alarmUpgraded_t*upedAlarm = new alarmUpgraded_t();
  707. upedAlarm->EntityName = new char[32];
  708. if (NULL != upedAlarm->EntityName)
  709. {
  710. memset(upedAlarm->EntityName, 0, 32);
  711. }
  712. upedAlarm->SN = new char[20];
  713. if (NULL != upedAlarm->SN)
  714. {
  715. memset(upedAlarm->SN, 0, 20);
  716. }
  717. upedAlarm->Description = new char[512];
  718. if (NULL != upedAlarm->Description)
  719. {
  720. memset(upedAlarm->Description, 0, 512);
  721. }
  722. strcpy(upedAlarm->EntityName,alarm->EntityName);
  723. strcpy(upedAlarm->SN,alarm->SN);
  724. upedAlarm->LifeID = alarm->LifeID;
  725. upedAlarm->Item = alarm->Item;
  726. upedAlarm->UserCode = alarm->UserCode;
  727. upedAlarm->Level = alarm->Level;
  728. strcpy(upedAlarm->Description,alarm->Description);
  729. upedAlarm->time = NowTime();
  730. //如果是相同实体且相同描述,则更新告警。否则做为新告警添加
  731. if (!update_UploadedAlarm(upedAlarm))
  732. {
  733. EnterCriticalSection(&cs);
  734. add_new_upalarm(upedAlarm,&m_uploaded_alarm);
  735. LeaveCriticalSection(&cs);
  736. }
  737. }
  738. bool AlarmFSM::find_UploadedAlarm(alarm_t*alarm)
  739. {
  740. EnterCriticalSection(&cs);
  741. if (is_UploadedAlarm_empty())
  742. {
  743. return false;
  744. }
  745. //time_t tNow = NowTime();
  746. //获取时间报错,改为直接获取
  747. time_t t_Now = time(0);
  748. struct tm* tm_Now = localtime(&t_Now);
  749. time_t tNow = mktime(tm_Now);
  750. //Dbg("tNow[%d]", tNow);
  751. alarmUpgraded_t* uploadedAlarm;
  752. list_for_each_entry(uploadedAlarm, &m_uploaded_alarm, alarmUpgraded_t, entry)
  753. {
  754. if (0 == strcmp(uploadedAlarm->EntityName, alarm->EntityName)
  755. && 0 == strcmp(uploadedAlarm->Description, alarm->Description))
  756. {
  757. if (tNow > uploadedAlarm->time)
  758. {
  759. UINT nSecond = tNow - uploadedAlarm->time;
  760. UINT nMin = nSecond/60;
  761. if (nMin <= m_nDealWarningTime)
  762. {
  763. //在指定时间内(可配置),收到同一实体的同一告警,去重
  764. //Dbg("find same uploaded alarm list, don't need to send again");
  765. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("find_UploadedAlarm")("find same uploaded alarm list, don't need to send again");
  766. return true;
  767. }
  768. }
  769. else
  770. {
  771. //Dbg("uploadedAlarm->time[%d] >= tNow[%d], time is wrong", uploadedAlarm->time, tNow);
  772. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("find_UploadedAlarm")("uploadedAlarm->time[%d] >= tNow[%d], time is wrong", uploadedAlarm->time, tNow);
  773. }
  774. }
  775. }
  776. LeaveCriticalSection(&cs);
  777. return false;
  778. }
  779. bool AlarmFSM::update_UploadedAlarm(alarmUpgraded_t*alarm)
  780. {
  781. EnterCriticalSection(&cs);
  782. if (is_UploadedAlarm_empty())
  783. {
  784. return false;
  785. }
  786. alarmUpgraded_t* uploadedAlarm;
  787. list_for_each_entry(uploadedAlarm, &m_uploaded_alarm, alarmUpgraded_t, entry)
  788. {
  789. if (0 == strcmp(uploadedAlarm->EntityName, alarm->EntityName)
  790. || 0 == strcmp(uploadedAlarm->Description, alarm->Description))
  791. {
  792. update_upalarm(alarm, uploadedAlarm);
  793. //替换后,旧的对象需要清理
  794. //free(uploadedAlarm->Description);
  795. //free(uploadedAlarm->EntityName);
  796. //free(uploadedAlarm->SN);
  797. delete[] uploadedAlarm->Description;
  798. uploadedAlarm->Description=NULL;
  799. delete[] uploadedAlarm->EntityName;
  800. uploadedAlarm->EntityName=NULL;
  801. delete[] uploadedAlarm->SN;
  802. uploadedAlarm->SN=NULL;
  803. delete uploadedAlarm;
  804. uploadedAlarm=NULL;
  805. return true;
  806. }
  807. }
  808. LeaveCriticalSection(&cs);
  809. return false;
  810. }
  811. //增加消息到新告警列表
  812. void AlarmFSM::add_NewAlarm(alarm_t* alarm)
  813. {
  814. EnterCriticalSection(&cs);
  815. if(m_NewUploading_alarm.size()>=UPLOG_MAX_COUNT){
  816. //Dbg("队列已满,删除首元素");
  817. m_iThrow++;
  818. alarm_t* oldAlarm = (alarm_t*)(*m_NewUploading_alarm.begin());//集合首个元素
  819. m_NewUploading_alarm.erase(m_NewUploading_alarm.begin());//集合删除首元素
  820. free(oldAlarm->Description);
  821. oldAlarm->Description=NULL;
  822. free(oldAlarm->EntityName);
  823. oldAlarm->EntityName=NULL;
  824. free(oldAlarm->SN);
  825. oldAlarm->SN=NULL;
  826. free(oldAlarm->warnTime);
  827. oldAlarm->warnTime=NULL;
  828. delete oldAlarm;
  829. oldAlarm=NULL;
  830. }
  831. m_NewUploading_alarm.push_back(alarm);//加入队列
  832. LeaveCriticalSection(&cs);
  833. }
  834. //取出消息从新告警列表
  835. alarm_t* AlarmFSM::removeAlarm()
  836. {//加锁,取出先进的队列日志,注意释放内存空间
  837. alarm_t* dAlarm = NULL;
  838. EnterCriticalSection(&cs);
  839. if(m_NewUploading_alarm.empty()){
  840. //return NULL;
  841. }else{
  842. dAlarm = (alarm_t*)(*m_NewUploading_alarm.begin());//集合首个元素
  843. m_NewUploading_alarm.erase(m_NewUploading_alarm.begin());//集合删除首元素
  844. }
  845. LeaveCriticalSection(&cs);
  846. return dAlarm;//返回
  847. }
  848. //关闭连接
  849. void AlarmFSM::closeClientConn()
  850. {
  851. if(m_pConnection!=NULL){
  852. m_pConnection->Close();
  853. m_pConnection->DecRefCount();
  854. m_pConnection = NULL;
  855. }
  856. }
  857. //发送新告警信息
  858. bool AlarmFSM::SendAlarm(){
  859. alarm_t*alarm = removeAlarm();
  860. if (alarm == NULL)
  861. {
  862. //Dbg("get alarm error");
  863. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("SendAlarm")("get alarm error");
  864. return false;
  865. }
  866. m_iSend++;//发送计数
  867. m_pConnection->SendAlarm(alarm);
  868. free(alarm->Description);
  869. alarm->Description=NULL;
  870. free(alarm->EntityName);
  871. alarm->EntityName=NULL;
  872. free(alarm->SN);
  873. alarm->SN=NULL;
  874. free(alarm->warnTime);
  875. alarm->warnTime=NULL;
  876. delete alarm;
  877. alarm=NULL;
  878. return true;
  879. }
  880. void AlarmFSM::HttpsLogCallBack(const char* logtxt)
  881. {
  882. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("HttpsLogCallBack")("http dbg: %s",logtxt);
  883. }
  884. std::string AlarmFSM::alarmJson()
  885. {
  886. vector<alarm_t*> unSendAlarm;
  887. string jsonStr ="";
  888. EnterCriticalSection(&cs);
  889. alarm_t* dAlarm = NULL;
  890. for(int i = 0 ;i<16;i++){
  891. if(m_NewUploading_alarm.empty()){
  892. break;
  893. }else{
  894. dAlarm = (alarm_t*)(*m_NewUploading_alarm.begin());//集合首个元素
  895. m_NewUploading_alarm.erase(m_NewUploading_alarm.begin());//集合删除首元素
  896. unSendAlarm.push_back(dAlarm);
  897. }
  898. }
  899. LeaveCriticalSection(&cs);
  900. if(unSendAlarm.size()==0){
  901. return jsonStr;//无发送直接返回空
  902. }
  903. Json::Value root;
  904. Json::Value arraylist;//消息列表
  905. Json::FastWriter fw;//写入对象
  906. int i = 0;
  907. CSystemStaticInfo si;
  908. {
  909. m_pEntity->GetFunction()->GetSystemStaticInfo(si);
  910. }
  911. for(auto iter = unSendAlarm.begin(); iter!=unSendAlarm.end(); iter++){
  912. Json::Value rootAlarm;
  913. Json::FastWriter fwAlarm;//写入对象
  914. alarm_t* alarmt = *iter;
  915. m_uuid =CUUID::Create(m_uuid);
  916. rootAlarm["IDKey"] = m_uuid.ToString().GetData();
  917. rootAlarm["BranchNo"] = m_branchNo.GetData();
  918. rootAlarm["TerminalNo"] = si.strTerminalID.GetData();
  919. rootAlarm["EntityName"] = alarmt->EntityName;
  920. rootAlarm["Item"] = CSimpleStringA::Format("%016llx", alarmt->Item).SubString(8,8).GetData();//16进制取后面八位
  921. char Level[2];
  922. sprintf(Level, "%c", alarmt->Level);
  923. rootAlarm["Level"] = CSimpleStringA::Format("%s",Level).GetData();
  924. rootAlarm["LifeID"] = CSimpleStringA::Format("%016llx", alarmt->LifeID).SubString(8,8).GetData();//16进制取后面八位
  925. rootAlarm["SN"] = alarmt->SN;
  926. rootAlarm["UserCode"] = CSimpleStringA::Format("%016llx", alarmt->UserCode).SubString(8,8).GetData();//16进制取后面八位
  927. rootAlarm["WarningTime"] = CSimpleStringA::Format("%s", alarmt->warnTime).GetData();
  928. rootAlarm["Description"] = CSimpleStringA::Format("%s", alarmt->Description).GetData();
  929. //增加终端版本号字段
  930. rootAlarm["VersionNo"] = si.InstallVersion.ToString().GetData();
  931. string alarmJsonStr = fwAlarm.write(rootAlarm);
  932. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("alarmJsonStr=%s",alarmJsonStr.c_str());
  933. arraylist[i]=alarmJsonStr.c_str();
  934. i++;
  935. }
  936. root["message_list"] = arraylist;
  937. root["topic"]=m_topic.GetData();
  938. jsonStr = fw.write(root);
  939. m_eachSum = i;
  940. //清理内存
  941. while(true){
  942. if(unSendAlarm.empty()){
  943. break;
  944. }else{
  945. dAlarm = (alarm_t*)(*unSendAlarm.begin());//集合首个元素
  946. unSendAlarm.erase(unSendAlarm.begin());//集合删除首元素
  947. }
  948. free(dAlarm->Description);
  949. dAlarm->Description=NULL;
  950. free(dAlarm->EntityName);
  951. dAlarm->EntityName=NULL;
  952. free(dAlarm->SN);
  953. dAlarm->SN=NULL;
  954. free(dAlarm->warnTime);
  955. dAlarm->warnTime=NULL;
  956. delete dAlarm;
  957. dAlarm=NULL;
  958. }
  959. return jsonStr;
  960. }
  961. #ifdef RVC_OS_WIN
  962. #else
  963. int AlarmFSM::HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  964. {
  965. char* tmpStr = *strBuf;
  966. int count = 0;
  967. for (int i = 0; i < len; ++i)
  968. {
  969. sprintf(tmpStr + count, "%0.2x", hexBuf[i]);
  970. Dbg("strBut:%s", tmpStr);
  971. count += 2;
  972. }
  973. return 0;
  974. }
  975. #endif