VariantManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. #include "stdafx.h"
  2. #include "kernel.h"
  3. #include "sharedefine.h"
  4. const char* VARIANT_FILE = "variants.dat";
  5. //---------------------------------------------- VariantsManager ----
  6. CVariantsManager::CVariantsManager()
  7. {
  8. m_bAnalogAlarm = true;
  9. }
  10. CVariantsManager::~CVariantsManager()
  11. {
  12. int i;
  13. for (i = 0; i < m_analog.GetSize(); i++)
  14. {
  15. CAnalog* analog = m_analog.GetAt(i);
  16. list<int>::iterator it;
  17. list<CString>::iterator it_desc;
  18. for( it = analog->m_listStatusID.begin(); it != analog->m_listStatusID.end(); )
  19. {
  20. analog->m_listStatusID.erase( it++ );
  21. }
  22. for( it_desc = analog->m_listStatusDesc.begin(); it_desc != analog->m_listStatusDesc.end(); )
  23. {
  24. analog->m_listStatusDesc.erase( it_desc++ );
  25. }
  26. delete m_analog[i];
  27. }
  28. m_analog.RemoveAll();
  29. }
  30. void CVariantsManager::MakeDeviceRelate()
  31. {
  32. int i, size = m_analog.GetSize();
  33. if( pDevicesManager == NULL ) return ;
  34. int nChannelIndex, nChannelSize = pDevicesManager->m_channels.GetSize();
  35. int nDevicesIndex, nDevicesSize;
  36. for (i = 0; i < size; i++)
  37. {
  38. m_analog.GetAt(i)->m_pDevice = NULL;
  39. for( nChannelIndex = 0; nChannelIndex < nChannelSize; nChannelIndex++ )
  40. {
  41. nDevicesSize = pDevicesManager->m_channels[nChannelIndex]->m_Devices.GetSize();
  42. for( nDevicesIndex = 0; nDevicesIndex < nDevicesSize; nDevicesIndex++ )
  43. {
  44. if( !pDevicesManager->m_channels[nChannelIndex]->m_Devices[nDevicesIndex]->m_strUID.Compare(m_analog.GetAt(i)->m_strVarUID) )
  45. {
  46. m_analog.GetAt(i)->m_pDevice = pDevicesManager->m_channels[nChannelIndex]->m_Devices[nDevicesIndex];
  47. }
  48. }
  49. }
  50. }
  51. }
  52. void CVariantsManager::Load(char *pStrDirectroy)
  53. {
  54. CFile file;
  55. CString fileName = CString(pStrDirectroy) + "\\" + _PROJECTDIR;
  56. fileName = fileName + VARIANT_FILE;
  57. if (0 != file.Open( fileName, CFile::modeRead))
  58. {
  59. CArchive ar(&file,CArchive::load);
  60. m_analog.Serialize(ar);
  61. ar.Close();
  62. file.Close();
  63. MakeDeviceRelate();
  64. CAnalog *pAnalog = FindAnalogByVarTypeID(SYS_COMMUNCTION_VAR_ID);
  65. if( pAnalog == NULL )
  66. {
  67. AddSysCommunctionVar();
  68. }
  69. AllocDatePlan();
  70. }
  71. }
  72. void CVariantsManager::SetDBType(char *pDBType)
  73. {
  74. m_strDBType = CString(pDBType);
  75. }
  76. bool CVariantsManager::IsExistDatePlan(char *pDevUid, int nVarID)
  77. {//0:星期一 1:星期二 6:星期天
  78. unsigned char szArray[7][24];
  79. memset(szArray, 0, sizeof(szArray) );
  80. CTime time = CTime::GetCurrentTime();
  81. // 获取当前时间
  82. int nWeekOfDay = time.GetDayOfWeek();
  83. if( nWeekOfDay == 1 )
  84. {
  85. nWeekOfDay = 6;
  86. }
  87. else
  88. {
  89. nWeekOfDay -= 2;
  90. }
  91. int nHours = time.GetHour();
  92. int nRet;
  93. nRet = CDBInterface::GetInstancePtr()->GetDatePlan( PLAN_TYPE_EQUIP, pDevUid, nVarID, szArray );
  94. if( nRet > 0 )
  95. {
  96. if( 15 == szArray[nWeekOfDay][nHours] )
  97. {
  98. return true;
  99. }
  100. //for( int x=0; x<7; x++ )
  101. //{
  102. // for(int y=0; y<24; y++)
  103. // {
  104. // if( 15 == szArray[x][y] )
  105. // {
  106. // return true;
  107. // }
  108. // }
  109. //}
  110. }
  111. else
  112. {
  113. return true;
  114. }
  115. return false;
  116. }
  117. void CVariantsManager::AllocDatePlan(void)
  118. {
  119. for( int i = 0; i < m_analog.GetSize(); i++ )
  120. {
  121. CAnalog *pAnalog = m_analog.GetAt(i);
  122. if( SYS_COMMUNCTION_VAR_ID == pAnalog->m_nVarTypeID )
  123. {
  124. pAnalog->m_bExistDatePlan = false;
  125. }
  126. else
  127. {
  128. pAnalog->m_bExistDatePlan = IsExistDatePlan( (char *)(LPCTSTR)pAnalog->m_strVarUID, pAnalog->m_nVarID );
  129. }
  130. }
  131. }
  132. void CVariantsManager::AddSysCommunctionVar()
  133. {
  134. CAnalog *pAnalog = new CAnalog();
  135. int nVarID;
  136. #if 0
  137. if( -1 == iSize )
  138. {
  139. nVarID = 0;
  140. }
  141. else
  142. {
  143. nVarID = m_analog.GetAt(iSize)->m_nVarID + 1;
  144. }
  145. #else
  146. nVarID = 0;
  147. #endif
  148. // 变量ID
  149. pAnalog->m_nVarID = nVarID;
  150. // 变量UID
  151. pAnalog->m_strVarUID = "0.0.0.0";
  152. // 变量名称
  153. pAnalog->m_strName = "SystemCommunication";
  154. // 变量描述
  155. pAnalog->m_strDesc = "Server Communication Status";
  156. // 变量类型ID
  157. pAnalog->m_nVarTypeID = SYS_COMMUNCTION_VAR_ID;
  158. // 变量数据长度
  159. pAnalog->m_nDataLen = 0;
  160. // 开的描述
  161. pAnalog->m_strOnDesc = "";
  162. // 关的描述
  163. pAnalog->m_strOffDesc = "";
  164. // 最大值
  165. pAnalog->m_nMaxValues = 0;
  166. // 最小值
  167. pAnalog->m_nMinValues = 0;
  168. // 最大转换比率
  169. pAnalog->m_nMaxConvtRate = 0;
  170. // 最小转换比率
  171. pAnalog->m_nMinConvtRate = 0;
  172. // 下限
  173. pAnalog->m_nLowerLimit = 0;
  174. // 上限
  175. pAnalog->m_nUpperLimit = 0;
  176. // 正常状态
  177. pAnalog->m_nNormalState = 0;
  178. // 设定值
  179. pAnalog->m_strSetValue = "";
  180. // 校正值
  181. pAnalog->m_nRearm = 0;
  182. // 偏移量
  183. pAnalog->m_nOffset = 0;
  184. // 单位
  185. pAnalog->m_strUnit = "";
  186. // 采集频率
  187. pAnalog->m_nCollectFreq = 900;
  188. // 转换系数
  189. pAnalog->m_fCoef = 1.0;
  190. // 告警级别
  191. pAnalog->m_nWaringLevel = 3;
  192. // 读写权限
  193. pAnalog->m_nPurview = 0;
  194. // 是否保存数值
  195. pAnalog->m_bIsSave = TRUE;
  196. // 是否实时采集数据
  197. pAnalog->m_bRealtimeFlag = TRUE;
  198. // 是否记录实时曲线
  199. pAnalog->m_bRecCurve = FALSE;
  200. // 是否允许DDE访问
  201. pAnalog->m_bIsDDE = FALSE;
  202. // 请求寄存器个数
  203. pAnalog->m_nRegNum = 0;
  204. // 功能码id
  205. pAnalog->m_nFuncCode = 0;
  206. // 寄存器子id
  207. pAnalog->m_nVarChildID = 0;
  208. // 位信息id
  209. pAnalog->m_nVarItemID = 0;
  210. // SNMP对象oid
  211. pAnalog->m_strSnmpOid = "";
  212. // SNMP域
  213. pAnalog->m_strFields = "";
  214. // 设备类型ID
  215. pAnalog->m_nDevID = 0;
  216. // 起始地址
  217. pAnalog->m_nRegStartAddr = 0;
  218. // Rs232请求命令
  219. pAnalog->m_strRs232Cmd = "";
  220. // Rs232请求类型
  221. pAnalog->m_strRs232Type = "";
  222. // Rs232数据索引
  223. pAnalog->m_nRs232Index = 0;
  224. // 起始BIT位
  225. pAnalog->m_nStartBit = 0;
  226. pAnalog->m_nEndBit = 0;
  227. pAnalog->m_strReserved1 = "";
  228. pAnalog->m_strReserved2 = "";
  229. pAnalog->m_strReserved3 = "";
  230. pAnalog->m_strReserved4 = "";
  231. pAnalog->m_strReserved5 = "";
  232. pAnalog->m_strReserved6 = "";
  233. pAnalog->m_strReserved7 = "";
  234. pAnalog->m_strReserved8 = "";
  235. pAnalog->m_strReserved9 = "";
  236. pAnalog->m_strReserved10 = "";
  237. pAnalog->m_nReserved1 = 0;
  238. pAnalog->m_nReserved2 = 0;
  239. pAnalog->m_nReserved3 = 0;
  240. pAnalog->m_nReserved4 = 0;
  241. pAnalog->m_nReserved5 = 0;
  242. pAnalog->m_nReserved6 = 0;
  243. pAnalog->m_nReserved7 = 0;
  244. pAnalog->m_nReserved8 = 0;
  245. pAnalog->m_nReserved9 = 0;
  246. pAnalog->m_nReserved10 = 0;
  247. pAnalog->m_bReserved1 = FALSE;
  248. pAnalog->m_bReserved2 = FALSE;
  249. pAnalog->m_bReserved3 = FALSE;
  250. pAnalog->m_bReserved4 = FALSE;
  251. pAnalog->m_bReserved5 = FALSE;
  252. pAnalog->m_bReserved6 = FALSE;
  253. pAnalog->m_bReserved7 = FALSE;
  254. pAnalog->m_bReserved8 = FALSE;
  255. pAnalog->m_bReserved9 = FALSE;
  256. pAnalog->m_bReserved10 = FALSE;
  257. pAnalog->m_pDevice = NULL;
  258. int i, nChannelSize = pDevicesManager->m_channels.GetSize();
  259. int j, nDevicesSize;
  260. for( i = 0; i < nChannelSize; i++ )
  261. {
  262. nDevicesSize = pDevicesManager->m_channels[i]->m_Devices.GetSize();
  263. for( j = 0; j < nDevicesSize; j++ )
  264. {
  265. if( !pDevicesManager->m_channels[i]->m_Devices[j]->m_strUID.Compare(pAnalog->m_strVarUID) )
  266. {
  267. pAnalog->m_pDevice = pDevicesManager->m_channels[i]->m_Devices[j];
  268. }
  269. }
  270. }
  271. pVariantsManager->m_analog.Add(pAnalog);
  272. }
  273. void CVariantsManager::SetSysComunicationVar( float fData )
  274. {
  275. CAnalog *pAnalog = FindAnalogByVarTypeID(SYS_COMMUNCTION_VAR_ID);
  276. if( NULL != pAnalog )
  277. {
  278. pAnalog->m_fData = fData;
  279. //LOG4C((LOG_NOTICE, "var name = %s, var= %s SetSysComunicationVar = %d", pAnalog->m_strName, pAnalog->m_strDesc, (int)pAnalog->m_fData));
  280. }
  281. }
  282. void CVariantsManager::Store(char *pStrDirectroy)
  283. {
  284. CFile file;
  285. CString fileName = CString(pStrDirectroy) + "\\" + _PROJECTDIR;
  286. fileName = fileName + VARIANT_FILE;
  287. if (0 != file.Open(fileName, CFile::modeWrite|CFile::modeCreate))
  288. {
  289. CArchive ar(&file,CArchive::store);
  290. m_analog.Serialize(ar);
  291. ar.Flush();
  292. ar.Close();
  293. file.Flush();
  294. file.Close();
  295. }
  296. }
  297. BOOL CVariantsManager::FindDevIsUse(CString strUid)
  298. {
  299. BOOL bResult = FALSE;
  300. int nChannelSize, nDeviceSize;
  301. nChannelSize = pDevicesManager->m_channels.GetSize();
  302. for( int i = 0; i < nChannelSize; i++ )
  303. {
  304. nDeviceSize = pDevicesManager->m_channels[i]->m_Devices.GetSize();
  305. for( int j = 0; j < nDeviceSize; j++ )
  306. {
  307. if( !strUid.Compare( pDevicesManager->m_channels[i]->m_Devices[j]->m_strUID ) )
  308. {
  309. if( pDevicesManager->m_channels[i]->m_Devices[j]->m_bUsed == TRUE )
  310. {
  311. bResult = TRUE;
  312. }
  313. }
  314. }
  315. }
  316. return bResult;
  317. }
  318. void CVariantsManager::Fresh(CString strUid)
  319. {
  320. int nVarSize = m_analog.GetSize();
  321. int nVarUseSize=0;
  322. for ( int i = 0; i< nVarSize; i++)
  323. {
  324. CAnalog* analog = m_analog.GetAt(i);
  325. BOOL bRet = FindDevIsUse( analog->m_strVarUID );
  326. if( bRet == TRUE )
  327. {
  328. if( analog->m_nPurview == 0 )//只读
  329. {
  330. //LOG4C((LOG_NOTICE, "请求变量ID=%d的值", analog->m_nVarID));
  331. int iPortIndex = nVarUseSize/VARNUM2PORT;
  332. if( iPortIndex%VARNUM2PORT==0 && iPortIndex>0 )
  333. iPortIndex--;
  334. analog->Fresh(iPortIndex);
  335. Sleep(10); // 让CPU释放一下。
  336. }
  337. nVarUseSize++;
  338. }
  339. }
  340. //FreshTime();
  341. }
  342. // My:向服务器请求更新当前视图的值;
  343. void CVariantsManager::FreshCurPic( CStringArray& saVarName )
  344. {
  345. int nVarSize = m_analog.GetSize();
  346. int nVarUseSize=0;
  347. int nCurVarCount = saVarName.GetCount();
  348. CString sName;
  349. for ( int i = 0; i< nVarSize; i++)
  350. {
  351. CAnalog* analog = m_analog.GetAt(i);
  352. BOOL bRet = FindDevIsUse( analog->m_strVarUID );
  353. if( bRet == TRUE )
  354. {
  355. if( analog->m_nPurview == 0 )//只读
  356. {
  357. for( int nCurIndex=0;nCurIndex<nCurVarCount;nCurIndex++ )
  358. {
  359. sName = saVarName.GetAt( nCurIndex );
  360. if( analog->m_strName == sName )
  361. {
  362. analog->Fresh(0);
  363. Sleep(5); // 让CPU释放一下。
  364. break;
  365. }
  366. }
  367. }
  368. nVarUseSize++;
  369. }
  370. }
  371. }
  372. // run
  373. float CVariantsManager::GetAnalogValue(CString name)
  374. {
  375. int aSize = m_analog.GetSize();
  376. for ( int i = 0; i < aSize; i++)
  377. {
  378. CAnalog* analog = m_analog.GetAt(i);
  379. if (analog->m_strName == name)
  380. {
  381. return analog->m_fData;
  382. }
  383. }
  384. return VARIANT_NOTFOUND;
  385. }
  386. // run
  387. CAnalog* CVariantsManager::FindAnalog(CString name)
  388. {
  389. int aSize = m_analog.GetSize();
  390. for ( int i = 0; i < aSize; i++)
  391. {
  392. CAnalog* analog = m_analog.GetAt(i);
  393. if ( analog->m_strName.Compare(name) == 0 )
  394. {
  395. return analog;
  396. }
  397. }
  398. return NULL;
  399. }
  400. // run
  401. CAnalog* CVariantsManager::FindAnalogByUID(CString strUID)
  402. {
  403. int aSize = m_analog.GetSize();
  404. for ( int i = 0; i < aSize; i++)
  405. {
  406. CAnalog* analog = m_analog.GetAt(i);
  407. if ( !analog->m_strVarUID.Compare(strUID) )
  408. {
  409. return analog;
  410. }
  411. }
  412. return NULL;
  413. }
  414. // run
  415. CAnalog* CVariantsManager::FindAnalog(int nVarID)
  416. {
  417. int aSize = m_analog.GetSize();
  418. for ( int i = 0; i < aSize; i++)
  419. {
  420. CAnalog* analog = m_analog.GetAt(i);
  421. if ( analog->m_nVarID == nVarID )
  422. {
  423. return analog;
  424. }
  425. }
  426. return NULL;
  427. }
  428. // run
  429. CString CVariantsManager::GetVarNameByID(int nVarID)
  430. {
  431. int aSize = m_analog.GetSize();
  432. for ( int i = 0; i < aSize; i++)
  433. {
  434. CAnalog* analog = m_analog.GetAt(i);
  435. if ( analog->m_nVarID == nVarID )
  436. {
  437. return analog->m_strName;
  438. }
  439. }
  440. return "";
  441. }
  442. // run
  443. CAnalog* CVariantsManager::FindAnalogByVarTypeID(int nVarTypeID)
  444. {
  445. int aSize = m_analog.GetSize();
  446. for ( int i = 0; i < aSize; i++)
  447. {
  448. CAnalog* analog = m_analog.GetAt(i);
  449. if ( analog->m_nVarTypeID == nVarTypeID )
  450. {
  451. return analog;
  452. }
  453. }
  454. return NULL;
  455. }
  456. BOOL CVariantsManager::DeleteAnalog(int nVarID)
  457. {
  458. int aSize = m_analog.GetSize();
  459. for ( int i = 0; i < aSize; i++)
  460. {
  461. CAnalog* analog = m_analog.GetAt(i);
  462. if ( analog->m_nVarID == nVarID )
  463. {
  464. list<int>::iterator it;
  465. list<CString>::iterator it_desc;
  466. for( it = analog->m_listStatusID.begin(); it != analog->m_listStatusID.end(); )
  467. {
  468. analog->m_listStatusID.erase( it++ );
  469. }
  470. for( it_desc = analog->m_listStatusDesc.begin(); it_desc != analog->m_listStatusDesc.end(); )
  471. {
  472. analog->m_listStatusDesc.erase( it_desc++ );
  473. }
  474. delete m_analog[i];
  475. m_analog.RemoveAt(i);
  476. return TRUE;
  477. }
  478. }
  479. return FALSE;
  480. }
  481. // run
  482. void CVariantsManager::SaveDayStatistic(int year, int month, int day)
  483. {
  484. int aSize = m_analog.GetSize();
  485. #if 0
  486. CDataList* pSaveDataList = new CDataList();
  487. for ( int i = 0; i < aSize; i++)
  488. {
  489. CAnalog* analog = m_analog.GetAt(i);
  490. if ( analog->m_bSave )
  491. {
  492. CDataUnit* unit = new CDataUnit();
  493. unit->max = analog->m_fDayMax;
  494. unit->ave = analog->m_fDayAvg;
  495. unit->min = analog->m_fDayMin;
  496. unit->day = day;
  497. unit->var = analog->m_strName;
  498. pSaveDataList->AddTail(unit);
  499. }
  500. }
  501. SaveMonthData(year, month, pSaveDataList);
  502. POSITION pos = pSaveDataList->GetHeadPosition();
  503. while (pos != NULL)
  504. {
  505. delete pSaveDataList->GetNext(pos);
  506. }
  507. pSaveDataList->RemoveAll();
  508. #endif
  509. }
  510. // run
  511. void CVariantsManager::InitDayStatistic(int day)
  512. {
  513. int aSize = m_analog.GetSize();
  514. for ( int i = 0; i < aSize; i++)
  515. {
  516. CAnalog* analog = m_analog.GetAt(i);
  517. analog->m_fDayMax = analog->m_fData;
  518. analog->m_fDayAvg = analog->m_fData;
  519. analog->m_fDayMin = analog->m_fData;
  520. analog->m_nMinCount = 0;
  521. analog->m_nDay = day;
  522. }
  523. }
  524. // run
  525. void CVariantsManager::SaveHourData(int year, int month, int day, int hour)
  526. {
  527. int aSize = m_analog.GetSize();
  528. #if 0
  529. CDataList* pSaveDataList = new CDataList();
  530. for ( int i = 0; i < aSize; i++)
  531. {
  532. CAnalog* analog = m_analog.GetAt(i);
  533. if ( analog->m_bSave )
  534. {
  535. CDataUnit* unit = new CDataUnit();
  536. unit->data = analog->m_fData;
  537. unit->day = day;
  538. unit->hour = hour;
  539. unit->minute = 0;
  540. unit->second = 0;
  541. unit->type = HOUR;
  542. unit->var = analog->m_strName;
  543. pSaveDataList->AddTail(unit);
  544. }
  545. }
  546. SaveDayData(year, month, day, pSaveDataList);
  547. #endif
  548. }
  549. // run
  550. BOOL CVariantsManager::SetAnalogValue(CString name, double value)
  551. {
  552. int aSize = m_analog.GetSize();
  553. for ( int i = 0; i < aSize; i++)
  554. {
  555. CAnalog* analog = m_analog.GetAt(i);
  556. if (analog->m_strName == name)
  557. {
  558. analog->SetValue((float)value );
  559. return TRUE;
  560. }
  561. }
  562. return FALSE;
  563. }
  564. void CVariantsManager::GetAnalogNameArray(CStringArray &nameArray)
  565. {
  566. nameArray.RemoveAll();
  567. int aSize = m_analog.GetSize();
  568. for ( int i = 0; i < aSize; i++)
  569. {
  570. CAnalog* analog = m_analog.GetAt(i);
  571. nameArray.Add(analog->m_strName);
  572. }
  573. }
  574. void CVariantsManager::FreshTime()
  575. {
  576. SYSTEMTIME st;
  577. GetLocalTime(&st);
  578. m_nMinutes = st.wHour * 60 + st.wMinute;
  579. }