//
//  Copyright © 2001 - 2002, 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:   TriggerController.h
//
//  Project:    6046
//
//  Author(s):  W. Arcus
//
//  Purpose:    
//
//  Notes:      
//

#include "StdAfx.h"
#include "TriggerController.h"

#include "..\Utils\NetUtils\7kProtocol.h"

namespace                                                   // Begin unnamed namespace to ensure internal linkage.
{
    enum E7KRECORDTYPES
    {
        recordType7kTrigger                 = 7300,
        recordType7kTriggerDeviceConfig,
        recordType7kTriggerSequenceSetup
    };
}                                                           // End unnamed namspace.

//////////////////////////////////////////////////////////////////////
// CTriggerController class implementation.

CTriggerController::CTriggerController( void )
{
    m_pCriticalParameterTable = NULL;
}

CTriggerController::~CTriggerController( void )
{
    m_pCriticalParameterTable = NULL;
}

void CTriggerController::SetCriticalParameterTable( const CCriticalParameterTable *pCriticalParameterTable )
{
    ASSERT( pCriticalParameterTable != NULL );

    m_pCriticalParameterTable = const_cast<CCriticalParameterTable *>( pCriticalParameterTable );
}

bool CTriggerController::Process7kMessage(  const BYTE          *pby7kRecord,
                                            const unsigned long &rulNumBytes,
                                            const unsigned long &rulTimeStamp )
{
    bool bSuccess = true;               // Assume success for now.

#if 1

#pragma CompileMessage_m( "TODO: COMPLETE TRIGGER CONTROLLER ONCE 7K COMPLETE; FUNCTIONAL DESCRIPTION FOLLOWS..." )

// This component of the PLC intercepts non-PLC 7k commands (during pre-processing of 7k command routing)
// and then set its internal state based on 7k records 7201 to 7203.
// 
// It looks up the sensor index for the trigger identifier then sets the corresponding 
// criticalParameterPulseRepPeriodX (where X is the sensor index). The critical parameter table object will
// then handle subsequent callback to subscribing clients for them to set their internal state as necessary.
// For example, the EdgeTech FSDW will need to change range/period settings in order to not exceed the 
// maximum sampling duration (governed by the pulse repetition period). If it does, the EdgeTech subsystem(s)
// will never output ping data since the trigger will occur before the expected number of samples can be 
// acquired.

    DBG_UNREFERENCED_PARAMETER( rulTimeStamp );
    DBG_UNREFERENCED_PARAMETER( rulNumBytes );
    DBG_UNREFERENCED_PARAMETER( pby7kRecord );

#else

    try
    {
        const E7KRECORDTYPES            eRecordType = static_cast<E7KRECORDTYPES>( C7kProtocol::RecordType( pby7kRecord, rulNumBytes ) );
        std::vector<CRITICALPARAMENTRY> CriticalParameters;
        //CRITICALPARAMENTRY              sEntry;

        switch ( eRecordType )
        {
            case recordType7kTrigger:
                break;

            case recordType7kTriggerDeviceConfig:
                break;

            case recordType7kTriggerSequenceSetup:

                //sEntry.m_ulParameterId       = criticalParameterPulseRepPeriod0 + iSensorIndex;
                //sEntry.m_ulTimeStampOfUpdate = rulTimeStamp;
                //sEntry.m_dValue              = dPulseRepPeriod;
                //CriticalParameters.push_back( sEntry );

                break;

            default:
                break;
        }

        if ( ! CriticalParameters.empty() )
        {
            ASSERT( m_pCriticalParameterTable != NULL );

            if ( m_pCriticalParameterTable != NULL )
            {
                if ( ! m_pCriticalParameterTable->UpdateParameters( static_cast<unsigned long>( CriticalParameters.size() ),
                                                                        &CriticalParameters[ 0 ],
                                                                            rulTimeStamp ) )
                {
                    bSuccess = false;
                    TRACE( _T( "CTriggerController::Process7kMessage(), m_pCriticalParameterTable->UpdateParameters() failed.\n" ) );
                }
            }
        }
    }
    catch ( ... )
    {
        bSuccess = false;
    }

#endif

    return bSuccess;
}

