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.
549 lines
15 KiB
549 lines
15 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 09/11/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: LgReportStorage.cpp
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Source file of LgReportStorage.cpp
|
|
//----------------------------------------------------------------------------
|
|
// LgReportEngine.cpp: implementation of the CLgReportStorage class.
|
|
//
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
|
#include "lglibreport.h"
|
|
#include "LgReportStorage.h"
|
|
#include "..\LgImageDIB.h"
|
|
#include "..\LgImageJPEG.h"
|
|
|
|
#ifdef _DEBUG
|
|
#undef THIS_FILE
|
|
static char THIS_FILE[]=__FILE__;
|
|
#define new DEBUG_NEW
|
|
#endif
|
|
|
|
CLgReportStorage::CLgReportStorage()
|
|
{
|
|
m_pInterface = NULL;
|
|
|
|
m_SetupSettings.Load( "report.dat" );
|
|
}
|
|
|
|
CLgReportStorage::~CLgReportStorage()
|
|
{
|
|
m_SetupSettings.Store( "report.dat" );
|
|
|
|
Destroy();
|
|
}
|
|
|
|
BOOL CLgReportStorage::Create( LPCTSTR pPathRoot, INT nMaxStorageDays, CLgReportInterface *pInterface )
|
|
{
|
|
if( !pInterface )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("Invalid parameters (%p)"), pInterface );
|
|
return FALSE;
|
|
}
|
|
|
|
m_pInterface = pInterface;
|
|
|
|
// create new storage provider
|
|
StorageClientToProviderLinkSP::Link( &m_ReportProvider );
|
|
|
|
// link provider to Dialog
|
|
if( !m_Dialog.Link( &m_ReportProvider ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("Dialog link provider failed") );
|
|
return FALSE;
|
|
}
|
|
|
|
// create client
|
|
CLgReportSettings::SETTINGS& settings = m_SetupSettings.GetSettings();
|
|
|
|
m_ReportClient.Create( this, settings.bAccessEnable, settings.wAccessPort );
|
|
|
|
// link report provider to client
|
|
if( !m_ReportClient.Link( &m_ReportProvider ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("Can't link provider to client") );
|
|
return FALSE;
|
|
}
|
|
|
|
// create provider
|
|
if( !m_ReportProvider.Create( settings.pAccessName, pPathRoot, nMaxStorageDays ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("CLgReportRemoteProviderEx::Create for <%s> failed"), settings.pAccessName );
|
|
return FALSE;
|
|
}
|
|
|
|
// add all providers to list
|
|
for( INT i = 0; i < m_SetupSettings.GetCount(); i++ )
|
|
{
|
|
CLgReportSiteInfo &info = m_SetupSettings.GetAt(i);
|
|
|
|
LPSTR pName = info.pName;
|
|
|
|
// add new provider to list
|
|
if( !m_arrReportProvider.AddEmpty() )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("Can't create new provider <%s>"), pName );
|
|
return FALSE;
|
|
}
|
|
|
|
// make easy refference to provider
|
|
CLgReportRemoteProviderEx &provider = m_arrReportProvider[i];
|
|
|
|
// link new provider to Dialog
|
|
if( !m_Dialog.Link( &provider ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("Dialog link for <%s> failed"), pName );
|
|
return FALSE;
|
|
}
|
|
|
|
// link new provider to client
|
|
if( !m_ReportClient.Link( &provider ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("Client link for <%s> failed"), pName );
|
|
return FALSE;
|
|
}
|
|
|
|
// create new provider
|
|
TCHAR pFullPath[MAX_PATH];
|
|
_tcsncpy( pFullPath, pPathRoot, MAX_PATH );
|
|
::PathAppend( pFullPath, pName );
|
|
|
|
if( !provider.Create( this, pName, pFullPath, i, info.pAddress, info.wPort ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::Create"), _T("CLgReportRemoteProviderEx::Create for <%s> failed"), pName );
|
|
return FALSE;
|
|
}
|
|
}
|
|
|
|
return TRUE;
|
|
/*
|
|
StorageClientToProviderLinkSP::Link( &m_pProvider[0] );
|
|
|
|
m_Dialog.Link( &m_pProvider[0] );
|
|
m_Dialog.Link( &m_pProviderRemote[0] );
|
|
|
|
m_pProviderRemote[0].CLgReportRemoteClient::Link( &m_pProvider[1] );
|
|
|
|
m_pProvider[1].LoaderClientToProviderLinkP::Link( m_pProviderRemote[1] );
|
|
m_pProvider[1].LoaderClientToProviderLinkP::Link( m_pProviderRemote[2] );
|
|
|
|
m_pProviderRemote[1].CLgReportRemoteClient::Link( &m_pProvider[0] );
|
|
m_pProviderRemote[2].CLgReportRemoteClient::Link( &m_pProvider[0] );
|
|
|
|
|
|
TCHAR pProviderPathRoot[5][MAX_PATH];
|
|
LPCTSTR pProviderName[5] =
|
|
{
|
|
_T("First"),
|
|
_T("Second"),
|
|
_T("Remote0"),
|
|
_T("Remote1"),
|
|
_T("Remote2"),
|
|
};
|
|
|
|
for( INT i = 0; i < 5; i++ )
|
|
{
|
|
_tcsncpy( pProviderPathRoot[i], pPathRoot, MAX_PATH );
|
|
::PathAppend( pProviderPathRoot[i], pProviderName[i] );
|
|
}
|
|
|
|
|
|
return m_pProvider[0].Create( pProviderName[0], pProviderPathRoot[0], nMaxStorageDays ) &&
|
|
m_pProvider[1].Create( pProviderName[1], pProviderPathRoot[1], nMaxStorageDays ) &&
|
|
m_pProviderRemote[0].Create( pProviderName[2], pProviderPathRoot[2] ) &&
|
|
m_pProviderRemote[1].Create( pProviderName[3], pProviderPathRoot[3] ) &&
|
|
m_pProviderRemote[2].Create( pProviderName[4], pProviderPathRoot[4] );
|
|
*/
|
|
}
|
|
|
|
void CLgReportStorage::Destroy()
|
|
{
|
|
m_ReportProvider.Destroy();
|
|
m_ReportClient.Destroy();
|
|
m_arrReportProvider.RemoveAll();
|
|
|
|
m_pInterface = NULL;
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
//
|
|
//
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
BOOL CLgReportStorage::OnClientConnect( INT nProvider, BOOL bConnected )
|
|
{
|
|
if( nProvider < 0 || nProvider >= m_arrReportProvider.GetCount() )
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::OnReportClientConnect"), _T("Provider index is wrong") );
|
|
|
|
return m_arrReportProvider[nProvider].DispatchConnect( bConnected );
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnClientPacket( INT nProvider, BYTE bPacketType, PVOID pBuffer, INT nBufferSize )
|
|
{
|
|
if( bPacketType != 0x01 )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::OnReportClientPacket"), _T("Unacceptable packet type <%d>"), bPacketType );
|
|
return FALSE;
|
|
}
|
|
|
|
if( nProvider < 0 || nProvider >= m_arrReportProvider.GetCount() )
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::OnReportClientPacket"), _T("Provider index is wrong") );
|
|
|
|
return m_arrReportProvider[nProvider].DispatchPacket( pBuffer, nBufferSize );
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnServerConnect( PVOID pConnection, BOOL bConnected )
|
|
{
|
|
return m_ReportClient.DispatchConnect( pConnection, bConnected );
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnServerPacket( PVOID pConnection, BYTE bPacketType, PVOID pBuffer, INT nBufferSize )
|
|
{
|
|
if( bPacketType != 0x01 )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::OnReportServerPacket"), _T("Unacceptable packet type <%d>"), bPacketType );
|
|
return FALSE;
|
|
}
|
|
|
|
return m_ReportClient.DispatchPacket( pConnection, pBuffer, nBufferSize );
|
|
}
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
//
|
|
//
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
BOOL CLgReportStorage::Reset()
|
|
{
|
|
return WriteReset();
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnSensor( LPCTSTR pName, float x, float y )
|
|
{
|
|
REPORT_FILTER_SOURCE rfs;
|
|
_tcscpy( rfs.pName, pName );
|
|
rfs.X = x;
|
|
rfs.Y = y;
|
|
return WriteFilterSource( &rfs );
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnFilter( BYTE bId, LPCTSTR pName )
|
|
{
|
|
REPORT_FILTER_EVENT rfe;
|
|
_tcscpy( rfe.pName, pName );
|
|
rfe.bId = bId;
|
|
return WriteFilterEvent( &rfe );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreBackground( CBitmap *pBitmap )
|
|
{
|
|
CLgImageDIB dib;
|
|
if( !dib.Create( (HBITMAP)pBitmap->GetSafeHandle() ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::StoreBackground"), _T("Can't create dib") );
|
|
return FALSE;
|
|
}
|
|
|
|
return StoreBackground( dib );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreBackground( CLgImageDIB& dib )
|
|
{
|
|
CLgImageJPEG jpeg;
|
|
if( !jpeg.Create( dib, dib ) )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::StoreBackground"), _T("Can't create jpeg") );
|
|
return FALSE;
|
|
}
|
|
|
|
return StoreBackground( jpeg );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreBackground( CLgImageJPEG& jpeg )
|
|
{
|
|
return WriteBackground( &jpeg );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreEvent( SYSTEMTIME *pST, BYTE bId, BOOL bSensor, WORD wSensorIndex, BOOL bPosition, float x, float y, LARGE_INTEGER *pOffset, LPCTSTR pFormat, va_list pMarker )
|
|
{
|
|
REPORT_EVENT re;
|
|
|
|
re.bId = bId;
|
|
re.bSensorValid = bSensor;
|
|
re.wSensorIndex = wSensorIndex;
|
|
re.bImageCount = 0;
|
|
re.stTime = *pST;
|
|
re.bPositionValid = bPosition;
|
|
re.fPositionX = x;
|
|
re.fPositionY = y;
|
|
|
|
INT nCount = _vsntprintf( re.pText, REPORT_EVENT_TEXT_MAX, pFormat, pMarker );
|
|
if( nCount < 0 )
|
|
{
|
|
re.pText[REPORT_EVENT_TEXT_MAX-4] = _T('.');
|
|
re.pText[REPORT_EVENT_TEXT_MAX-3] = _T('.');
|
|
re.pText[REPORT_EVENT_TEXT_MAX-2] = _T('.');
|
|
re.pText[REPORT_EVENT_TEXT_MAX-1] = 0;
|
|
nCount = REPORT_EVENT_TEXT_MAX;
|
|
}
|
|
|
|
return WriteEvent( &re, &pOffset->QuadPart );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreImage( HBITMAP hBitmap, LARGE_INTEGER *pOffset )
|
|
{
|
|
CLgImageDIB dib;
|
|
if( !dib.Create( hBitmap ) )
|
|
return FALSE;
|
|
|
|
return StoreImage( dib, pOffset );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreImage( CLgImageDIB& dib, LARGE_INTEGER *pOffset )
|
|
{
|
|
CLgImageJPEG jpeg;
|
|
if( !jpeg.Create( dib, dib ) )
|
|
return FALSE;
|
|
|
|
return StoreImage( jpeg, pOffset );
|
|
}
|
|
|
|
BOOL CLgReportStorage::StoreImage( CLgImageJPEG& jpeg, LARGE_INTEGER *pOffset )
|
|
{
|
|
return WriteImage( &jpeg, &pOffset->QuadPart );
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// CLEANUP ENGINE
|
|
//
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
/*
|
|
void CLgReportStorage::DeleteOldFiles(SYSTEMTIME st)
|
|
{
|
|
WIN32_FIND_DATA win32FD;
|
|
LARGE_INTEGER ft;
|
|
_TCHAR pBuffer[MAX_PATH];
|
|
LPTSTR pNames = NULL;
|
|
INT nNamesCount = 0;
|
|
|
|
::SystemTimeToFileTime( &st, (LPFILETIME)&ft );
|
|
|
|
ft.QuadPart -= DAY2NANO( m_nMaxStorageDays );
|
|
|
|
_tcsncpy( pBuffer, (LPCTSTR)m_strPathRoot, MAX_PATH );
|
|
::PathAppend( pBuffer, _T("*.*") );
|
|
|
|
HANDLE hFF = ::FindFirstFile(pBuffer, &win32FD);
|
|
|
|
if(hFF != INVALID_HANDLE_VALUE)
|
|
do{
|
|
if( ( win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != FILE_ATTRIBUTE_DIRECTORY )
|
|
continue;
|
|
|
|
SYSTEMTIME cst = { 0 };
|
|
if( _stscanf( win32FD.cFileName, PATH_DATE_FORMAT, &cst.wYear, &cst.wMonth, &cst.wDay ) != 3 )
|
|
continue;
|
|
|
|
// if it is current report then don't delete it
|
|
if( m_stReportTime.wYear == cst.wYear &&
|
|
m_stReportTime.wMonth == cst.wMonth &&
|
|
m_stReportTime.wDay == cst.wDay )
|
|
continue;
|
|
|
|
LARGE_INTEGER cft;
|
|
::SystemTimeToFileTime(&cst, (LPFILETIME)&cft);
|
|
if( cft.QuadPart >= ft.QuadPart )
|
|
continue;
|
|
|
|
_tcsncpy( pBuffer, (LPCTSTR)m_strPathRoot, MAX_PATH );
|
|
::PathAppend( pBuffer, win32FD.cFileName );
|
|
|
|
INT nLength = _tcslen(pBuffer) + 1;
|
|
pNames = (LPTSTR)realloc( pNames, sizeof( TCHAR ) * ( nNamesCount + nLength ) );
|
|
if( pNames )
|
|
{
|
|
memcpy( pNames + nNamesCount, pBuffer, nLength );
|
|
nNamesCount += nLength;
|
|
}
|
|
}while(::FindNextFile(hFF, &win32FD));
|
|
|
|
if(nNamesCount)
|
|
{
|
|
pNames = (LPTSTR)realloc( pNames, sizeof( TCHAR ) * ( nNamesCount + 1 ) );
|
|
if( pNames )
|
|
{
|
|
// add one more zero
|
|
pNames[nNamesCount] = 0;
|
|
|
|
SHFILEOPSTRUCT sfs = { 0 };
|
|
|
|
sfs.wFunc = FO_DELETE;
|
|
sfs.pFrom = pNames;
|
|
sfs.fFlags = FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS;
|
|
sfs.lpszProgressTitle = _T("Deleting old history...");
|
|
|
|
::SHFileOperation(&sfs);
|
|
|
|
delete pNames;
|
|
}
|
|
}
|
|
|
|
::FindClose(hFF);
|
|
}
|
|
|
|
BOOL CLgReportStorage::CheckOldFiles(SYSTEMTIME st)
|
|
{
|
|
WIN32_FIND_DATA win32FD;
|
|
LARGE_INTEGER ft;
|
|
_TCHAR pBuffer[MAX_PATH];
|
|
BOOL fExist = FALSE;
|
|
|
|
::SystemTimeToFileTime(&st, (LPFILETIME)&ft);
|
|
ft.QuadPart -= DAY2NANO( m_nMaxStorageDays );
|
|
|
|
_tcsncpy( pBuffer, (LPCTSTR)m_strPathRoot, MAX_PATH );
|
|
::PathAppend( pBuffer, _T("*.*") );
|
|
|
|
HANDLE hFF = ::FindFirstFile(pBuffer, &win32FD);
|
|
|
|
if(hFF != INVALID_HANDLE_VALUE)
|
|
do{
|
|
if(win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
|
{
|
|
SYSTEMTIME cst = { 0 };
|
|
|
|
if(_stscanf(win32FD.cFileName, PATH_DATE_FORMAT, &cst.wYear, &cst.wMonth, &cst.wDay ) == 3)
|
|
{
|
|
LARGE_INTEGER cft;
|
|
|
|
::SystemTimeToFileTime(&cst, (LPFILETIME)&cft);
|
|
|
|
if(cft.QuadPart < ft.QuadPart)
|
|
fExist = TRUE;
|
|
}
|
|
}
|
|
}while(::FindNextFile(hFF, &win32FD));
|
|
|
|
::FindClose(hFF);
|
|
|
|
return fExist;
|
|
}
|
|
|
|
DWORD CLgReportStorage::ThreadProc()
|
|
{
|
|
__try
|
|
{
|
|
TIME_ZONE_INFORMATION tzi;
|
|
LARGE_INTEGER lt;
|
|
SYSTEMTIME st;
|
|
BOOL fNeedCheck = TRUE;
|
|
|
|
::GetTimeZoneInformation(&tzi);
|
|
|
|
HANDLE hTimer = ::CreateWaitableTimer(NULL, FALSE, NULL);
|
|
|
|
::GetLocalTime(&st);
|
|
|
|
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
|
|
|
|
::SystemTimeToFileTime( &st, (LPFILETIME)< );
|
|
|
|
lt.QuadPart += DAY2NANO( 1 ) + MINUTE2NANO( tzi.Bias );
|
|
|
|
::SetWaitableTimer(hTimer, <, 0UL, NULL, NULL, FALSE);
|
|
|
|
HANDLE phEvents[] = { CLgThread::GetThreadStopEvent(), hTimer };
|
|
|
|
if( CheckOldFiles( st ) )
|
|
{
|
|
if( m_pInterface->OnAskForDeleteOld() )
|
|
DeleteOldFiles(st);
|
|
}
|
|
|
|
while(TRUE)
|
|
{
|
|
switch(::WaitForMultipleObjects(2, phEvents, FALSE, INFINITE))
|
|
{
|
|
case WAIT_OBJECT_0 + 0:
|
|
goto THREAD_EXIT;
|
|
|
|
case WAIT_OBJECT_0 + 1:
|
|
|
|
::CancelWaitableTimer(hTimer);
|
|
|
|
::GetLocalTime(&st);
|
|
|
|
DeleteOldFiles(st);
|
|
|
|
st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
|
|
|
|
::SystemTimeToFileTime(&st, (LPFILETIME)<);
|
|
|
|
lt.QuadPart += DAY2NANO( 1 ) + MINUTE2NANO( tzi.Bias );
|
|
|
|
::SetWaitableTimer(hTimer, <, 0UL, NULL, NULL, FALSE);
|
|
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
} // end of while(TRUE)
|
|
|
|
THREAD_EXIT:
|
|
::CancelWaitableTimer(hTimer);
|
|
::CloseHandle(hTimer);
|
|
}
|
|
__except( EXCEPTION_EXECUTE_HANDLER )
|
|
{
|
|
LGLOG( LOG_LEVEL_LOW, LOG_TYPE_ERROR, this, _T("CLgReportStorage::ThreadProc"), _T("Exception 0x%08X"), _exception_code() );
|
|
}
|
|
|
|
return 0UL;
|
|
}
|
|
*/
|
|
|
|
BOOL CLgReportStorage::SetMaxStoreTime(INT nDays)
|
|
{
|
|
return m_ReportProvider.SetMaxStoreTime( nDays );
|
|
// return m_pProvider[0].SetMaxStoreTime( nDays );
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnCreateSite()
|
|
{
|
|
return m_pInterface->OnCreateSite();
|
|
}
|
|
|
|
BOOL CLgReportStorage::OnAskForDeleteOld()
|
|
{
|
|
return m_pInterface->OnAskForDeleteOld();
|
|
}
|
|
|
|
BOOL CLgReportStorage::Dlg_Create( HWND hParentWnd )
|
|
{
|
|
return m_Dialog.Create( hParentWnd );
|
|
}
|
|
|
|
BOOL CLgReportStorage::Dlg_DoModal( HWND hParentWnd )
|
|
{
|
|
return m_Dialog.DoModal( hParentWnd );
|
|
}
|
|
|
|
BOOL CLgReportStorage::DlgSetup_DoModal( HWND hParentWnd )
|
|
{
|
|
return m_DialogSetup.DoModal( hParentWnd, m_SetupSettings );
|
|
}
|
|
|
|
void CLgReportStorage::Dlg_Destroy()
|
|
{
|
|
m_Dialog.DestroyWindow();
|
|
}
|