//
//  Copyright © 2003, RESON Inc. All Rights Reserved.
//
//  No part of this file may be reproduced or transmitted in any form or by
//  any means, electronic or mechanical, including photocopy, recording, or
//  information storage or retrieval system, without permission in writing
//  from RESON Inc.
//
//  Filename:   TimeEmitter.cpp
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:
//

#include "StdAfx.h"
#include "TimeEmitter.h"

#include "RegistryKeys.h"

#include <atlbase.h>

//////////////////////////////////////////////////////////////////////
// CTimeEmitter class implementation.

using namespace N6046RegistryKeys;

CTimeEmitter::CTimeEmitter( void )
             :CGeneralScheduler()
{
    try
    {
        m_bEnabled = false;

        RECORDTYPE7400TIME sRTH = { 0 };
        m_TimeRecord.Encode( 7400UL, &sRTH );

        CRegKey Key;
        DWORD   dwEnable = ( m_bEnabled ? 1UL : 0UL );

        Key.Attach( hKey6046_c );

        if ( Key.Open( hKey6046_c, lpsz6046RegistryKey_c ) == ERROR_SUCCESS )
        {
            if ( Key.QueryValue( dwEnable, lpsz6046TimeMessageEmissionKey_c ) == ERROR_SUCCESS )
            {
                m_bEnabled = ( dwEnable != 0UL );
            }
            else if ( Key.SetValue( dwEnable, lpsz6046TimeMessageEmissionKey_c ) != ERROR_SUCCESS )
            {
                TRACE( _T( "CTimeEmitter::CTimeEmitter(), Key.SetValue() failed\n" ) );
                ASSERT( false );
            }

            Key.Close();
        }
    }
    catch ( ... )
    {
        TRACE( _T( "CTimeEmitter::CTimeEmitter(), Unspecified exception caught\n" ) );
        ASSERT( false );
    }
}

CTimeEmitter::~CTimeEmitter( void )
{
    try
    {
        CRegKey Key;

        Key.Attach( hKey6046_c );

        if ( Key.Open( hKey6046_c, lpsz6046RegistryKey_c ) == ERROR_SUCCESS )
        {
            if ( Key.SetValue( ( m_bEnabled ? 1UL: 0UL ), lpsz6046TimeMessageEmissionKey_c ) != ERROR_SUCCESS )
            {
                TRACE( _T( "CTimeEmitter::~CTimeEmitter(), Key.SetValue() failed\n" ) );
                ASSERT( false );
            }

            Key.Close();
        }

        m_bEnabled = false;
    }
    catch ( ... )
    {
        TRACE( _T( "CTimeEmitter::~CTimeEmitter(), Unspecified exception caught\n" ) );
    }
}

void CTimeEmitter::Enable( const bool &rbEnable )
{
    m_bEnabled = rbEnable;
}

bool CTimeEmitter::IsEnabled( void ) const
{
    return m_bEnabled;
}

CTimeEmitter::operator BYTE * ( void ) const
{
    return static_cast<BYTE *>( m_TimeRecord );
}

CTimeEmitter::operator unsigned long ( void ) const
{
    return static_cast<unsigned long>( m_TimeRecord );
}

bool CTimeEmitter::GenerateMessage( void )
{
    return CSystemTime::TimeStampTo7kTime( CSystemTime::GetTickCount(), static_cast<TIME7K *>( m_TimeRecord ) );
}

