//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: LgMaskScheduleEngine.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgMaskScheduleEngine.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "LgMaskScheduleEngine.h" CLgMaskScheduleEngine::CLgMaskScheduleEngine() { m_nDayOfWeek = -1; m_pMaskScheduleCallback = NULL; m_pMaskScheduleCallbackParam = 0; m_liNextTimeInSec.QuadPart = 0; m_dwNextTimeInSecUpdate = 0; } CLgMaskScheduleEngine::~CLgMaskScheduleEngine() { Destroy(); } BOOL CLgMaskScheduleEngine::Create( PMASK_SWITCH_CALLBACK pMaskScheduleCallback, LPVOID lParam ) { // mark that it is nessessary to make initializatioin m_nDayOfWeek = -1; m_pMaskScheduleCallback = pMaskScheduleCallback; m_pMaskScheduleCallbackParam = lParam; return TRUE; } void CLgMaskScheduleEngine::Destroy() { __try { DeleteAll(); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Destroy"), _T("Exception 0x%08X"), _exception_code() ); } } void CLgMaskScheduleEngine::OnTimer( SYSTEMTIME *pST ) { try { // perform initialization at the begining of the new day if( m_nDayOfWeek != pST->wDayOfWeek ) { // determine special day entry BOOL bSpecialDayEntry = FALSE; // find any special day schedule entry for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ ) { PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i]; // if rule applies only to special day if( !pSchedule->bSpecialDay ) continue; if( pST->wYear == pSchedule->stDate.wYear && pST->wMonth == pSchedule->stDate.wMonth && pST->wDay == pSchedule->stDate.wDay ) { // mark that need to be performed pSchedule->bPerform = TRUE; // mark that found special day entry bSpecialDayEntry = TRUE; // log informaion LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "found special day rule #%d", i ); } else { // mark that need does not to be performed pSchedule->bPerform = FALSE; } } // find any special day schedule entry for( i = 0; i < m_HzScheduleArray.GetCount(); i++ ) { PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i]; // if rule applies only to week day if( pSchedule->bSpecialDay ) continue; // if this day is required day of week and no special day rules if( pSchedule->bWeek[ pST->wDayOfWeek ] && !bSpecialDayEntry ) { // mark that need to be performed pSchedule->bPerform = TRUE; // log informaion LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "found week day rule #%d", i ); } else { // mark that need does not to be performed pSchedule->bPerform = FALSE; } } // store current day m_nDayOfWeek = pST->wDayOfWeek; // check immidiatly m_liNextTimeInSec.QuadPart = 0i64; } // NOTE: // should be added switch management by time, not by the order DWORD dwTime = GetTickCount(); m_liNextTimeInSec.QuadPart -= (__int64)( dwTime - m_dwNextTimeInSecUpdate ); m_dwNextTimeInSecUpdate = dwTime; if( m_liNextTimeInSec.QuadPart > 0 ) return; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "Next switch task time comes" ); // prepare time in sec INT nTimeInSec = pST->wHour * 3600 + pST->wMinute * 60 + pST->wSecond; // prepare next time check m_liNextTimeInSec.QuadPart = INFINITE; // check schedule plan CLgArray arrSwitches; for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ ) { PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i]; // if rule should be performed if( !pSchedule->bPerform ) continue; // convert time from H:M:S to S INT nScheduleTimeInSec = pSchedule->stTime.wHour * 3600 + pSchedule->stTime.wMinute * 60 + pSchedule->stTime.wSecond; // check date & time if( nTimeInSec < nScheduleTimeInSec ) { // prepare next time LARGE_INTEGER liNextTimeInSec = { nScheduleTimeInSec - nTimeInSec }; if( m_liNextTimeInSec.QuadPart > liNextTimeInSec.QuadPart ) m_liNextTimeInSec.QuadPart = liNextTimeInSec.QuadPart; } else { // mark as performed pSchedule->bPerform = FALSE; // log information LGLOG( LOG_LEVEL_HIGH, LOG_TYPE_INFO, this, "CLgMaskScheduleEngine::OnTimer", "rule #%d", i ); // add switches for each lgses for( INT j = 0; j < pSchedule->arrLgs.GetCount(); j++ ) { CLgMaskScheduleLgs &lgs = pSchedule->arrLgs[j]; // try to find among existing BOOL bFound = FALSE; for( INT k = 0; k < arrSwitches.GetCount(); k++ ) { CLgMaskScheduleLgsSwitch &sw = arrSwitches[k]; if( sw.dwId != lgs.dwId ) continue; for( INT l = 0; l < LGS_MASK_SCHEDULE_COUNT; l++ ) { if( !pSchedule->pHzEnable[l] ) continue; sw.pHzOn[l] = pSchedule->pHzOn[l]; sw.pHzEnable[l] = TRUE; } bFound = TRUE; } // add new switch if( !bFound ) { CLgMaskScheduleLgsSwitch sw; sw.dwId = lgs.dwId; memcpy( sw.pName, lgs.pName, sizeof(sw.pName) ); memcpy( sw.pHzEnable, pSchedule->pHzEnable, sizeof(sw.pHzEnable) ); memcpy( sw.pHzOn, pSchedule->pHzOn, sizeof(sw.pHzOn) ); arrSwitches.Add( sw ); } } } } for( i = 0; i < arrSwitches.GetCount(); i++ ) MaskSwitch( arrSwitches[i] ); // convert to milliseconds m_liNextTimeInSec.QuadPart *= 1000i64; m_dwNextTimeInSecUpdate = ::GetTickCount(); } catch( ... ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::OnTimer"), _T("C++ exception") ); } } void CLgMaskScheduleEngine::UpdateSchedule( INT nIndex ) { try { // get current time & date SYSTEMTIME st; ::GetLocalTime( &st ); // check immidiatly m_liNextTimeInSec.QuadPart = 0i64; // if this rule is rule for special day if( m_HzScheduleArray[nIndex].bSpecialDay ) { if( st.wYear == m_HzScheduleArray[nIndex].stDate.wYear && st.wMonth == m_HzScheduleArray[nIndex].stDate.wMonth && st.wDay == m_HzScheduleArray[nIndex].stDate.wDay ) { // now is a special day for this rule m_HzScheduleArray[nIndex].bPerform = TRUE; return; } // requared day isn't today m_HzScheduleArray[nIndex].bPerform = FALSE; return; } // find any other special day schedule entry for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ ) { PMASK_SCHEDULE_EX pSchedule = &m_HzScheduleArray[i]; // if rule applies only to special day if( pSchedule->bSpecialDay ) { if( st.wYear == pSchedule->stDate.wYear && st.wMonth == pSchedule->stDate.wMonth && st.wDay == pSchedule->stDate.wDay ) { // mark that it is not need to be performed m_HzScheduleArray[nIndex].bPerform = FALSE; return; } } } // if this day is required day of week and no special day rules if( m_HzScheduleArray[nIndex].bWeek[ st.wDayOfWeek ] ) // mark that need to be performed m_HzScheduleArray[nIndex].bPerform = TRUE; else // mark that it is not need to be performed m_HzScheduleArray[nIndex].bPerform = FALSE; } catch( ... ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::UpdateSchedule"), _T("C++ exception") ); } } static TCHAR g_SheduleVersion[MAX_PATH] = _T("LGS MASK SCHEDULE VERSION 0.0"); BOOL CLgMaskScheduleEngine::Load( LPCTSTR pFileName ) { // open file FILE *f = fopen( pFileName, "rb" ); if( !f ) return FALSE; // store version TCHAR pVersion[MAX_PATH]; fread( pVersion, 1, sizeof( pVersion ), f ); if( _tcsncmp( pVersion, g_SheduleVersion, MAX_PATH ) ) { fclose( f ); return TRUE; } // read cumder of items INT nCount = 0; fread( &nCount, 1, sizeof( nCount ), f ); // validate number of items if( nCount < 0 && nCount > 64000 ) { fclose( f ); return FALSE; } try { // read all schedule plan for( INT i = 0; i < nCount; i++ ) { MASK_SCHEDULE schedule; // load from file fread( &schedule.pHzOn, 1, sizeof( schedule.pHzOn ), f ); fread( schedule.pHzEnable, 1, sizeof( schedule.pHzEnable ), f ); fread( &schedule.stTime, 1, sizeof( schedule.stTime ), f ); fread( &schedule.bSpecialDay, 1, sizeof( schedule.bSpecialDay ), f ); fread( schedule.bWeek, 1, max( sizeof(schedule.bWeek), sizeof(schedule.stDate) ), f ); // store lgs array INT nCount = 0; fread( &nCount, 1, sizeof(nCount), f ); for( INT j = 0; j < nCount; j++ ) { CLgMaskScheduleLgs lgs; fread( &lgs, 1, sizeof(lgs), f ); schedule.arrLgs.Add( lgs ); } Add( &schedule ); } } catch( ... ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Load"), _T("C++ exception") ); } // mark that nessesary to initialize m_nDayOfWeek = -1; // close file fclose( f ); return TRUE; } BOOL CLgMaskScheduleEngine::Save( LPCTSTR pFileName ) { // open file FILE *f = fopen( pFileName, "wb" ); if( !f ) return FALSE; try { // store version fwrite( g_SheduleVersion, 1, sizeof( g_SheduleVersion ), f ); // write cumder of items INT nCount = m_HzScheduleArray.GetCount(); fwrite( &nCount, 1, sizeof( nCount ), f ); // write all schedule plan for( INT i = 0; i < m_HzScheduleArray.GetCount(); i++ ) { MASK_SCHEDULE &schedule = m_HzScheduleArray[i]; // store timing data fwrite( schedule.pHzOn, 1, sizeof( schedule.pHzOn ), f ); fwrite( schedule.pHzEnable, 1, sizeof( schedule.pHzEnable ), f ); fwrite( &schedule.stTime, 1, sizeof( schedule.stTime ), f ); fwrite( &schedule.bSpecialDay, 1, sizeof( schedule.bSpecialDay ), f ); fwrite( schedule.bWeek, 1, max( sizeof(schedule.bWeek), sizeof(schedule.stDate) ), f ); // store lgs array INT nCount = schedule.arrLgs.GetCount(); fwrite( &nCount, 1, sizeof(nCount), f ); for( INT j = 0; j < nCount; j++ ) { CLgMaskScheduleLgs &lgs = schedule.arrLgs[j]; fwrite( &lgs, 1, sizeof(lgs), f ); } } } catch( ... ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Save"), _T("C++ exception") ); } // close file fclose( f ); return TRUE; } BOOL CLgMaskScheduleEngine::Add( PMASK_SCHEDULE pHzSchedule ) { BOOL bResult = FALSE; try { // perform strcut convertion from MASK_SCHEDULE to MASK_SCHEDULE_EX MASK_SCHEDULE_EX HzScheduleEx; PMASK_SCHEDULE pHzScheduleEx = &HzScheduleEx; *pHzScheduleEx = *pHzSchedule; HzScheduleEx.bPerform = TRUE; // plase int orules array bResult = m_HzScheduleArray.Add( HzScheduleEx ); } catch( ... ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Add"), _T("C++ exception") ); } return bResult; } BOOL CLgMaskScheduleEngine::Delete( INT nIndex ) { BOOL bResult = FALSE; __try { // remove from rules array bResult = m_HzScheduleArray.Remove( nIndex ); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::Delete"), _T("Exception 0x%08X"), _exception_code() ); } return bResult; } void CLgMaskScheduleEngine::DeleteAll() { __try { // remove from rules array m_HzScheduleArray.RemoveAll(); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::DeleteAll"), _T("Exception 0x%08X"), _exception_code() ); } } void CLgMaskScheduleEngine::MaskSwitch( CLgMaskScheduleLgsSwitch& arrSwitch ) { __try { m_pMaskScheduleCallback( m_pMaskScheduleCallbackParam, arrSwitch ); } __except( EXCEPTION_EXECUTE_HANDLER ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgMaskScheduleEngine::MaskSwitch"), _T("Exception 0x%08X"), _exception_code() ); } }