//
//  Copyright © 2004, 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:   PLCConfiguration.h
//
//  Project:    6046.
//
//  Author(s):  W. Arcus
//
//  Purpose:    Defines the class CPLCConfiguration to read and parse the new
//              xml based sensor and module configuration.
//
//  Notes:
//

#if !defined(AFX_PLCCONFIGURATION_H__8D22B0C6_64A2_468D_8A63_BEC4C48BD048__INCLUDED_)
#define AFX_PLCCONFIGURATION_H__8D22B0C6_64A2_468D_8A63_BEC4C48BD048__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#pragma warning( disable : 4018 )
#pragma warning( disable : 4146 )
#pragma warning( push, 3 )
#include <string>                                                           // STL string support.
#include <vector>                                                           // Linked list support.
#pragma warning( pop )
#pragma warning( default : 4146 )
#pragma warning( default : 4018 )

#include "Sensor.h"

class CPLCConfiguration  
{
public:

    ///////////////
    // Definitions.

    typedef struct tagSENSORINFO
    {
        unsigned long       m_ulModuleNumber;
        unsigned long       m_ulSubsystemId;
        int                 m_iDelay;
        unsigned long       m_ulDeviceId;
        unsigned short      m_unSystemEnumerator;
        std::string         m_SensorDescription;
        int                 m_iFailRestartPeriod;
    }
    SENSORINFO, *PSENSORINFO;

    ///////////////
    // Services.

                            CPLCConfiguration   (   void );
    virtual                ~CPLCConfiguration   (   void );

    bool                    ParseFile           (   const CString              &rsFullFileName );
    void                    SetSearchPosition   (   const int                  &riPosition );

    bool                    WriteFile           (   const CString              &rsFileName,
                                                    SENSORINFO * const          psSensorInfo,
                                                    const unsigned long        &rulSensors );

    // Module data extraction.

    unsigned long           NumberOfModules     (   void )  const;
    unsigned long           ModuleNumber        (   const unsigned long        &rulModuleIndex ) const;
    LPCTSTR                 ModuleName          (   const unsigned long        &rulModuleIndex ) const;
    LPCTSTR                 ModuleDescription   (   const unsigned long        &rulModuleIndex ) const;

    // Sensor data extraction.

    unsigned long           NumberOfSensors     (   void )  const;
    unsigned long           SensorModuleId      (   const unsigned long        &rulSensorIndex ) const;
    unsigned long           SubsystemId         (   const unsigned long        &rulSensorIndex ) const;
    int                     Delay               (   const unsigned long        &rulSensorIndex ) const;
    unsigned long           DeviceId            (   const unsigned long        &rulSensorIndex ) const;
    unsigned short          Enumerator          (   const unsigned long        &rulSensorIndex ) const;
    int                     FailRestart         (   const unsigned long        &rulSensorIndex ) const;

    LPCTSTR                 SensorDescription   (   const unsigned long        &rulSensorIndex ) const;

private:

    ///////////////
    // Definitions.

    typedef struct tagMODULEINFO
    {
        unsigned long       m_ulModuleNumber;
        std::string         m_ModuleName;
        std::string         m_ModuleDescription;
    }
    MODULEINFO, *PMODULEINFO;

    ///////////////
    // Attributes.

    int                     m_iSearchStartPosition;
    CString                 m_XML;

    std::vector<MODULEINFO> m_ModuleInfo;
    std::vector<SENSORINFO> m_SensorInfo;

    ///////////////
    // Services.

    void                    Clear               (   SENSORINFO                 &rsSensorInfo );
    void                    Clear               (   MODULEINFO                 &rsModuleInfo );

    bool                    LocateTag           (   const CString              &sType,
                                                    const bool                 &rbResetSearchPosition );

    bool                    TagText             (   const CString              &rsTag,
                                                    CString                    &rsAttribute );

    bool                    Attribute           (   const CString              &rsTag,
                                                    const CString              &rsField,
                                                    CString                    &rsAttribute );

    bool                    Attribute           (   const CString              &rsTag,
                                                    const CString              &rsField,
                                                    unsigned long              &rulAttribute );

    bool                    Attribute           (   const CString              &rsTag,
                                                    const CString              &rsField,
                                                    int                        &riAttribute );

    CString                 AttributeText       (   LPCTSTR                     lpszTag,
                                                    LPCTSTR                     lpszAttribute );

    CString                 TagText             (   LPCTSTR                     lpszTagName );

    CString                 ExtractAttributeData(   const int                  &riStart );

    CString                 ExtractTagText      (   const int                  &riStart );

    bool                    FindTag             (   LPCTSTR                     lpszTag,
                                                    int                        &riStart,
                                                    int                        &riStop );

    bool                    FindAttribute       (   LPCTSTR                     lpszAttribute,
                                                    int                        &riStart,
                                                    int                        &riStop  );

                            CPLCConfiguration   (   const CPLCConfiguration    &rRhs );         // Not implemented thus private.
    CPLCConfiguration &     operator =          (   const CPLCConfiguration    &rRhs );         // Not implemented thus private.
};

#endif // !defined(AFX_PLCCONFIGURATION_H__8D22B0C6_64A2_468D_8A63_BEC4C48BD048__INCLUDED_)
