/****************************************************************************

Module: Test3.cpp

Purpose:

   This module reads the configuration information from a logger using 
   Phase 1 communication.

****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
// Include files.
/////////////////////////////////////////////////////////////////////////////

#include "afx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "spectrum.h"

/////////////////////////////////////////////////////////////////////////////
// Local definitions.
/////////////////////////////////////////////////////////////////////////////

// Define the API version and the API date.
#define API_VERSION "V3.5-Beta3"
#define API_DATE    "April 10, 2001"

// Define a macro for checking the status code returned from the 
// Spectrum API routines.
#define CHECK_STATUS_CODE(StatusCode)                     \
{                                                         \
   if ((StatusCode) != SPC_SC_Success)                    \
   {                                                      \
      char szStatusText[512];                             \
      SPUTL_FormatStatusCode((StatusCode), szStatusText); \
      printf("ERROR: %s\n", szStatusText);                \
      return(StatusCode);                                 \
   }                                                      \
}

/////////////////////////////////////////////////////////////////////////////
// This routine reads the configuration information from a logger using
// Phase 1 communication.
/////////////////////////////////////////////////////////////////////////////

SPT_lStatusCode main(int argc, char* argv[])
{
   SPT_lStatusCode     lStatusCode;
   SPT_lComPortNumber  lComPortNumber;
   SPT_ulLoggerAddress ulLoggerAddress;
   char                szBuffer[512];

   // Ensure the user passed the correct number of parameters.
   if (argc != 2)
   {
      printf("\n");
      printf("PURPOSE:\n");
      printf("\n");
      printf("   This program reads the configuration information from a logger using\n");
      printf("   Phase 1 communication.\n");
      printf("\n");
      printf("USAGE:\n");
      printf("\n");
      printf("   Test3 ComPortNumber\n");
      printf("\n");
      printf("EXAMPLE:\n");
      printf("\n");
      printf("   Test3 1\n");
      return(SPC_SC_Success);
   }

   // Check the API version.
   char szApiVersion[256];

   lStatusCode = SPAPI_GetApiVersion(szApiVersion);
   CHECK_STATUS_CODE(lStatusCode);

   if (strcmp(szApiVersion, API_VERSION) != 0)
   {
      printf("ERROR: Invalid API version detected");
      return(SPC_SC_Success);
   }

   // Check the API date.
   char szApiDate[256];

   lStatusCode = SPAPI_GetApiDate(szApiDate);
   CHECK_STATUS_CODE(lStatusCode);

   if (strcmp(szApiDate, API_DATE) != 0)
   {
      printf("ERROR: Invalid API date detected");
      return(SPC_SC_Success);
   }

   // Get the COM port number.
   lComPortNumber = atoi(argv[1]);

   // Set the logger address.
   ulLoggerAddress = 0;

   // Enable Phase 1 communication.
   lStatusCode = SPAPI_SetProtocol(FALSE);
   CHECK_STATUS_CODE(lStatusCode);

   // Create a logger object.
   SPT_ulLoggerHandle ulLoggerHandle;    
   lStatusCode = SPLGR_CreateLoggerObject(&ulLoggerHandle);
   CHECK_STATUS_CODE(lStatusCode);

   // Read the logger.
   lStatusCode = SPLGR_ReadLogger(ulLoggerHandle, 
                                  lComPortNumber,
                                  ulLoggerAddress,
                                  "");
   CHECK_STATUS_CODE(lStatusCode);

   // Get the description.
   lStatusCode = SPLGR_GetDescription(ulLoggerHandle, szBuffer);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Description:                %s\n", szBuffer);

   // Get the hardware model.
   lStatusCode = SPLGR_GetHardwareModel(ulLoggerHandle, szBuffer);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Hardware Model:             %s\n", szBuffer);

   // Get the hardware revision.
   lStatusCode = SPLGR_GetHardwareRevision(ulLoggerHandle, szBuffer);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Hardware Revision:          %s\n", szBuffer);

   // Get the firmware version.
   lStatusCode = SPLGR_GetFirmwareVersion(ulLoggerHandle, szBuffer);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Firmware Version:           %s\n", szBuffer);

   // Get the serial number.
   lStatusCode = SPLGR_GetSerialNumber(ulLoggerHandle, szBuffer);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Serial Number:              %s\n", szBuffer);

   // Get the sample count.
   SPT_lSampleCount lSampleCount;
   lStatusCode = SPLGR_GetSampleCount(ulLoggerHandle, &lSampleCount);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Sample Count:               %d\n", lSampleCount);

   // Get the sample interval.
   SPT_lTime lSampleInterval;
   lStatusCode = SPLGR_GetSampleInterval(ulLoggerHandle, &lSampleInterval);
   CHECK_STATUS_CODE(lStatusCode);
   printf("Sample Interval:            %d\n", lSampleInterval);

   // Get the sample start time.
   SPT_lTime lSampleStartTime;
   lStatusCode = SPLGR_GetSampleStartTime(ulLoggerHandle, &lSampleStartTime);
   CHECK_STATUS_CODE(lStatusCode);
   SPUTL_FormatDateAndTime(lSampleStartTime, szBuffer);
   printf("Sample Start Time:          %s\n", szBuffer);

   // Loop for each channel.
   for (SPT_lChannelIndex lChannelIndex = 0; lChannelIndex < 8; lChannelIndex++)
   {
      // Get the channel enabled flag.
      BOOL bChannelEnabled;
      lStatusCode = SPLGR_GetChannelEnabled(ulLoggerHandle, 
                                            lChannelIndex, 
                                            &bChannelEnabled);
      CHECK_STATUS_CODE(lStatusCode);
      
      // See if the channel is enabled.
      if (bChannelEnabled)
      {
         // Get the channel description.
         lStatusCode = SPLGR_GetChannelDescription(ulLoggerHandle, 
                                                   lChannelIndex, 
                                                   szBuffer);
         CHECK_STATUS_CODE(lStatusCode);
         printf("Channel %d Description:      %s\n", 
                lChannelIndex + 1, 
                szBuffer);

         // Get the channel value.
         double dChannelValue;
         lStatusCode = SPLGR_GetChannelValue(ulLoggerHandle,
                                             lChannelIndex,
                                             &dChannelValue);
         CHECK_STATUS_CODE(lStatusCode);

         // Get the channel units.
         char szUnits[512];
         lStatusCode = SPLGR_GetChannelUnits(ulLoggerHandle, 
                                             lChannelIndex, 
                                             szUnits);
         CHECK_STATUS_CODE(lStatusCode);

         // Display the channel value and the channel units.
         printf("Channel %d Value:            %.2lf %s\n", 
                lChannelIndex + 1,
                dChannelValue, 
                szUnits);
      }
   }

   // Delete the logger object.
   lStatusCode = SPLGR_DeleteLoggerObject(ulLoggerHandle);
   CHECK_STATUS_CODE(lStatusCode);

   return(SPC_SC_Success);
}
