/* ****************************
 * *                          *
 * *      IRIS  System        *
 * * Product Printout Utility *
 * *                          *
 * ****************************
 * File: iris/examiners/product_stat.C
 * 
 *   COPYRIGHT (c) 1991, 1992, 1994, 1996, 1997, 1998, 1999, 2000,
 *                    2001, 2002, 2003, 2004  BY
 *       SIGMET INCORPORATED, WESTFORD MASSACHUSETTS, U.S.A.
 * 
 * THIS SOFTWARE IS FURNISHED UNDER A LICENSE AND MAY BE USED AND  COPIED
 * ONLY  IN  ACCORDANCE WITH  THE  TERMS  OF  SUCH  LICENSE  AND WITH THE
 * INCLUSION OF THE ABOVE COPYRIGHT NOTICE.  THIS SOFTWARE  OR  ANY OTHER
 * COPIES  THEREOF MAY NOT BE PROVIDED OR OTHERWISE MADE AVAILABLE TO ANY
 * OTHER PERSON.  NO TITLE TO AND OWNERSHIP OF  THE  SOFTWARE  IS  HEREBY
 * TRANSFERED. 
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sigtypes.h"
#include "headers.h"
#include "product.h"
#include "setup.h"
#include "antenna_lib.h"
#include "user_lib.h"
#include "misc_lib.h"


/* Function prototypes used just within this file
 */
static void print_process_status
  (const struct status_one_process *stat_a,
   const char *sname_a );

static void print_device_status
  (const struct status_one_device *pDevice_a,
   const char *sname_a );

static void print_antenna_critical
(UINT4 iStatusMask,
 UINT4 iStatusAll,
 UINT4 StatusCritical );

/* ================================================================
 * Print out information about a warning product file:
 */
void product_status
(const struct stat_product *pStatus_a)
{
  SINT4 iOutput, iMessage;
  const struct status_results *results ;

  results = &pStatus_a->Result;

  /* Print out IRIS style: */
  switch( results->misc.iris_style)
    {
    case STYLE_RADAR:
      printf("Site style is: RADAR\n" );
      break;

    case STYLE_ANALYSIS:
      printf("Site style is: ANALYSIS\n" );
      break;

    default:
      printf("Site style is: UNKNOWN (%d)\n", results->misc.iris_style );
      break;
    }
  {
    char sFaults[80];
    sFaults[0] = 0;
  
    if ( STAT_FLG_CRITICAL & results->misc.iflags ) strcat( sFaults, " CRITICAL" );
    if ( STAT_FLG_CRIT_MSG & results->misc.iflags ) strcat( sFaults, " CRIT-MSG" );
    if ( STAT_FLG_FAULT    & results->misc.iflags ) strcat( sFaults, " FAULT" );
    
    if (0 == strlen(sFaults) ) strcat( sFaults, " OK" );
    printf("Overall status is:%s\n", sFaults );
  }

  print_process_status( &results->process.ingest,        "IRIS_INGEST  " );
  print_process_status( &results->process.ingfio,        "IRIS_INGFIO  " );
  print_process_status( &results->process.output_master, "IRIS_OUTPUT  " );
  print_process_status( &results->process.product,       "IRIS_PRODUCT " );
  print_process_status( &results->process.watchdog,      "IRIS_WATCHDOG" );
  print_process_status( &results->process.reingest,      "IRIS_REINGEST" );
  print_process_status( &results->process.network,       "IRIS_NETWORK " );
  print_process_status( &results->process.nordrad,       "IRIS_NORDRAD " );
  print_process_status( &results->process.server,        "IRIS_SERVER  " );
  print_process_status( &results->process.ribbon,        "IRIS_RIBBON  " );
  print_process_status( &results->process.input_master,  "IRIS_INPUT   " );
  print_device_status( &results->device.dsp,        "DSP" );
  print_device_status( &results->device.antenna,    "RCP" );
  for (iOutput=0; iOutput<MIN(18,results->misc.idevice_count); iOutput++)
    print_device_status( &results->device.output[iOutput], NULL );

  printf( "\nRST mode: '%s'\n", results->misc.srst_name );
  if ( results->misc.iris_style == STYLE_RADAR )
    printf( "TSC mode: '%s'\n", results->misc.stsc_name );
  printf( "PSC mode: '%s'\n", results->misc.spsc_name );
  printf( "POM mode: '%s'\n", results->misc.spom_name );
  if ( results->misc.iris_style == STYLE_RADAR )
    printf( "Active task: '%s'\n", results->misc.sactive_tname );
  printf( "Active product: '%s'\n", results->misc.sactive_pname );
  if ( results->misc.iris_style == STYLE_RADAR )
    {
      printf("\nAntenna Position, azimuth: %5.2f, elevation: %5.2f\n",
	     fPDegFromBin4(results->antenna.iazpos), 
	     fElDegFromBin4(results->antenna.ielpos) );
      if (results->antenna.ibite_flag)
	printf( "Bite fault summary shows %d\n", results->antenna.ibite_field );
      else
	printf( "Bite fault summary shows OK\n" );
      
      print_antenna_critical( results->antenna.istatus_mask, 
			      results->antenna.istatus,
			      results->antenna.iStatusCritical );
      /*      print_bite_faults     ( results->antenna.iBiteFaults ); */
    }

  if (results->message.index)
    {
      char sTZName[TZNAME_SIZE];
      char sTimeBuffer[21];
      TZNameBuild( sTZName, pStatus_a->Header.end.sLocalTZName, 
		   pStatus_a->Header.end.iMinutesWest,
		   pStatus_a->Header.pcf.ing_time.imills );
      printf( "\nTop message #%d,  Repeats: %d\n", results->message.index,
	      results->message.irepeat_count );
      printf( "%s\n", results->message.stext );
      printf( "Process: %s, Name: %s\n", results->message.sprocess, 
	      results->message.sname );
      printf( "Time: %s %s\n", 
	      shhmmssddmonyyyy_serv_r( &results->message.time, sTimeBuffer ), sTZName );
    }
  else
    printf( "No messages\n" );

  printf( "Message list contains %d messages:\n", pStatus_a->Header.end.iresults_count-1 );
  for (iMessage=0; iMessage<pStatus_a->Header.end.iresults_count-1; iMessage++)
    {
      printf( "%s", pStatus_a->Messages[iMessage].sText );
    }

  return;
}

