Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

468 lines
12 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 01/01/2000
  5. // Last modification: 08/10/2003
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: LgMaskScheduleEngine.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of LgMaskScheduleEngine.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "LgMaskScheduleEngine.h"
  16. CLgMaskScheduleEngine::CLgMaskScheduleEngine()
  17. {
  18. m_nDayOfWeek = -1;
  19. m_pMaskScheduleCallback = NULL;
  20. m_pMaskScheduleCallbackParam = 0;
  21. m_liNextTimeInSec.QuadPart = 0;
  22. m_dwNextTimeInSecUpdate = 0;
  23. }
  24. CLgMaskScheduleEngine::~CLgMaskScheduleEngine()
  25. {
  26. Destroy();
  27. }
  28. BOOL CLgMaskScheduleEngine::Create( PMASK_SWITCH_CALLBACK pMaskScheduleCallback, LPVOID lParam )
  29. {
  30. // mark that it is nessessary to make initializatioin
  31. m_nDayOfWeek = -1;
  32. m_pMaskScheduleCallback = pMaskScheduleCallback;
  33. m_pMaskScheduleCallbackParam = lParam;
  34. return TRUE;
  35. }
  36. void CLgMaskScheduleEngine::Destroy()
  37. {
  38. __try
  39. {
  40. DeleteAll();
  41. }
  42. __except( EXCEPTION_EXECUTE_HANDLER )
  43. {
  44. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Destroy"), _T("Exception 0x%08X"), _exception_code() );
  45. }
  46. }
  47. void CLgMaskScheduleEngine::OnTimer( SYSTEMTIME *pST )
  48. {
  49. try
  50. {
  51. // perform initialization at the begining of the new day
  52. if( m_nDayOfWeek != pST->wDayOfWeek )
  53. {
  54. // determine special day entry
  55. BOOL bSpecialDayEntry = FALSE;
  56. // find any special day schedule entry
  57. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  58. {
  59. PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  60. // if rule applies only to special day
  61. if( !pSchedule->bSpecialDay )
  62. continue;
  63. if( pST->wYear == pSchedule->stDate.wYear &&
  64. pST->wMonth == pSchedule->stDate.wMonth &&
  65. pST->wDay == pSchedule->stDate.wDay )
  66. {
  67. // mark that need to be performed
  68. pSchedule->bPerform = TRUE;
  69. // mark that found special day entry
  70. bSpecialDayEntry = TRUE;
  71. // log informaion
  72. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "found special day rule #%d", i );
  73. }
  74. else
  75. {
  76. // mark that need does not to be performed
  77. pSchedule->bPerform = FALSE;
  78. }
  79. }
  80. // find any special day schedule entry
  81. for( i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  82. {
  83. PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  84. // if rule applies only to week day
  85. if( pSchedule->bSpecialDay )
  86. continue;
  87. // if this day is required day of week and no special day rules
  88. if( pSchedule->bWeek[ pST->wDayOfWeek ] && !bSpecialDayEntry )
  89. {
  90. // mark that need to be performed
  91. pSchedule->bPerform = TRUE;
  92. // log informaion
  93. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "found week day rule #%d", i );
  94. }
  95. else
  96. {
  97. // mark that need does not to be performed
  98. pSchedule->bPerform = FALSE;
  99. }
  100. }
  101. // store current day
  102. m_nDayOfWeek = pST->wDayOfWeek;
  103. // check immidiatly
  104. m_liNextTimeInSec.QuadPart = 0i64;
  105. }
  106. // NOTE:
  107. // should be added switch management by time, not by the order
  108. DWORD dwTime = GetTickCount();
  109. m_liNextTimeInSec.QuadPart -= (__int64)( dwTime - m_dwNextTimeInSecUpdate );
  110. m_dwNextTimeInSecUpdate = dwTime;
  111. if( m_liNextTimeInSec.QuadPart > 0 )
  112. return;
  113. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "Next switch task time comes" );
  114. // prepare time in sec
  115. INT nTimeInSec = pST->wHour * 3600 + pST->wMinute * 60 + pST->wSecond;
  116. // prepare next time check
  117. m_liNextTimeInSec.QuadPart = INFINITE;
  118. // check schedule plan
  119. CLgArray<CLgMaskScheduleLgsSwitch> arrSwitches;
  120. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  121. {
  122. PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  123. // if rule should be performed
  124. if( !pSchedule->bPerform )
  125. continue;
  126. // convert time from H:M:S to S
  127. INT nScheduleTimeInSec = pSchedule->stTime.wHour * 3600 + pSchedule->stTime.wMinute * 60 + pSchedule->stTime.wSecond;
  128. // check date & time
  129. if( nTimeInSec < nScheduleTimeInSec )
  130. {
  131. // prepare next time
  132. LARGE_INTEGER liNextTimeInSec = { nScheduleTimeInSec - nTimeInSec };
  133. if( m_liNextTimeInSec.QuadPart > liNextTimeInSec.QuadPart )
  134. m_liNextTimeInSec.QuadPart = liNextTimeInSec.QuadPart;
  135. }
  136. else
  137. {
  138. // mark as performed
  139. pSchedule->bPerform = FALSE;
  140. // log information
  141. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "rule #%d", i );
  142. // add switches for each lgses
  143. for( INT j = 0; j < pSchedule->arrLgs.GetCount(); j++ )
  144. {
  145. CLgMaskScheduleLgs &lgs = pSchedule->arrLgs[j];
  146. // try to find among existing
  147. BOOL bFound = FALSE;
  148. for( INT k = 0; k < arrSwitches.GetCount(); k++ )
  149. {
  150. CLgMaskScheduleLgsSwitch &sw = arrSwitches[k];
  151. if( sw.dwId != lgs.dwId )
  152. continue;
  153. for( INT l = 0; l < LGS_MASK_SCHEDULE_COUNT; l++ )
  154. {
  155. if( !pSchedule->pHzEnable[l] )
  156. continue;
  157. sw.pHzOn[l] = pSchedule->pHzOn[l];
  158. sw.pHzEnable[l] = TRUE;
  159. }
  160. bFound = TRUE;
  161. }
  162. // add new switch
  163. if( !bFound )
  164. {
  165. CLgMaskScheduleLgsSwitch sw;
  166. sw.dwId = lgs.dwId;
  167. memcpy( sw.pName, lgs.pName, sizeof(sw.pName) );
  168. memcpy( sw.pHzEnable, pSchedule->pHzEnable, sizeof(sw.pHzEnable) );
  169. memcpy( sw.pHzOn, pSchedule->pHzOn, sizeof(sw.pHzOn) );
  170. arrSwitches.Add( sw );
  171. }
  172. }
  173. }
  174. }
  175. for( i = 0; i < arrSwitches.GetCount(); i++ )
  176. MaskSwitch( arrSwitches[i] );
  177. // convert to milliseconds
  178. m_liNextTimeInSec.QuadPart *= 1000i64;
  179. m_dwNextTimeInSecUpdate = ::GetTickCount();
  180. }
  181. catch( ... )
  182. {
  183. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::OnTimer"), _T("C++ exception") );
  184. }
  185. }
  186. void CLgMaskScheduleEngine::UpdateSchedule( INT nIndex )
  187. {
  188. try
  189. {
  190. // get current time & date
  191. SYSTEMTIME st;
  192. ::GetLocalTime( &st );
  193. // check immidiatly
  194. m_liNextTimeInSec.QuadPart = 0i64;
  195. // if this rule is rule for special day
  196. if( m_HzScheduleArray[nIndex].bSpecialDay )
  197. {
  198. if( st.wYear == m_HzScheduleArray[nIndex].stDate.wYear &&
  199. st.wMonth == m_HzScheduleArray[nIndex].stDate.wMonth &&
  200. st.wDay == m_HzScheduleArray[nIndex].stDate.wDay )
  201. {
  202. // now is a special day for this rule
  203. m_HzScheduleArray[nIndex].bPerform = TRUE;
  204. return;
  205. }
  206. // requared day isn't today
  207. m_HzScheduleArray[nIndex].bPerform = FALSE;
  208. return;
  209. }
  210. // find any other special day schedule entry
  211. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  212. {
  213. PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  214. // if rule applies only to special day
  215. if( pSchedule->bSpecialDay )
  216. {
  217. if( st.wYear == pSchedule->stDate.wYear &&
  218. st.wMonth == pSchedule->stDate.wMonth &&
  219. st.wDay == pSchedule->stDate.wDay )
  220. {
  221. // mark that it is not need to be performed
  222. m_HzScheduleArray[nIndex].bPerform = FALSE;
  223. return;
  224. }
  225. }
  226. }
  227. // if this day is required day of week and no special day rules
  228. if( m_HzScheduleArray[nIndex].bWeek[ st.wDayOfWeek ] )
  229. // mark that need to be performed
  230. m_HzScheduleArray[nIndex].bPerform = TRUE;
  231. else
  232. // mark that it is not need to be performed
  233. m_HzScheduleArray[nIndex].bPerform = FALSE;
  234. }
  235. catch( ... )
  236. {
  237. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::UpdateSchedule"), _T("C++ exception") );
  238. }
  239. }
  240. static TCHAR g_SheduleVersion[MAX_PATH] = _T("LGS MASK SCHEDULE VERSION 0.0");
  241. BOOL CLgMaskScheduleEngine::Load( LPCTSTR pFileName )
  242. {
  243. // open file
  244. FILE *f = fopen( pFileName, "rb" );
  245. if( !f )
  246. return FALSE;
  247. // store version
  248. TCHAR pVersion[MAX_PATH];
  249. fread( pVersion, 1, sizeof( pVersion ), f );
  250. if( _tcsncmp( pVersion, g_SheduleVersion, MAX_PATH ) )
  251. {
  252. fclose( f );
  253. return TRUE;
  254. }
  255. // read cumder of items
  256. INT nCount = 0;
  257. fread( &nCount, 1, sizeof( nCount ), f );
  258. // validate number of items
  259. if( nCount < 0 && nCount > 64000 )
  260. {
  261. fclose( f );
  262. return FALSE;
  263. }
  264. try
  265. {
  266. // read all schedule plan
  267. for( INT i = 0; i < nCount; i++ )
  268. {
  269. MASK_SCHEDULE schedule;
  270. // load from file
  271. fread( &schedule.pHzOn, 1, sizeof( schedule.pHzOn ), f );
  272. fread( schedule.pHzEnable, 1, sizeof( schedule.pHzEnable ), f );
  273. fread( &schedule.stTime, 1, sizeof( schedule.stTime ), f );
  274. fread( &schedule.bSpecialDay, 1, sizeof( schedule.bSpecialDay ), f );
  275. fread( schedule.bWeek, 1, max( sizeof(schedule.bWeek), sizeof(schedule.stDate) ), f );
  276. // store lgs array
  277. INT nCount = 0;
  278. fread( &nCount, 1, sizeof(nCount), f );
  279. for( INT j = 0; j < nCount; j++ )
  280. {
  281. CLgMaskScheduleLgs lgs;
  282. fread( &lgs, 1, sizeof(lgs), f );
  283. schedule.arrLgs.Add( lgs );
  284. }
  285. Add( &schedule );
  286. }
  287. }
  288. catch( ... )
  289. {
  290. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Load"), _T("C++ exception") );
  291. }
  292. // mark that nessesary to initialize
  293. m_nDayOfWeek = -1;
  294. // close file
  295. fclose( f );
  296. return TRUE;
  297. }
  298. BOOL CLgMaskScheduleEngine::Save( LPCTSTR pFileName )
  299. {
  300. // open file
  301. FILE *f = fopen( pFileName, "wb" );
  302. if( !f )
  303. return FALSE;
  304. try
  305. {
  306. // store version
  307. fwrite( g_SheduleVersion, 1, sizeof( g_SheduleVersion ), f );
  308. // write cumder of items
  309. INT nCount = m_HzScheduleArray.GetCount();
  310. fwrite( &nCount, 1, sizeof( nCount ), f );
  311. // write all schedule plan
  312. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  313. {
  314. MASK_SCHEDULE &schedule = m_HzScheduleArray[i];
  315. // store timing data
  316. fwrite( schedule.pHzOn, 1, sizeof( schedule.pHzOn ), f );
  317. fwrite( schedule.pHzEnable, 1, sizeof( schedule.pHzEnable ), f );
  318. fwrite( &schedule.stTime, 1, sizeof( schedule.stTime ), f );
  319. fwrite( &schedule.bSpecialDay, 1, sizeof( schedule.bSpecialDay ), f );
  320. fwrite( schedule.bWeek, 1, max( sizeof(schedule.bWeek), sizeof(schedule.stDate) ), f );
  321. // store lgs array
  322. INT nCount = schedule.arrLgs.GetCount();
  323. fwrite( &nCount, 1, sizeof(nCount), f );
  324. for( INT j = 0; j < nCount; j++ )
  325. {
  326. CLgMaskScheduleLgs &lgs = schedule.arrLgs[j];
  327. fwrite( &lgs, 1, sizeof(lgs), f );
  328. }
  329. }
  330. }
  331. catch( ... )
  332. {
  333. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Save"), _T("C++ exception") );
  334. }
  335. // close file
  336. fclose( f );
  337. return TRUE;
  338. }
  339. BOOL CLgMaskScheduleEngine::Add( PMASK_SCHEDULE pHzSchedule )
  340. {
  341. BOOL bResult = FALSE;
  342. try
  343. {
  344. // perform strcut convertion from MASK_SCHEDULE to MASK_SCHEDULE_EX
  345. MASK_SCHEDULE_EX HzScheduleEx;
  346. PMASK_SCHEDULE pHzScheduleEx = &HzScheduleEx;
  347. *pHzScheduleEx = *pHzSchedule;
  348. HzScheduleEx.bPerform = TRUE;
  349. // plase int orules array
  350. bResult = m_HzScheduleArray.Add( HzScheduleEx );
  351. }
  352. catch( ... )
  353. {
  354. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Add"), _T("C++ exception") );
  355. }
  356. return bResult;
  357. }
  358. BOOL CLgMaskScheduleEngine::Delete( INT nIndex )
  359. {
  360. BOOL bResult = FALSE;
  361. __try
  362. {
  363. // remove from rules array
  364. bResult = m_HzScheduleArray.Remove( nIndex );
  365. }
  366. __except( EXCEPTION_EXECUTE_HANDLER )
  367. {
  368. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Delete"), _T("Exception 0x%08X"), _exception_code() );
  369. }
  370. return bResult;
  371. }
  372. void CLgMaskScheduleEngine::DeleteAll()
  373. {
  374. __try
  375. {
  376. // remove from rules array
  377. m_HzScheduleArray.RemoveAll();
  378. }
  379. __except( EXCEPTION_EXECUTE_HANDLER )
  380. {
  381. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::DeleteAll"), _T("Exception 0x%08X"), _exception_code() );
  382. }
  383. }
  384. void CLgMaskScheduleEngine::MaskSwitch( CLgMaskScheduleLgsSwitch& arrSwitch )
  385. {
  386. __try
  387. {
  388. m_pMaskScheduleCallback( m_pMaskScheduleCallbackParam, arrSwitch );
  389. }
  390. __except( EXCEPTION_EXECUTE_HANDLER )
  391. {
  392. LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::MaskSwitch"), _T("Exception 0x%08X"), _exception_code() );
  393. }
  394. }