NoticeQueue.cpp 56 KB

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