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.

293 lines
7.2 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: 12/08/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. }
  117. }
  118. }
  119. // pefrom hz switching
  120. for( i = 0; i < 5; i++ )
  121. {
  122. // if changed
  123. if( pStateHZ[i] != pHzOn[i] )
  124. {
  125. // perform switching
  126. DoSwitchHZ( i, pHzOn[i] );
  127. }
  128. }
  129. }
  130. void CHZMaskSchedule::UpdateSchedule( INT nIndex )
  131. {
  132. // get current time & date
  133. SYSTEMTIME st;
  134. ::GetLocalTime( &st );
  135. // if this rule is rule for special day
  136. if( m_HzScheduleArray[nIndex].bSpecialDay )
  137. {
  138. if( st.wYear == m_HzScheduleArray[nIndex].stDate.wYear &&
  139. st.wMonth == m_HzScheduleArray[nIndex].stDate.wMonth &&
  140. st.wDay == m_HzScheduleArray[nIndex].stDate.wDay )
  141. {
  142. // now is a special day for this rule
  143. m_HzScheduleArray[nIndex].bPerform = TRUE;
  144. return;
  145. }
  146. // requared day isn't today
  147. m_HzScheduleArray[nIndex].bPerform = FALSE;
  148. return;
  149. }
  150. // find any other special day schedule entry
  151. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  152. {
  153. PHZ_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i];
  154. // if rule applies only to special day
  155. if( pSchedule->bSpecialDay )
  156. {
  157. if( st.wYear == pSchedule->stDate.wYear &&
  158. st.wMonth == pSchedule->stDate.wMonth &&
  159. st.wDay == pSchedule->stDate.wDay )
  160. {
  161. // mark that it is not need to be performed
  162. m_HzScheduleArray[nIndex].bPerform = FALSE;
  163. return;
  164. }
  165. }
  166. }
  167. // if this day is required day of week and no special day rules
  168. if( m_HzScheduleArray[nIndex].bWeek[ st.wDayOfWeek ] )
  169. // mark that need to be performed
  170. m_HzScheduleArray[nIndex].bPerform = TRUE;
  171. else
  172. // mark that it is not need to be performed
  173. m_HzScheduleArray[nIndex].bPerform = FALSE;
  174. }
  175. BOOL CHZMaskSchedule::Load( LPCTSTR pFileName )
  176. {
  177. // open file
  178. FILE *f = fopen( pFileName, "rb" );
  179. if( !f )
  180. return FALSE;
  181. // read cumder of items
  182. INT nCount = 0;
  183. fread( &nCount, 1, sizeof( nCount ), f );
  184. // validate number of items
  185. if( nCount < 0 && nCount > 64000 )
  186. {
  187. fclose( f );
  188. return FALSE;
  189. }
  190. // read all schedule plan
  191. for( INT i = 0; i < nCount; i++ )
  192. {
  193. HZ_SCHEDULE HzSchedule;
  194. // load from file
  195. fread( &HzSchedule, 1, sizeof( HZ_SCHEDULE ), f );
  196. Add( &HzSchedule );
  197. }
  198. // mark that nessesary to initialize
  199. m_nDayOfWeek = -1;
  200. // close file
  201. fclose( f );
  202. return TRUE;
  203. }
  204. BOOL CHZMaskSchedule::Save( LPCTSTR pFileName )
  205. {
  206. // open file
  207. FILE *f = fopen( pFileName, "wb" );
  208. if( !f )
  209. return FALSE;
  210. // write cumder of items
  211. INT nCount = m_HzScheduleArray.GetCount();
  212. fwrite( &nCount, 1, sizeof( nCount ), f );
  213. // write all schedule plan
  214. for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ )
  215. {
  216. PHZ_SCHEDULE pSchedule = &m_HzScheduleArray[i];
  217. fwrite( pSchedule, 1, sizeof( HZ_SCHEDULE ), f );
  218. }
  219. // close file
  220. fclose( f );
  221. return TRUE;
  222. }
  223. BOOL CHZMaskSchedule::Add( PHZ_SCHEDULE pHzSchedule )
  224. {
  225. // perform strcut convertion from HZ_SCHEDULE to HZ_SCHEDULE_EX
  226. HZ_SCHEDULE_EX HzScheduleEx;
  227. PHZ_SCHEDULE pHzScheduleEx = &HzScheduleEx;
  228. *pHzScheduleEx = *pHzSchedule;
  229. HzScheduleEx.bPerform = TRUE;
  230. // plase int orules array
  231. return m_HzScheduleArray.Add( HzScheduleEx );
  232. }
  233. BOOL CHZMaskSchedule::Delete( INT nIndex )
  234. {
  235. // remove from rules array
  236. return m_HzScheduleArray.Remove( nIndex );
  237. }
  238. void CHZMaskSchedule::DeleteAll()
  239. {
  240. // remove from rules array
  241. m_HzScheduleArray.RemoveAll();
  242. }
  243. void CHZMaskSchedule::DoSwitchHZ( INT nHz, BOOL bOn )
  244. {
  245. LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CHZMaskSchedule::SwitchHZ", "Hidden zone %d %s", nHz, bOn ? "on" : "off" );
  246. // switch hidden zone
  247. m_pApplication->SetViewZoneMode( nHz, bOn );
  248. }