NoticeQueue.cpp 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372
  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. #include <io.h>
  19. int g_nAlarmIndex=0;//报警索引,为了在IDE上面确认而设
  20. int g_nAlarmIndexLast=-1;//上一次的報警索引,確保每次報警生成Wav文件只一次
  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. int nCallTimes; // 拨打次数
  30. int nNoticeDelayTime; // 通知延时
  31. SYSTEMTIME time;
  32. }SEND_NOTICE, *PSEND_NOTICE;
  33. typedef list<SEND_NOTICE> LIST_SEND_NOTICE;
  34. LIST_SEND_NOTICE g_listSendVoice;
  35. LIST_SEND_NOTICE g_listSendSms;
  36. LIST_SEND_NOTICE g_listSendEmail;
  37. HANDLE g_hSendVoiceThread = NULL;
  38. HANDLE g_hSendSmsThread = NULL;
  39. HANDLE g_hSendEmailThread = NULL;
  40. HANDLE g_hCreateWavThread = NULL;
  41. HANDLE g_hCheckReDetectTimeThread = NULL;
  42. HANDLE g_hAWAThread = NULL;
  43. CRITICAL_SECTION g_csVarNotice; // 临界区对ProcessVarNotice过程全局变量进行保护
  44. CRITICAL_SECTION g_csSendVoice; // 电话临界区
  45. CRITICAL_SECTION g_csSendSms; // 短信临界区
  46. CRITICAL_SECTION g_csSendEmali; // 邮件临界区
  47. CRITICAL_SECTION g_csCreateWav;
  48. HANDLE g_hDeleteVarSem; // 所有读变量list, 都要等这个信号量,保证变量出队列成功后,才能读
  49. CStringArray g_saWavPath,g_saWavContent;
  50. // 删除所有变量
  51. void DeleteAllSendVoiceList();
  52. void DeleteAllSendSmsList();
  53. void DeleteAllSendEmailList();
  54. // 添加变量到語音list
  55. void InsertSendVoiceList(char *pDevUid, int nVarID, char *pTel, char *pContent,int nAlarmIndex);
  56. //IDE 确认后从語音list删除
  57. int DeleteSendVoiceList( char *pDevUid, int nVarID,int iAlarmIndex );
  58. // 添加变量到短信list
  59. void InsertSendSmsList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex);
  60. //IDE 确认后从短信list删除
  61. int DeleteSendSmsList( char *pDevUid, int nVarID,int iAlarmIndex );
  62. // 添加变量到邮件list
  63. void InsertSendEmailList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex);
  64. //IDE 确认后从邮件list删除
  65. int DeleteSendEmailList( char *pDevUid, int nVarID,int iAlarmIndex );
  66. static DWORD WINAPI SendVoiceThread(LPVOID lpParameter);
  67. static DWORD WINAPI SendSmsThread(LPVOID lpParameter);
  68. static DWORD WINAPI SendEmailThread(LPVOID lpParameter);
  69. static DWORD WINAPI CreateWavThread(LPVOID lpParameter);
  70. static DWORD WINAPI CheckReDetectTimeThread(LPVOID lpParameter);
  71. static DWORD WINAPI AWAThread(LPVOID lpParameter);
  72. void GetAWAData()
  73. {
  74. //LOG4C((LOG_NOTICE, "GetAllActionWithAlarm in"));
  75. CDBInterface::GetInstancePtr()->GetAllActionWithAlarm();
  76. if( g_AWA[0].sDoSet!="" )
  77. {
  78. DWORD dwThreadId;
  79. MTVERIFY( g_hAWAThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)AWAThread,NULL, 0, &dwThreadId) );
  80. //LOG4C((LOG_NOTICE, "GetAllActionWithAlarm OK"));
  81. }
  82. }
  83. /************************************************************************/
  84. /* 函数:[1/26/2017 Home];
  85. /* 描述:初始化通知库;
  86. /* 参数:;
  87. /* [IN] :;
  88. /* [OUT] :;
  89. /* [IN/OUT] :;
  90. /* 返回:void;
  91. /* 注意:;
  92. /* 示例:;
  93. /*
  94. /* 修改:;
  95. /* 日期:;
  96. /* 内容:;
  97. /************************************************************************/
  98. int InitNotice(void)// 1语音卡不存在 2动态库不存在
  99. {
  100. if( g_hCheckReDetectTimeThread==NULL )
  101. {
  102. DWORD dwThreadId2;
  103. MTVERIFY( g_hCheckReDetectTimeThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CheckReDetectTimeThread,
  104. NULL, 0, &dwThreadId2) );
  105. }
  106. MTVERIFY( g_hDeleteVarSem = CreateEvent(NULL, TRUE, TRUE, "DeleteVarList") );
  107. // 语音卡;
  108. InitializeCriticalSection( &g_csVarNotice );
  109. // 短信猫;
  110. InitializeCriticalSection( &g_csSendVoice );
  111. // 语音文件;
  112. InitializeCriticalSection( &g_csCreateWav );
  113. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  114. // 初始化远程报警通知;
  115. if( g_bSendVoice )
  116. {
  117. g_hVoiceLibModule = NULL;
  118. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  119. if( g_nNoticeCardType == 1 )
  120. {
  121. wsprintf(strFile, "%s\\Dll\\Voice.dll", g_strDirectory);
  122. g_hVoiceLibModule = ::LoadLibrary(strFile);
  123. if (NULL != g_hVoiceLibModule)
  124. {
  125. pStoneUDllRegisterVoice = (STONEU_DLLRegisterVoice)::GetProcAddress(g_hVoiceLibModule, "STONEU_DLLRegisterVoice");
  126. pStoneUDllUnRegisterVoice = (STONEU_DLLUnRegisterVoice)::GetProcAddress(g_hVoiceLibModule, "STONEU_DLLUnRegisterVoice");
  127. pStoneUDllAllocateChannel = (STONEU_DLLAllocateChannel)::GetProcAddress(g_hVoiceLibModule, "STONEU_DLLAllocateChannel");
  128. }
  129. if( ( NULL != g_hVoiceLibModule ) && ( NULL != pStoneUDllRegisterVoice ) )
  130. {
  131. int nRet = pStoneUDllRegisterVoice(g_strDirectory, g_nTTSType);
  132. if( nRet != 0 )
  133. {
  134. AfxFreeLibrary( g_hVoiceLibModule );
  135. g_hVoiceLibModule = NULL;
  136. g_bSendVoice = FALSE;
  137. return 1;
  138. }
  139. }
  140. else
  141. {
  142. AfxFreeLibrary( g_hVoiceLibModule );
  143. g_hVoiceLibModule = NULL;
  144. g_bSendVoice = FALSE;
  145. return 2;
  146. }
  147. }
  148. else if( g_nNoticeCardType == 2 )
  149. {
  150. wsprintf(strFile, "%s\\Dll\\UsbVoice.dll", g_strDirectory);
  151. g_hVoiceLibModule = ::LoadLibrary(strFile);
  152. if( NULL != g_hVoiceLibModule )
  153. {
  154. pStoneUDllRegisterVoice = (STONEU_DLLRegisterVoice)::GetProcAddress(g_hVoiceLibModule, "STONEU_DLLRegisterVoice");
  155. pStoneUDllUnRegisterVoice = (STONEU_DLLUnRegisterVoice)::GetProcAddress(g_hVoiceLibModule, "STONEU_DLLUnRegisterVoice");
  156. pStoneUDllAllocateChannel = (STONEU_DLLAllocateChannel)::GetProcAddress(g_hVoiceLibModule, "STONEU_DLLAllocateChannel");
  157. }
  158. if( ( NULL != g_hVoiceLibModule ) && ( NULL != pStoneUDllRegisterVoice ) )
  159. {
  160. int nRet = pStoneUDllRegisterVoice(g_strDirectory, g_nTTSType);
  161. if( nRet != 0 )
  162. {
  163. AfxFreeLibrary( g_hVoiceLibModule );
  164. g_hVoiceLibModule = NULL;
  165. g_bSendVoice = 0;
  166. return 1;
  167. }
  168. }
  169. else
  170. {
  171. AfxFreeLibrary( g_hVoiceLibModule );
  172. g_hVoiceLibModule = NULL;
  173. g_bSendVoice = 0;
  174. return 2;
  175. }
  176. }
  177. DWORD dwThreadId;
  178. MTVERIFY( g_hSendVoiceThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendVoiceThread,
  179. NULL, 0, &dwThreadId) );
  180. MTVERIFY( g_hCreateWavThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)CreateWavThread,
  181. NULL, 0, &dwThreadId) );
  182. //初始化TTS
  183. hTTSModule = NULL;
  184. wsprintf(strFile, "%s\\Dll\\TTSDll.dll", g_strDirectory);
  185. hTTSModule = ::LoadLibrary(strFile);
  186. if (NULL != hTTSModule)
  187. {
  188. pStoneUDllRegisterTTS = (MySTONEU_DLLRegisterTTS)::GetProcAddress(hTTSModule, "STONEU_DLLRegisterTTS");
  189. pStoneUDllUnRegisterTTS = (MySTONEU_DLLUnRegisterTTS)::GetProcAddress(hTTSModule, "STONEU_DLLUnRegisterTTS");
  190. pStoneUDLLTxtToWav = (MySTONEU_DLLTxtToWav)::GetProcAddress(hTTSModule, "STONEU_DLLTxtToWav");
  191. }
  192. if( ( NULL != hTTSModule ) && ( NULL != pStoneUDllRegisterTTS ) )
  193. {
  194. if( !pStoneUDllRegisterTTS() )
  195. {
  196. FreeLibrary( hTTSModule );
  197. hTTSModule = NULL;
  198. g_bSendVoice = 0;
  199. return 1;
  200. }
  201. }
  202. else
  203. {
  204. FreeLibrary( hTTSModule );
  205. hTTSModule = NULL;
  206. g_bSendVoice = 0;
  207. return 2;
  208. }
  209. }
  210. return 0;
  211. }
  212. void UnInitNotice(void)
  213. {
  214. if( g_hCheckReDetectTimeThread )
  215. {
  216. MTVERIFY( WaitForSingleObject( g_hCheckReDetectTimeThread, INFINITE ) != WAIT_FAILED );
  217. MTVERIFY( CloseHandle(g_hCheckReDetectTimeThread) );
  218. g_hCheckReDetectTimeThread = NULL;
  219. }
  220. if( g_hSendVoiceThread )
  221. {
  222. //MTVERIFY( SetEvent( g_hDeleteVarSem ) );
  223. MTVERIFY( WaitForSingleObject( g_hSendVoiceThread, INFINITE ) != WAIT_FAILED );
  224. MTVERIFY( CloseHandle(g_hSendVoiceThread) );
  225. g_hSendVoiceThread = NULL;
  226. }
  227. if( g_hCreateWavThread )
  228. {
  229. //MTVERIFY( SetEvent( g_hDeleteVarSem ) );
  230. MTVERIFY( WaitForSingleObject( g_hCreateWavThread, INFINITE ) != WAIT_FAILED );
  231. MTVERIFY( CloseHandle(g_hCreateWavThread) );
  232. g_hCreateWavThread = NULL;
  233. }
  234. if( g_hDeleteVarSem )
  235. {
  236. MTVERIFY( CloseHandle( g_hDeleteVarSem) );
  237. g_hDeleteVarSem = NULL;
  238. }
  239. if( g_bSendVoice )
  240. {
  241. if( ( NULL != g_hVoiceLibModule ) && ( NULL != pStoneUDllUnRegisterVoice ) )
  242. pStoneUDllUnRegisterVoice();
  243. if( ( NULL != hTTSModule ) && ( NULL != pStoneUDllUnRegisterTTS ) )
  244. pStoneUDllUnRegisterTTS();
  245. }
  246. DeleteAllSendEmailList();
  247. DeleteAllSendSmsList();
  248. DeleteAllSendVoiceList();
  249. DeleteAllVarNoticeList();
  250. DeleteCriticalSection( &g_csVarNotice );
  251. DeleteCriticalSection( &g_csSendVoice );
  252. DeleteCriticalSection( &g_csCreateWav );
  253. if (g_hVoiceLibModule != NULL)
  254. {
  255. ::FreeLibrary(g_hVoiceLibModule);
  256. g_hVoiceLibModule = NULL;
  257. }
  258. if (hTTSModule != NULL)
  259. {
  260. FreeLibrary(hTTSModule);
  261. hTTSModule = NULL;
  262. }
  263. }
  264. int InitEmail(char *pSmtpServer, // Smtp服务器
  265. int nSmtpPort, // Smtp端口
  266. int nIsNeed, // 是否需要smtp身份验证
  267. char *pUserAcc, // Email帐号
  268. char *pUserPwd, // Email密码
  269. char *pFromEmailAddr, // 发送人Email地址
  270. int nTimeOut // 超时
  271. )
  272. {
  273. InitializeCriticalSection( &g_csSendEmali );
  274. DWORD dwThreadId2;
  275. MTVERIFY( g_hSendEmailThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendEmailThread,NULL, 0, &dwThreadId2) );
  276. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  277. g_hEmailLibModule = NULL;
  278. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  279. wsprintf(strFile, "%s\\Dll\\Email.dll", g_strDirectory);
  280. g_hEmailLibModule = AfxLoadLibrary(strFile);
  281. if ( g_hEmailLibModule == NULL)
  282. {
  283. printf("加载Email.dll失败,错误码:%d\n", GetLastError());
  284. return -1;
  285. }
  286. if (NULL != g_hEmailLibModule)
  287. {
  288. pEmailDllInit = (EMAIL_DLLInit)::GetProcAddress(g_hEmailLibModule, "EMAIL_DLLInit");
  289. pEmailDllUnInit = (EMAIL_DLLUnInit)::GetProcAddress(g_hEmailLibModule, "EMAIL_DLLUnInit");
  290. pEmailDllSendEmail = (EMAIL_DLLSendEmail)::GetProcAddress(g_hEmailLibModule, "EMAIL_DLLSendEmail");
  291. }
  292. if( ( NULL != g_hEmailLibModule ) && ( NULL != pEmailDllInit ) )
  293. {
  294. int nRet = pEmailDllInit( g_strDirectory,
  295. pSmtpServer,
  296. nSmtpPort,
  297. nIsNeed,
  298. pUserAcc,
  299. pUserPwd,
  300. pFromEmailAddr,
  301. nTimeOut
  302. );
  303. if( nRet != 0 )
  304. {
  305. AfxFreeLibrary( g_hEmailLibModule );
  306. g_hEmailLibModule = NULL;
  307. return 1;
  308. }
  309. }
  310. else
  311. {
  312. AfxFreeLibrary( g_hEmailLibModule );
  313. g_hEmailLibModule = NULL;
  314. }
  315. return 0;
  316. }
  317. int UnInitEmail()
  318. {
  319. if( g_hSendEmailThread )
  320. {
  321. MTVERIFY( WaitForSingleObject( g_hSendEmailThread, INFINITE ) != WAIT_FAILED );
  322. MTVERIFY( CloseHandle(g_hSendEmailThread) );
  323. g_hSendEmailThread = NULL;
  324. }
  325. if( NULL != g_hEmailLibModule && NULL != pEmailDllUnInit )
  326. {
  327. pEmailDllUnInit();
  328. AfxFreeLibrary( g_hEmailLibModule );
  329. g_hEmailLibModule = NULL;
  330. }
  331. DeleteCriticalSection( &g_csSendEmali );
  332. return 0;
  333. }
  334. int InitSms(int nAddr, // 保留,因为短信猫是串口232没有地址
  335. int nCommPort, // 串口号
  336. int nRate, // 波特率
  337. int nDataBit, // 数据位
  338. int nStopBit, // 停止位
  339. int nParity, // 校验位
  340. int nInterval, // 间隔时间
  341. int nNeedLanguageTrans, // 是否需要语言转换
  342. int nMaxChar, // 短信猫一条短信最大支持的字符个数
  343. int nMakeCall)
  344. {
  345. InitializeCriticalSection( &g_csSendSms );
  346. DWORD dwThreadId2;
  347. MTVERIFY( g_hSendSmsThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)SendSmsThread,NULL, 0, &dwThreadId2) );
  348. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  349. g_hSmsLibModule = NULL;
  350. CHAR strFile[MAX_LIST_LENGTH + 1] = "";
  351. wsprintf(strFile, "%s\\Dll\\sms.dll", g_strDirectory);
  352. g_hSmsLibModule = AfxLoadLibrary(strFile);
  353. if ( g_hSmsLibModule == NULL )
  354. {
  355. printf("加载SMS.dll失败,错误码:%d\n", GetLastError());
  356. return -1;
  357. }
  358. if (NULL != g_hSmsLibModule)
  359. {
  360. pSmsDllInit = (SMS_DLLInit)::GetProcAddress(g_hSmsLibModule, "SMS_DLLInit");
  361. pSmsDllUnInit = (SMS_DLLUnInit)::GetProcAddress(g_hSmsLibModule, "SMS_DLLUnInit");
  362. pSmsDllSendSms = (SMS_DLLSendSms)::GetProcAddress(g_hSmsLibModule, "SMS_DLLSendSms");
  363. pSmsDllGetCSQ = (SMS_DLLGetCSQ)::GetProcAddress(g_hSmsLibModule, "SMS_DLLGetCSQ");
  364. pSmsDllSetCallBack = (SMS_DLLSetCallBack)::GetProcAddress(g_hSmsLibModule, "SMS_DLLSetCallBack");
  365. pSmsGetBalance = (SMS_DLLGetBalance)::GetProcAddress(g_hSmsLibModule, "SMS_DLLGetBalance");
  366. }
  367. if( ( NULL != g_hSmsLibModule ) && ( NULL != pSmsDllInit ) )
  368. {
  369. int nRet = pSmsDllInit(g_strDirectory,
  370. nAddr,
  371. nCommPort,
  372. nRate,
  373. nDataBit,
  374. nStopBit,
  375. nParity,
  376. nInterval,
  377. nNeedLanguageTrans,
  378. nMaxChar,
  379. nMakeCall
  380. );
  381. if( nRet != 0 )
  382. {
  383. LOG4C((LOG_NOTICE, "打开 COM%d 失败,不能进行短信操作",nCommPort));
  384. pSmsDllInit = NULL;
  385. pSmsDllUnInit = NULL;
  386. pSmsDllSendSms = NULL;
  387. pSmsDllGetCSQ = NULL;
  388. pSmsDllSetCallBack = NULL;
  389. pSmsGetBalance = NULL;
  390. AfxFreeLibrary( g_hSmsLibModule );
  391. g_hSmsLibModule = NULL;
  392. g_nSendSms = 0;
  393. return 1;
  394. }
  395. }
  396. else
  397. {
  398. LOG4C((LOG_NOTICE, "找不到 %s ,不能进行短信操作",strFile));
  399. pSmsDllInit = NULL;
  400. pSmsDllUnInit = NULL;
  401. pSmsDllSendSms = NULL;
  402. pSmsDllGetCSQ = NULL;
  403. pSmsDllSetCallBack = NULL;
  404. pSmsGetBalance = NULL;
  405. AfxFreeLibrary( g_hSmsLibModule );
  406. g_hSmsLibModule = NULL;
  407. g_nSendSms = 0;
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. int UnInitSms()
  413. {
  414. if( g_hSendSmsThread )
  415. {
  416. MTVERIFY( WaitForSingleObject( g_hSendSmsThread, INFINITE ) != WAIT_FAILED );
  417. MTVERIFY( CloseHandle(g_hSendSmsThread) );
  418. g_hSendSmsThread = NULL;
  419. }
  420. if( NULL != g_hSmsLibModule && NULL != pSmsDllUnInit )
  421. {
  422. pSmsDllUnInit();
  423. AfxFreeLibrary( g_hSmsLibModule );
  424. g_hSmsLibModule = NULL;
  425. }
  426. DeleteCriticalSection( &g_csSendSms );
  427. return 0;
  428. }
  429. ///////////////////////////////////////////////////////////////////////////////////////////////
  430. // 把短信信息写入TXT,让短信模块读取
  431. INT WriteToTxt(CHAR *pDevUid, int nVarID, CHAR *pContent,int iAlarmIndex )
  432. {
  433. CString strMobileTel;
  434. char szResultMsg[MAX_MSG_LENGTH + 1] = {0};
  435. int nResult = -1;
  436. memset(szResultMsg, 0, sizeof(szResultMsg));
  437. CString sPathName;
  438. sPathName.Format( "%s\\Log\\SupportSmsPlatform.txt",g_strDirectory );
  439. CString strTemp;
  440. CStdioFile MyFile;
  441. if(!MyFile.Open(sPathName,CFile::modeWrite))
  442. MyFile.Open(sPathName,CFile::modeCreate|CFile::modeWrite);
  443. for( int i = 0; i < (int)g_vtUserRoleInfo.size(); i++ )
  444. {
  445. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  446. {
  447. for( int j = 0; j < (int)g_vtUserRoleInfo[i].vtUserInfo.size(); j++ )
  448. {
  449. strMobileTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szMobileTel);
  450. if( strMobileTel.GetLength()<11 ) continue; //判断是不是手机号码 只根据长度
  451. if( strMobileTel.Compare("") )
  452. {
  453. CTime time = CTime::GetCurrentTime();
  454. CString strTime;
  455. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  456. strTemp.Format("%d***%s***%s***%s\n",iAlarmIndex,strTime,strMobileTel,pContent);
  457. MyFile.SeekToEnd();
  458. MyFile.WriteString(strTemp);
  459. }
  460. }
  461. }
  462. }
  463. MyFile.Close();
  464. return 0;
  465. }
  466. ///////////////////////////////////////////////////////////////////////////////////////////////
  467. ///////////////////////////////////////////////////////////////////////////////////////////////
  468. ////// 函数名称:SendNotice ///////
  469. ////// 函数说明:开始语音通知 ///////
  470. ////// 参数列表:strTel -- 待通知的电话号码 ///////
  471. ////// strContent -- 短信内容 ///////
  472. ////// 作 者:sailing ///////
  473. ////// 创者时间:2009-02-23 ///////
  474. ///////////////////////////////////////////////////////////////////////////////////////////////
  475. INT SendNotice(CHAR *pUid, int nVarID,int nAlarmIndex, CHAR *pTel, CHAR *pContent)
  476. {
  477. //如果是短信确认关闭报警通知则退出
  478. if( !g_bAlarmNoticeFlag )
  479. return -1;
  480. int nRet = 0;
  481. CString str, strMsg;
  482. str.Format( "%s%s%s",g_strNoticePre,pContent,g_strHintVoiceEnd ); // g_strHintAlarmWelcome +
  483. //str = str + g_strHintVoiceEnd; //" 重新请按1,确认请按2,结束请直接挂机";
  484. if( ( NULL != g_hVoiceLibModule ) && (NULL != pStoneUDllAllocateChannel) )
  485. {
  486. //strMsg.Format("开始发送语音通知!", pTel);
  487. //AddToPrintQueue( MSG_OK, MSG_NOTICE_MD, (char*)(LPCTSTR)strMsg, strMsg.GetLength() );
  488. //LOG4C((LOG_NOTICE, "str = %s", str));
  489. int nResult = pStoneUDllAllocateChannel( pTel, g_strNoticeTelPre, pUid, nVarID,nAlarmIndex, (char *)(LPCTSTR)str );
  490. nRet = nResult;
  491. if( nRet != CALL_LOST )
  492. {
  493. CTime time = CTime::GetCurrentTime();
  494. CString strTime;
  495. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  496. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  497. pTel,
  498. "",
  499. (char *)(LPCTSTR)strTime,
  500. (char *)(LPCTSTR)pContent,
  501. "");
  502. }
  503. }
  504. return nRet;
  505. }
  506. // 发送Sms
  507. INT SendSms(CHAR *pDevUid, int nVarID, CHAR *pTel, CHAR *pContent)
  508. {
  509. CString strMobileTel;
  510. char szResultMsg[MAX_MSG_LENGTH + 1] = {0};
  511. int nResult = -1;
  512. memset(szResultMsg, 0, sizeof(szResultMsg));
  513. strMobileTel.Format( "%s",pTel );
  514. CTime time = CTime::GetCurrentTime();
  515. CString strTime;
  516. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  517. if( pSmsDllSendSms != NULL && g_bAlarmNoticeFlag )
  518. {
  519. if( strMobileTel.GetLength()<11 )
  520. {
  521. LOG4C((LOG_NOTICE, "短信号码有误, tel: %s", strMobileTel));
  522. return 0; //判断是不是手机号码 只根据长度
  523. }
  524. CString sTemp;
  525. sTemp.Format( "%s%s",g_strNoticePre,pContent );
  526. nResult = pSmsDllSendSms((char *)(LPCTSTR)strMobileTel, (char *)(LPCTSTR)sTemp, szResultMsg);
  527. if( nResult != 0 ) // 发送失败
  528. {
  529. // 发送失败,重试5次,如果还是失败,则放弃
  530. for( int iReSend = 0; iReSend < 5; iReSend++ )
  531. {
  532. nResult = pSmsDllSendSms((char *)(LPCTSTR)strMobileTel, (char *)(LPCTSTR)sTemp, szResultMsg);
  533. if( nResult == 0 )
  534. {
  535. CString strTemp = "";
  536. strTemp.Format("send sms success, tel: %s, content: %s", strMobileTel, pContent);
  537. LOG4C((LOG_NOTICE, "send sms success, tel: %s, content: %s, code: %s,%d", strMobileTel, pContent,szResultMsg,iReSend+1));
  538. CDBInterface::GetInstancePtr()->InsertSmsRecord(
  539. (char *)(LPCTSTR)strMobileTel,
  540. "",
  541. (char *)(LPCTSTR)strTime,
  542. (char *)(LPCTSTR)strTemp,
  543. "");
  544. break;
  545. }
  546. else
  547. {
  548. if( iReSend==4 )
  549. {
  550. int nNowData=0;
  551. if( !pSmsDllGetCSQ( 15,31,nNowData ) )//短信猫信号弱
  552. {
  553. CDBInterface::GetInstancePtr()->InsertSmsRecord(
  554. "",
  555. "",
  556. (char *)(LPCTSTR)strTime,
  557. "send sms lost,Because the signal is too weak",
  558. "");
  559. LOG4C((LOG_NOTICE, "send sms tel: %s lost,Because the signal=%d is too weak",strMobileTel,nNowData ));
  560. return 0;
  561. }
  562. LOG4C((LOG_NOTICE, "send sms lost, tel: %s, content: %s, code: %s", strMobileTel, pContent, szResultMsg));
  563. }
  564. }
  565. Sleep( 100 );
  566. }
  567. }
  568. else
  569. {
  570. CDBInterface::GetInstancePtr()->InsertSmsRecord(
  571. (char *)(LPCTSTR)strMobileTel,
  572. "",
  573. (char *)(LPCTSTR)strTime,
  574. pContent,
  575. "");
  576. LOG4C((LOG_NOTICE, "send sms success, tel: %s, content: %s, code: %s", strMobileTel, pContent, szResultMsg));
  577. }
  578. }
  579. return 0;
  580. }
  581. // 发送Email
  582. INT SendEmail(CHAR *pDevUid, int nVarID, CHAR *pContent)
  583. {
  584. int nRet = -1;
  585. CString strEmail;
  586. char szResultMsg[MAX_MSG_LENGTH + 1] = {0};
  587. int nResult = -1;
  588. memset( szResultMsg, 0, sizeof(szResultMsg) );
  589. for( int i = 0; i < (int)g_vtUserRoleInfo.size(); i++ )
  590. {
  591. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  592. {
  593. nRet = 0;
  594. for( int j = 0; j < (int)g_vtUserRoleInfo[i].vtUserInfo.size(); j++ )
  595. {
  596. strEmail = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szEmail);
  597. if( strEmail.Compare("") )
  598. {
  599. CTime time = CTime::GetCurrentTime();
  600. CString strTime;
  601. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  602. if( pEmailDllSendEmail )
  603. {
  604. CString sTemp;
  605. sTemp.Format( "%s%s",g_strNoticePre,pContent );
  606. nResult = pEmailDllSendEmail(
  607. (char *)(LPCSTR)strEmail,
  608. g_strEmailSubject,
  609. (char *)(LPCSTR)sTemp,
  610. (char *)(LPCTSTR)strTime,
  611. szResultMsg );
  612. if( nResult != 0 ) //发送失败
  613. {
  614. CString sError;
  615. sError.Format( "Contect:%s send lost: %s", pContent, szResultMsg );
  616. CDBInterface::GetInstancePtr()->InsertEmailRecord(
  617. (char *)(LPCTSTR)strEmail,
  618. "",
  619. (char *)(LPCTSTR)strTime,
  620. (char *)(LPCTSTR)sError,
  621. "");
  622. LOG4C((LOG_NOTICE, "Email to %s, Contect:%s send lost: %s", strEmail, pContent, szResultMsg));
  623. }
  624. else
  625. {
  626. CDBInterface::GetInstancePtr()->InsertEmailRecord(
  627. (char *)(LPCTSTR)strEmail,
  628. "",
  629. (char *)(LPCTSTR)strTime,
  630. pContent,
  631. "");
  632. LOG4C((LOG_NOTICE, "Email to %s, Contect:%s send success: %s", strEmail, pContent, szResultMsg));
  633. }
  634. }
  635. }
  636. }
  637. }
  638. }
  639. return nRet;
  640. }
  641. // 添加有管理某个设备所有用户的电话号码和手机号码
  642. void InsertUserTel(VAR_NOTICE *pVarNotice)
  643. {
  644. CString strTel;
  645. int nDevSize = (int)g_vtUserRoleInfo.size();
  646. for( int i = 0; i < nDevSize; i++ )
  647. {
  648. //LOG4C((LOG_NOTICE, "g_vtUserRoleInfo[i].szDevUID=%s", g_vtUserRoleInfo[i].szDevUID));
  649. //TRACE("设备UID=%s\r\n", g_vtUserRoleInfo[i].szDevUID);
  650. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pVarNotice->szDevUID) == 0 )
  651. {
  652. int nUserSize = (int)g_vtUserRoleInfo[i].vtUserInfo.size();
  653. for( int j = 0; j < nUserSize; j++ )
  654. {
  655. // 加入电话号码
  656. strTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szTel);
  657. if( strTel.Compare("") )
  658. {
  659. pVarNotice->listTel.insert(pVarNotice->listTel.end(), strTel);
  660. //TRACE1("电话号码:%s\r\n", strTel);
  661. }
  662. #if 0 //移动电话 不打电话
  663. strTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szMobileTel);
  664. if( strTel.Compare("") )
  665. {
  666. pVarNotice->listTel.insert(pVarNotice->listTel.end(), strTel);
  667. //TRACE1("手机号码:%s\r\n", strTel);
  668. }
  669. #endif
  670. }
  671. }
  672. }
  673. }
  674. // 更新要发送对象的手机号码
  675. INT UpdateActiveTel( CHAR *pDevUid, CHAR *pStrTel )
  676. {
  677. int nSize = g_vtUserRoleInfo.size();
  678. for( int i = 0; i < nSize; i++ )
  679. {
  680. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid) == 0 )
  681. {
  682. //g_vtUserTelInfo[i].strActiveTel = pStrTel;
  683. }
  684. }
  685. return 0;
  686. }
  687. // 报警状态恢复正常处理
  688. void ProcessVarStatusReturnNormal(char *pDevUid,
  689. int nVarID,
  690. int nAlarmStatus,
  691. int nNormalStatus,
  692. int nIdenfityTime,
  693. int nReDetectTime,
  694. int nNormalIsNotice,
  695. char *pDevName,
  696. char *pVarDesc,
  697. char *pDevArea,
  698. double dbData,
  699. char *pContent,
  700. VAR_NOTICE *pVarNotice,
  701. int nUpperLimit,
  702. int nLowerLimit,
  703. int nWarningLevel )
  704. {
  705. if( nNormalIsNotice ) // 恢复正常触发远程报警
  706. {
  707. int nDeviceIndex = -1, nVarIndex = -1;
  708. BOOL bFind = FindVar(pDevUid, nVarID, nDeviceIndex, nVarIndex);
  709. if( bFind == TRUE )
  710. {
  711. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  712. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  713. EnterCriticalSection( &g_csVarNotice );
  714. pBaseVar->m_nAlarmNumber = 0;
  715. pVarNotice->nLastStatus = nAlarmStatus;
  716. pVarNotice->nCallTime = 0;
  717. pVarNotice->nSmsTime = 0;
  718. LeaveCriticalSection( &g_csVarNotice );
  719. }
  720. // 语音通知
  721. if( g_bSendVoice )
  722. {
  723. if( nWarningLevel>=g_nLevelWav )
  724. {
  725. LIST_TEL::iterator it;
  726. CString *pStrTel;
  727. for( it = pVarNotice->listTel.begin(); it != pVarNotice->listTel.end(); it++ )
  728. {
  729. pStrTel = &(*it);
  730. // 加入语音发送list
  731. EnterCriticalSection( &g_csVarNotice );
  732. //LOG4C((LOG_NOTICE, "拔打恢复正常电话,号码:%s, 内容:%s", (char *)(LPCTSTR)*pStrTel, pContent));
  733. InsertSendVoiceList(pDevUid, nVarID, (char *)(LPCTSTR)*pStrTel, pContent,g_nAlarmIndex);
  734. //pVarNotice->listTel.erase(it);
  735. LeaveCriticalSection( &g_csVarNotice );
  736. }
  737. }
  738. else
  739. LOG4C((LOG_NOTICE, "没有达到语音通知级别 nWarningLevel = %d, g_nLevelWav = %d", nWarningLevel,g_nLevelWav ));
  740. }
  741. //发送恢复正常信息给客户端
  742. g_pServerSocket[1]->AutoSendAlarmDataToClient( pDevUid,nVarID,1,pContent,dbData,nUpperLimit,nLowerLimit,nNormalStatus,nAlarmStatus,nWarningLevel,g_nAlarmIndex,g_nNoticeDelay );
  743. g_nDealAWAStatus = 3;//处理声光报警状态 -1未初始化 0初始化 1在处理中 2处理完成 3新事件
  744. // 短信通知
  745. if( g_nSendSms==1 )
  746. {
  747. if( nWarningLevel>=g_nLevelSms )
  748. InsertSendSmsList( pDevUid, nVarID, pContent,g_nAlarmIndex );//SendSms(pDevUid, nVarID, pContent);
  749. else
  750. LOG4C((LOG_NOTICE, "没有达到短信通知级别 nWarningLevel = %d, g_nLevelWav = %d", nWarningLevel,g_nLevelSms ));
  751. }
  752. else if( g_nSendSms==2 )//支持短信平台
  753. {
  754. WriteToTxt(pDevUid, nVarID, pContent,g_nAlarmIndex);
  755. }
  756. // Email通知
  757. if( g_nSendEmail )
  758. {
  759. if( nWarningLevel>=g_nLevelEmail )
  760. InsertSendEmailList( pDevUid, nVarID, pContent,g_nAlarmIndex );//SendEmail(pDevUid, nVarID, pContent);
  761. else
  762. LOG4C((LOG_NOTICE, "没有达到邮件通知级别 nWarningLevel = %d, g_nLevelWav = %d", nWarningLevel,g_nLevelEmail ));
  763. }
  764. }
  765. g_nAlarmIndex++;
  766. if( g_nAlarmIndex>=5000 )
  767. g_nAlarmIndex = 0;
  768. CString strTime;
  769. CTime tm;
  770. tm = CTime::GetCurrentTime();
  771. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  772. CDBInterface::GetInstancePtr()->InsertWarningRecord(pDevName, pVarDesc, (char *)(LPCTSTR)strTime, nAlarmStatus, dbData, pDevArea, pContent);
  773. }
  774. void ProcessVarAlarm(char *pDevUid,
  775. int nVarID,
  776. int nAlarmStatus,
  777. int nNormalStatus,
  778. int nIdenfityTime,
  779. int nReDetectTime,
  780. int nNormalIsNotice,
  781. char *pDevName,
  782. char *pVarDesc,
  783. char *pDevArea,
  784. double dbData,
  785. char *pContent,
  786. VAR_NOTICE *pVarNotice,
  787. int nUpperLimit,
  788. int nLowerLimit,
  789. int nWarningLevel )
  790. {
  791. //// add 2011-09-30
  792. //int nDeviceIndex = -1, nVarIndex = -1;
  793. //BOOL bFind = FindVar(pDevUid, nVarID, nDeviceIndex, nVarIndex);
  794. //if( bFind == TRUE )
  795. //{
  796. // CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  797. // CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  798. // EnterCriticalSection( &g_csVarNotice );
  799. // pBaseVar->m_nAlarmNumber = 0;
  800. // pVarNotice->nLastStatus = nAlarmStatus;
  801. // pVarNotice->nCallTime = 0;
  802. // pVarNotice->nSmsTime = 0;
  803. // LeaveCriticalSection( &g_csVarNotice );
  804. //}// add 2011-09-30
  805. //语音通知
  806. if( g_bSendVoice )
  807. {
  808. if( nWarningLevel>=g_nLevelWav )
  809. {
  810. LIST_TEL::iterator it;
  811. CString *pStrTel;
  812. for( it = pVarNotice->listTel.begin(); it != pVarNotice->listTel.end(); it++ )
  813. {
  814. pStrTel = &(*it);
  815. // 加入语音发送list
  816. EnterCriticalSection( &g_csVarNotice );
  817. //LOG4C((LOG_NOTICE, "拔打报警电话,号码:%s, 内容:%s", (char *)(LPCTSTR)*pStrTel, pContent));
  818. InsertSendVoiceList(pDevUid, nVarID, (char *)(LPCTSTR)*pStrTel, pContent,g_nAlarmIndex);
  819. LeaveCriticalSection( &g_csVarNotice );
  820. }
  821. }
  822. else
  823. LOG4C((LOG_NOTICE, "没有达到语音通知级别 nWarningLevel = %d, g_nLevelWav = %d", nWarningLevel,g_nLevelWav ));
  824. }
  825. //发送报警信息给客户端
  826. g_pServerSocket[1]->AutoSendAlarmDataToClient( pDevUid,nVarID,0,pContent,dbData,nUpperLimit,nLowerLimit,nNormalStatus,nAlarmStatus,nWarningLevel,g_nAlarmIndex,g_nNoticeDelay );
  827. g_nDealAWAStatus = 3;//处理声光报警状态 -1未初始化 0初始化 1在处理中 2处理完成 3新事件
  828. pVarNotice->dwReDetectTick = GetTickCount();
  829. //(M1)47=00(M4)24=00(W14)23=00()=00
  830. //(M1)014=00(M4)456=00()=01
  831. //短信通知
  832. if( g_nSendSms==1 )
  833. {
  834. if( nWarningLevel>=g_nLevelSms )
  835. InsertSendSmsList( pDevUid, nVarID, pContent,g_nAlarmIndex );//SendSms(pDevUid, nVarID, pContent);
  836. else
  837. LOG4C((LOG_NOTICE, "没有达到短信通知级别 nWarningLevel = %d, g_nLevelWav = %d", nWarningLevel,g_nLevelSms ));
  838. }
  839. else if( g_nSendSms==2 )//支持短信平台
  840. {
  841. WriteToTxt(pDevUid, nVarID, pContent,g_nAlarmIndex);
  842. }
  843. //邮件通知
  844. if( g_nSendEmail )
  845. {
  846. // 发送Email,所有用户一次性发完
  847. if( nWarningLevel>=g_nLevelEmail )
  848. InsertSendEmailList( pDevUid, nVarID, pContent,g_nAlarmIndex );//SendEmail(pDevUid, nVarID, pContent);
  849. else
  850. LOG4C((LOG_NOTICE, "没有达到邮件通知级别 nWarningLevel = %d, g_nLevelWav = %d", nWarningLevel,g_nLevelEmail ));
  851. }
  852. g_nAlarmIndex++;
  853. if( g_nAlarmIndex>=5000 )
  854. g_nAlarmIndex = 0;
  855. CString strTime;
  856. CTime tm;
  857. tm = CTime::GetCurrentTime();
  858. strTime = tm.Format("%Y-%m-%d %H:%M:%S");
  859. CDBInterface::GetInstancePtr()->InsertWarningRecord(pDevName, pVarDesc, (char *)(LPCTSTR)strTime, nAlarmStatus, dbData, pDevArea, pContent);
  860. }
  861. // 处理变量远程报警通知
  862. int ProcessVarNotice(char *pDevUid,
  863. int nVarID,
  864. int nUpperLimit,
  865. int nLowerLimit,
  866. int nOffSet,
  867. int nAlarmStatus,
  868. int nNormalStatus,
  869. int nIdenfityTime,
  870. int nReDetectTime,
  871. int nNormalIsNotice,
  872. int nWarningLevel,
  873. char *pDevName,
  874. char *pVarDesc,
  875. char *pDevArea,
  876. double dbData,
  877. char *pContent)
  878. {
  879. VAR_NOTICE *pVarNotice = NULL;
  880. pVarNotice = FindVarNoticeList(pDevUid, nVarID);
  881. if( NULL == pVarNotice )
  882. {
  883. if( nAlarmStatus != nNormalStatus )
  884. {
  885. CString strTemp = CString(pContent);
  886. if( strTemp.Find(g_strHintReturnNormal) != -1 )
  887. {
  888. return 0;
  889. }
  890. VAR_NOTICE tagVarNotice;
  891. strcpy(tagVarNotice.szDevUID, pDevUid);
  892. tagVarNotice.nVarID = nVarID;
  893. tagVarNotice.nResult = 0;
  894. tagVarNotice.nCallTime = 0;
  895. tagVarNotice.nSmsTime = 0;
  896. tagVarNotice.bQueueExist = false;
  897. tagVarNotice.dwIdentityTick = GetTickCount(); // 记录辨识时间
  898. tagVarNotice.dwIntervalTick = 0;
  899. tagVarNotice.dwReDetectTick = 0;
  900. tagVarNotice.nLastStatus = nAlarmStatus;
  901. tagVarNotice.bFirstDeal = true;
  902. tagVarNotice.nReDetectTime = nReDetectTime;
  903. // 添加有权限用户的电话号码和手机号码
  904. InsertUserTel(&tagVarNotice);
  905. tagVarNotice.nTelInex = 0;
  906. // 加入list,第一次加入不进行任何处理
  907. EnterCriticalSection( &g_csVarNotice );
  908. InsertVarNoticeList(tagVarNotice);
  909. LeaveCriticalSection( &g_csVarNotice );
  910. }
  911. }
  912. else
  913. {
  914. if( nAlarmStatus == nNormalStatus ) // 正常状态
  915. {
  916. // 防止报警恢复正常之后会一直打电话
  917. if( nAlarmStatus == pVarNotice->nLastStatus )
  918. {
  919. EnterCriticalSection( &g_csVarNotice );
  920. pVarNotice->nLastStatus = nAlarmStatus;
  921. LeaveCriticalSection( &g_csVarNotice );
  922. return 0;
  923. }
  924. EnterCriticalSection( &g_csVarNotice );
  925. pVarNotice->nLastStatus = nAlarmStatus;
  926. LeaveCriticalSection( &g_csVarNotice );
  927. //LOG4C((LOG_NOTICE, "nLowerLimit=%d nUpperLimit=%d nOffSet=%d",nLowerLimit,nUpperLimit,nOffSet));
  928. //模拟量才有偏移量
  929. if( nOffSet > 0 )//当偏移量大于0时,判断当前值是否达到偏移量这个条件
  930. {
  931. //CString strTemp = CString(pContent);
  932. //if( strTemp.Find(g_strHintReturnNormal) != -1 )//chn modify
  933. {
  934. //LOG4C((LOG_NOTICE, "nLowerLimit=%d nUpperLimit=%d nOffSet=%d",nLowerLimit,nUpperLimit,nOffSet));
  935. if( dbData>=nLowerLimit+nOffSet && dbData<=nUpperLimit-nOffSet )
  936. {
  937. ProcessVarStatusReturnNormal(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  938. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc,pDevArea, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit,nWarningLevel );
  939. }
  940. }
  941. }
  942. else
  943. {
  944. ProcessVarStatusReturnNormal(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  945. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc,pDevArea, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit,nWarningLevel );
  946. }
  947. }
  948. else if( nAlarmStatus != nNormalStatus ) // 报警状态
  949. {
  950. //因为第一次加入List不进行任何处理,但状态已经保存,所以要加一个标志来判断一下
  951. if( nAlarmStatus == pVarNotice->nLastStatus && !pVarNotice->bFirstDeal )
  952. {
  953. EnterCriticalSection( &g_csVarNotice );
  954. pVarNotice->nLastStatus = nAlarmStatus;
  955. LeaveCriticalSection( &g_csVarNotice );
  956. return 0;
  957. }
  958. pVarNotice->bFirstDeal = false;//
  959. EnterCriticalSection( &g_csVarNotice );
  960. pVarNotice->nLastStatus = nAlarmStatus;
  961. LeaveCriticalSection( &g_csVarNotice );
  962. //if( nOffSet > 0 )
  963. //{
  964. // CString strTemp = CString(pContent);
  965. // //if( strTemp.Find(g_strHintUpperLimit) != -1 || strTemp.Find(g_strHintLowerLimit) != -1 )
  966. // {
  967. // if( dbData > nUpperLimit + nOffSet || dbData < nLowerLimit - nOffSet )
  968. // {
  969. // ProcessVarAlarm(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  970. // nReDetectTime, nNormalIsNotice, pDevName, pVarDesc, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit,nWarningLevel );
  971. // }
  972. // }
  973. //}
  974. //else
  975. {
  976. ProcessVarAlarm(pDevUid, nVarID, nAlarmStatus, nNormalStatus, nIdenfityTime,
  977. nReDetectTime, nNormalIsNotice, pDevName, pVarDesc,pDevArea, dbData, pContent, pVarNotice,nUpperLimit,nLowerLimit,nWarningLevel );
  978. }
  979. }
  980. }
  981. return 1;
  982. }
  983. ///////////////////////////////////////////////////////////////////////
  984. // 报警变量list,主要报警处理,防止一直加入通知list中
  985. // 从list中查找变量,没找到返回空,找到返回相应的指针地址
  986. VAR_NOTICE* FindVarNoticeList(char *pDevUid, int nVarID)
  987. {
  988. VAR_NOTICE* pVarNotice = NULL;
  989. LIST_VAR_NOTICE::iterator it;
  990. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); it++ )
  991. {
  992. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  993. {
  994. pVarNotice = &(*it);
  995. }
  996. }
  997. return pVarNotice;
  998. }
  999. // 添加变量到list
  1000. void InsertVarNoticeList(char *pDevUid, int nVarID)
  1001. {
  1002. VAR_NOTICE tagVarNotice;
  1003. strcpy(tagVarNotice.szDevUID, pDevUid);
  1004. tagVarNotice.nVarID = nVarID;
  1005. g_listVarNotice.insert(g_listVarNotice.end(), tagVarNotice);
  1006. }
  1007. // 添加变量到list
  1008. void InsertVarNoticeList(VAR_NOTICE varNotice)
  1009. {
  1010. g_listVarNotice.insert(g_listVarNotice.end(), varNotice);
  1011. }
  1012. // 从list中移除变量
  1013. int DeleteVarNoticeList(char *pDevUid, int nVarID)
  1014. {
  1015. int nRet = -1;
  1016. VAR_NOTICE* pVarNotice = NULL;
  1017. LIST_VAR_NOTICE::iterator it;
  1018. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); )
  1019. {
  1020. pVarNotice = &(*it);
  1021. if( strcmp(pVarNotice->szDevUID, pDevUid) == 0 &&
  1022. pVarNotice->nVarID == nVarID )
  1023. {
  1024. g_listVarNotice.erase(it++);
  1025. nRet = 0;
  1026. break;
  1027. }
  1028. else
  1029. {
  1030. it++;
  1031. }
  1032. }
  1033. return nRet;
  1034. }
  1035. // 删除所有变量
  1036. void DeleteAllVarNoticeList()
  1037. {
  1038. LIST_VAR_NOTICE::iterator it;
  1039. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); )
  1040. {
  1041. g_listVarNotice.erase(it++);
  1042. }
  1043. }
  1044. ///////////////////////////////////////////////////////////////////////////
  1045. // 发送通知list, 和上面的设备变量不同
  1046. // 从list中查找变量,没找到返回空,找到返回相应的指针地址
  1047. SEND_NOTICE* FindSendNoticeList(char *pDevUid, int nVarID)
  1048. {
  1049. SEND_NOTICE* pSendNotice = NULL;
  1050. LIST_SEND_NOTICE::iterator it;
  1051. for( it = g_listSendVoice.begin(); it != g_listSendVoice.end(); it++ )
  1052. {
  1053. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  1054. {
  1055. pSendNotice = &(*it);
  1056. }
  1057. }
  1058. return pSendNotice;
  1059. }
  1060. // 添加变量到list
  1061. void InsertSendVoiceList(char *pDevUid, int nVarID, char *pTel, char *pContent,int nAlarmIndex )
  1062. {
  1063. //如果是短信确认关闭报警通知则退出
  1064. if( !g_bAlarmNoticeFlag )
  1065. return ;
  1066. if( g_bSendVoice==0 )
  1067. return;
  1068. if( pStoneUDLLTxtToWav && g_nAlarmIndexLast!=nAlarmIndex )
  1069. {
  1070. g_nAlarmIndexLast = nAlarmIndex;
  1071. CString strFileName,sContent;
  1072. EnterCriticalSection( &g_csCreateWav );
  1073. CString strFileName2;
  1074. strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pDevUid, nVarID,nAlarmIndex );
  1075. try
  1076. {
  1077. pStoneUDLLTxtToWav( pContent, (char *)(LPCTSTR)strFileName2, g_nTTSType, g_nTTSRate, g_nTTSVolume ); //设置音量,范围是 0 - 100
  1078. }
  1079. catch(...)
  1080. {
  1081. LOG4C((LOG_NOTICE, "pStoneUDLLTxtToWav1 出错"));
  1082. }
  1083. g_saWavPath.Add( strFileName2 );
  1084. g_saWavContent.Add( pContent );
  1085. CString sTemp;
  1086. sTemp.Format( "%s%s%s",g_strNoticePre,pContent,g_strHintVoiceEnd );
  1087. CString strFileName1;
  1088. strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pDevUid, nVarID,nAlarmIndex );
  1089. g_saWavPath.Add( strFileName1 );
  1090. g_saWavContent.Add( sTemp );
  1091. LeaveCriticalSection( &g_csCreateWav );
  1092. }
  1093. //LOG4C((LOG_NOTICE, "pDevUid = %s,nVarID = %d, pTel = %s, 内容:%s , nAlarmIndex = %d ", pDevUid,nVarID,pTel, pContent,nAlarmIndex ));
  1094. SEND_NOTICE tagSendNotice;
  1095. strcpy(tagSendNotice.szDevUID, pDevUid);
  1096. tagSendNotice.nVarID = nVarID;
  1097. strcpy(tagSendNotice.szTel, pTel);
  1098. strcpy(tagSendNotice.szContect, pContent);
  1099. tagSendNotice.nAlarmIndex = nAlarmIndex;
  1100. GetLocalTime( &tagSendNotice.time );
  1101. tagSendNotice.nCallTimes = 0;
  1102. if( strcmp("0.0.0.0", pDevUid)==0 && nVarID==1 )
  1103. tagSendNotice.nNoticeDelayTime = 0;
  1104. else
  1105. tagSendNotice.nNoticeDelayTime = g_nNoticeDelay+2;
  1106. EnterCriticalSection( &g_csSendVoice );
  1107. g_listSendVoice.insert(g_listSendVoice.end(), tagSendNotice);
  1108. LeaveCriticalSection( &g_csSendVoice );
  1109. }
  1110. // 从list中移除变量
  1111. int DeleteSendVoiceList(char *pDevUid, int nVarID)
  1112. {
  1113. int nRet = -1;
  1114. SEND_NOTICE* pSendNotice = NULL;
  1115. LIST_SEND_NOTICE::iterator it;
  1116. for( it = g_listSendVoice.begin(); it != g_listSendVoice.end(); )
  1117. {
  1118. pSendNotice = &(*it);
  1119. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID )
  1120. {
  1121. EnterCriticalSection( &g_csSendVoice );
  1122. g_listSendVoice.erase(it++);
  1123. LeaveCriticalSection( &g_csSendVoice );
  1124. nRet = 0;
  1125. break;
  1126. }
  1127. else
  1128. {
  1129. it++;
  1130. }
  1131. }
  1132. return nRet;
  1133. }
  1134. // IDE确认后从list中移除变量
  1135. int DeleteSendVoiceList( char *pDevUid, int nVarID,int iAlarmIndex )
  1136. {
  1137. int nRet = -1;
  1138. SEND_NOTICE* pSendNotice = NULL;
  1139. LIST_SEND_NOTICE::iterator it;
  1140. for( it = g_listSendVoice.begin(); it != g_listSendVoice.end(); )
  1141. {
  1142. pSendNotice = &(*it);
  1143. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1144. {
  1145. ////删除WAV
  1146. //CString strFileName1;
  1147. //strFileName1.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pDevUid, nVarID,iAlarmIndex );
  1148. //if( PathFileExists(strFileName1) )
  1149. // DeleteFile( strFileName1 );
  1150. //CString strFileName2;
  1151. //strFileName2.Format("%s\\wav\\temp_%s_%d_%d_IDE.wav", g_strDirectory, pDevUid, nVarID,iAlarmIndex );
  1152. //if( PathFileExists(strFileName2) )
  1153. // DeleteFile( strFileName2 );
  1154. ////结束删除
  1155. //LOG4C((LOG_NOTICE, "IDE确认語音 %s %s",pSendNotice->szTel,pSendNotice->szContect));
  1156. EnterCriticalSection( &g_csSendVoice );
  1157. g_listSendVoice.erase(it++);
  1158. LeaveCriticalSection( &g_csSendVoice );
  1159. }
  1160. else
  1161. {
  1162. it++;
  1163. }
  1164. }
  1165. return nRet;
  1166. }
  1167. // 删除所有变量
  1168. void DeleteAllSendVoiceList()
  1169. {
  1170. #if 0
  1171. // 正确使用方法1
  1172. std::list< int> List;
  1173. std::list< int>::iterator itList;
  1174. for( itList = List.begin(); itList != List.end(); )
  1175. {
  1176. if( WillDelete( *itList) )
  1177. {
  1178. itList = List.erase( itList);
  1179. }
  1180. else
  1181. itList++;
  1182. }
  1183. // 正确使用方法2
  1184. std::list< int> List;
  1185. std::list< int>::iterator itList;
  1186. for( itList = List.begin(); itList != List.end(); )
  1187. {
  1188. if( WillDelete( *itList) )
  1189. {
  1190. List.erase( itList++);
  1191. }
  1192. else
  1193. itList++;
  1194. }
  1195. // 错误使用方法1
  1196. std::list< int> List;
  1197. std::list< int>::iterator itList;
  1198. for( itList = List.begin(); itList != List.end(); itList++)
  1199. {
  1200. if( WillDelete( *itList) )
  1201. {
  1202. List.erase( itList);
  1203. }
  1204. }
  1205. // 错误使用方法2
  1206. std::list< int> List;
  1207. std::list< int>::iterator itList;
  1208. for( itList = List.begin(); itList != List.end(); )
  1209. {
  1210. if( WillDelete( *itList) )
  1211. {
  1212. itList = List.erase( ++itList);
  1213. }
  1214. else
  1215. itList++;
  1216. }
  1217. #endif
  1218. LIST_SEND_NOTICE::iterator it;
  1219. for( it = g_listSendVoice.begin(); it != g_listSendVoice.end(); )
  1220. {
  1221. g_listSendVoice.erase( it++ );
  1222. }
  1223. }
  1224. // 添加变量到Sms list
  1225. void InsertSendSmsList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex )
  1226. {
  1227. //如果是短信确认关闭报警通知则退出
  1228. if( !g_bAlarmNoticeFlag || g_nSendSms==0 )
  1229. return;
  1230. //是自检
  1231. if( strcmp("0.0.0.0", pDevUid)==0 )
  1232. {
  1233. char chTel[MAX_TEL_LENGTH] = {0};
  1234. char chMobile[MAX_TEL_LENGTH] = {0};
  1235. char chFax[MAX_TEL_LENGTH] = {0};
  1236. char chEmail[MAX_EMAIL_LENGTH] = {0};
  1237. for( int i=0;i<g_vtUserInfo.size();i++ )
  1238. {
  1239. strcpy(chTel, g_vtUserInfo[i].szTel);
  1240. strcpy(chMobile, g_vtUserInfo[i].szMobileTel);
  1241. CString sTemp;
  1242. sTemp.Format( "%s",chMobile );
  1243. if( sTemp.GetLength()>=11 )
  1244. {
  1245. CTime time = CTime::GetCurrentTime();
  1246. CString strTime;
  1247. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1248. CString strMobileTel;
  1249. strMobileTel.Format("%s",chMobile );
  1250. SEND_NOTICE tagSendNotice;
  1251. strcpy(tagSendNotice.szDevUID, pDevUid);
  1252. tagSendNotice.nVarID = nVarID;
  1253. strcpy(tagSendNotice.szTel, strMobileTel);
  1254. strcpy(tagSendNotice.szContect, pContent);
  1255. tagSendNotice.nAlarmIndex = nAlarmIndex;
  1256. GetLocalTime( &tagSendNotice.time );
  1257. tagSendNotice.nCallTimes = 0;
  1258. tagSendNotice.nNoticeDelayTime = 0;
  1259. EnterCriticalSection( &g_csSendSms );
  1260. g_listSendSms.insert(g_listSendSms.end(), tagSendNotice);
  1261. LeaveCriticalSection( &g_csSendSms );
  1262. }
  1263. }
  1264. }
  1265. else
  1266. {
  1267. for( int i = 0; i < (int)g_vtUserRoleInfo.size(); i++ )
  1268. {
  1269. //LOG4C((LOG_NOTICE, "g_vtUserRoleInfo[i].szDevUID = %s, pDevUid = %s", g_vtUserRoleInfo[i].szDevUID, pDevUid));
  1270. if( strcmp(g_vtUserRoleInfo[i].szDevUID, pDevUid)==0 )
  1271. {
  1272. //LOG4C((LOG_NOTICE, "g_vtUserRoleInfo[i].vtUserInfo.size = %d", g_vtUserRoleInfo[i].vtUserInfo.size()));
  1273. for( int j = 0; j < (int)g_vtUserRoleInfo[i].vtUserInfo.size(); j++ )
  1274. {
  1275. CString strMobileTel = CString(g_vtUserRoleInfo[i].vtUserInfo[j].szMobileTel);
  1276. if( strMobileTel.GetLength()<11 )
  1277. continue;
  1278. SEND_NOTICE tagSendNotice;
  1279. strcpy(tagSendNotice.szDevUID, pDevUid);
  1280. tagSendNotice.nVarID = nVarID;
  1281. strcpy(tagSendNotice.szTel, strMobileTel);
  1282. strcpy(tagSendNotice.szContect, pContent);
  1283. tagSendNotice.nAlarmIndex = nAlarmIndex;
  1284. GetLocalTime( &tagSendNotice.time );
  1285. tagSendNotice.nCallTimes = 0;
  1286. tagSendNotice.nNoticeDelayTime = g_nNoticeDelay+2;
  1287. EnterCriticalSection( &g_csSendSms );
  1288. g_listSendSms.insert(g_listSendSms.end(), tagSendNotice);
  1289. LeaveCriticalSection( &g_csSendSms );
  1290. }
  1291. }
  1292. }
  1293. }
  1294. }
  1295. // IDE确认后从Sms list中移除变量
  1296. int DeleteSendSmsList( char *pDevUid, int nVarID,int iAlarmIndex )
  1297. {
  1298. int nRet = -1;
  1299. SEND_NOTICE* pSendNotice = NULL;
  1300. LIST_SEND_NOTICE::iterator it;
  1301. for( it = g_listSendSms.begin(); it != g_listSendSms.end(); )
  1302. {
  1303. pSendNotice = &(*it);
  1304. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1305. {
  1306. LOG4C((LOG_NOTICE, "IDE确认短信 %s",pSendNotice->szContect));
  1307. EnterCriticalSection( &g_csSendSms );
  1308. g_listSendSms.erase(it++);
  1309. LeaveCriticalSection( &g_csSendSms );
  1310. nRet = 0;
  1311. //break;
  1312. }
  1313. else
  1314. {
  1315. it++;
  1316. }
  1317. }
  1318. return nRet;
  1319. }
  1320. // 删除Sms所有变量
  1321. void DeleteAllSendSmsList()
  1322. {
  1323. EnterCriticalSection( &g_csSendSms );
  1324. LIST_SEND_NOTICE::iterator it;
  1325. for( it = g_listSendSms.begin(); it != g_listSendSms.end(); )
  1326. {
  1327. g_listSendSms.erase( it++ );
  1328. }
  1329. LeaveCriticalSection( &g_csSendSms );
  1330. }
  1331. // 添加变量到Email list
  1332. void InsertSendEmailList(char *pDevUid, int nVarID, char *pContent,int nAlarmIndex )
  1333. {
  1334. //如果是短信确认关闭报警通知则退出
  1335. if( !g_bAlarmNoticeFlag )
  1336. return ;
  1337. //LOG4C((LOG_NOTICE, "InsertSendSmsList" ));
  1338. SEND_NOTICE tagSendNotice;
  1339. strcpy(tagSendNotice.szDevUID, pDevUid);
  1340. tagSendNotice.nVarID = nVarID;
  1341. strcpy(tagSendNotice.szTel, "");
  1342. strcpy(tagSendNotice.szContect, pContent);
  1343. tagSendNotice.nAlarmIndex = nAlarmIndex;
  1344. GetLocalTime( &tagSendNotice.time );
  1345. tagSendNotice.nCallTimes = 0;
  1346. if( strcmp("0.0.0.0", pDevUid)==0 && nVarID==1 )
  1347. tagSendNotice.nNoticeDelayTime = 0;
  1348. else
  1349. tagSendNotice.nNoticeDelayTime = g_nNoticeDelay+2;
  1350. EnterCriticalSection( &g_csSendEmali );
  1351. g_listSendEmail.insert(g_listSendEmail.end(), tagSendNotice);
  1352. LeaveCriticalSection( &g_csSendEmali );
  1353. }
  1354. // IDE确认后从Email list中移除变量
  1355. int DeleteSendEmailList( char *pDevUid, int nVarID,int iAlarmIndex )
  1356. {
  1357. int nRet = -1;
  1358. SEND_NOTICE* pSendNotice = NULL;
  1359. LIST_SEND_NOTICE::iterator it;
  1360. for( it = g_listSendEmail.begin(); it != g_listSendEmail.end(); )
  1361. {
  1362. pSendNotice = &(*it);
  1363. if( strcmp(pSendNotice->szDevUID, pDevUid) == 0 && pSendNotice->nVarID == nVarID && pSendNotice->nAlarmIndex==iAlarmIndex )
  1364. {
  1365. LOG4C((LOG_NOTICE, "IDE确认Email %s",pSendNotice->szContect));
  1366. EnterCriticalSection( &g_csSendEmali );
  1367. g_listSendEmail.erase(it++);
  1368. LeaveCriticalSection( &g_csSendEmali );
  1369. nRet = 0;
  1370. break;
  1371. }
  1372. else
  1373. {
  1374. it++;
  1375. }
  1376. }
  1377. return nRet;
  1378. }
  1379. // 删除Sms所有变量
  1380. void DeleteAllSendEmailList()
  1381. {
  1382. EnterCriticalSection( &g_csSendEmali );
  1383. LIST_SEND_NOTICE::iterator it;
  1384. for( it = g_listSendEmail.begin(); it != g_listSendEmail.end(); )
  1385. {
  1386. g_listSendEmail.erase( it++ );
  1387. }
  1388. LeaveCriticalSection( &g_csSendEmali );
  1389. }
  1390. int GetTelIndex( char *pDevUid, int nVarID, CString sTel )
  1391. {
  1392. int nRet=-1;
  1393. if( g_listVarNotice.size()==0 )
  1394. {
  1395. LOG4C((LOG_NOTICE, "g_listVarNotice.size()= %d",g_listVarNotice.size()));
  1396. return nRet;
  1397. }
  1398. VAR_NOTICE* pVarNotice = NULL;
  1399. LIST_VAR_NOTICE::iterator it;
  1400. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); it++ )
  1401. {
  1402. if( strcmp(it->szDevUID, pDevUid) == 0 && it->nVarID == nVarID )
  1403. {
  1404. pVarNotice = &(*it);
  1405. break;
  1406. }
  1407. }
  1408. if( pVarNotice )
  1409. {
  1410. LIST_TEL::iterator itTel;
  1411. CString *pStrTel;
  1412. for( itTel = pVarNotice->listTel.begin(); itTel != pVarNotice->listTel.end(); itTel++ )
  1413. {
  1414. pStrTel = &(*itTel);
  1415. nRet++;
  1416. if( strcmp(sTel, *pStrTel) == 0 )
  1417. {
  1418. break;
  1419. }
  1420. }
  1421. }
  1422. else
  1423. LOG4C((LOG_NOTICE, "pVarNotice=NULL g_listVarNotice.size() = %d",g_listVarNotice.size()));
  1424. return nRet;
  1425. }
  1426. ////////////////////////////////////////////////////////////////////////////////
  1427. bool DeleteWav( CString strPath, CString strFileName, int nLayer )
  1428. {
  1429. CStringArray saFilePath;
  1430. long hfile;
  1431. struct _finddata_t fFile;
  1432. CString strCurr = strPath + "\\" + strFileName;
  1433. int nDone=0;
  1434. hfile = _findfirst( (char *)(LPCTSTR)strCurr, &fFile );
  1435. if( -1L != hfile )
  1436. {
  1437. while( !( nDone = _findnext(hfile, &fFile) ) )
  1438. {
  1439. if( !strcmp(fFile.name,"..") ) continue;
  1440. if( ( fFile.attrib==16 || fFile.attrib==18 || fFile.attrib==48 || fFile.attrib==2064 ||
  1441. fFile.attrib==2096 || fFile.attrib==2098 || fFile.attrib==16432 || fFile.attrib==16434 ) ) //_A_SUBDIR
  1442. {
  1443. }
  1444. else
  1445. {
  1446. CString str = fFile.name;
  1447. CString sFileNameTemp = strPath + "\\" + str;
  1448. if( -1 != str.Find( "temp" ) )
  1449. {
  1450. saFilePath.Add( sFileNameTemp );
  1451. }
  1452. }
  1453. }
  1454. _findclose(hfile);
  1455. }
  1456. for( int i=0;i<saFilePath.GetCount();i++ )
  1457. {
  1458. DeleteFile( saFilePath.GetAt( i ) );
  1459. }
  1460. return true;
  1461. }
  1462. bool IsCreateWavOK( CString sWavPath )
  1463. {
  1464. bool bRet = true;
  1465. CString sPath;
  1466. for( int i=0;i<g_saWavPath.GetCount();i++ )
  1467. {
  1468. sPath = g_saWavPath.GetAt( 0 );
  1469. if( sPath==sWavPath )
  1470. {
  1471. bRet = false;
  1472. break;
  1473. }
  1474. }
  1475. return bRet;
  1476. }
  1477. DWORD WINAPI SendVoiceThread(LPVOID lpParameter)
  1478. {
  1479. do
  1480. {
  1481. if( g_listSendVoice.size() == 0 )
  1482. {
  1483. CTime ctNow = CTime::GetCurrentTime();
  1484. int nHour;
  1485. nHour = ctNow.GetHour();
  1486. static bool bCanDelWav = true;
  1487. if( nHour == 23 && bCanDelWav )
  1488. {
  1489. bCanDelWav = false;
  1490. CString strDectory;
  1491. strDectory.Format("%s\\wav", g_strDirectory );
  1492. DeleteWav( strDectory,"*.*",0 );
  1493. //LOG4C((LOG_NOTICE, "清空语音文件" ));
  1494. }
  1495. else if( nHour != 23 )
  1496. {
  1497. bCanDelWav = true;
  1498. }
  1499. }
  1500. else
  1501. {
  1502. SEND_NOTICE *pSendNotice;
  1503. LIST_SEND_NOTICE::iterator it;
  1504. it = g_listSendVoice.begin();
  1505. pSendNotice = &(*it);
  1506. CString sCurrentTel;
  1507. CString sDevUID;
  1508. CString sContent;
  1509. int nVarID = 0;
  1510. if( pSendNotice )
  1511. {
  1512. SYSTEMTIME st;
  1513. GetLocalTime( &st );
  1514. CTime time1(st);
  1515. CTime time2(pSendNotice->time);
  1516. CTimeSpan spantime = time1 - time2;
  1517. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1518. if( nSecNum > pSendNotice->nNoticeDelayTime )
  1519. {
  1520. CString strFileName;
  1521. strFileName.Format("%s\\wav\\temp_%s_%d_%d.wav", g_strDirectory, pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex );
  1522. if( !IsCreateWavOK( strFileName ) )
  1523. continue;
  1524. LOG4C((LOG_NOTICE, "开始拨打 电话 %s%s",g_strNoticeTelPre,pSendNotice->szTel ));
  1525. int nRet = SendNotice(pSendNotice->szDevUID, pSendNotice->nVarID,pSendNotice->nAlarmIndex, pSendNotice->szTel, pSendNotice->szContect);
  1526. sCurrentTel.Format( "%s",pSendNotice->szTel );
  1527. sDevUID.Format( "%s",pSendNotice->szDevUID );
  1528. sContent.Format( "%s",pSendNotice->szContect);
  1529. nVarID = pSendNotice->nVarID;
  1530. if( nRet==CALL_SUCCESS_SINGLE_CONFIRM )//确认当前通知
  1531. {
  1532. LOG4C((LOG_NOTICE, "电话 %s%s 用户确认当前通知 (%d)内容:%s",g_strNoticeTelPre,pSendNotice->szTel,g_listSendVoice.size(),pSendNotice->szContect ));
  1533. int nCurrentTelIndex=0,nTelIndex=0;
  1534. nCurrentTelIndex = GetTelIndex( pSendNotice->szDevUID, pSendNotice->nVarID, sCurrentTel );
  1535. //LOG4C((LOG_NOTICE, "nCurrentTelIndex = %d",nCurrentTelIndex));
  1536. EnterCriticalSection( &g_csSendVoice );
  1537. g_listSendVoice.erase(it++);//先把当前的删除
  1538. LeaveCriticalSection( &g_csSendVoice );
  1539. if( g_listSendVoice.size() > 0 )
  1540. {
  1541. CTime time = CTime::GetCurrentTime();
  1542. CString strTime,sTemp;
  1543. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1544. sTemp.Format( "确认通知《%s》 ",sContent );
  1545. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  1546. (char *)(LPCTSTR)sCurrentTel,
  1547. "",
  1548. (char *)(LPCTSTR)strTime,
  1549. (char *)(LPCTSTR)sTemp,
  1550. "");
  1551. EnterCriticalSection( &g_csSendVoice );
  1552. LIST_SEND_NOTICE::iterator itTemp;
  1553. for( itTemp = g_listSendVoice.begin(); itTemp != g_listSendVoice.end(); )
  1554. {
  1555. nTelIndex = GetTelIndex( itTemp->szDevUID,itTemp->nVarID,itTemp->szTel );
  1556. //LOG4C((LOG_NOTICE, "%s,nCurrentTelIndex = %d,%s,nTelIndex=%d ",sCurrentTel,nCurrentTelIndex,itTemp->szTel,nTelIndex ));
  1557. if( strcmp("0.0.0.0", sDevUID)==0 && nVarID==1 )//是自检
  1558. {
  1559. if( strcmp("0.0.0.0", itTemp->szDevUID)==0 && itTemp->nVarID==1 )
  1560. {
  1561. LOG4C((LOG_NOTICE, "自检通知已经被确认,跳过号码 %s",itTemp->szTel ));
  1562. g_listSendVoice.erase(itTemp++);
  1563. }
  1564. else
  1565. break;
  1566. }
  1567. else
  1568. {
  1569. if( nCurrentTelIndex<nTelIndex )
  1570. {
  1571. LOG4C((LOG_NOTICE, "通知已经被确认,跳过号码 %s",itTemp->szTel ));
  1572. g_listSendVoice.erase(itTemp++);
  1573. }
  1574. else
  1575. {
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. LeaveCriticalSection( &g_csSendVoice );
  1581. }
  1582. }
  1583. else if( nRet==CALL_SUCCESS_ALL_CONFIRM )//确认全部通知
  1584. {
  1585. LOG4C((LOG_NOTICE, "电话 %s%s 用户确认所有通知 (%d)内容:%s",g_strNoticeTelPre,pSendNotice->szTel,g_listSendVoice.size(),pSendNotice->szContect ));
  1586. EnterCriticalSection( &g_csSendVoice );
  1587. g_listSendVoice.erase(it++);//先把当前的删除
  1588. if( g_listSendVoice.size() > 0 )
  1589. {
  1590. CTime time = CTime::GetCurrentTime();
  1591. CString strTime;
  1592. strTime = time.Format("%Y-%m-%d %H:%M:%S");
  1593. CDBInterface::GetInstancePtr()->InsertNoticeRecord(
  1594. (char *)(LPCTSTR)sCurrentTel,
  1595. "",
  1596. (char *)(LPCTSTR)strTime,
  1597. "config all alert",
  1598. "");
  1599. LIST_SEND_NOTICE::iterator itTemp;
  1600. for( itTemp = g_listSendVoice.begin(); itTemp != g_listSendVoice.end(); )
  1601. {
  1602. LOG4C((LOG_NOTICE, "通知全部已经被确认,跳过号码 %s",itTemp->szTel ));
  1603. g_listSendVoice.erase(itTemp++);
  1604. }
  1605. }
  1606. LeaveCriticalSection( &g_csSendVoice );
  1607. }
  1608. else if( nRet == CALL_SUCCESS_NOT_CONFIRM )
  1609. {
  1610. LOG4C((LOG_NOTICE, "电话 %s%s 用户没有确认通知 (%d)内容:%s",g_strNoticeTelPre,pSendNotice->szTel,g_listSendVoice.size(),pSendNotice->szContect ));
  1611. EnterCriticalSection( &g_csSendVoice );
  1612. g_listSendVoice.erase(it++);
  1613. LeaveCriticalSection( &g_csSendVoice );
  1614. }
  1615. else if( nRet == CALL_LOST )
  1616. {
  1617. LOG4C((LOG_NOTICE, "拨打电话 %s%s 失败 (%d)内容:%s",g_strNoticeTelPre,pSendNotice->szTel,g_listSendVoice.size(),pSendNotice->szContect ));
  1618. EnterCriticalSection( &g_csSendVoice );
  1619. pSendNotice->nCallTimes++;
  1620. if( pSendNotice->nCallTimes >= g_nNoticeCallTimes )
  1621. g_listSendVoice.erase(it++);
  1622. LeaveCriticalSection( &g_csSendVoice );
  1623. }
  1624. }
  1625. Sleep( 2 * 1000 );//Sleep( 30 * 1000 );
  1626. }
  1627. }
  1628. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1629. return 0;
  1630. }
  1631. DWORD WINAPI SendSmsThread(LPVOID lpParameter)
  1632. {
  1633. do
  1634. {
  1635. if( g_listSendSms.size() > 0 )
  1636. {
  1637. SEND_NOTICE *pSendNotice;
  1638. LIST_SEND_NOTICE::iterator it;
  1639. it = g_listSendSms.begin();
  1640. pSendNotice = &(*it);
  1641. CString sCurrentTel;
  1642. static bool bCanSendSms=false;
  1643. if( pSendNotice && !bCanSendSms )
  1644. {
  1645. //LOG4C((LOG_NOTICE, "SendSmsThread size>0" ));
  1646. SYSTEMTIME st;
  1647. GetLocalTime( &st );
  1648. CTime time1(st);
  1649. CTime time2(pSendNotice->time);
  1650. CTimeSpan spantime = time1 - time2;
  1651. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1652. if( nSecNum > pSendNotice->nNoticeDelayTime )
  1653. {
  1654. LOG4C((LOG_NOTICE, "SendSmsThread %s",pSendNotice->szContect));
  1655. bCanSendSms = true;
  1656. SendSms( pSendNotice->szDevUID,pSendNotice->nVarID,pSendNotice->szTel,pSendNotice->szContect );
  1657. bCanSendSms = false;
  1658. EnterCriticalSection( &g_csSendSms );
  1659. g_listSendSms.erase(it++);
  1660. LeaveCriticalSection( &g_csSendSms );
  1661. }
  1662. }
  1663. }
  1664. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1665. return 0;
  1666. }
  1667. DWORD WINAPI SendEmailThread(LPVOID lpParameter)
  1668. {
  1669. do
  1670. {
  1671. if( g_listSendEmail.size() > 0 )
  1672. {
  1673. SEND_NOTICE *pSendNotice=NULL;
  1674. LIST_SEND_NOTICE::iterator it;
  1675. it = g_listSendEmail.begin();
  1676. pSendNotice = &(*it);
  1677. static bool bCanSendEmail=false;
  1678. if( pSendNotice && !bCanSendEmail )
  1679. {
  1680. SYSTEMTIME st;
  1681. GetLocalTime( &st );
  1682. CTime time1(st);
  1683. CTime time2(pSendNotice->time);
  1684. CTimeSpan spantime = time1 - time2;
  1685. int nSecNum = spantime.GetSeconds() + spantime.GetMinutes()*60 + spantime.GetHours()*60*60;
  1686. if( nSecNum > pSendNotice->nNoticeDelayTime )
  1687. {
  1688. LOG4C((LOG_NOTICE, "*****开始一轮邮件通知*****" ));
  1689. bCanSendEmail = true;
  1690. int nRet = SendEmail( pSendNotice->szDevUID,pSendNotice->nVarID,pSendNotice->szContect );
  1691. if( nRet==-1 )
  1692. LOG4C((LOG_NOTICE, "没有分配设备" ));
  1693. bCanSendEmail = false;
  1694. LOG4C((LOG_NOTICE, "*****结束一轮邮件通知*****" ));
  1695. EnterCriticalSection( &g_csSendEmali );
  1696. g_listSendEmail.erase(it++);
  1697. LeaveCriticalSection( &g_csSendEmali );
  1698. }
  1699. }
  1700. }
  1701. }while( WaitForSingleObject(g_hRunObject, 200L) == WAIT_TIMEOUT );
  1702. return 0;
  1703. }
  1704. DWORD WINAPI CreateWavThread(LPVOID lpParameter)
  1705. {
  1706. do
  1707. {
  1708. static bool sbCreateNow = false;
  1709. int nSize1 = g_saWavPath.GetCount();
  1710. int nSize2 = g_saWavContent.GetCount();
  1711. if( nSize1>0 && nSize2>0 && !sbCreateNow )
  1712. {
  1713. if( pStoneUDLLTxtToWav )
  1714. {
  1715. sbCreateNow = true;
  1716. EnterCriticalSection( &g_csCreateWav );
  1717. CString sPath = g_saWavPath.GetAt(0);
  1718. CString sContent = g_saWavContent.GetAt(0);
  1719. //LOG4C((LOG_NOTICE, "pStoneUDLLTxtToWav in"));
  1720. try
  1721. {
  1722. pStoneUDLLTxtToWav( (char *)(LPCTSTR)sContent, (char *)(LPCTSTR)sPath,g_nTTSType,g_nTTSRate, g_nTTSVolume );
  1723. }
  1724. catch(...)
  1725. {
  1726. LOG4C((LOG_NOTICE, "pStoneUDLLTxtToWav2 出错"));
  1727. }
  1728. //LOG4C((LOG_NOTICE, "pStoneUDLLTxtToWav out"));
  1729. g_saWavPath.RemoveAt(0);
  1730. g_saWavContent.RemoveAt(0);
  1731. sbCreateNow = false;
  1732. LeaveCriticalSection( &g_csCreateWav );
  1733. }
  1734. }
  1735. Sleep( 100 );
  1736. }while( WaitForSingleObject( g_hRunObject, 125L ) == WAIT_TIMEOUT );
  1737. return 0;
  1738. }
  1739. DWORD WINAPI AWAThread(LPVOID lpParameter)//声光报警线程
  1740. {
  1741. int nTimeTick=0;
  1742. do
  1743. {
  1744. if( g_nDealAWAStatus==0 || g_nDealAWAStatus==2 || g_nDealAWAStatus==3 )
  1745. {
  1746. g_FunSetAWA();
  1747. }
  1748. if( nTimeTick++ >= 120 && g_nDealAWAStatus==-1 )
  1749. g_nDealAWAStatus = 0;//处理声光报警状态 -1未初始化 0初始化 1在处理中 2处理完成 3新事件
  1750. }while( WaitForSingleObject(g_hRunObject, 1000L) == WAIT_TIMEOUT );
  1751. return 0;
  1752. }
  1753. //判断是否达到重新检测时间
  1754. DWORD WINAPI CheckReDetectTimeThread(LPVOID lpParameter)
  1755. {
  1756. do
  1757. {
  1758. if( g_listVarNotice.size() > 0 )
  1759. {
  1760. VAR_NOTICE* pVarNotice = NULL;
  1761. LIST_VAR_NOTICE::iterator it;
  1762. for( it = g_listVarNotice.begin(); it != g_listVarNotice.end(); it++ )
  1763. {
  1764. pVarNotice = &(*it);
  1765. if( pVarNotice->dwReDetectTick == 0 )
  1766. {
  1767. EnterCriticalSection( &g_csVarNotice );
  1768. pVarNotice->dwReDetectTick = GetTickCount();
  1769. LeaveCriticalSection( &g_csVarNotice );
  1770. }
  1771. // 超过重新检测等待时间,从list中删除该变量;
  1772. if( GetTickCount() - pVarNotice->dwReDetectTick > (DWORD)(pVarNotice->nReDetectTime * 1000 * 60) )
  1773. {
  1774. int nDeviceIndex = -1, nVarIndex = -1;
  1775. BOOL bFind = FindVar(pVarNotice->szDevUID, pVarNotice->nVarID, nDeviceIndex, nVarIndex);
  1776. if( bFind == TRUE )
  1777. {
  1778. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  1779. CBaseVar *pBaseVar = pDev->m_Vars[nVarIndex];
  1780. EnterCriticalSection( &g_csVarNotice );
  1781. pBaseVar->m_nAlarmNumber = 0;
  1782. pVarNotice->nCallTime = 0;
  1783. pVarNotice->nSmsTime = 0;
  1784. pBaseVar->m_nAlarmStatus = 3;//设为恢复正常状态
  1785. pVarNotice->bFirstDeal = true;
  1786. pVarNotice->dwReDetectTick = GetTickCount();
  1787. //LOG4C((LOG_NOTICE, "超过重新检测等待时间 设为恢复正常状态 %d",pVarNotice->nReDetectTime));
  1788. LeaveCriticalSection( &g_csVarNotice );
  1789. }
  1790. //// 从list中删除变量时,要保证其它地方不去取list变量
  1791. //EnterCriticalSection( &g_csVarNotice ); // ResetEvent时也不允许任何人进来,故此把临界区放在ResetEvent之前。
  1792. //ResetEvent( g_hDeleteVarSem );
  1793. ////DeleteVarNoticeList(pVarNotice->szDevUID, pVarNotice->nVarID);
  1794. //SetEvent( g_hDeleteVarSem );
  1795. //LeaveCriticalSection( &g_csVarNotice );
  1796. }
  1797. }
  1798. }
  1799. }while( WaitForSingleObject(g_hRunObject, 10000L) == WAIT_TIMEOUT );
  1800. return 0;
  1801. }
  1802. int GetVarstatus(double iParaValue, int iUpperLimit, int iLowerLimit, int iNormalState)
  1803. {
  1804. int iResult = iNormalState;
  1805. if ( ( iUpperLimit >0 || iLowerLimit > 0 ) && iUpperLimit > iLowerLimit)
  1806. {
  1807. //iResult = (iParaValue >= iUpperLimit || iParaValue <= iLowerLimit);
  1808. if( iParaValue > iUpperLimit )
  1809. {
  1810. iResult = UPPER_LIMIT_ID; // 上限告警
  1811. }
  1812. if( iParaValue < iLowerLimit )
  1813. {
  1814. iResult = LOWER_LIMIT_ID; // 下限告警
  1815. }
  1816. else
  1817. {
  1818. return iResult;
  1819. }
  1820. }
  1821. else if( (int)iParaValue != iNormalState )
  1822. {
  1823. iResult = (int)iParaValue;
  1824. }
  1825. return iResult;
  1826. }
  1827. int VarAlarmNotice(CDevice *pDev, CBaseVar *pBaseVar)
  1828. {
  1829. // 组织报警内容
  1830. CString strContect;
  1831. char szStatusDesc[MAX_EQUIP_DESC + 1] = {0};
  1832. char szStatusID[MAX_ID + 1] = {0};
  1833. memset(szStatusDesc, 0, sizeof(szStatusDesc));
  1834. if( pBaseVar->m_nWaringLevel == 0 ) return 1;
  1835. int nTempStatus = GetVarstatus(pBaseVar->m_dbData, pBaseVar->m_nUpperLimit, pBaseVar->m_nLowerLimit, pBaseVar->m_nNormalState);
  1836. if( UPPER_LIMIT_ID == nTempStatus ) // 上限告警
  1837. {
  1838. strcpy(szStatusDesc, g_strHintUpperLimit);
  1839. //LOG4C((LOG_NOTICE, "var desc = %s, %s", pBaseVar->m_strDesc, szStatusDesc));
  1840. }
  1841. else if( LOWER_LIMIT_ID == nTempStatus ) // 下限告警
  1842. {
  1843. strcpy(szStatusDesc, g_strHintLowerLimit);
  1844. }
  1845. else if( nTempStatus != pBaseVar->m_nNormalState )
  1846. {
  1847. list<string>::iterator it_desc, it_id;
  1848. if( pBaseVar->m_listStatusDesc.size() > 0 && pBaseVar->m_listStatusID.size() > 0 )
  1849. {
  1850. for( it_desc = pBaseVar->m_listStatusDesc.begin(), it_id = pBaseVar->m_listStatusID.begin();
  1851. it_desc != pBaseVar->m_listStatusDesc.end(), it_id != pBaseVar->m_listStatusID.end(); )
  1852. {
  1853. CString strStatusID = (*it_id++).c_str();
  1854. CString strStatusDesc = (*it_desc++).c_str();
  1855. if( atoi(strStatusID) == nTempStatus )
  1856. {
  1857. //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));
  1858. strcpy(szStatusDesc, (char *)(LPCTSTR)strStatusDesc);
  1859. //LOG4C((LOG_NOTICE, "szStatusDesc=%s", szStatusDesc));
  1860. break;
  1861. }
  1862. }
  1863. }
  1864. if( strcmp(szStatusDesc, "") == 0 )
  1865. {
  1866. //LOG4C((LOG_NOTICE, "VarAlarmNotice var = %s nTempStatus = %d NormalState = %d statudesc is null!", pBaseVar->m_strDesc, nTempStatus, pBaseVar->m_nNormalState));
  1867. return 1;
  1868. }
  1869. }
  1870. if( nTempStatus != pBaseVar->m_nNormalState ) // 报警状态
  1871. {
  1872. if( pBaseVar->m_strReserved2.Compare("") != 0 )//报警描述 不为空
  1873. {
  1874. CString sLowerDesc,sUpperDesc,sData;
  1875. sData.Format( "%d%s",(int)pBaseVar->m_dbData, pBaseVar->m_strUnit );
  1876. int nPos = pBaseVar->m_strReserved2.Find( ";" );
  1877. if( nPos!=-1 )
  1878. {
  1879. sLowerDesc = pBaseVar->m_strReserved2.Left( nPos );
  1880. sUpperDesc = pBaseVar->m_strReserved2.Right( pBaseVar->m_strReserved2.GetLength()-nPos-1 );
  1881. }
  1882. else
  1883. {
  1884. sLowerDesc = pBaseVar->m_strReserved2;
  1885. sUpperDesc = pBaseVar->m_strReserved2;
  1886. }
  1887. if( UPPER_LIMIT_ID == nTempStatus ) //上限
  1888. {
  1889. int nPosData = sUpperDesc.Find( "*" );
  1890. if( nPosData!=-1 )
  1891. sUpperDesc.Replace( "*",sData );
  1892. strContect = sUpperDesc;
  1893. }
  1894. else if( LOWER_LIMIT_ID == nTempStatus ) //下限
  1895. {
  1896. int nPosData = sLowerDesc.Find( "*" );
  1897. if( nPosData!=-1 )
  1898. sLowerDesc.Replace( "*",sData );
  1899. strContect = sLowerDesc;
  1900. }
  1901. else
  1902. {
  1903. int nPosData = sLowerDesc.Find( "*" );
  1904. if( nPosData!=-1 )
  1905. sLowerDesc.Replace( "*",sData );
  1906. strContect = sLowerDesc;
  1907. }
  1908. }
  1909. else
  1910. {
  1911. if( pBaseVar->m_bReserved1 == true )
  1912. {
  1913. if( pBaseVar->m_strReserved1.Compare("") == 0 )
  1914. {
  1915. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pBaseVar->m_dbData, pBaseVar->m_strUnit);
  1916. }
  1917. else
  1918. {
  1919. int nDeviceIndex = -1, nVarIndex = -1;
  1920. BOOL bFind = FindVar(pBaseVar->m_strVarUID, pBaseVar->m_strReserved1, nDeviceIndex, nVarIndex);
  1921. if( bFind == FALSE )
  1922. {
  1923. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pBaseVar->m_dbData, pBaseVar->m_strUnit);
  1924. }
  1925. else
  1926. {
  1927. CDevice *pDev = g_pDevicesManager->m_Devices[nDeviceIndex];
  1928. CBaseVar *pTempBaseVar = pDev->m_Vars[nVarIndex];
  1929. strContect.Format("%s%s:%s, %s%d%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc, g_strHintCurrValue, (int)pTempBaseVar->m_dbData, pBaseVar->m_strUnit);
  1930. }
  1931. }
  1932. }
  1933. else
  1934. {
  1935. strContect.Format("%s%s:%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, szStatusDesc);
  1936. }
  1937. }
  1938. if( pBaseVar->m_bExistDatePlan )
  1939. {
  1940. if( pBaseVar->m_dwReturnNormalIdentityTick>0 )
  1941. {
  1942. pBaseVar->m_dwReturnNormalIdentityTick = 0;
  1943. //LOG4C(( LOG_NOTICE,"var=%s 恢复正常未达到辩识时间条件又收到报警值,放弃加入",pBaseVar->m_strDesc));
  1944. }
  1945. if( pBaseVar->m_dwIdentityTick == 0 && pBaseVar->m_nAlarmStatus!=2 )
  1946. {
  1947. //EnterCriticalSection( &g_csVarNotice );
  1948. pBaseVar->m_dwIdentityTick = GetTickCount();
  1949. //LeaveCriticalSection( &g_csVarNotice );
  1950. //LOG4C((LOG_NOTICE, "var=%s 达到报警条件", pBaseVar->m_strDesc));
  1951. }
  1952. else if( pBaseVar->m_nAlarmStatus==0 )
  1953. {
  1954. //LOG4C((LOG_NOTICE, "var=%s 第一次达到报警条件,不用判断辩识时间,直接加入", pBaseVar->m_strDesc));
  1955. EnterCriticalSection( &g_csVarNotice );
  1956. pBaseVar->m_nAlarmStatus = 1;//设为第一次报警状态
  1957. LeaveCriticalSection( &g_csVarNotice );
  1958. ProcessVarNotice(
  1959. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1960. pBaseVar->m_nVarID,
  1961. pBaseVar->m_nUpperLimit,
  1962. pBaseVar->m_nLowerLimit,
  1963. pBaseVar->m_nOffset,
  1964. nTempStatus,
  1965. pBaseVar->m_nNormalState,
  1966. pBaseVar->m_nIdentifyTime,
  1967. pBaseVar->m_nReDetectTime,
  1968. pBaseVar->m_bNormalIsNotice,
  1969. pBaseVar->m_nWaringLevel,
  1970. (char *)(LPCTSTR)pDev->m_strDeviceName,
  1971. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  1972. (char *)(LPCTSTR)pDev->m_strReserved1,
  1973. pBaseVar->m_dbData,
  1974. (char *)(LPCTSTR)strContect );
  1975. EnterCriticalSection( &g_csVarNotice );
  1976. pBaseVar->m_strContent = strContect;
  1977. pBaseVar->m_nLastStatus = nTempStatus;
  1978. pBaseVar->m_dwIdentityTick = 0;
  1979. LeaveCriticalSection( &g_csVarNotice );
  1980. }
  1981. else if( GetTickCount() - pBaseVar->m_dwIdentityTick > (DWORD)pBaseVar->m_nIdentifyTime * 1000 && pBaseVar->m_nAlarmStatus!=2 )
  1982. {
  1983. LOG4C((LOG_NOTICE, "var=%s 达到报警辩识时间条件,加入", pBaseVar->m_strDesc));
  1984. if( pBaseVar->m_nAlarmNumber < 10 )
  1985. pBaseVar->m_nAlarmNumber++;
  1986. EnterCriticalSection( &g_csVarNotice );
  1987. pBaseVar->m_nAlarmStatus = 2;//设为第二次报警状态
  1988. LeaveCriticalSection( &g_csVarNotice );
  1989. ProcessVarNotice(
  1990. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  1991. pBaseVar->m_nVarID,
  1992. pBaseVar->m_nUpperLimit,
  1993. pBaseVar->m_nLowerLimit,
  1994. pBaseVar->m_nOffset,
  1995. nTempStatus,
  1996. pBaseVar->m_nNormalState,
  1997. pBaseVar->m_nIdentifyTime,
  1998. pBaseVar->m_nReDetectTime,
  1999. pBaseVar->m_bNormalIsNotice,
  2000. pBaseVar->m_nWaringLevel,
  2001. (char *)(LPCTSTR)pDev->m_strDeviceName,
  2002. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  2003. (char *)(LPCTSTR)pDev->m_strReserved1,
  2004. pBaseVar->m_dbData,
  2005. (char *)(LPCTSTR)strContect );
  2006. EnterCriticalSection( &g_csVarNotice );
  2007. pBaseVar->m_strContent = strContect;
  2008. pBaseVar->m_nLastStatus = nTempStatus;
  2009. pBaseVar->m_dwIdentityTick = 0;
  2010. LeaveCriticalSection( &g_csVarNotice );
  2011. if( 1 == g_nSnmpEnable )
  2012. {
  2013. if( pBaseVar->m_bReserved4==TRUE )
  2014. {
  2015. //LOG4C((LOG_NOTICE, "pBaseVar->m_bReserved4=%d pBaseVar->m_bIsChange=%d", pBaseVar->m_bReserved4,pBaseVar->m_bIsChange ));
  2016. CString strSetValue;
  2017. strSetValue.Format("1*%.1f#%s", pBaseVar->m_dbData,strContect);
  2018. CString strObjID;
  2019. strObjID.Format("%s.%d", g_strSnmpOidRoot, pBaseVar->m_nVarID);
  2020. //LOG4C((LOG_NOTICE, "strObjID: %s", strObjID ));
  2021. pSnmpDllSetValue(
  2022. g_strSnmpIP,
  2023. g_strSnmpField,
  2024. (char *)(LPCTSTR)strObjID,
  2025. "str",
  2026. 1,
  2027. 300,
  2028. (char *)(LPCTSTR)strSetValue
  2029. );
  2030. }
  2031. }
  2032. }
  2033. }
  2034. }
  2035. else
  2036. {
  2037. if( pBaseVar->m_dwIdentityTick>0 )
  2038. {
  2039. pBaseVar->m_dwIdentityTick = 0;
  2040. //LOG4C(( LOG_NOTICE,"var=%s 报警未达到辩识时间条件又收到正常值,放弃加入",pBaseVar->m_strDesc));
  2041. }
  2042. if( pBaseVar->m_nAlarmNumber >= 1 && pBaseVar->m_nAlarmNumber < 11 )
  2043. {
  2044. if( pBaseVar->m_nUpperLimit>0 || pBaseVar->m_nLowerLimit>0 )//模拟量
  2045. {
  2046. //LOG4C((LOG_NOTICE, "m_dbData=%d m_nLowerLimit=%d m_nOffset=%d m_nUpperLimit=%d", (int)pBaseVar->m_dbData,pBaseVar->m_nLowerLimit,pBaseVar->m_nOffset,pBaseVar->m_nUpperLimit));
  2047. if( pBaseVar->m_dbData<=pBaseVar->m_nLowerLimit+pBaseVar->m_nOffset ||
  2048. pBaseVar->m_dbData>=pBaseVar->m_nUpperLimit-pBaseVar->m_nOffset )
  2049. {
  2050. //LOG4C((LOG_NOTICE, "偏移量作用"));
  2051. return 0;
  2052. }
  2053. }
  2054. if( pBaseVar->m_strReserved3.Compare("") != 0 )//恢复描述 不为空
  2055. {
  2056. CString sLowerDesc,sUpperDesc,sData;
  2057. sData.Format( "%d%s",(int)pBaseVar->m_dbData, pBaseVar->m_strUnit );
  2058. CString sDesc = pBaseVar->m_strReserved3;
  2059. int nPosData = sDesc.Find( "*" );
  2060. if( nPosData!=-1 )
  2061. sDesc.Replace( "*",sData );
  2062. strContect = sDesc;
  2063. }
  2064. else
  2065. strContect.Format("%s%s:%s ", pDev->m_strReserved1, pBaseVar->m_strDesc, g_strHintReturnNormal);
  2066. if( pBaseVar->m_dwReturnNormalIdentityTick == 0 )
  2067. {
  2068. //EnterCriticalSection( &g_csVarNotice );
  2069. pBaseVar->m_dwReturnNormalIdentityTick = GetTickCount();
  2070. //LeaveCriticalSection( &g_csVarNotice );
  2071. //LOG4C((LOG_NOTICE, "var=%s 达到恢复正常条件", pBaseVar->m_strDesc));
  2072. }
  2073. else if( GetTickCount() - pBaseVar->m_dwReturnNormalIdentityTick > (DWORD)pBaseVar->m_nIdentifyTime * 1000 )
  2074. {
  2075. LOG4C((LOG_NOTICE, "var=%s 达到恢复正常辩识时间条件,加入", pBaseVar->m_strDesc));
  2076. pBaseVar->m_nAlarmNumber = 0;
  2077. EnterCriticalSection( &g_csVarNotice );
  2078. pBaseVar->m_nAlarmStatus = 3;//设为恢复正常状态
  2079. LeaveCriticalSection( &g_csVarNotice );
  2080. //回复正常
  2081. ProcessVarNotice(
  2082. (char *)(LPCTSTR)pBaseVar->m_strVarUID,
  2083. pBaseVar->m_nVarID,
  2084. pBaseVar->m_nUpperLimit,
  2085. pBaseVar->m_nLowerLimit,
  2086. pBaseVar->m_nOffset,
  2087. nTempStatus,
  2088. pBaseVar->m_nNormalState,
  2089. pBaseVar->m_nIdentifyTime,
  2090. pBaseVar->m_nReDetectTime,
  2091. pBaseVar->m_bNormalIsNotice,
  2092. pBaseVar->m_nWaringLevel,
  2093. (char *)(LPCTSTR)pDev->m_strDeviceName,
  2094. (char *)(LPCTSTR)pBaseVar->m_strDesc,
  2095. (char *)(LPCTSTR)pDev->m_strReserved1,
  2096. pBaseVar->m_dbData,
  2097. (char *)(LPCTSTR)strContect );
  2098. EnterCriticalSection( &g_csVarNotice );
  2099. pBaseVar->m_strContent = strContect;
  2100. pBaseVar->m_nLastStatus = nTempStatus;
  2101. pBaseVar->m_dwReturnNormalIdentityTick = 0;
  2102. LeaveCriticalSection( &g_csVarNotice );
  2103. if( 1 == g_nSnmpEnable )
  2104. {
  2105. if( pBaseVar->m_bReserved4==TRUE )
  2106. {
  2107. //LOG4C((LOG_NOTICE, "pBaseVar->m_bReserved4=%d pBaseVar->m_bIsChange=%d", pBaseVar->m_bReserved4,pBaseVar->m_bIsChange ));
  2108. CString strSetValue;
  2109. strSetValue.Format("2*%.1f#%s", pBaseVar->m_dbData,strContect);
  2110. CString strObjID;
  2111. strObjID.Format("%s.%d", g_strSnmpOidRoot, pBaseVar->m_nVarID);
  2112. //LOG4C((LOG_NOTICE, "strObjID: %s", strObjID ));
  2113. pSnmpDllSetValue(
  2114. g_strSnmpIP,
  2115. g_strSnmpField,
  2116. (char *)(LPCTSTR)strObjID,
  2117. "str",
  2118. 1,
  2119. 300,
  2120. (char *)(LPCTSTR)strSetValue
  2121. );
  2122. }
  2123. }
  2124. }
  2125. }
  2126. }
  2127. return 0;
  2128. }