//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: Patrol.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of Patrol.cpp //---------------------------------------------------------------------------- // Patrol.cpp: implementation of the CPatrol class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" //#include "chicago.h" #include "Patrol.h" #include "SensorSPRU.h" #include "SensorArray.h" #include "..\..\LgLib\Version.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CPatrol::CPatrol() { m_pPatrols = NULL; m_nPatrolsCount = 0; m_hMutex = ::CreateMutex( NULL, FALSE, NULL ); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CPatrol::CPatrol") ); } CPatrol::~CPatrol() { if( m_nPatrolsCount ) { for( int i = 0; i < m_nPatrolsCount; i++ ) delete [] m_pPatrols[i].pPoints; delete [] m_pPatrols; } ::CloseHandle( m_hMutex ); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CPatrol::~CPatrol") ); } void CPatrol::SetSensorArray( CSensorArray *pSensorArray ) { m_pSensorArray = pSensorArray; m_nPatrolsCount = 1; m_pPatrols = new PATROL[ m_nPatrolsCount ]; { strcpy(m_pPatrols[0].pName, "PATROL TEST" ); m_pPatrols[0].nCount = 6; m_pPatrols[0].pPoints = new PATROL::POINT[ m_pPatrols[0].nCount ]; m_pPatrols[0].bActive = FALSE; for( int j = 0; j < m_pPatrols[0].nCount; j++ ) { m_pPatrols[0].pPoints[j].nAddress = 1; m_pPatrols[0].pPoints[j].nBitMask = 1 << j; m_pPatrols[0].pPoints[j].nTime = 10000; m_pPatrols[0].pPoints[j].nTolerance = 50; m_pPatrols[0].pPoints[j].pSensor = NULL; } } } BOOL CPatrol::Load( CFile& file ) { file.Read( &m_nPatrolsCount, sizeof(int) ); m_pPatrols = new PATROL[ m_nPatrolsCount ]; for( int i = 0; i < m_nPatrolsCount; i++ ) { FILE_PATROL_HEADER fph; file.Read( &fph, sizeof(fph) ); memcpy( m_pPatrols[i].pName, fph.pName, STRING_MAX ); m_pPatrols[i].nCount = fph.nCount; m_pPatrols[i].pPoints = new PATROL::POINT[ fph.nCount ]; m_pPatrols[i].bActive = FALSE; for( int j = 0; j < m_pPatrols[i].nCount; j++ ) { FILE_PATROL_POINT fpp; file.Read( &fpp, sizeof(fpp) ); m_pPatrols[i].pPoints[j].nAddress = fpp.nAddress; m_pPatrols[i].pPoints[j].nBitMask = fpp.nBitMask; m_pPatrols[i].pPoints[j].nTime = fpp.nTime; m_pPatrols[i].pPoints[j].nTolerance = fpp.nTolerance; m_pPatrols[i].pPoints[j].pSensor = NULL; } } return TRUE; } void CPatrol::LinkSensor( CSensor *pSensor ) { for( int p = 0; p < m_nPatrolsCount; p++ ) { for( int l = 0; l < m_pPatrols[p].nCount; l++ ) { if( m_pPatrols[p].pPoints[l].nAddress == pSensor->GetAddress() ) { if( m_pPatrols[p].pPoints[l].nBitMask & ((CSensorSPRU*)pSensor)->GetBitMask() ) { m_pPatrols[p].pPoints[l].pSensor = pSensor; pSensor->SetPatrol( TRUE ); } } } } } void CPatrol::Activate( int nIndex, BOOL bActive ) { DWORD dwCT = ::GetTickCount(); ::WaitForSingleObject( m_hMutex, INFINITE ); if( m_pPatrols[nIndex].bActive != bActive ) { if( bActive ) { m_pPatrols[nIndex].minTime = m_pPatrols[nIndex].pPoints[0].nTime * ( 100 - m_pPatrols[nIndex].pPoints[0].nTolerance ) / 100 + dwCT; m_pPatrols[nIndex].maxTime = m_pPatrols[nIndex].pPoints[0].nTime * ( 100 + m_pPatrols[nIndex].pPoints[0].nTolerance ) / 100 + dwCT; m_pPatrols[nIndex].nActive = 0; m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PATROL, TRUE, 0, 0, RGB(0,0xFF,0), "patrol started" ); ((CSensorSPRU*)m_pPatrols[nIndex].pPoints[ 0 ].pSensor)->SetInactiveTime( m_pPatrols[nIndex].pPoints[ 0 ].nBitMask, m_pPatrols[nIndex].minTime, m_pPatrols[nIndex].maxTime ); } m_pPatrols[nIndex].bActive = bActive; } ::ReleaseMutex( m_hMutex ); } BOOL CPatrol::IsActive( int nIndex ) { ::WaitForSingleObject( m_hMutex, INFINITE ); BOOL bActive = m_pPatrols[nIndex].bActive; ::ReleaseMutex( m_hMutex ); return bActive; } void CPatrol::CheckTime() { DWORD dwCT = ::GetTickCount(); ::WaitForSingleObject( m_hMutex, INFINITE ); for( int i = 0; i < m_nPatrolsCount; i++ ) { if( m_pPatrols[i].bActive ) { if( dwCT >= m_pPatrols[i].maxTime ) { m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PATROL, FALSE, m_pPatrols[i].nActive, 0, RGB(0xFF,0,0), "patrol time expaired" ); m_pPatrols[i].bActive = FALSE; } } } ::ReleaseMutex( m_hMutex ); } BYTE CPatrol::CheckSensor( CSensor *pSensor, LPBYTE pByte ) { DWORD dwCT = ::GetTickCount(); BYTE bResult = 0; ::WaitForSingleObject( m_hMutex, INFINITE ); for( int i = 0; i < m_nPatrolsCount; i++ ) { if( m_pPatrols[i].bActive && dwCT > m_pPatrols[i].minTime ) { if( m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].pSensor->GetArrayIndex() == pSensor->GetArrayIndex() ) { // if( m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nBitMask & ((CSensorSPRU*)pSensor)->GetBitMask() ) if( m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nBitMask & (*pByte) ) { // (*pByte) ^= m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nBitMask; // bResult |= m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nBitMask; m_pPatrols[i].nActive++; if( m_pPatrols[i].nActive == m_pPatrols[i].nCount ) { m_pPatrols[i].bActive = FALSE; m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PATROL, TRUE, 0, 0, RGB(0,0xFF,0), "patrol successefull" ); } else { m_pPatrols[i].minTime = m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nTime * ( 100 - m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nTolerance ) / 100 + dwCT; m_pPatrols[i].maxTime = m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nTime * ( 100 + m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nTolerance ) / 100 + dwCT; m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PATROL, TRUE, 0, 0, RGB(0,0xFF,0), "patrol step passed" ); ((CSensorSPRU*)m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].pSensor)->SetInactiveTime( m_pPatrols[i].pPoints[ m_pPatrols[i].nActive ].nBitMask, m_pPatrols[i].minTime, m_pPatrols[i].maxTime ); } } } } } /* PATROL *pPatrol = pSensor->GetPatrol(); if( pPatrol ) { DWORD dwCT = ::GetTickCount(); if( pPatrol->bActive && ( pPatrol->minTime < dwCT ) ) { if( pPatrol->pPoints[ pPatrol->nActive ].pSensor->GetArrayIndex() == pSensor->GetArrayIndex() ) { if( pPatrol->pPoints[ pPatrol->nActive ].nBitMask & pBuffer[1] ) { if( pPatrol->nActive+1 == pPatrol->nCount ) { pPatrol->bActive = FALSE; m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PATROL, TRUE, 0, 0, RGB(0,0xFF,0), "patrol successefuly finished" ); } else { pPatrol->minTime = pPatrol->pPoints[ pPatrol->nActive+1 ].nTime * ( 100 - pPatrol->pPoints[ pPatrol->nActive+1 ].nTolerance ) / 100 + dwCT; pPatrol->maxTime = pPatrol->pPoints[ pPatrol->nActive+1 ].nTime * ( 100 + pPatrol->pPoints[ pPatrol->nActive+1 ].nTolerance ) / 100 + dwCT; m_pSensorArray->GetEventQueue()->PostEvent( LGS_EVENT_PATROL, TRUE, 0, 0, RGB(0,0xFF,0), "patrol step passed" ); } pBuffer[1] ^= pPatrol->pPoints[ pPatrol->nActive ].nBitMask; pPatrol->nActive++; } } } } */ ::ReleaseMutex( m_hMutex ); return bResult; }