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.

306 lines
7.5 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: 03/11/2002
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: HZMaskSchedule.cpp
  9. // AUTHOR: Sergey Chizhevskiy
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file of HZMaskSchedule.cpp
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "HZMaskSchedule.h"
  16. #include "Application.h"
  17. CHZMaskSchedule::CHZMaskSchedule()
  18. {
  19. m_pApplication = NULL;
  20. m_nDayOfWeek = -1;
  21. }
  22. CHZMaskSchedule::~CHZMaskSchedule()
  23. {
  24. Destroy();
  25. }
  26. BOOL CHZMaskSchedule::Create( CApplication *pApplication )
  27. {
  28. m_pApplication = pApplication;
  29. // mark that it is nessessary to make initializatioin
  30. m_nDayOfWeek = -1;
  31. return TRUE;
  32. }
  33. void CHZMaskSchedule::Destroy()
  34. {
  35. DeleteAll();
  36. }
  37. void CHZMaskSchedule::OnTimer( SYSTEMTIME *pST, PBOOL pStateHZ )
  38. {
  39. // perform initialization at the begining of the new day
  40. if( m_nDayOfWeek != pST->wDayOfWeek )
  41. {
  42. // determine special day entry
  43. BOOL bSpecialDayEntry = FALSE;
  44. // find any special day schedule entry
  45. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  46. {
  47. PHZ_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  48. // if rule applies only to special day
  49. if( pSchedule->bSpecialDay )
  50. {
  51. if( pST->wYear == pSchedule->stDate.wYear &&
  52. pST->wMonth == pSchedule->stDate.wMonth &&
  53. pST->wDay == pSchedule->stDate.wDay )
  54. {
  55. // mark that need to be performed
  56. pSchedule->bPerform = TRUE;
  57. // mark that found special day entry
  58. bSpecialDayEntry = TRUE;
  59. // log informaion
  60. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CHZMaskSchedule::OnTimer", "found special day rule #%d", i );
  61. }
  62. else
  63. {
  64. // mark that need does not to be performed
  65. pSchedule->bPerform = FALSE;
  66. }
  67. }
  68. }
  69. // find any special day schedule entry
  70. for( i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  71. {
  72. PHZ_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  73. // if rule applies only to week day
  74. if( !pSchedule->bSpecialDay )
  75. {
  76. // if this day is required day of week and no special day rules
  77. if( pSchedule->bWeek[ pST->wDayOfWeek ] && !bSpecialDayEntry )
  78. {
  79. // mark that need to be performed
  80. pSchedule->bPerform = TRUE;
  81. // log informaion
  82. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CHZMaskSchedule::OnTimer", "found week day rule #%d", i );
  83. }
  84. else
  85. {
  86. // mark that need does not to be performed
  87. pSchedule->bPerform = FALSE;
  88. }
  89. }
  90. }
  91. // store current day
  92. m_nDayOfWeek = pST->wDayOfWeek;
  93. }
  94. // check schedule plan
  95. BOOL pHzOn[5] = { pStateHZ[0], pStateHZ[1], pStateHZ[2], pStateHZ[3], pStateHZ[4] };
  96. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  97. {
  98. PHZ_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  99. // if rule should be performed
  100. if( pSchedule->bPerform )
  101. {
  102. // convert time from H:M:S to S
  103. INT nTimeInSec = pST->wHour * 3600 + pST->wMinute * 60 + pST->wSecond;
  104. INT nScheduleTimeInSec = pSchedule->stTime.wHour * 3600 + pSchedule->stTime.wMinute * 60 + pSchedule->stTime.wSecond;
  105. // check date & time
  106. if( nTimeInSec > nScheduleTimeInSec )
  107. {
  108. // mark as performed
  109. pSchedule->bPerform = FALSE;
  110. // log information
  111. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CHZMaskSchedule::OnTimer", "rule #%d", i );
  112. // update masks array
  113. for( INT j = 0; j < 5; j++ )
  114. if( pSchedule->pHzEnable[j] )
  115. pHzOn[j] = pSchedule->pHzOn[j];
  116. // perform reset if nessessary
  117. if( pSchedule->bResetLGS )
  118. // perform reset
  119. ResetLGS();
  120. }
  121. }
  122. }
  123. // pefrom hz switching
  124. for( i = 0; i < 5; i++ )
  125. {
  126. // if changed
  127. if( pStateHZ[i] != pHzOn[i] )
  128. {
  129. // perform switching
  130. SwitchHZ( i, pHzOn[i] );
  131. }
  132. }
  133. }
  134. void CHZMaskSchedule::UpdateSchedule( INT nIndex )
  135. {
  136. // get current time & date
  137. SYSTEMTIME st;
  138. ::GetLocalTime( &st );
  139. // if this rule is rule for special day
  140. if( m_HzScheduleArray[nIndex].bSpecialDay )
  141. {
  142. if( st.wYear == m_HzScheduleArray[nIndex].stDate.wYear &&
  143. st.wMonth == m_HzScheduleArray[nIndex].stDate.wMonth &&
  144. st.wDay == m_HzScheduleArray[nIndex].stDate.wDay )
  145. {
  146. // now is a special day for this rule
  147. m_HzScheduleArray[nIndex].bPerform = TRUE;
  148. return;
  149. }
  150. // requared day isn't today
  151. m_HzScheduleArray[nIndex].bPerform = FALSE;
  152. return;
  153. }
  154. // find any other special day schedule entry
  155. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  156. {
  157. PHZ_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  158. // if rule applies only to special day
  159. if( pSchedule->bSpecialDay )
  160. {
  161. if( st.wYear == pSchedule->stDate.wYear &&
  162. st.wMonth == pSchedule->stDate.wMonth &&
  163. st.wDay == pSchedule->stDate.wDay )
  164. {
  165. // mark that it is not need to be performed
  166. m_HzScheduleArray[nIndex].bPerform = FALSE;
  167. return;
  168. }
  169. }
  170. }
  171. // if this day is required day of week and no special day rules
  172. if( m_HzScheduleArray[nIndex].bWeek[ st.wDayOfWeek ] )
  173. // mark that need to be performed
  174. m_HzScheduleArray[nIndex].bPerform = TRUE;
  175. else
  176. // mark that it is not need to be performed
  177. m_HzScheduleArray[nIndex].bPerform = FALSE;
  178. }
  179. BOOL CHZMaskSchedule::Load( LPCTSTR pFileName )
  180. {
  181. // open file
  182. FILE *f = fopen( pFileName, "rb" );
  183. if( !f )
  184. return FALSE;
  185. // read cumder of items
  186. INT nCount = 0;
  187. fread( &nCount, 1, sizeof( nCount ), f );
  188. // validate number of items
  189. if( nCount < 0 && nCount > 64000 )
  190. {
  191. fclose( f );
  192. return FALSE;
  193. }
  194. // read all schedule plan
  195. for( INT i = 0; i < nCount; i++ )
  196. {
  197. HZ_SCHEDULE HzSchedule;
  198. // load from file
  199. fread( &HzSchedule, 1, sizeof( HZ_SCHEDULE ), f );
  200. Add( &HzSchedule );
  201. }
  202. // mark that nessesary to initialize
  203. m_nDayOfWeek = -1;
  204. // close file
  205. fclose( f );
  206. return TRUE;
  207. }
  208. BOOL CHZMaskSchedule::Save( LPCTSTR pFileName )
  209. {
  210. // open file
  211. FILE *f = fopen( pFileName, "wb" );
  212. if( !f )
  213. return FALSE;
  214. // write cumder of items
  215. INT nCount = m_HzScheduleArray.GetCount();
  216. fwrite( &nCount, 1, sizeof( nCount ), f );
  217. // write all schedule plan
  218. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  219. {
  220. PHZ_SCHEDULE pSchedule = &m_HzScheduleArray[i];
  221. fwrite( pSchedule, 1, sizeof( HZ_SCHEDULE ), f );
  222. }
  223. // close file
  224. fclose( f );
  225. return TRUE;
  226. }
  227. BOOL CHZMaskSchedule::Add( PHZ_SCHEDULE pHzSchedule )
  228. {
  229. // perform strcut convertion from HZ_SCHEDULE to HZ_SCHEDULE_EX
  230. HZ_SCHEDULE_EX HzScheduleEx;
  231. PHZ_SCHEDULE pHzScheduleEx = &HzScheduleEx;
  232. *pHzScheduleEx = *pHzSchedule;
  233. HzScheduleEx.bPerform = TRUE;
  234. // plase int orules array
  235. return m_HzScheduleArray.Add( HzScheduleEx );
  236. }
  237. BOOL CHZMaskSchedule::Delete( INT nIndex )
  238. {
  239. // remove from rules array
  240. return m_HzScheduleArray.Remove( nIndex );
  241. }
  242. void CHZMaskSchedule::DeleteAll()
  243. {
  244. // remove from rules array
  245. m_HzScheduleArray.RemoveAll();
  246. }
  247. void CHZMaskSchedule::SwitchHZ( INT nHz, BOOL bOn )
  248. {
  249. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CHZMaskSchedule::SwitchHZ", "Hidden zone %d %s", nHz, bOn ? "on" : "off" );
  250. // switch hidden zone
  251. m_pApplication->SetViewZoneMode( nHz, bOn );
  252. }
  253. void CHZMaskSchedule::ResetLGS()
  254. {
  255. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CHZMaskSchedule::ResetLGS", "Reset LGS" );
  256. // switch hidden zone
  257. m_pApplication->ResetLgs( FALSE );
  258. }