/* ==========================================
 * Print out the state of one process from the status product.
 */
static void print_process_status
  (const struct status_one_process *stat_a,
   const char *sname_a )
{
  printf( "Status of %s ", sname_a );
  switch ( stat_a->icommand)
    {
    case PROC_CMD_ON:
      printf(" ON/");
      break;

    case PROC_CMD_OFF:
      printf("OFF/");
      break;

    default:
      printf("???"); printf("/");
      break;
    }

  printf( "%s\n", sProcessMode( stat_a->imode) );

  return;
}
/* ==========================================
 * Print out the state of one device from the status product.
 */
static void print_device_status
  (const struct status_one_device *pDevice_a,
   const char *sname_a )    /* Optional device name */
{
  if (sname_a)
    printf( "Status of %-9s ", sname_a );
  else
    printf( "Status of %-9s ", sdevice_name( pDevice_a->itype, pDevice_a->iunit ) );
  printf( "%-9s", sDeviceStatus( pDevice_a->istatus ));
  printf( "%-10s", sProcessMode( pDevice_a->imode ));
  printf( "%s\n", pDevice_a->sname );

  return;
}
/* ==========================================
 * Print out critical antenna fault information.
 */
static void print_antenna_critical
(UINT4 iStatusMask_a,
 UINT4 iStatusAll_a,
 UINT4 iStatusCritical_a )
{
  if (iStatusMask_a&ASF_LOWAIRFLOW)
    {
      if (iStatusAll_a & ASF_LOWAIRFLOW )
	{
	  if (iStatusCritical_a & ASF_LOWAIRFLOW )
	    printf( "Low Airflow: CRITICAL\n" );
	  else
	    printf( "Low Airflow: FAULT\n" );
	}
      else
	printf( "Low Airflow: OK\n" );
    }

  if (iStatusMask_a&ASF_INTERLOCK)
    {
      if (iStatusAll_a & ASF_INTERLOCK )
	{
	  if (iStatusCritical_a & ASF_INTERLOCK )
	    printf( "  Interlock: CRITICAL\n" );
	  else
	    printf( "  Interlock: FAULT\n" );
	}
      else
	printf( "  Interlock: OK\n" );
    }

  if (iStatusMask_a&ASF_MAGTFLT)
    {
      if (iStatusAll_a & ASF_MAGTFLT )
	{
	  if (iStatusCritical_a & ASF_MAGTFLT )
	    printf( "  Magnatron: CRITICAL\n" );
	  else
	    printf( "  Magnatron: FAULT\n" );
	}
      else
	printf( "  Magnatron: OK\n" );
    }

  if (iStatusMask_a&ASF_LOW_WGP)
    {
      if (iStatusAll_a & ASF_LOW_WGP )
	{
	  if (iStatusCritical_a & ASF_LOW_WGP )
	    printf( "  Waveguide: CRITICAL\n" );
	  else
	    printf( "  Waveguide: FAULT\n" );
	}
      else
	printf( "  Waveguide: OK\n" );
    }

}
