AlarmFSM.cpp 32 KB

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