NoticeQueue.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. //////////////////////////////////////////////////////////////////////////////
  2. ////// //////
  3. ////// 文 件: NoticeQueue.cpp //////
  4. ////// 作 者: sailing //////
  5. ////// 创建时间: 2008-02-23 //////
  6. ////// 说 明: 1、主要语音通知 //////
  7. ////// 2、语音查询待完善 //////
  8. ////// 修改时间: //////
  9. ////// 修改说明: //////
  10. ////// //////
  11. //////////////////////////////////////////////////////////////////////////////
  12. #pragma once
  13. #include "stdafx.h"
  14. #include "NoticeQueue.h"
  15. #include "Global.h"
  16. #include "SysLib.h"
  17. #include "shlwapi.h"
  18. #define NOTICE_DELAY 35 //通知延时
  19. int g_nAlarmIndex=0;//报警索引,为了在IDE上面确认而设
  20. // 发送通知
  21. typedef struct _SEND_NOTICE
  22. {
  23. char szDevUID[20]; // 设备Uid
  24. int nVarID; // 变量ID
  25. char szTel[MAX_TEL_LENGTH + 1]; // 电话号码
  26. char szContect[MAX_NOTICE_CONTENT_LEN + 1]; // 内容
  27. int nAlarmIndex; // 报警索引
  28. SYSTEMTIME time;
  29. }SEND_NOTICE, *PSEND_NOTICE;
  30. typedef list<SEND_NOTICE> LIST_SEND_NOTICE;
  31. LIST_SEND_NOTICE g_listSendNotice;
  32. LIST_SEND_NOTICE g_listSendSms;
  33. HANDLE g_hSendNoticeThread = NULL;
  34. HANDLE g_hSendSmsThread = NULL;
  35. CRITICAL_SECTION g_csVarNotice; // 临界区对ProcessVarNotice过程全局变量进行保护
  36. CRITICAL_SECTION g_csSendNotice; // 临界区在发送线程里对全局变量进行保护
  37. HANDLE g_hDeleteVarSem; // 所有读变量list, 都要等这个信号量,保证变量出队列成功后,才能读
  38. // 删除所有变量
  39. void DeleteAllSendNoticeList();
  40. void DeleteAllSendSmsList();
  41. // 添加变量到語音list
  42. void InsertSendNoticeList(char *pDevUid, int nVarID, char *pTel, char *pContent,int nAlarmIndex);
  43. //IDE 确认后从語音list删除
  44. int DeleteSendNoticeList( char *pDevUid, int nVarID,int iAlarmIndex );
  45. // 添加变量到短信list
  46. void InsertSendSmsList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex);
  47. //IDE 确认后从短信list删除
  48. int DeleteSendSmsList( char *pDevUid, int nVarID,int iAlarmIndex );
  49. static DWORD WINAPI SendNoticeThread(LPVOID lpParameter);
  50. static DWORD WINAPI SendSmsThread(LPVOID lpParameter);
  51. // 获取报警状态ID对应的意义,用来组织报警内容 for Jesse 091119
  52. INT GetAlarmStatusDesc(char chDevUid[20], int nVarID, int nAlarmStatus, char *pStatusDesc);
  53. int InitNotice(void)
  54. {
  55. // 初始化远程报警通知
  56. if( g_nSendNotice )
  57. {
  58. g_hNoticeLibModule = NULL;
  59. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  60. if( g_nNoticeCardType == 1 )
  61. {
  62. wsprintf(strFile, "%s\\Dll\\Voice.dll", g_strDirectory);
  63. g_hNoticeLibModule = ::LoadLibrary(strFile);
  64. if (NULL != g_hNoticeLibModule)
  65. {
  66. pStoneUDllRegisterVoice = (STONEU_DLLRegisterVoice)::GetProcAddress(g_hNoticeLibModule, "STONEU_DLLRegisterVoice");
  67. pStoneUDllUnRegisterVoice = (STONEU_DLLUnRegisterVoice)::GetProcAddress(g_hNoticeLibModule, "STONEU_DLLUnRegisterVoice");
  68. pStoneUDllAllocateChannel = (STONEU_DLLAllocateChannel)::GetProcAddress(g_hNoticeLibModule, "STONEU_DLLAllocateChannel");
  69. }
  70. if( ( NULL != g_hNoticeLibModule ) && ( NULL != pStoneUDllRegisterVoice ) )
  71. {
  72. int nRet = pStoneUDllRegisterVoice(g_strDirectory, g_nTTSType);
  73. if( nRet != 0 )
  74. {
  75. AfxFreeLibrary( g_hNoticeLibModule );
  76. g_hNoticeLibModule = NULL;
  77. return 1;
  78. }
  79. }
  80. else
  81. {
  82. AfxFreeLibrary( g_hNoticeLibModule );
  83. g_hNoticeLibModule = NULL;
  84. }
  85. }
  86. else if( g_nNoticeCardType == 2 )
  87. {
  88. wsprintf(strFile, "%s\\Dll\\UsbVoice.dll", g_strDirectory);
  89. g_hNoticeLibModule = ::LoadLibrary(strFile);
  90. if( NULL != g_hNoticeLibModule )
  91. {
  92. pStoneUDllRegisterVoice = (STONEU_DLLRegisterVoice)::GetProcAddress(g_hNoticeLibModule, "STONEU_DLLRegisterVoice");
  93. pStoneUDllUnRegisterVoice = (STONEU_DLLUnRegisterVoice)::GetProcAddress(g_hNoticeLibModule, "STONEU_DLLUnRegisterVoice");
  94. pStoneUDllAllocateChannel = (STONEU_DLLAllocateChannel)::GetProcAddress(g_hNoticeLibModule, "STONEU_DLLAllocateChannel");
  95. }
  96. if( ( NULL != g_hNoticeLibModule ) && ( NULL != pStoneUDllRegisterVoice ) )
  97. {
  98. int nRet = pStoneUDllRegisterVoice(g_strDirectory, g_nTTSType);
  99. if( nRet != 0 )
  100. {
  101. AfxFreeLibrary( g_hNoticeLibModule );
  102. g_hNoticeLibModule = NULL;
  103. return 1;
  104. }
  105. }
  106. else
  107. {
  108. AfxFreeLibrary( g_hNoticeLibModule );
  109. g_hNoticeLibModule = NULL;
  110. }
  111. }
  112. DWORD dwThreadId;
  113. MTVERIFY( g_hSendNoticeThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendNoticeThread,
  114. NULL, 0, &dwThreadId) );
  115. //初始化TTS
  116. hTTSModule = NULL;
  117. wsprintf(strFile, "%s\\Dll\\TTSDll.dll", g_strDirectory);
  118. hTTSModule = ::LoadLibrary(strFile);
  119. if (NULL != hTTSModule)
  120. {
  121. pStoneUDllRegisterTTS = (MySTONEU_DLLRegisterTTS)::GetProcAddress(hTTSModule, "STONEU_DLLRegisterTTS");
  122. pStoneUDllUnRegisterTTS = (MySTONEU_DLLUnRegisterTTS)::GetProcAddress(hTTSModule, "STONEU_DLLUnRegisterTTS");
  123. pStoneUDLLTxtToWav = (MySTONEU_DLLTxtToWav)::GetProcAddress(hTTSModule, "STONEU_DLLTxtToWav");
  124. }
  125. if( ( NULL != hTTSModule ) && ( NULL != pStoneUDllRegisterTTS ) )
  126. {
  127. if( !pStoneUDllRegisterTTS() )
  128. {
  129. FreeLibrary( hTTSModule );
  130. hTTSModule = NULL;
  131. }
  132. }
  133. else
  134. {
  135. FreeLibrary( hTTSModule );
  136. hTTSModule = NULL;
  137. }
  138. }
  139. //MTVERIFY( g_hDeleteVarSem = CreateEvent(NULL, TRUE, TRUE, "DeleteVarList") );
  140. //InitializeCriticalSection( &g_csVarNotice );
  141. //InitializeCriticalSection( &g_csSendNotice );
  142. return 0;
  143. }
  144. void UnInitNotice(void)
  145. {
  146. if( g_hSendNoticeThread )
  147. {
  148. //MTVERIFY( SetEvent( g_hDeleteVarSem ) );
  149. MTVERIFY( WaitForSingleObject( g_hSendNoticeThread, INFINITE ) != WAIT_FAILED );
  150. MTVERIFY( CloseHandle(g_hSendNoticeThread) );
  151. g_hSendNoticeThread = NULL;
  152. }
  153. //DeleteCriticalSection( &g_csVarNotice );
  154. //DeleteCriticalSection( &g_csSendNotice );
  155. //
  156. //if( g_hDeleteVarSem )
  157. //{
  158. // MTVERIFY( CloseHandle( g_hDeleteVarSem) );
  159. // g_hDeleteVarSem = NULL;
  160. //}
  161. if( g_nSendNotice )
  162. {
  163. if( ( NULL != g_hNoticeLibModule ) && ( NULL != pStoneUDllUnRegisterVoice ) )
  164. pStoneUDllUnRegisterVoice();
  165. if( ( NULL != hTTSModule ) && ( NULL != pStoneUDllUnRegisterTTS ) )
  166. pStoneUDllUnRegisterTTS();
  167. }
  168. DeleteAllSendSmsList();
  169. DeleteAllSendNoticeList();
  170. DeleteAllVarNoticeList();
  171. if (g_hNoticeLibModule != NULL)
  172. {
  173. ::FreeLibrary(g_hNoticeLibModule);
  174. g_hNoticeLibModule = NULL;
  175. }
  176. if (hTTSModule != NULL)
  177. {
  178. FreeLibrary(hTTSModule);
  179. hTTSModule = NULL;
  180. }
  181. }
  182. int InitEmail(char *pSmtpServer, // Smtp服务器
  183. int nSmtpPort, // Smtp端口
  184. int nIsNeed, // 是否需要smtp身份验证
  185. char *pUserAcc, // Email帐号
  186. char *pUserPwd, // Email密码
  187. char *pFromEmailAddr, // 发送人Email地址
  188. int nTimeOut // 超时
  189. )
  190. {
  191. g_hEmailLibModule = NULL;
  192. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  193. wsprintf(strFile, "%s\\Dll\\Email.dll", g_strDirectory);
  194. g_hEmailLibModule = AfxLoadLibrary(strFile);
  195. if (NULL != g_hEmailLibModule)
  196. {
  197. pEmailDllInit = (EMAIL_DLLInit)::GetProcAddress(g_hEmailLibModule, "EMAIL_DLLInit");
  198. pEmailDllUnInit = (EMAIL_DLLUnInit)::GetProcAddress(g_hEmailLibModule, "EMAIL_DLLUnInit");
  199. pEmailDllSendEmail = (EMAIL_DLLSendEmail)::GetProcAddress(g_hEmailLibModule, "EMAIL_DLLSendEmail");
  200. }
  201. if( ( NULL != g_hEmailLibModule ) && ( NULL != pEmailDllInit ) )
  202. {
  203. int nRet = pEmailDllInit( g_strDirectory,
  204. pSmtpServer,
  205. nSmtpPort,
  206. nIsNeed,
  207. pUserAcc,
  208. pUserPwd,
  209. pFromEmailAddr,
  210. nTimeOut
  211. );
  212. if( nRet != 0 )
  213. {
  214. AfxFreeLibrary( g_hEmailLibModule );
  215. g_hEmailLibModule = NULL;
  216. return 1;
  217. }
  218. }
  219. else
  220. {
  221. AfxFreeLibrary( g_hEmailLibModule );
  222. g_hEmailLibModule = NULL;
  223. }
  224. return 0;
  225. }
  226. int UnInitEmail()
  227. {
  228. if( NULL != g_hEmailLibModule && NULL != pEmailDllUnInit )
  229. {
  230. pEmailDllUnInit();
  231. AfxFreeLibrary( g_hEmailLibModule );
  232. g_hEmailLibModule = NULL;
  233. }
  234. return 0;
  235. }
  236. int InitSms(int nAddr, // 保留,因为短信猫是串口232没有地址
  237. int nCommPort, // 串口号
  238. int nRate, // 波特率
  239. int nDataBit, // 数据位
  240. int nStopBit, // 停止位
  241. int nParity, // 校验位
  242. int nInterval, // 间隔时间
  243. int nNeedLanguageTrans, // 是否需要语言转换
  244. int nMaxChar, // 短信猫一条短信最大支持的字符个数
  245. int nMakeCall)
  246. {
  247. g_hSmsLibModule = NULL;
  248. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  249. wsprintf(strFile, "%s\\Dll\\sms.dll", g_strDirectory);
  250. g_hSmsLibModule = AfxLoadLibrary(strFile);
  251. if (NULL != g_hSmsLibModule)
  252. {
  253. pSmsDllInit = (SMS_DLLInit)::GetProcAddress(g_hSmsLibModule, "SMS_DLLInit");
  254. pSmsDllUnInit = (SMS_DLLUnInit)::GetProcAddress(g_hSmsLibModule, "SMS_DLLUnInit");
  255. pSmsDllSendSms = (SMS_DLLSendSms)::GetProcAddress(g_hSmsLibModule, "SMS_DLLSendSms");
  256. pSmsDllGetCSQ = (SMS_DLLGetCSQ)::GetProcAddress(g_hSmsLibModule, "SMS_DLLGetCSQ");
  257. pSmsDllSetCallBack = (SMS_DLLSetCallBack)::GetProcAddress(g_hSmsLibModule, "SMS_DLLSetCallBack");
  258. }
  259. if( ( NULL != g_hSmsLibModule ) && ( NULL != pSmsDllInit ) )
  260. {
  261. int nRet = pSmsDllInit(g_strDirectory,
  262. nAddr,
  263. nCommPort,
  264. nRate,
  265. nDataBit,
  266. nStopBit,
  267. nParity,
  268. nInterval,
  269. nNeedLanguageTrans,
  270. nMaxChar,
  271. nMakeCall
  272. );
  273. if( nRet != 0 )
  274. {
  275. pSmsDllInit = NULL;
  276. pSmsDllUnInit = NULL;
  277. pSmsDllSendSms = NULL;
  278. pSmsDllGetCSQ = NULL;
  279. AfxFreeLibrary( g_hSmsLibModule );
  280. g_hSmsLibModule = NULL;
  281. return 1;
  282. }
  283. }
  284. else
  285. {
  286. pSmsDllInit = NULL;
  287. pSmsDllUnInit = NULL;
  288. pSmsDllSendSms = NULL;
  289. pSmsDllGetCSQ = NULL;
  290. AfxFreeLibrary( g_hSmsLibModule );
  291. g_hSmsLibModule = NULL;
  292. }
  293. DWORD dwThreadId2;
  294. MTVERIFY( g_hSendSmsThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendSmsThread,
  295. NULL, 0, &dwThreadId2) );
  296. return 0;
  297. }
  298. int UnInitSms()
  299. {
  300. if( g_hSendSmsThread )
  301. {
  302. MTVERIFY( WaitForSingleObject( g_hSendSmsThread, INFINITE ) != WAIT_FAILED );
  303. MTVERIFY( CloseHandle(g_hSendSmsThread) );
  304. g_hSendSmsThread = NULL;
  305. }
  306. if( NULL != g_hSmsLibModule && NULL != pSmsDllUnInit )
  307. {
  308. pSmsDllUnInit();
  309. AfxFreeLibrary( g_hSmsLibModule );
  310. g_hSmsLibModule = NULL;
  311. }
  312. return 0;
  313. }
  314. ///////////////////////////////////////////////////////////////////////////////////////////////
  315. // 把短信信息写入TXT,让短信模块读取
  316. INT WriteToTxt(CHAR *pDevUid, int nVarID, CHAR *pContent,int iAlarmIndex )
  317. {
  318. CString strMobileTel;
  319. char szResultMsg[MAX_MSG_LENGTH + 1] = {0};
  320. int nResult = -1;
  321. memset(szResultMsg, 0, sizeof(szResultMsg));
  322. CString sPathName;
  323. sPathName.Format( "%s\\Log\\SupportSmsPlatform.txt",g_strDirectory );
  324. CString strTemp;
  325. CStdioFile MyFile;
  326. if(!MyFile.Open(sPathName,CFile::modeWrite))
  327. MyFile.Open(sPathName,CFile::modeCreate|CFile::modeWrite);
  328. for( int i = 0; i < (int)g_vtUserRoleInfo.size(); i++ )
  329. {
  330. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  331. {
  332. for( int j = 0; j < (int)g_vtUserRoleInfo[i].vtUserInfo.size(); j++ )
  333. {
  334. strMobileTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szMobileTel);
  335. if( strMobileTel.GetLength()!=11 ) continue; //判断是不是手机号码 只根据长度
  336. if( strMobileTel.Compare("") )
  337. {
  338. CTime time = CTime::GetCurrentTime();
  339. CString strTime;
  340. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  341. strTemp.Format("%d***%s***%s***%s\n",iAlarmIndex,strTime,strMobileTel,pContent);
  342. MyFile.SeekToEnd();
  343. MyFile.WriteString(strTemp);
  344. }
  345. }
  346. }
  347. }
  348. MyFile.Close();
  349. return 0;
  350. }
  351. ///////////////////////////////////////////////////////////////////////////////////////////////
  352. ///////////////////////////////////////////////////////////////////////////////////////////////
  353. ////// 函数名称:SendNotice ///////
  354. ////// 函数说明:开始语音通知 ///////
  355. ////// 参数列表:strTel -- 待通知的电话号码 ///////
  356. ////// strContent -- 短信内容 ///////
  357. ////// 作 者:sailing ///////
  358. ////// 创者时间:2009-02-23 ///////
  359. ///////////////////////////////////////////////////////////////////////////////////////////////
  360. INT SendNotice(CHAR *pUid, int nVarID,int nAlarmIndex, CHAR *pTel, CHAR *pContent)
  361. {
  362. //如果是短信确认关闭报警通知则退出
  363. if( !g_bAlarmNoticeFlag )
  364. return -1;
  365. int nRet = 0;
  366. CString str, strMsg;
  367. str = CString(" ") + pContent; // g_strHintAlarmWelcome +
  368. str = str + g_strHintVoiceEnd; //" 重新请按1,确认请按2,结束请直接挂机";
  369. if( ( NULL != g_hNoticeLibModule ) && (NULL != pStoneUDllAllocateChannel) )
  370. {
  371. //strMsg.Format("开始发送语音通知!", pTel);
  372. //AddToPrintQueue( MSG_OK, MSG_NOTICE_MD, (char*)(LPCTSTR)strMsg, strMsg.GetLength() );
  373. int nResult = pStoneUDllAllocateChannel( pTel, g_strNoticeTelPre, pUid, nVarID,nAlarmIndex, (char *)(LPCTSTR)str );
  374. nRet = nResult;
  375. if( nRet != CALL_LOST )
  376. {
  377. CTime time = CTime::GetCurrentTime();
  378. CString strTime;
  379. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  380. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  381. pTel,
  382. "",
  383. (char *)(LPCTSTR)strTime,
  384. (char *)(LPCTSTR)pContent,
  385. "");
  386. }
  387. }
  388. return nRet;
  389. }
  390. // 发送Sms
  391. INT SendSms(CHAR *pDevUid, int nVarID, CHAR *pContent)
  392. {
  393. CString strMobileTel;
  394. char szResultMsg[MAX_MSG_LENGTH + 1] = {0};
  395. int nResult = -1;
  396. memset(szResultMsg, 0, sizeof(szResultMsg));
  397. //如果是短信确认关闭报警通知则退出
  398. if( !g_bAlarmNoticeFlag )
  399. return 0;
  400. //LOG4C((LOG_NOTICE, "g_vtUserRoleInfo.size = %d", g_vtUserRoleInfo.size()));
  401. for( int i = 0; i < (int)g_vtUserRoleInfo.size(); i++ )
  402. {
  403. //LOG4C((LOG_NOTICE, "g_vtUserRoleInfo[i].szDevUID = %s, pDevUid = %s", g_vtUserRoleInfo[i].szDevUID, pDevUid));
  404. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  405. {
  406. //LOG4C((LOG_NOTICE, "g_vtUserRoleInfo[i].vtUserInfo.size = %d", g_vtUserRoleInfo[i].vtUserInfo.size()));
  407. for( int j = 0; j < (int)g_vtUserRoleInfo[i].vtUserInfo.size(); j++ )
  408. {
  409. strMobileTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szMobileTel);
  410. //LOG4C((LOG_NOTICE, "strMobileTel = %s", strMobileTel));
  411. if( strMobileTel.Compare("") )
  412. {
  413. CTime time = CTime::GetCurrentTime();
  414. CString strTime;
  415. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  416. // 开始通知
  417. if( pSmsDllSendSms != NULL && g_bAlarmNoticeFlag )
  418. {
  419. //LogEvent("发送短信,号码:%s, 内容:%s", strTel, pContent);
  420. if( strMobileTel.GetLength()!=11 ) continue; //判断是不是手机号码 只根据长度
  421. //LOG4C((LOG_NOTICE, "pSmsDllSendSms %s,%s",strMobileTel, pContent ));
  422. nResult = pSmsDllSendSms((char *)(LPCTSTR)strMobileTel, pContent, szResultMsg);
  423. //LOG4C((LOG_NOTICE, "pSmsDllSendSms nResult = %d",nResult ));
  424. if( nResult != 0 ) // 发送失败
  425. {
  426. int nNowData=0;
  427. if( !pSmsDllGetCSQ( 15,30,nNowData ) )//短信猫信号弱
  428. {
  429. CDBInterface::GetInstancePtr()->InsertSmsRecord(
  430. "",
  431. "",
  432. (char *)(LPCTSTR)strTime,
  433. "send sms lost,Because the signal is too weak",
  434. "");
  435. LOG4C((LOG_NOTICE, "send sms lost,Because the signal is too weak" ));
  436. return 0;
  437. }
  438. LOG4C((LOG_NOTICE, "send sms lost, tel: %s, content: %s, code: %s", strMobileTel, pContent, szResultMsg));
  439. // 发送失败,重试十次,如果还是失败,则放弃
  440. for( int iReSend = 0; iReSend < 20; iReSend++ )
  441. {
  442. nResult = pSmsDllSendSms((char *)(LPCTSTR)strMobileTel, pContent, szResultMsg);
  443. if( nResult == 0 )
  444. {
  445. CString strTemp = "";
  446. strTemp.Format("The last may be sent lost, again sent: %s", pContent);
  447. LOG4C((LOG_NOTICE, "send sms success, tel: %s, content: %s", strMobileTel, pContent));
  448. CDBInterface::GetInstancePtr()->InsertSmsRecord(
  449. (char *)(LPCTSTR)strMobileTel,
  450. "",
  451. (char *)(LPCTSTR)strTime,
  452. (char *)(LPCTSTR)strTemp,
  453. "");
  454. break;
  455. }
  456. else
  457. {
  458. LOG4C((LOG_NOTICE, "send sms lost, tel: %s, content: %s", strMobileTel, pContent));
  459. }
  460. Sleep( 100 );
  461. }
  462. }
  463. else
  464. {
  465. CDBInterface::GetInstancePtr()->InsertSmsRecord(
  466. (char *)(LPCTSTR)strMobileTel,
  467. "",
  468. (char *)(LPCTSTR)strTime,
  469. pContent,
  470. "");
  471. LOG4C((LOG_NOTICE, "send sms success, tel: %s, content: %s, code: %s", strMobileTel, pContent, szResultMsg));
  472. }
  473. }
  474. }
  475. }
  476. }
  477. }
  478. return 0;
  479. }
  480. // 发送Email
  481. INT SendEmail(CHAR *pDevUid, int nVarID, CHAR *pContent)
  482. {
  483. CString strEmail;
  484. char szResultMsg[MAX_MSG_LENGTH + 1] = {0};
  485. int nResult = -1;
  486. memset( szResultMsg, 0, sizeof(szResultMsg) );
  487. for( int i = 0; i < (int)g_vtUserRoleInfo.size(); i++ )
  488. {
  489. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  490. {
  491. for( int j = 0; j < (int)g_vtUserRoleInfo[i].vtUserInfo.size(); j++ )
  492. {
  493. strEmail = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szEmail);
  494. if( strEmail.Compare("") )
  495. {
  496. CTime time = CTime::GetCurrentTime();
  497. CString strTime;
  498. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  499. if( pEmailDllSendEmail )
  500. {
  501. //LOG4C((LOG_NOTICE, "start send email"));
  502. // 开始通知
  503. nResult = pEmailDllSendEmail(
  504. (char *)(LPCSTR)strEmail,
  505. g_strEmailSubject,
  506. pContent,
  507. (char *)(LPCTSTR)strTime,
  508. szResultMsg );
  509. if( nResult != 0 ) //发送失败
  510. {
  511. LOG4C((LOG_NOTICE, "Email to %s, Contect:%s send lost: %s", strEmail, pContent, szResultMsg));
  512. }
  513. else
  514. {
  515. CDBInterface::GetInstancePtr()->InsertEmailRecord(
  516. (char *)(LPCTSTR)strEmail,
  517. "",
  518. (char *)(LPCTSTR)strTime,
  519. pContent,
  520. "");
  521. LOG4C((LOG_NOTICE, "Email to %s, Contect:%s send success: %s", strEmail, pContent, szResultMsg));
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. return 0;
  529. }
  530. // 添加有管理某个设备所有用户的电话号码和手机号码
  531. void InsertUserTel(VAR_NOTICE *pVarNotice)
  532. {
  533. CString strTel;
  534. int nDevSize = (int)g_vtUserRoleInfo.size();
  535. for( int i = 0; i < nDevSize; i++ )
  536. {
  537. //TRACE("设备UID=%s\r\n", g_vtUserRoleInfo[i].szDevUID);
  538. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pVarNotice->szDevUID) == 0 )
  539. {
  540. int nUserSize = (int)g_vtUserRoleInfo[i].vtUserInfo.size();
  541. for( int j = 0; j < nUserSize; j++ )
  542. {
  543. // 加入电话号码
  544. strTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szTel);
  545. if( strTel.Compare("") )
  546. {
  547. pVarNotice->listTel.insert(pVarNotice->listTel.end(), strTel);
  548. //TRACE1("电话号码:%s\r\n", strTel);
  549. }
  550. //// 加入手机号码
  551. //strTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szMobileTel);
  552. //if( strTel.Compare("") )
  553. //{
  554. // pVarNotice->listTel.insert(pVarNotice->listTel.end(), strTel);
  555. // //TRACE1("手机号码:%s\r\n", strTel);
  556. //}
  557. }
  558. }
  559. }
  560. }
  561. // 更新要发送对象的手机号码
  562. INT UpdateActiveTel( CHAR *pDevUid, CHAR *pStrTel )
  563. {
  564. int nSize = g_vtUserRoleInfo.size();
  565. for( int i = 0; i < nSize; i++ )
  566. {
  567. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  568. {
  569. //g_vtUserTelInfo[i].strActiveTel = pStrTel;
  570. }
  571. }
  572. return 0;
  573. }
  574. // 报警状态恢复正常处理
  575. void ProcessVarStatusReturnNormal(char *pDevUid,
  576. int nVarID,
  577. int nAlarmStatus,
  578. int nNormalStatus,
  579. int nIdenfityTime,
  580. int nReDetectTime,
  581. int nNormalIsNotice,
  582. char *pDevName,
  583. char *pVarDesc,
  584. double dbData,
  585. char *pContent,
  586. VAR_NOTICE *pVarNotice,
  587. int nUpperLimit,
  588. int nLowerLimit )
  589. {
  590. if( nNormalIsNotice ) // 恢复正常触发远程报警
  591. {
  592. if( g_nSendNotice )
  593. {
  594. LIST_TEL::iterator it;
  595. CString *pStrTel;
  596. for( it = pVarNotice->listTel.begin(); it != pVarNotice->listTel.end(); it++ )
  597. {
  598. pStrTel = &(*it);
  599. // 加入语音发送list
  600. EnterCriticalSection( &g_csVarNotice );
  601. //LOG4C((LOG_NOTICE, "拔打恢复正常电话,号码:%s, 内容:%s", (char *)(LPCTSTR)*pStrTel, pContent));
  602. InsertSendNoticeList(pDevUid, nVarID, (char *)(LPCTSTR)*pStrTel, pContent,g_nAlarmIndex);
  603. //pVarNotice->listTel.erase(it);
  604. LeaveCriticalSection( &g_csVarNotice );
  605. }
  606. }
  607. //发送恢复正常信息给客户端
  608. g_pServerSocket[1]->AutoSendAlarmDataToClient( pDevUid,nVarID,1,pContent,dbData,nUpperLimit,nLowerLimit,nNormalStatus,nAlarmStatus,g_nAlarmIndex );
  609. int nDeviceIndex = -1, nVarIndex = -1;
  610. BOOL bFind = FindVar(pDevUid, nVarID, nDeviceIndex, nVarIndex);
  611. if( bFind == TRUE )
  612. {
  613. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  614. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  615. EnterCriticalSection( &g_csVarNotice );
  616. pBaseVar->m_nAlarmNumber = 0;
  617. pVarNotice->nLastStatus = nAlarmStatus;
  618. pVarNotice->nCallTime = 0;
  619. pVarNotice->nSmsTime = 0;
  620. LeaveCriticalSection( &g_csVarNotice );
  621. }
  622. if( g_nSendSms==1 )
  623. {
  624. // 发送短信
  625. InsertSendSmsList( pDevUid, nVarID, pContent,g_nAlarmIndex );
  626. //SendSms(pDevUid, nVarID, pContent);
  627. }
  628. else if( g_nSendSms==2 )//支持短信平台
  629. {
  630. WriteToTxt(pDevUid, nVarID, pContent,g_nAlarmIndex);
  631. }
  632. if( g_nSendEmail )
  633. {
  634. // 发送Email
  635. SendEmail(pDevUid, nVarID, pContent);
  636. }
  637. }
  638. g_nAlarmIndex++;
  639. if( g_nAlarmIndex>=1000 )
  640. g_nAlarmIndex = 0;
  641. CString strTime;
  642. CTime tm;
  643. tm = CTime::GetCurrentTime();
  644. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  645. CDBInterface::GetInstancePtr()->InsertWarningRecord(pDevName, pVarDesc, (char *)(LPCTSTR)strTime, nAlarmStatus, dbData, "", pContent);
  646. }
  647. void ProcessVarAlarm(char *pDevUid,
  648. int nVarID,
  649. int nAlarmStatus,
  650. int nNormalStatus,
  651. int nIdenfityTime,
  652. int nReDetectTime,
  653. int nNormalIsNotice,
  654. char *pDevName,
  655. char *pVarDesc,
  656. double dbData,
  657. char *pContent,
  658. VAR_NOTICE *pVarNotice,
  659. int nUpperLimit,
  660. int nLowerLimit )
  661. {
  662. if( pVarNotice->nCallTime > g_nNoticeCallTimes - 1 ) // 第一次拔打没算次数, 所以这里减1
  663. {
  664. if( pVarNotice->dwReDetectTick == 0 )
  665. {
  666. EnterCriticalSection( &g_csVarNotice );
  667. pVarNotice->dwReDetectTick = GetTickCount();
  668. LeaveCriticalSection( &g_csVarNotice );
  669. }
  670. // 超过重新检测等待时间,从list中删除该变量
  671. if( GetTickCount() - pVarNotice->dwReDetectTick > (DWORD)(nReDetectTime * 1000 * 60) )
  672. {
  673. int nDeviceIndex = -1, nVarIndex = -1;
  674. BOOL bFind = FindVar(pDevUid, nVarID, nDeviceIndex, nVarIndex);
  675. if( bFind == TRUE )
  676. {
  677. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  678. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  679. EnterCriticalSection( &g_csVarNotice );
  680. pBaseVar->m_nAlarmNumber = 0;
  681. pBaseVar->m_nAlarmStatus = 0;
  682. LeaveCriticalSection( &g_csVarNotice );
  683. }
  684. // 从list中删除变量时,要保证其它地方不去取list变量
  685. EnterCriticalSection( &g_csVarNotice ); // ResetEvent时也不允许任何人进来,故此把临界区放在ResetEvent之前。
  686. ResetEvent( g_hDeleteVarSem );
  687. DeleteVarNoticeList(pDevUid, nVarID);
  688. SetEvent( g_hDeleteVarSem );
  689. LeaveCriticalSection( &g_csVarNotice );
  690. }
  691. return;
  692. }
  693. //语音通知
  694. if( g_nSendNotice )
  695. {
  696. LIST_TEL::iterator it;
  697. CString *pStrTel;
  698. for( it = pVarNotice->listTel.begin(); it != pVarNotice->listTel.end(); it++ )
  699. {
  700. pStrTel = &(*it);
  701. // 加入语音发送list
  702. EnterCriticalSection( &g_csVarNotice );
  703. //LOG4C((LOG_NOTICE, "拔打报警电话,号码:%s, 内容:%s", (char *)(LPCTSTR)*pStrTel, pContent));
  704. InsertSendNoticeList(pDevUid, nVarID, (char *)(LPCTSTR)*pStrTel, pContent,g_nAlarmIndex);
  705. LeaveCriticalSection( &g_csVarNotice );
  706. }
  707. }
  708. //发送报警信息给客户端
  709. g_pServerSocket[1]->AutoSendAlarmDataToClient( pDevUid,nVarID,0,pContent,dbData,nUpperLimit,nLowerLimit,nNormalStatus,nAlarmStatus,g_nAlarmIndex );
  710. EnterCriticalSection( &g_csVarNotice );
  711. pVarNotice->nCallTime++;
  712. LeaveCriticalSection( &g_csVarNotice );
  713. if( pVarNotice->nSmsTime > g_nSmsCallTimes - 1 ) // 第一次发送没算次数, 所以这里减1
  714. {
  715. if( g_nSendNotice == 0 )
  716. {
  717. if( pVarNotice->dwReDetectTick == 0 )
  718. {
  719. EnterCriticalSection( &g_csVarNotice );
  720. pVarNotice->dwReDetectTick = GetTickCount();
  721. LeaveCriticalSection( &g_csVarNotice );
  722. }
  723. // 超过重新检测等待时间,从list中删除该变量
  724. if( GetTickCount() - pVarNotice->dwReDetectTick > (DWORD)(nReDetectTime * 1000 * 60) )
  725. {
  726. int nDeviceIndex = -1, nVarIndex = -1;
  727. BOOL bFind = FindVar(pDevUid, nVarID, nDeviceIndex, nVarIndex);
  728. if( bFind == TRUE )
  729. {
  730. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  731. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  732. EnterCriticalSection( &g_csVarNotice );
  733. pBaseVar->m_nAlarmNumber = 0;
  734. pBaseVar->m_nAlarmStatus = 0;
  735. LeaveCriticalSection( &g_csVarNotice );
  736. }
  737. // 从list中删除变量时,要保证其它地方不去取list变量
  738. EnterCriticalSection( &g_csVarNotice ); // ResetEvent时也不允许任何人进来,故此把临界区放在ResetEvent之前。
  739. ResetEvent( g_hDeleteVarSem );
  740. DeleteVarNoticeList(pDevUid, nVarID);
  741. SetEvent( g_hDeleteVarSem );
  742. LeaveCriticalSection( &g_csVarNotice );
  743. }
  744. }
  745. return;
  746. }
  747. //LOG4C((LOG_NOTICE, "send sms:%s", pContent));
  748. //短信通知
  749. if( g_nSendSms==1 )
  750. {
  751. // 发送短信,所有用户一次性发完
  752. InsertSendSmsList( pDevUid, nVarID, pContent,g_nAlarmIndex );
  753. //SendSms(pDevUid, nVarID, pContent);
  754. }
  755. else if( g_nSendSms==2 )//支持短信平台
  756. {
  757. WriteToTxt(pDevUid, nVarID, pContent,g_nAlarmIndex);
  758. }
  759. //邮件通知
  760. if( g_nSendEmail )
  761. {
  762. // 发送Email,所有用户一次性发完
  763. SendEmail(pDevUid, nVarID, pContent);
  764. }
  765. EnterCriticalSection( &g_csVarNotice );
  766. pVarNotice->nSmsTime++;
  767. LeaveCriticalSection( &g_csVarNotice );
  768. g_nAlarmIndex++;
  769. if( g_nAlarmIndex>=1000 )
  770. g_nAlarmIndex = 0;
  771. CString strTime;
  772. CTime tm;
  773. tm = CTime::GetCurrentTime();
  774. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  775. CDBInterface::GetInstancePtr()->InsertWarningRecord(pDevName, pVarDesc, (char *)(LPCTSTR)strTime, nAlarmStatus, dbData, "", pContent);
  776. }
  777. // 处理变量远程报警通知
  778. int ProcessVarNotice(char *pDevUid,
  779. int nVarID,
  780. int nUpperLimit,
  781. int nLowerLimit,
  782. int nOffSet,
  783. int nAlarmStatus,
  784. int nNormalStatus,
  785. int nIdenfityTime,
  786. int nReDetectTime,
  787. int nNormalIsNotice,
  788. char *pDevName,
  789. char *pVarDesc,
  790. double dbData,
  791. char *pContent)
  792. {
  793. VAR_NOTICE *pVarNotice = NULL;
  794. pVarNotice = FindVarNoticeList(pDevUid, nVarID);
  795. if( NULL == pVarNotice )
  796. {
  797. if( nAlarmStatus != nNormalStatus )
  798. {
  799. CString strTemp = CString(pContent);
  800. if( strTemp.Find(g_strHintReturnNormal) != -1 )
  801. {
  802. return 0;
  803. }
  804. VAR_NOTICE tagVarNotice;
  805. strcpy(tagVarNotice.szDevUID, pDevUid);
  806. tagVarNotice.nVarID = nVarID;
  807. tagVarNotice.nResult = 0;
  808. tagVarNotice.nCallTime = 0;
  809. tagVarNotice.nSmsTime = 0;
  810. tagVarNotice.bQueueExist = false;
  811. tagVarNotice.dwIdentityTick = GetTickCount(); // 记录辨识时间
  812. tagVarNotice.dwIntervalTick = 0;
  813. tagVarNotice.dwReDetectTick = 0;
  814. tagVarNotice.nLastStatus = nAlarmStatus;
  815. tagVarNotice.bFirstDeal = true;
  816. // 添加有权限用户的电话号码和手机号码
  817. InsertUserTel(&tagVarNotice);
  818. tagVarNotice.nTelInex = 0;
  819. // 加入list,第一次加入不进行任何处理
  820. EnterCriticalSection( &g_csVarNotice );
  821. InsertVarNoticeList(tagVarNotice);
  822. LeaveCriticalSection( &g_csVarNotice );
  823. }
  824. }
  825. else
  826. {
  827. if( nAlarmStatus == nNormalStatus ) // 正常状态
  828. {
  829. // 防止报警恢复正常之后会一直打电话
  830. if( nAlarmStatus == pVarNotice->nLastStatus )
  831. {
  832. EnterCriticalSection( &g_csVarNotice );
  833. pVarNotice->nLastStatus = nAlarmStatus;
  834. LeaveCriticalSection( &g_csVarNotice );
  835. return 0;
  836. }
  837. EnterCriticalSection( &g_csVarNotice );
  838. pVarNotice->nLastStatus = nAlarmStatus;
  839. LeaveCriticalSection( &g_csVarNotice );
  840. //模拟量才有偏移量
  841. if( nOffSet > 0 )//当偏移量大于0时,判断当前值是否达到偏移量这个条件
  842. {
  843. CString strTemp = CString(pContent);
  844. //if( strTemp.Find(g_strHintReturnNormal) != -1 )//chn modify
  845. {
  846. if( dbData <= nUpperLimit - nOffSet || dbData >= nLowerLimit + nOffSet )
  847. {
  848. ProcessVarStatusReturnNormal(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  849. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit );
  850. }
  851. }
  852. }
  853. else
  854. {
  855. ProcessVarStatusReturnNormal(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  856. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit );
  857. }
  858. }
  859. else if( nAlarmStatus != nNormalStatus ) // 报警状态
  860. {
  861. //因为第一次加入List不进行任何处理,但状态已经保存,所以要加一个标志来判断一下
  862. if( nAlarmStatus == pVarNotice->nLastStatus && !pVarNotice->bFirstDeal )
  863. {
  864. EnterCriticalSection( &g_csVarNotice );
  865. pVarNotice->nLastStatus = nAlarmStatus;
  866. LeaveCriticalSection( &g_csVarNotice );
  867. return 0;
  868. }
  869. pVarNotice->bFirstDeal = false;//
  870. EnterCriticalSection( &g_csVarNotice );
  871. pVarNotice->nLastStatus = nAlarmStatus;
  872. LeaveCriticalSection( &g_csVarNotice );
  873. if( nOffSet > 0 )
  874. {
  875. CString strTemp = CString(pContent);
  876. //if( strTemp.Find(g_strHintUpperLimit) != -1 || strTemp.Find(g_strHintLowerLimit) != -1 )
  877. {
  878. if( dbData > nUpperLimit + nOffSet || dbData < nLowerLimit - nOffSet )
  879. {
  880. ProcessVarAlarm(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  881. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit );
  882. }
  883. }
  884. }
  885. else
  886. {
  887. ProcessVarAlarm(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  888. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit );
  889. }
  890. }
  891. }
  892. return 1;
  893. }
  894. ///////////////////////////////////////////////////////////////////////
  895. // 报警变量list,主要报警处理,防止一直加入通知list中
  896. // 从list中查找变量,没找到返回空,找到返回相应的指针地址
  897. VAR_NOTICE* FindVarNoticeList(char *pDevUid, int nVarID)
  898. {
  899. VAR_NOTICE* pVarNotice = NULL;
  900. LIST_VAR_NOTICE::iterator it;
  901. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); it++ )
  902. {
  903. if( strcmp(it->szDevUID, pDevUid) == 0 &&
  904. it->nVarID == nVarID )
  905. {
  906. pVarNotice = &(*it);
  907. }
  908. }
  909. return pVarNotice;
  910. }
  911. // 添加变量到list
  912. void InsertVarNoticeList(char *pDevUid, int nVarID)
  913. {
  914. VAR_NOTICE tagVarNotice;
  915. strcpy(tagVarNotice.szDevUID, pDevUid);
  916. tagVarNotice.nVarID = nVarID;
  917. g_listVarNotice.insert(g_listVarNotice.end(), tagVarNotice);
  918. }
  919. // 添加变量到list
  920. void InsertVarNoticeList(VAR_NOTICE varNotice)
  921. {
  922. g_listVarNotice.insert(g_listVarNotice.end(), varNotice);
  923. }
  924. // 从list中移除变量
  925. int DeleteVarNoticeList(char *pDevUid, int nVarID)
  926. {
  927. int nRet = -1;
  928. VAR_NOTICE* pVarNotice = NULL;
  929. LIST_VAR_NOTICE::iterator it;
  930. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); )
  931. {
  932. pVarNotice = &(*it);
  933. if( strcmp(pVarNotice->szDevUID, pDevUid) == 0 &&
  934. pVarNotice->nVarID == nVarID )
  935. {
  936. g_listVarNotice.erase(it++);
  937. nRet = 0;
  938. break;
  939. }
  940. else
  941. {
  942. it++;
  943. }
  944. }
  945. return nRet;
  946. }
  947. // 删除所有变量
  948. void DeleteAllVarNoticeList()
  949. {
  950. LIST_VAR_NOTICE::iterator it;
  951. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); )
  952. {
  953. g_listVarNotice.erase(it++);
  954. }
  955. }
  956. ///////////////////////////////////////////////////////////////////////////
  957. // 发送通知list, 和上面的设备变量不同
  958. // 从list中查找变量,没找到返回空,找到返回相应的指针地址
  959. SEND_NOTICE* FindSendNoticeList(char *pDevUid, int nVarID)
  960. {
  961. SEND_NOTICE* pSendNotice = NULL;
  962. LIST_SEND_NOTICE::iterator it;
  963. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); it++ )
  964. {
  965. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  966. {
  967. pSendNotice = &(*it);
  968. }
  969. }
  970. return pSendNotice;
  971. }
  972. // 添加变量到list
  973. void InsertSendNoticeList(char *pDevUid, int nVarID, char *pTel, char *pContent,int nAlarmIndex )
  974. {
  975. if( pStoneUDLLTxtToWav )
  976. {
  977. //生成WAV
  978. CString sTemp;
  979. sTemp.Format( "%s%s",pContent,g_strHintVoiceEnd );
  980. CString strFileName1;
  981. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pDevUid, nVarID,nAlarmIndex );
  982. pStoneUDLLTxtToWav( (char *)(LPCTSTR)sTemp, (char *)(LPCTSTR)strFileName1, g_nTTSType, -3, 100 ); //设置音量,范围是 0 - 100
  983. CString strFileName2;
  984. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pDevUid, nVarID,nAlarmIndex );
  985. pStoneUDLLTxtToWav( pContent, (char *)(LPCTSTR)strFileName2, g_nTTSType, -3, 100 ); //设置音量,范围是 0 - 100
  986. //结束生成
  987. }
  988. SEND_NOTICE tagSendNotice;
  989. strcpy(tagSendNotice.szDevUID, pDevUid);
  990. tagSendNotice.nVarID = nVarID;
  991. strcpy(tagSendNotice.szTel, pTel);
  992. strcpy(tagSendNotice.szContect, pContent);
  993. tagSendNotice.nAlarmIndex = nAlarmIndex;
  994. GetLocalTime( &tagSendNotice.time );
  995. EnterCriticalSection( &g_csSendNotice );
  996. g_listSendNotice.insert(g_listSendNotice.end(), tagSendNotice);
  997. LeaveCriticalSection( &g_csSendNotice );
  998. }
  999. // 从list中移除变量
  1000. int DeleteSendNoticeList(char *pDevUid, int nVarID)
  1001. {
  1002. int nRet = -1;
  1003. SEND_NOTICE* pSendNotice = NULL;
  1004. LIST_SEND_NOTICE::iterator it;
  1005. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); )
  1006. {
  1007. pSendNotice = &(*it);
  1008. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID )
  1009. {
  1010. EnterCriticalSection( &g_csSendNotice );
  1011. g_listSendNotice.erase(it++);
  1012. LeaveCriticalSection( &g_csSendNotice );
  1013. nRet = 0;
  1014. break;
  1015. }
  1016. else
  1017. {
  1018. it++;
  1019. }
  1020. }
  1021. return nRet;
  1022. }
  1023. // IDE确认后从list中移除变量
  1024. int DeleteSendNoticeList( char *pDevUid, int nVarID,int iAlarmIndex )
  1025. {
  1026. int nRet = -1;
  1027. SEND_NOTICE* pSendNotice = NULL;
  1028. LIST_SEND_NOTICE::iterator it;
  1029. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); )
  1030. {
  1031. pSendNotice = &(*it);
  1032. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1033. {
  1034. //删除WAV
  1035. CString strFileName1;
  1036. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pDevUid, nVarID,iAlarmIndex );
  1037. if( PathFileExists(strFileName1) )
  1038. DeleteFile( strFileName1 );
  1039. CString strFileName2;
  1040. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pDevUid, nVarID,iAlarmIndex );
  1041. if( PathFileExists(strFileName2) )
  1042. DeleteFile( strFileName2 );
  1043. //结束删除
  1044. LOG4C((LOG_NOTICE, "IDE确认語音 %s %s",pSendNotice->szTel,pSendNotice->szContect));
  1045. EnterCriticalSection( &g_csSendNotice );
  1046. g_listSendNotice.erase(it++);
  1047. LeaveCriticalSection( &g_csSendNotice );
  1048. }
  1049. else
  1050. {
  1051. it++;
  1052. }
  1053. }
  1054. return nRet;
  1055. }
  1056. // 删除所有变量
  1057. void DeleteAllSendNoticeList()
  1058. {
  1059. #if 0
  1060. // 正确使用方法1
  1061. std::list< int> List;
  1062. std::list< int>::iterator itList;
  1063. for( itList = List.begin(); itList != List.end(); )
  1064. {
  1065. if( WillDelete( *itList) )
  1066. {
  1067. itList = List.erase( itList);
  1068. }
  1069. else
  1070. itList++;
  1071. }
  1072. // 正确使用方法2
  1073. std::list< int> List;
  1074. std::list< int>::iterator itList;
  1075. for( itList = List.begin(); itList != List.end(); )
  1076. {
  1077. if( WillDelete( *itList) )
  1078. {
  1079. List.erase( itList++);
  1080. }
  1081. else
  1082. itList++;
  1083. }
  1084. // 错误使用方法1
  1085. std::list< int> List;
  1086. std::list< int>::iterator itList;
  1087. for( itList = List.begin(); itList != List.end(); itList++)
  1088. {
  1089. if( WillDelete( *itList) )
  1090. {
  1091. List.erase( itList);
  1092. }
  1093. }
  1094. // 错误使用方法2
  1095. std::list< int> List;
  1096. std::list< int>::iterator itList;
  1097. for( itList = List.begin(); itList != List.end(); )
  1098. {
  1099. if( WillDelete( *itList) )
  1100. {
  1101. itList = List.erase( ++itList);
  1102. }
  1103. else
  1104. itList++;
  1105. }
  1106. #endif
  1107. LIST_SEND_NOTICE::iterator it;
  1108. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); )
  1109. {
  1110. g_listSendNotice.erase( it++ );
  1111. }
  1112. }
  1113. // 添加变量到Sms list
  1114. void InsertSendSmsList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex )
  1115. {
  1116. //LOG4C((LOG_NOTICE, "InsertSendSmsList" ));
  1117. SEND_NOTICE tagSendNotice;
  1118. strcpy(tagSendNotice.szDevUID, pDevUid);
  1119. tagSendNotice.nVarID = nVarID;
  1120. strcpy(tagSendNotice.szTel, "");
  1121. strcpy(tagSendNotice.szContect, pContent);
  1122. tagSendNotice.nAlarmIndex = nAlarmIndex;
  1123. GetLocalTime( &tagSendNotice.time );
  1124. EnterCriticalSection( &g_csSendNotice );
  1125. g_listSendSms.insert(g_listSendSms.end(), tagSendNotice);
  1126. LeaveCriticalSection( &g_csSendNotice );
  1127. }
  1128. // IDE确认后从Sms list中移除变量
  1129. int DeleteSendSmsList( char *pDevUid, int nVarID,int iAlarmIndex )
  1130. {
  1131. int nRet = -1;
  1132. SEND_NOTICE* pSendNotice = NULL;
  1133. LIST_SEND_NOTICE::iterator it;
  1134. for( it = g_listSendSms.begin(); it != g_listSendSms.end(); )
  1135. {
  1136. pSendNotice = &(*it);
  1137. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1138. {
  1139. LOG4C((LOG_NOTICE, "IDE确认短信 %s",pSendNotice->szContect));
  1140. EnterCriticalSection( &g_csSendNotice );
  1141. g_listSendSms.erase(it++);
  1142. LeaveCriticalSection( &g_csSendNotice );
  1143. nRet = 0;
  1144. break;
  1145. }
  1146. else
  1147. {
  1148. it++;
  1149. }
  1150. }
  1151. return nRet;
  1152. }
  1153. // 删除Sms所有变量
  1154. void DeleteAllSendSmsList()
  1155. {
  1156. LIST_SEND_NOTICE::iterator it;
  1157. for( it = g_listSendSms.begin(); it != g_listSendSms.end(); )
  1158. {
  1159. g_listSendSms.erase( it++ );
  1160. }
  1161. }
  1162. int GetTelIndex( char *pDevUid, int nVarID, CString sTel )
  1163. {
  1164. int nRet=-1;
  1165. if( g_listVarNotice.size()==0 )
  1166. {
  1167. LOG4C((LOG_NOTICE, "g_listVarNotice.size()= %d",g_listVarNotice.size()==0));
  1168. return nRet;
  1169. }
  1170. VAR_NOTICE* pVarNotice = NULL;
  1171. LIST_VAR_NOTICE::iterator it;
  1172. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); it++ )
  1173. {
  1174. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  1175. {
  1176. pVarNotice = &(*it);
  1177. break;
  1178. }
  1179. }
  1180. if( pVarNotice )
  1181. {
  1182. LIST_TEL::iterator itTel;
  1183. CString *pStrTel;
  1184. for( itTel = pVarNotice->listTel.begin(); itTel != pVarNotice->listTel.end(); itTel++ )
  1185. {
  1186. pStrTel = &(*itTel);
  1187. nRet++;
  1188. if( strcmp(sTel, *pStrTel) == 0 )
  1189. {
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. else
  1195. LOG4C((LOG_NOTICE, "pVarNotice=NULL g_listVarNotice.size() = %d",g_listVarNotice.size()));
  1196. return nRet;
  1197. }
  1198. ////////////////////////////////////////////////////////////////////////////////
  1199. DWORD WINAPI SendNoticeThread(LPVOID lpParameter)
  1200. {
  1201. do
  1202. {
  1203. if( g_listSendNotice.size() > 0 )
  1204. {
  1205. SEND_NOTICE *pSendNotice;
  1206. LIST_SEND_NOTICE::iterator it;
  1207. it = g_listSendNotice.begin();
  1208. pSendNotice = &(*it);
  1209. CString sCurrentTel;
  1210. if( pSendNotice )
  1211. {
  1212. SYSTEMTIME st;
  1213. GetLocalTime( &st );
  1214. CTime time1(st);
  1215. CTime time2(pSendNotice->time);
  1216. CTimeSpan spantime = time1 - time2;
  1217. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1218. if( nSecNum > NOTICE_DELAY )
  1219. {
  1220. LOG4C((LOG_NOTICE, "开始拨打电话 %s",pSendNotice->szTel));
  1221. int nRet = SendNotice(pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex, pSendNotice->szTel, pSendNotice->szContect);
  1222. sCurrentTel.Format( "%s",pSendNotice->szTel );
  1223. LOG4C((LOG_NOTICE, "结束拨打电话 %s",pSendNotice->szTel));
  1224. //删除WAV
  1225. CString strFileName1;
  1226. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex );
  1227. if( PathFileExists(strFileName1) )
  1228. DeleteFile( strFileName1 );
  1229. CString strFileName2;
  1230. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex );
  1231. if( PathFileExists(strFileName2) )
  1232. DeleteFile( strFileName2 );
  1233. //结束删除
  1234. if( nRet==CALL_SUCCESS_SINGLE_CONFIRM )//确认当前通知
  1235. {
  1236. int nCurrentTelIndex=0,nTelIndex=0;
  1237. nCurrentTelIndex = GetTelIndex( pSendNotice->szDevUID, pSendNotice->nVarID, sCurrentTel );
  1238. EnterCriticalSection( &g_csSendNotice );
  1239. g_listSendNotice.erase(it++);//先把当前的删除
  1240. LeaveCriticalSection( &g_csSendNotice );
  1241. if( g_listSendNotice.size() > 0 )
  1242. {
  1243. CTime time = CTime::GetCurrentTime();
  1244. CString strTime;
  1245. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1246. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  1247. (char *)(LPCTSTR)sCurrentTel,
  1248. "",
  1249. (char *)(LPCTSTR)strTime,
  1250. "config one alert",
  1251. "");
  1252. EnterCriticalSection( &g_csSendNotice );
  1253. LIST_SEND_NOTICE::iterator itTemp;
  1254. for( itTemp = g_listSendNotice.begin(); itTemp != g_listSendNotice.end(); )
  1255. {
  1256. nTelIndex = GetTelIndex( itTemp->szDevUID,itTemp->nVarID,itTemp->szTel );
  1257. //LOG4C((LOG_NOTICE, "%s,nCurrentTelIndex = %d,%s,nTelIndex=%d ",sCurrentTel,nCurrentTelIndex,itTemp->szTel,nTelIndex ));
  1258. if( nCurrentTelIndex<nTelIndex )
  1259. {
  1260. LOG4C((LOG_NOTICE, "通知已经被确认,跳过号码 %s",itTemp->szTel ));
  1261. g_listSendNotice.erase(itTemp++);
  1262. }
  1263. else
  1264. {
  1265. break;
  1266. }
  1267. }
  1268. LeaveCriticalSection( &g_csSendNotice );
  1269. }
  1270. }
  1271. else if( nRet==CALL_SUCCESS_ALL_CONFIRM )//确认全部通知
  1272. {
  1273. EnterCriticalSection( &g_csSendNotice );
  1274. g_listSendNotice.erase(it++);//先把当前的删除
  1275. if( g_listSendNotice.size() > 0 )
  1276. {
  1277. CTime time = CTime::GetCurrentTime();
  1278. CString strTime;
  1279. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1280. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  1281. (char *)(LPCTSTR)sCurrentTel,
  1282. "",
  1283. (char *)(LPCTSTR)strTime,
  1284. "config all alert",
  1285. "");
  1286. LIST_SEND_NOTICE::iterator itTemp;
  1287. for( itTemp = g_listSendNotice.begin(); itTemp != g_listSendNotice.end(); )
  1288. {
  1289. LOG4C((LOG_NOTICE, "通知全部已经被确认,跳过号码 %s",itTemp->szTel ));
  1290. g_listSendNotice.erase(itTemp++);
  1291. }
  1292. }
  1293. LeaveCriticalSection( &g_csSendNotice );
  1294. }
  1295. else if( nRet != CALL_LOST )
  1296. {
  1297. EnterCriticalSection( &g_csSendNotice );
  1298. g_listSendNotice.erase(it++);
  1299. LeaveCriticalSection( &g_csSendNotice );
  1300. }
  1301. }
  1302. Sleep( 10 * 1000 );//Sleep( 30 * 1000 );
  1303. }
  1304. }
  1305. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1306. return 0;
  1307. }
  1308. DWORD WINAPI SendSmsThread(LPVOID lpParameter)
  1309. {
  1310. do
  1311. {
  1312. if( g_listSendSms.size() > 0 )
  1313. {
  1314. SEND_NOTICE *pSendNotice;
  1315. LIST_SEND_NOTICE::iterator it;
  1316. it = g_listSendSms.begin();
  1317. pSendNotice = &(*it);
  1318. CString sCurrentTel;
  1319. if( pSendNotice )
  1320. {
  1321. //LOG4C((LOG_NOTICE, "SendSmsThread size>0" ));
  1322. SYSTEMTIME st;
  1323. GetLocalTime( &st );
  1324. CTime time1(st);
  1325. CTime time2(pSendNotice->time);
  1326. CTimeSpan spantime = time1 - time2;
  1327. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1328. if( nSecNum > NOTICE_DELAY )
  1329. {
  1330. //LOG4C((LOG_NOTICE, "SendSmsThread NOTICE_DELAY" ));
  1331. SendSms( pSendNotice->szDevUID,pSendNotice->nVarID,pSendNotice->szContect );
  1332. EnterCriticalSection( &g_csSendNotice );
  1333. g_listSendSms.erase(it++);
  1334. LeaveCriticalSection( &g_csSendNotice );
  1335. }
  1336. }
  1337. }
  1338. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1339. return 0;
  1340. }
  1341. int GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
  1342. {
  1343. int iResult = iNormalState;
  1344. if ( ( iUpperLimit >0 || iLowerLimit > 0 ) && iUpperLimit > iLowerLimit)
  1345. {
  1346. //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
  1347. if( iParaValue > iUpperLimit )
  1348. {
  1349. iResult = UPPER_LIMIT_ID; // 上限告警
  1350. }
  1351. if( iParaValue < iLowerLimit )
  1352. {
  1353. iResult = LOWER_LIMIT_ID; // 下限告警
  1354. }
  1355. else
  1356. {
  1357. return iResult;
  1358. }
  1359. }
  1360. else if( (int)iParaValue != iNormalState )
  1361. {
  1362. iResult = (int)iParaValue;
  1363. }
  1364. return iResult;
  1365. }
  1366. int VarAlarmNotice(CDevice *pDev, CBaseVar *pBaseVar)
  1367. {
  1368. // 组织报警内容
  1369. CString strContect;
  1370. char szStatusDesc[MAX_EQUIP_DESC + 1] = {0};
  1371. char szStatusID[MAX_ID + 1] = {0};
  1372. memset(szStatusDesc, 0, sizeof(szStatusDesc));
  1373. if( pBaseVar->m_nWaringLevel == 0 ) return 1;
  1374. int nTempStatus = GetVarstatus(pBaseVar->m_dbData, pBaseVar->m_nUpperLimit, pBaseVar->m_nLowerLimit, pBaseVar->m_nNormalState);
  1375. if( UPPER_LIMIT_ID == nTempStatus ) // 上限告警
  1376. {
  1377. strcpy(szStatusDesc, g_strHintUpperLimit);
  1378. //LOG4C((LOG_NOTICE, "var desc = %s, %s", pBaseVar->m_strDesc, szStatusDesc));
  1379. }
  1380. else if( LOWER_LIMIT_ID == nTempStatus ) // 下限告警
  1381. {
  1382. strcpy(szStatusDesc, g_strHintLowerLimit);
  1383. }
  1384. else if( nTempStatus != pBaseVar->m_nNormalState )
  1385. {
  1386. list<string>::iterator it_desc, it_id;
  1387. if( pBaseVar->m_listStatusDesc.size() > 0 && pBaseVar->m_listStatusID.size() > 0 )
  1388. {
  1389. for( it_desc = pBaseVar->m_listStatusDesc.begin(), it_id = pBaseVar->m_listStatusID.begin();
  1390. it_desc != pBaseVar->m_listStatusDesc.end(), it_id != pBaseVar->m_listStatusID.end(); )
  1391. {
  1392. CString strStatusID = (*it_id++).c_str();
  1393. CString strStatusDesc = (*it_desc++).c_str();
  1394. if( atoi(strStatusID) == nTempStatus )
  1395. {
  1396. //LOG4C((LOG_NOTICE, "var desc = %s, nTempStatus = %d, StatusID = %d statusdesc = %s, value = %d",pBaseVar->m_strDesc, nTempStatus, atoi(strStatusID), strStatusDesc, (int)pBaseVar->m_dbData));
  1397. strcpy(szStatusDesc, (char *)(LPCTSTR)strStatusDesc);
  1398. //LOG4C((LOG_NOTICE, "szStatusDesc=%s", szStatusDesc));
  1399. break;
  1400. }
  1401. }
  1402. }
  1403. if( strcmp(szStatusDesc, "") == 0 )
  1404. {
  1405. //LOG4C((LOG_NOTICE, "VarAlarmNotice var = %s nTempStatus = %d NormalState = %d statudesc is null!", pBaseVar->m_strDesc, nTempStatus, pBaseVar->m_nNormalState));
  1406. return 1;
  1407. }
  1408. }
  1409. if( nTempStatus != pBaseVar->m_nNormalState ) // 报警状态
  1410. {
  1411. if( pBaseVar->m_bReserved1 == true )
  1412. {
  1413. if( pBaseVar->m_strReserved1.Compare("") == 0 )
  1414. {
  1415. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pBaseVar->m_dbData, pBaseVar->m_strUnit);
  1416. }
  1417. else
  1418. {
  1419. int nDeviceIndex = -1, nVarIndex = -1;
  1420. BOOL bFind = FindVar(pBaseVar->m_strVarUID, pBaseVar->m_strReserved1, nDeviceIndex, nVarIndex);
  1421. if( bFind == FALSE )
  1422. {
  1423. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pBaseVar->m_dbData, pBaseVar->m_strUnit);
  1424. }
  1425. else
  1426. {
  1427. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  1428. CBaseVar *pTempBaseVar = pDev->m_Vars[nVarIndex];
  1429. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pTempBaseVar->m_dbData, pBaseVar->m_strUnit);
  1430. }
  1431. }
  1432. }
  1433. else
  1434. {
  1435. strContect.Format("%s%s:%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc);
  1436. }
  1437. if( pBaseVar->m_bExistDatePlan )
  1438. {
  1439. if( pBaseVar->m_dwReturnNormalIdentityTick>0 )
  1440. {
  1441. pBaseVar->m_dwReturnNormalIdentityTick = 0;
  1442. //LOG4C(( LOG_NOTICE,"var=%s 恢复正常未达到辩识时间条件又收到报警值,放弃加入",pBaseVar->m_strDesc));
  1443. }
  1444. if( pBaseVar->m_dwIdentityTick == 0 && pBaseVar->m_nAlarmStatus!=2 )
  1445. {
  1446. //EnterCriticalSection( &g_csVarNotice );
  1447. pBaseVar->m_dwIdentityTick = GetTickCount();
  1448. //LeaveCriticalSection( &g_csVarNotice );
  1449. //LOG4C((LOG_NOTICE, "var=%s 达到报警条件", pBaseVar->m_strDesc));
  1450. }
  1451. else if( pBaseVar->m_nAlarmStatus==0 )
  1452. {
  1453. //LOG4C((LOG_NOTICE, "var=%s 第一次达到报警条件,不用判断辩识时间,直接加入", pBaseVar->m_strDesc));
  1454. ProcessVarNotice(
  1455. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1456. pBaseVar->m_nVarID,
  1457. pBaseVar->m_nUpperLimit,
  1458. pBaseVar->m_nLowerLimit,
  1459. pBaseVar->m_nOffset,
  1460. nTempStatus,
  1461. pBaseVar->m_nNormalState,
  1462. pBaseVar->m_nIdentifyTime,
  1463. pBaseVar->m_nReDetectTime,
  1464. pBaseVar->m_bNormalIsNotice,
  1465. "",
  1466. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1467. pBaseVar->m_dbData,
  1468. (char *)(LPCTSTR)strContect );
  1469. EnterCriticalSection( &g_csVarNotice );
  1470. pBaseVar->m_nAlarmStatus = 1;//设为第一次报警状态
  1471. pBaseVar->m_strContent = strContect;
  1472. pBaseVar->m_nLastStatus = nTempStatus;
  1473. pBaseVar->m_dwIdentityTick = 0;
  1474. LeaveCriticalSection( &g_csVarNotice );
  1475. }
  1476. else if( GetTickCount() - pBaseVar->m_dwIdentityTick > (DWORD)pBaseVar->m_nIdentifyTime * 1000 && pBaseVar->m_nAlarmStatus!=2 )
  1477. {
  1478. LOG4C((LOG_NOTICE, "var=%s 达到报警辩识时间条件,加入", pBaseVar->m_strDesc));
  1479. if( 1 == g_nSnmpEnable )
  1480. {
  1481. CString strSetValue;
  1482. strSetValue.Format("%.1f,A", pBaseVar->m_dbData);
  1483. CString strTime;
  1484. CTime tm;
  1485. tm = CTime::GetCurrentTime();
  1486. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  1487. CString strTrapContent = "";
  1488. strTrapContent.Format("%s_%s_%s,A", pDev->m_strDeviceName, strTime, strContect);
  1489. //LOG4C((LOG_NOTICE, "%s", strSetValue));
  1490. CString strObjID;
  1491. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1492. pSnmpDllSetValue(
  1493. g_strSnmpIP,
  1494. g_strSnmpField,
  1495. (char *)(LPCTSTR)strObjID,
  1496. "str",
  1497. 1,
  1498. 300,
  1499. (char *)(LPCTSTR)strTrapContent
  1500. );
  1501. }
  1502. if( pBaseVar->m_nAlarmNumber < 10 )
  1503. pBaseVar->m_nAlarmNumber++;
  1504. ProcessVarNotice(
  1505. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1506. pBaseVar->m_nVarID,
  1507. pBaseVar->m_nUpperLimit,
  1508. pBaseVar->m_nLowerLimit,
  1509. pBaseVar->m_nOffset,
  1510. nTempStatus,
  1511. pBaseVar->m_nNormalState,
  1512. pBaseVar->m_nIdentifyTime,
  1513. pBaseVar->m_nReDetectTime,
  1514. pBaseVar->m_bNormalIsNotice,
  1515. "",
  1516. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1517. pBaseVar->m_dbData,
  1518. (char *)(LPCTSTR)strContect );
  1519. EnterCriticalSection( &g_csVarNotice );
  1520. pBaseVar->m_nAlarmStatus = 2;//设为第二次报警状态
  1521. pBaseVar->m_strContent = strContect;
  1522. pBaseVar->m_nLastStatus = nTempStatus;
  1523. pBaseVar->m_dwIdentityTick = 0;
  1524. LeaveCriticalSection( &g_csVarNotice );
  1525. }
  1526. else
  1527. {
  1528. #if 0
  1529. if( 1 == g_nSnmpEnable )
  1530. {
  1531. if( TRUE == pBaseVar->m_bIsChange )
  1532. {
  1533. CString strSetValue;
  1534. strSetValue.Format("%.1f", pBaseVar->m_dbData);
  1535. CString strObjID;
  1536. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1537. pSnmpDllSetValue(
  1538. g_strSnmpIP,
  1539. g_strSnmpField,
  1540. (char *)(LPCTSTR)strObjID,
  1541. "str",
  1542. 1,
  1543. 300,
  1544. (char *)(LPCTSTR)strSetValue
  1545. );
  1546. }
  1547. }
  1548. #endif
  1549. }
  1550. }
  1551. }
  1552. else
  1553. {
  1554. if( pBaseVar->m_dwIdentityTick>0 )
  1555. {
  1556. pBaseVar->m_dwIdentityTick = 0;
  1557. //LOG4C(( LOG_NOTICE,"var=%s 报警未达到辩识时间条件又收到正常值,放弃加入",pBaseVar->m_strDesc));
  1558. }
  1559. if( pBaseVar->m_nAlarmNumber >= 1 && pBaseVar->m_nAlarmNumber < 11 )
  1560. {
  1561. strContect.Format("%s%s:%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, g_strHintReturnNormal);
  1562. if( pBaseVar->m_dwReturnNormalIdentityTick == 0 )
  1563. {
  1564. //EnterCriticalSection( &g_csVarNotice );
  1565. pBaseVar->m_dwReturnNormalIdentityTick = GetTickCount();
  1566. //LeaveCriticalSection( &g_csVarNotice );
  1567. //LOG4C((LOG_NOTICE, "var=%s 达到恢复正常条件", pBaseVar->m_strDesc));
  1568. }
  1569. else if( GetTickCount() - pBaseVar->m_dwReturnNormalIdentityTick > (DWORD)pBaseVar->m_nIdentifyTime * 1000 )
  1570. {
  1571. LOG4C((LOG_NOTICE, "var=%s 达到恢复正常辩识时间条件,加入", pBaseVar->m_strDesc));
  1572. pBaseVar->m_nAlarmNumber = 0;
  1573. //回复正常
  1574. ProcessVarNotice(
  1575. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1576. pBaseVar->m_nVarID,
  1577. pBaseVar->m_nUpperLimit,
  1578. pBaseVar->m_nLowerLimit,
  1579. pBaseVar->m_nOffset,
  1580. nTempStatus,
  1581. pBaseVar->m_nNormalState,
  1582. pBaseVar->m_nIdentifyTime,
  1583. pBaseVar->m_nReDetectTime,
  1584. pBaseVar->m_bNormalIsNotice,
  1585. "",
  1586. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1587. pBaseVar->m_dbData,
  1588. (char *)(LPCTSTR)strContect );
  1589. EnterCriticalSection( &g_csVarNotice );
  1590. pBaseVar->m_nAlarmStatus = 3;//设为恢复正常状态
  1591. pBaseVar->m_strContent = strContect;
  1592. pBaseVar->m_nLastStatus = nTempStatus;
  1593. pBaseVar->m_dwReturnNormalIdentityTick = 0;
  1594. LeaveCriticalSection( &g_csVarNotice );
  1595. if( 1 == g_nSnmpEnable )
  1596. {
  1597. CString strSetValue;
  1598. strSetValue.Format("%.1f,N", pBaseVar->m_dbData);
  1599. CString strTime;
  1600. CTime tm;
  1601. tm = CTime::GetCurrentTime();
  1602. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  1603. CString strTrapContent = "";
  1604. strTrapContent.Format("%s_%s_%s,N", pDev->m_strDeviceName, strTime, strContect);
  1605. //LOG4C((LOG_NOTICE, "%s", strTrapContent));
  1606. CString strObjID;
  1607. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1608. pSnmpDllSetValue(
  1609. g_strSnmpIP,
  1610. g_strSnmpField,
  1611. (char *)(LPCTSTR)strObjID,
  1612. "str",
  1613. 1,
  1614. 300,
  1615. (char *)(LPCTSTR)strTrapContent
  1616. );
  1617. }
  1618. }
  1619. }
  1620. else
  1621. {
  1622. #if 0
  1623. if( 1 == g_nSnmpEnable )
  1624. {
  1625. if( TRUE == pBaseVar->m_bIsChange )
  1626. {
  1627. CString strSetValue;
  1628. strSetValue.Format("%.1f", pBaseVar->m_dbData);
  1629. CString strObjID;
  1630. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1631. pSnmpDllSetValue(
  1632. g_strSnmpIP,
  1633. g_strSnmpField,
  1634. (char *)(LPCTSTR)strObjID,
  1635. "str",
  1636. "str",
  1637. 1,
  1638. 300,
  1639. (char *)(LPCTSTR)strSetValue
  1640. );
  1641. }
  1642. }
  1643. #endif
  1644. }
  1645. }
  1646. return 0;
  1647. }