NoticeQueue.cpp 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  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); // 查找报警变量是否存在报警列表中,不存在返回NULL,存在返回指针;
  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 && it->nVarID == nVarID )
  904. pVarNotice = &(*it);
  905. }
  906. return pVarNotice;
  907. }
  908. // 添加变量到list
  909. void InsertVarNoticeList(char *pDevUid, int nVarID)
  910. {
  911. VAR_NOTICE tagVarNotice;
  912. strcpy(tagVarNotice.szDevUID, pDevUid);
  913. tagVarNotice.nVarID = nVarID;
  914. g_listVarNotice.insert(g_listVarNotice.end(), tagVarNotice);
  915. }
  916. // 添加变量到list
  917. void InsertVarNoticeList(VAR_NOTICE varNotice)
  918. {
  919. g_listVarNotice.insert(g_listVarNotice.end(), varNotice);
  920. }
  921. // 从list中移除变量
  922. int DeleteVarNoticeList(char *pDevUid, int nVarID)
  923. {
  924. int nRet = -1;
  925. VAR_NOTICE* pVarNotice = NULL;
  926. LIST_VAR_NOTICE::iterator it;
  927. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); )
  928. {
  929. pVarNotice = &(*it);
  930. if( strcmp(pVarNotice->szDevUID, pDevUid) == 0 &&
  931. pVarNotice->nVarID == nVarID )
  932. {
  933. g_listVarNotice.erase(it++);
  934. nRet = 0;
  935. break;
  936. }
  937. else
  938. {
  939. it++;
  940. }
  941. }
  942. return nRet;
  943. }
  944. // 删除所有变量
  945. void DeleteAllVarNoticeList()
  946. {
  947. LIST_VAR_NOTICE::iterator it;
  948. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); )
  949. {
  950. g_listVarNotice.erase(it++);
  951. }
  952. }
  953. ///////////////////////////////////////////////////////////////////////////
  954. // 发送通知list, 和上面的设备变量不同
  955. // 从list中查找变量,没找到返回空,找到返回相应的指针地址
  956. SEND_NOTICE* FindSendNoticeList(char *pDevUid, int nVarID)
  957. {
  958. SEND_NOTICE* pSendNotice = NULL;
  959. LIST_SEND_NOTICE::iterator it;
  960. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); it++ )
  961. {
  962. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  963. pSendNotice = &(*it);
  964. }
  965. return pSendNotice;
  966. }
  967. // 添加变量到list
  968. void InsertSendNoticeList(char *pDevUid, int nVarID, char *pTel, char *pContent,int nAlarmIndex )
  969. {
  970. if( pStoneUDLLTxtToWav )
  971. {
  972. //生成WAV
  973. CString sTemp;
  974. sTemp.Format( "%s%s",pContent,g_strHintVoiceEnd );
  975. CString strFileName1;
  976. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pDevUid, nVarID,nAlarmIndex );
  977. pStoneUDLLTxtToWav( (char *)(LPCTSTR)sTemp, (char *)(LPCTSTR)strFileName1, g_nTTSType, -3, 100 ); //设置音量,范围是 0 - 100
  978. CString strFileName2;
  979. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pDevUid, nVarID,nAlarmIndex );
  980. pStoneUDLLTxtToWav( pContent, (char *)(LPCTSTR)strFileName2, g_nTTSType, -3, 100 ); //设置音量,范围是 0 - 100
  981. //结束生成
  982. }
  983. SEND_NOTICE tagSendNotice;
  984. strcpy(tagSendNotice.szDevUID, pDevUid);
  985. tagSendNotice.nVarID = nVarID;
  986. strcpy(tagSendNotice.szTel, pTel);
  987. strcpy(tagSendNotice.szContect, pContent);
  988. tagSendNotice.nAlarmIndex = nAlarmIndex;
  989. GetLocalTime( &tagSendNotice.time );
  990. EnterCriticalSection( &g_csSendNotice );
  991. g_listSendNotice.insert(g_listSendNotice.end(), tagSendNotice);
  992. LeaveCriticalSection( &g_csSendNotice );
  993. }
  994. // 从list中移除变量
  995. int DeleteSendNoticeList(char *pDevUid, int nVarID)
  996. {
  997. int nRet = -1;
  998. SEND_NOTICE* pSendNotice = NULL;
  999. LIST_SEND_NOTICE::iterator it;
  1000. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); )
  1001. {
  1002. pSendNotice = &(*it);
  1003. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID )
  1004. {
  1005. EnterCriticalSection( &g_csSendNotice );
  1006. g_listSendNotice.erase(it++);
  1007. LeaveCriticalSection( &g_csSendNotice );
  1008. nRet = 0;
  1009. break;
  1010. }
  1011. else
  1012. {
  1013. it++;
  1014. }
  1015. }
  1016. return nRet;
  1017. }
  1018. // IDE确认后从list中移除变量
  1019. int DeleteSendNoticeList( char *pDevUid, int nVarID,int iAlarmIndex )
  1020. {
  1021. int nRet = -1;
  1022. SEND_NOTICE* pSendNotice = NULL;
  1023. LIST_SEND_NOTICE::iterator it;
  1024. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); )
  1025. {
  1026. pSendNotice = &(*it);
  1027. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1028. {
  1029. //删除WAV
  1030. CString strFileName1;
  1031. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pDevUid, nVarID,iAlarmIndex );
  1032. if( PathFileExists(strFileName1) )
  1033. DeleteFile( strFileName1 );
  1034. CString strFileName2;
  1035. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pDevUid, nVarID,iAlarmIndex );
  1036. if( PathFileExists(strFileName2) )
  1037. DeleteFile( strFileName2 );
  1038. //结束删除
  1039. LOG4C((LOG_NOTICE, "IDE确认語音 %s %s",pSendNotice->szTel,pSendNotice->szContect));
  1040. EnterCriticalSection( &g_csSendNotice );
  1041. g_listSendNotice.erase(it++);
  1042. LeaveCriticalSection( &g_csSendNotice );
  1043. }
  1044. else
  1045. {
  1046. it++;
  1047. }
  1048. }
  1049. return nRet;
  1050. }
  1051. // 删除所有变量
  1052. void DeleteAllSendNoticeList()
  1053. {
  1054. #if 0
  1055. // 正确使用方法1
  1056. std::list< int> List;
  1057. std::list< int>::iterator itList;
  1058. for( itList = List.begin(); itList != List.end(); )
  1059. {
  1060. if( WillDelete( *itList) )
  1061. {
  1062. itList = List.erase( itList);
  1063. }
  1064. else
  1065. itList++;
  1066. }
  1067. // 正确使用方法2
  1068. std::list< int> List;
  1069. std::list< int>::iterator itList;
  1070. for( itList = List.begin(); itList != List.end(); )
  1071. {
  1072. if( WillDelete( *itList) )
  1073. {
  1074. List.erase( itList++);
  1075. }
  1076. else
  1077. itList++;
  1078. }
  1079. // 错误使用方法1
  1080. std::list< int> List;
  1081. std::list< int>::iterator itList;
  1082. for( itList = List.begin(); itList != List.end(); itList++)
  1083. {
  1084. if( WillDelete( *itList) )
  1085. {
  1086. List.erase( itList);
  1087. }
  1088. }
  1089. // 错误使用方法2
  1090. std::list< int> List;
  1091. std::list< int>::iterator itList;
  1092. for( itList = List.begin(); itList != List.end(); )
  1093. {
  1094. if( WillDelete( *itList) )
  1095. {
  1096. itList = List.erase( ++itList);
  1097. }
  1098. else
  1099. itList++;
  1100. }
  1101. #endif
  1102. LIST_SEND_NOTICE::iterator it;
  1103. for( it = g_listSendNotice.begin(); it != g_listSendNotice.end(); )
  1104. {
  1105. g_listSendNotice.erase( it++ );
  1106. }
  1107. }
  1108. // 添加变量到Sms list
  1109. void InsertSendSmsList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex )
  1110. {
  1111. //LOG4C((LOG_NOTICE, "InsertSendSmsList" ));
  1112. SEND_NOTICE tagSendNotice;
  1113. strcpy(tagSendNotice.szDevUID, pDevUid);
  1114. tagSendNotice.nVarID = nVarID;
  1115. strcpy(tagSendNotice.szTel, "");
  1116. strcpy(tagSendNotice.szContect, pContent);
  1117. tagSendNotice.nAlarmIndex = nAlarmIndex;
  1118. GetLocalTime( &tagSendNotice.time );
  1119. EnterCriticalSection( &g_csSendNotice );
  1120. g_listSendSms.insert(g_listSendSms.end(), tagSendNotice);
  1121. LeaveCriticalSection( &g_csSendNotice );
  1122. }
  1123. // IDE确认后从Sms list中移除变量
  1124. int DeleteSendSmsList( char *pDevUid, int nVarID,int iAlarmIndex )
  1125. {
  1126. int nRet = -1;
  1127. SEND_NOTICE* pSendNotice = NULL;
  1128. LIST_SEND_NOTICE::iterator it;
  1129. for( it = g_listSendSms.begin(); it != g_listSendSms.end(); )
  1130. {
  1131. pSendNotice = &(*it);
  1132. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1133. {
  1134. LOG4C((LOG_NOTICE, "IDE确认短信 %s",pSendNotice->szContect));
  1135. EnterCriticalSection( &g_csSendNotice );
  1136. g_listSendSms.erase(it++);
  1137. LeaveCriticalSection( &g_csSendNotice );
  1138. nRet = 0;
  1139. break;
  1140. }
  1141. else
  1142. {
  1143. it++;
  1144. }
  1145. }
  1146. return nRet;
  1147. }
  1148. // 删除Sms所有变量
  1149. void DeleteAllSendSmsList()
  1150. {
  1151. LIST_SEND_NOTICE::iterator it;
  1152. for( it = g_listSendSms.begin(); it != g_listSendSms.end(); )
  1153. {
  1154. g_listSendSms.erase( it++ );
  1155. }
  1156. }
  1157. int GetTelIndex( char *pDevUid, int nVarID, CString sTel )
  1158. {
  1159. int nRet=-1;
  1160. if( g_listVarNotice.size()==0 )
  1161. {
  1162. LOG4C((LOG_NOTICE, "g_listVarNotice.size()= %d",g_listVarNotice.size()==0));
  1163. return nRet;
  1164. }
  1165. VAR_NOTICE* pVarNotice = NULL;
  1166. LIST_VAR_NOTICE::iterator it;
  1167. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); it++ )
  1168. {
  1169. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  1170. {
  1171. pVarNotice = &(*it);
  1172. break;
  1173. }
  1174. }
  1175. if( pVarNotice )
  1176. {
  1177. LIST_TEL::iterator itTel;
  1178. CString *pStrTel;
  1179. for( itTel = pVarNotice->listTel.begin(); itTel != pVarNotice->listTel.end(); itTel++ )
  1180. {
  1181. pStrTel = &(*itTel);
  1182. nRet++;
  1183. if( strcmp(sTel, *pStrTel) == 0 )
  1184. {
  1185. break;
  1186. }
  1187. }
  1188. }
  1189. else
  1190. LOG4C((LOG_NOTICE, "pVarNotice=NULL g_listVarNotice.size() = %d",g_listVarNotice.size()));
  1191. return nRet;
  1192. }
  1193. ////////////////////////////////////////////////////////////////////////////////
  1194. DWORD WINAPI SendNoticeThread(LPVOID lpParameter)
  1195. {
  1196. do
  1197. {
  1198. if( g_listSendNotice.size() > 0 )
  1199. {
  1200. SEND_NOTICE *pSendNotice;
  1201. LIST_SEND_NOTICE::iterator it;
  1202. it = g_listSendNotice.begin();
  1203. pSendNotice = &(*it);
  1204. CString sCurrentTel;
  1205. if( pSendNotice )
  1206. {
  1207. SYSTEMTIME st;
  1208. GetLocalTime( &st );
  1209. CTime time1(st);
  1210. CTime time2(pSendNotice->time);
  1211. CTimeSpan spantime = time1 - time2;
  1212. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1213. if( nSecNum > NOTICE_DELAY )
  1214. {
  1215. LOG4C((LOG_NOTICE, "开始拨打电话 %s",pSendNotice->szTel));
  1216. int nRet = SendNotice(pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex, pSendNotice->szTel, pSendNotice->szContect);
  1217. sCurrentTel.Format( "%s",pSendNotice->szTel );
  1218. LOG4C((LOG_NOTICE, "结束拨打电话 %s",pSendNotice->szTel));
  1219. //删除WAV
  1220. CString strFileName1;
  1221. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex );
  1222. if( PathFileExists(strFileName1) )
  1223. DeleteFile( strFileName1 );
  1224. CString strFileName2;
  1225. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex );
  1226. if( PathFileExists(strFileName2) )
  1227. DeleteFile( strFileName2 );
  1228. //结束删除
  1229. if( nRet==CALL_SUCCESS_SINGLE_CONFIRM )//确认当前通知
  1230. {
  1231. int nCurrentTelIndex=0,nTelIndex=0;
  1232. nCurrentTelIndex = GetTelIndex( pSendNotice->szDevUID, pSendNotice->nVarID, sCurrentTel );
  1233. EnterCriticalSection( &g_csSendNotice );
  1234. g_listSendNotice.erase(it++);//先把当前的删除
  1235. LeaveCriticalSection( &g_csSendNotice );
  1236. if( g_listSendNotice.size() > 0 )
  1237. {
  1238. CTime time = CTime::GetCurrentTime();
  1239. CString strTime;
  1240. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1241. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  1242. (char *)(LPCTSTR)sCurrentTel,
  1243. "",
  1244. (char *)(LPCTSTR)strTime,
  1245. "config one alert",
  1246. "");
  1247. EnterCriticalSection( &g_csSendNotice );
  1248. LIST_SEND_NOTICE::iterator itTemp;
  1249. for( itTemp = g_listSendNotice.begin(); itTemp != g_listSendNotice.end(); )
  1250. {
  1251. nTelIndex = GetTelIndex( itTemp->szDevUID,itTemp->nVarID,itTemp->szTel );
  1252. //LOG4C((LOG_NOTICE, "%s,nCurrentTelIndex = %d,%s,nTelIndex=%d ",sCurrentTel,nCurrentTelIndex,itTemp->szTel,nTelIndex ));
  1253. if( nCurrentTelIndex<nTelIndex )
  1254. {
  1255. LOG4C((LOG_NOTICE, "通知已经被确认,跳过号码 %s",itTemp->szTel ));
  1256. g_listSendNotice.erase(itTemp++);
  1257. }
  1258. else
  1259. {
  1260. break;
  1261. }
  1262. }
  1263. LeaveCriticalSection( &g_csSendNotice );
  1264. }
  1265. }
  1266. else if( nRet==CALL_SUCCESS_ALL_CONFIRM )//确认全部通知
  1267. {
  1268. EnterCriticalSection( &g_csSendNotice );
  1269. g_listSendNotice.erase(it++);//先把当前的删除
  1270. if( g_listSendNotice.size() > 0 )
  1271. {
  1272. CTime time = CTime::GetCurrentTime();
  1273. CString strTime;
  1274. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1275. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  1276. (char *)(LPCTSTR)sCurrentTel,
  1277. "",
  1278. (char *)(LPCTSTR)strTime,
  1279. "config all alert",
  1280. "");
  1281. LIST_SEND_NOTICE::iterator itTemp;
  1282. for( itTemp = g_listSendNotice.begin(); itTemp != g_listSendNotice.end(); )
  1283. {
  1284. LOG4C((LOG_NOTICE, "通知全部已经被确认,跳过号码 %s",itTemp->szTel ));
  1285. g_listSendNotice.erase(itTemp++);
  1286. }
  1287. }
  1288. LeaveCriticalSection( &g_csSendNotice );
  1289. }
  1290. else if( nRet != CALL_LOST )
  1291. {
  1292. EnterCriticalSection( &g_csSendNotice );
  1293. g_listSendNotice.erase(it++);
  1294. LeaveCriticalSection( &g_csSendNotice );
  1295. }
  1296. }
  1297. Sleep( 10 * 1000 );//Sleep( 30 * 1000 );
  1298. }
  1299. }
  1300. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1301. return 0;
  1302. }
  1303. DWORD WINAPI SendSmsThread(LPVOID lpParameter)
  1304. {
  1305. do
  1306. {
  1307. if( g_listSendSms.size() > 0 )
  1308. {
  1309. SEND_NOTICE *pSendNotice; // 这个变量有意义么?块局部变量,每次do后都没用了,it++要来何用?
  1310. LIST_SEND_NOTICE::iterator it;
  1311. it = g_listSendSms.begin();
  1312. pSendNotice = &(*it);
  1313. CString sCurrentTel;
  1314. if( pSendNotice )
  1315. {
  1316. //LOG4C((LOG_NOTICE, "SendSmsThread size>0" ));
  1317. SYSTEMTIME st;
  1318. GetLocalTime( &st );
  1319. CTime time1(st);
  1320. CTime time2(pSendNotice->time);
  1321. CTimeSpan spantime = time1 - time2;
  1322. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1323. if( nSecNum > NOTICE_DELAY )
  1324. {
  1325. //LOG4C((LOG_NOTICE, "SendSmsThread NOTICE_DELAY" ));
  1326. SendSms( pSendNotice->szDevUID,pSendNotice->nVarID,pSendNotice->szContect );
  1327. EnterCriticalSection( &g_csSendNotice );
  1328. g_listSendSms.erase(it++);
  1329. LeaveCriticalSection( &g_csSendNotice );
  1330. }
  1331. }
  1332. }
  1333. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1334. return 0;
  1335. }
  1336. int GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
  1337. {
  1338. int iResult = iNormalState;
  1339. if ( ( iUpperLimit >0 || iLowerLimit > 0 ) && iUpperLimit > iLowerLimit)
  1340. {
  1341. //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
  1342. if( iParaValue > iUpperLimit )
  1343. {
  1344. iResult = UPPER_LIMIT_ID; // 上限告警
  1345. }
  1346. if( iParaValue < iLowerLimit )
  1347. {
  1348. iResult = LOWER_LIMIT_ID; // 下限告警
  1349. }
  1350. else
  1351. {
  1352. return iResult;
  1353. }
  1354. }
  1355. else if( (int)iParaValue != iNormalState )
  1356. {
  1357. iResult = (int)iParaValue;
  1358. }
  1359. return iResult;
  1360. }
  1361. int VarAlarmNotice(CDevice *pDev, CBaseVar *pBaseVar)
  1362. {
  1363. // 组织报警内容
  1364. CString strContect;
  1365. char szStatusDesc[MAX_EQUIP_DESC + 1] = {0};
  1366. char szStatusID[MAX_ID + 1] = {0};
  1367. memset(szStatusDesc, 0, sizeof(szStatusDesc));
  1368. if( pBaseVar->m_nWaringLevel == 0 ) return 1;
  1369. int nTempStatus = GetVarstatus(pBaseVar->m_dbData, pBaseVar->m_nUpperLimit, pBaseVar->m_nLowerLimit, pBaseVar->m_nNormalState);
  1370. if( UPPER_LIMIT_ID == nTempStatus ) // 上限告警
  1371. {
  1372. strcpy(szStatusDesc, g_strHintUpperLimit);
  1373. //LOG4C((LOG_NOTICE, "var desc = %s, %s", pBaseVar->m_strDesc, szStatusDesc));
  1374. }
  1375. else if( LOWER_LIMIT_ID == nTempStatus ) // 下限告警
  1376. {
  1377. strcpy(szStatusDesc, g_strHintLowerLimit);
  1378. }
  1379. else if( nTempStatus != pBaseVar->m_nNormalState )
  1380. {
  1381. list<string>::iterator it_desc, it_id;
  1382. if( pBaseVar->m_listStatusDesc.size() > 0 && pBaseVar->m_listStatusID.size() > 0 )
  1383. {
  1384. for( it_desc = pBaseVar->m_listStatusDesc.begin(), it_id = pBaseVar->m_listStatusID.begin();
  1385. it_desc != pBaseVar->m_listStatusDesc.end(), it_id != pBaseVar->m_listStatusID.end(); )
  1386. {
  1387. CString strStatusID = (*it_id++).c_str();
  1388. CString strStatusDesc = (*it_desc++).c_str();
  1389. if( atoi(strStatusID) == nTempStatus )
  1390. {
  1391. //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));
  1392. strcpy(szStatusDesc, (char *)(LPCTSTR)strStatusDesc);
  1393. //LOG4C((LOG_NOTICE, "szStatusDesc=%s", szStatusDesc));
  1394. break;
  1395. }
  1396. }
  1397. }
  1398. if( strcmp(szStatusDesc, "") == 0 )
  1399. {
  1400. //LOG4C((LOG_NOTICE, "VarAlarmNotice var = %s nTempStatus = %d NormalState = %d statudesc is null!", pBaseVar->m_strDesc, nTempStatus, pBaseVar->m_nNormalState));
  1401. return 1;
  1402. }
  1403. }
  1404. if( nTempStatus != pBaseVar->m_nNormalState ) // 报警状态
  1405. {
  1406. if( pBaseVar->m_bReserved1 == true )
  1407. {
  1408. if( pBaseVar->m_strReserved1.Compare("") == 0 )
  1409. {
  1410. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pBaseVar->m_dbData, pBaseVar->m_strUnit);
  1411. }
  1412. else
  1413. {
  1414. int nDeviceIndex = -1, nVarIndex = -1;
  1415. BOOL bFind = FindVar(pBaseVar->m_strVarUID, pBaseVar->m_strReserved1, nDeviceIndex, nVarIndex);
  1416. if( bFind == FALSE )
  1417. {
  1418. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pBaseVar->m_dbData, pBaseVar->m_strUnit);
  1419. }
  1420. else
  1421. {
  1422. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  1423. CBaseVar *pTempBaseVar = pDev->m_Vars[nVarIndex];
  1424. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pTempBaseVar->m_dbData, pBaseVar->m_strUnit);
  1425. }
  1426. }
  1427. }
  1428. else
  1429. {
  1430. strContect.Format("%s%s:%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc);
  1431. }
  1432. if( pBaseVar->m_bExistDatePlan )
  1433. {
  1434. if( pBaseVar->m_dwReturnNormalIdentityTick>0 )
  1435. {
  1436. pBaseVar->m_dwReturnNormalIdentityTick = 0;
  1437. //LOG4C(( LOG_NOTICE,"var=%s 恢复正常未达到辩识时间条件又收到报警值,放弃加入",pBaseVar->m_strDesc));
  1438. }
  1439. if( pBaseVar->m_dwIdentityTick == 0 && pBaseVar->m_nAlarmStatus!=2 )
  1440. {
  1441. //EnterCriticalSection( &g_csVarNotice );
  1442. pBaseVar->m_dwIdentityTick = GetTickCount();
  1443. //LeaveCriticalSection( &g_csVarNotice );
  1444. //LOG4C((LOG_NOTICE, "var=%s 达到报警条件", pBaseVar->m_strDesc));
  1445. }
  1446. else if( pBaseVar->m_nAlarmStatus==0 )
  1447. {
  1448. //LOG4C((LOG_NOTICE, "var=%s 第一次达到报警条件,不用判断辩识时间,直接加入", pBaseVar->m_strDesc));
  1449. ProcessVarNotice(
  1450. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1451. pBaseVar->m_nVarID,
  1452. pBaseVar->m_nUpperLimit,
  1453. pBaseVar->m_nLowerLimit,
  1454. pBaseVar->m_nOffset,
  1455. nTempStatus,
  1456. pBaseVar->m_nNormalState,
  1457. pBaseVar->m_nIdentifyTime,
  1458. pBaseVar->m_nReDetectTime,
  1459. pBaseVar->m_bNormalIsNotice,
  1460. "",
  1461. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1462. pBaseVar->m_dbData,
  1463. (char *)(LPCTSTR)strContect );
  1464. EnterCriticalSection( &g_csVarNotice );
  1465. pBaseVar->m_nAlarmStatus = 1;//设为第一次报警状态
  1466. pBaseVar->m_strContent = strContect;
  1467. pBaseVar->m_nLastStatus = nTempStatus;
  1468. pBaseVar->m_dwIdentityTick = 0;
  1469. LeaveCriticalSection( &g_csVarNotice );
  1470. }
  1471. else if( GetTickCount() - pBaseVar->m_dwIdentityTick > (DWORD)pBaseVar->m_nIdentifyTime * 1000 && pBaseVar->m_nAlarmStatus!=2 )
  1472. {
  1473. LOG4C((LOG_NOTICE, "var=%s 达到报警辩识时间条件,加入", pBaseVar->m_strDesc));
  1474. if( 1 == g_nSnmpEnable )
  1475. {
  1476. CString strSetValue;
  1477. strSetValue.Format("%.1f,A", pBaseVar->m_dbData);
  1478. CString strTime;
  1479. CTime tm;
  1480. tm = CTime::GetCurrentTime();
  1481. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  1482. CString strTrapContent = "";
  1483. strTrapContent.Format("%s_%s_%s,A", pDev->m_strDeviceName, strTime, strContect);
  1484. //LOG4C((LOG_NOTICE, "%s", strSetValue));
  1485. CString strObjID;
  1486. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1487. pSnmpDllSetValue(
  1488. g_strSnmpIP,
  1489. g_strSnmpField,
  1490. (char *)(LPCTSTR)strObjID,
  1491. "str",
  1492. 1,
  1493. 300,
  1494. (char *)(LPCTSTR)strTrapContent
  1495. );
  1496. }
  1497. if( pBaseVar->m_nAlarmNumber < 10 )
  1498. pBaseVar->m_nAlarmNumber++;
  1499. ProcessVarNotice(
  1500. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1501. pBaseVar->m_nVarID,
  1502. pBaseVar->m_nUpperLimit,
  1503. pBaseVar->m_nLowerLimit,
  1504. pBaseVar->m_nOffset,
  1505. nTempStatus,
  1506. pBaseVar->m_nNormalState,
  1507. pBaseVar->m_nIdentifyTime,
  1508. pBaseVar->m_nReDetectTime,
  1509. pBaseVar->m_bNormalIsNotice,
  1510. "",
  1511. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1512. pBaseVar->m_dbData,
  1513. (char *)(LPCTSTR)strContect );
  1514. EnterCriticalSection( &g_csVarNotice );
  1515. pBaseVar->m_nAlarmStatus = 2;//设为第二次报警状态
  1516. pBaseVar->m_strContent = strContect;
  1517. pBaseVar->m_nLastStatus = nTempStatus;
  1518. pBaseVar->m_dwIdentityTick = 0;
  1519. LeaveCriticalSection( &g_csVarNotice );
  1520. }
  1521. else
  1522. {
  1523. #if 0
  1524. if( 1 == g_nSnmpEnable )
  1525. {
  1526. if( TRUE == pBaseVar->m_bIsChange )
  1527. {
  1528. CString strSetValue;
  1529. strSetValue.Format("%.1f", pBaseVar->m_dbData);
  1530. CString strObjID;
  1531. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1532. pSnmpDllSetValue(
  1533. g_strSnmpIP,
  1534. g_strSnmpField,
  1535. (char *)(LPCTSTR)strObjID,
  1536. "str",
  1537. 1,
  1538. 300,
  1539. (char *)(LPCTSTR)strSetValue
  1540. );
  1541. }
  1542. }
  1543. #endif
  1544. }
  1545. }
  1546. }
  1547. else
  1548. {
  1549. if( pBaseVar->m_dwIdentityTick>0 )
  1550. {
  1551. pBaseVar->m_dwIdentityTick = 0;
  1552. //LOG4C(( LOG_NOTICE,"var=%s 报警未达到辩识时间条件又收到正常值,放弃加入",pBaseVar->m_strDesc));
  1553. }
  1554. if( pBaseVar->m_nAlarmNumber >= 1 && pBaseVar->m_nAlarmNumber < 11 )
  1555. {
  1556. strContect.Format("%s%s:%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, g_strHintReturnNormal);
  1557. if( pBaseVar->m_dwReturnNormalIdentityTick == 0 )
  1558. {
  1559. //EnterCriticalSection( &g_csVarNotice );
  1560. pBaseVar->m_dwReturnNormalIdentityTick = GetTickCount();
  1561. //LeaveCriticalSection( &g_csVarNotice );
  1562. //LOG4C((LOG_NOTICE, "var=%s 达到恢复正常条件", pBaseVar->m_strDesc));
  1563. }
  1564. else if( GetTickCount() - pBaseVar->m_dwReturnNormalIdentityTick > (DWORD)pBaseVar->m_nIdentifyTime * 1000 )
  1565. {
  1566. LOG4C((LOG_NOTICE, "var=%s 达到恢复正常辩识时间条件,加入", pBaseVar->m_strDesc));
  1567. pBaseVar->m_nAlarmNumber = 0;
  1568. //回复正常
  1569. ProcessVarNotice(
  1570. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1571. pBaseVar->m_nVarID,
  1572. pBaseVar->m_nUpperLimit,
  1573. pBaseVar->m_nLowerLimit,
  1574. pBaseVar->m_nOffset,
  1575. nTempStatus,
  1576. pBaseVar->m_nNormalState,
  1577. pBaseVar->m_nIdentifyTime,
  1578. pBaseVar->m_nReDetectTime,
  1579. pBaseVar->m_bNormalIsNotice,
  1580. "",
  1581. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1582. pBaseVar->m_dbData,
  1583. (char *)(LPCTSTR)strContect );
  1584. EnterCriticalSection( &g_csVarNotice );
  1585. pBaseVar->m_nAlarmStatus = 3;//设为恢复正常状态
  1586. pBaseVar->m_strContent = strContect;
  1587. pBaseVar->m_nLastStatus = nTempStatus;
  1588. pBaseVar->m_dwReturnNormalIdentityTick = 0;
  1589. LeaveCriticalSection( &g_csVarNotice );
  1590. if( 1 == g_nSnmpEnable )
  1591. {
  1592. CString strSetValue;
  1593. strSetValue.Format("%.1f,N", pBaseVar->m_dbData);
  1594. CString strTime;
  1595. CTime tm;
  1596. tm = CTime::GetCurrentTime();
  1597. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  1598. CString strTrapContent = "";
  1599. strTrapContent.Format("%s_%s_%s,N", pDev->m_strDeviceName, strTime, strContect);
  1600. //LOG4C((LOG_NOTICE, "%s", strTrapContent));
  1601. CString strObjID;
  1602. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1603. pSnmpDllSetValue(
  1604. g_strSnmpIP,
  1605. g_strSnmpField,
  1606. (char *)(LPCTSTR)strObjID,
  1607. "str",
  1608. 1,
  1609. 300,
  1610. (char *)(LPCTSTR)strTrapContent
  1611. );
  1612. }
  1613. }
  1614. }
  1615. else
  1616. {
  1617. #if 0
  1618. if( 1 == g_nSnmpEnable )
  1619. {
  1620. if( TRUE == pBaseVar->m_bIsChange )
  1621. {
  1622. CString strSetValue;
  1623. strSetValue.Format("%.1f", pBaseVar->m_dbData);
  1624. CString strObjID;
  1625. strObjID.Format("%s.%d", g_strSnmpObjOid, pBaseVar->m_nVarID);
  1626. pSnmpDllSetValue(
  1627. g_strSnmpIP,
  1628. g_strSnmpField,
  1629. (char *)(LPCTSTR)strObjID,
  1630. "str",
  1631. "str",
  1632. 1,
  1633. 300,
  1634. (char *)(LPCTSTR)strSetValue
  1635. );
  1636. }
  1637. }
  1638. #endif
  1639. }
  1640. }
  1641. return 0;
  1642. }