amvtest.cpp

This sample program demonstrates the features of the Alarm Viewer API:

#include "amvtest.h"

#include <conio.h>

#include <inc_path\am_defs.h>

BOOL CAmvTest::m_bConnected=FALSE;

// Construct the CAmvTest object.

// Allocate and initialize a connection to AMRP.

CAmvTest::CAmvTest()

{

   m_bConnected=FALSE;

//EXAMPLE : CAmvConn::CAmvConn()

   m_pAmvConn=new CAmvConn(this,

                             &DispFunc,

                             &ClearDisp,

                             &LostAM,

                             &MaxAlarms,

                             &SetDisplayRedraw,

                             &UpdateCount,

                             &DoRCMError,

                             &DoConnectionFormed,

                             &NotifyAlmGen,

                             &NotifyAlmMod,

                             &NotifyAlmDel);

}

// Release the connection to AMRP

CAmvTest::~CAmvTest()

{

   //EXAMPLE : CAmvConn::~CAmvConn()

   delete m_pAmvConn;

}

// The next set of methods are static methods used as callback

// functions when initializing the AMRP connection.

// Note that as static methods, there is no this pointer so we

// must get the pointer to the CAmvTest from the context.

// Save each new alarm in the alarm list for the CAmvTest object.

Void CAmvTest::DispFunc(struct testContext *pContext, AlarmInfo *pBuf)

{

   _tprintf("\nDispFunc called\n");

   struct AlarmInfo *pAlarmInfo = new struct AlarmInfo;

   //memcpy(pAlarmInfo,pBuf, sizeof(struct AlarmInfo));

   *pAlarmInfo = *pBuf ;

   CAmvTest* pAmvTest = (CAmvTest*)pContext->who;

   pAmvTest->m_listAlarms.AddTail(pAlarmInfo);

}

// Remove all the alarms in the list.

Void CAmvTest::ClearDisp(struct testContext *pContext)

{

   _tprintf("\nClearDisp called\n");

   // Clear the list

   CAmvTest* pAmvTest = (CAmvTest*)pContext->who;

   pAmvTest->m_listAlarms.DeleteAll();

}

// Called when the connection to the AMRP is lost

void CAmvTest::LostAM(struct testContext *pContext)

{

   _tprintf("\nLostAM called\n");

}

// Indicate the maximum number of alarms that can be handled

// Typically, this is the number that will fit in a window or control

int CAmvTest::MaxAlarms(struct testContext *pContext)

{

   _tprintf("\nMaxAlarms called\n");

   return 3; // 3 alarms per screen for console

}

// Turn display redraw on or off

void CAmvTest::SetDisplayRedraw(struct testContext *pContext,int nVal)

{

   _tprintf("\nSetDisplayRedraw called with nVal=%d\n",nVal);

   CAmvTest* pAmvTest = (CAmvTest*)pContext->who;

   if (!nVal)

   {

      // disable list draw

      pAmvTest->m_listAlarms.SetRedraw(FALSE);

   }

   else

   {

      // Enable list draw

      pAmvTest->m_listAlarms.SetRedraw(TRUE);

   }

   pAmvTest->m_listAlarms.DrawList(pAmvTest->m_pAmvConn);

}

// Called when alarm count or status changes

void CAmvTest::UpdateCount(struct testContext *pContext,

                             RCM_ALARM_DATA* pAlarmData)

{

   _tprintf("\nUpdateCount called\n");

   _tprintf("\n%d alarms current at %s\n",

             pAlarmData->alarm_count,

             pAlarmData->alarm_date);

}

// Called when RCM connection fails

void CAmvTest::DoRCMError(struct testContext *pContext,int nState)

{

   _tprintf("\nDoRCMError called\n");

}

// Called when connection is formed.

// Set flag to record connection active.

Void CAmvTest::DoConnectionFormed(struct testContext *pContext)

{

   _tprintf("\nDoConnectionFormed called\n");

   m_bConnected = TRUE;

}

// Called in dynamic mode when an alarm is generated

void CAmvTest::NotifyAlmGen(struct testContext *pContext,

                              AlarmInfo *pAlarmInfo)

{

   _tprintf("\nNotifyAlmGen called\n");

}

// Called in dynamic mode when an alarm is modified

void CAmvTest::NotifyAlmMod(struct testContext *pContext,

                              AlarmInfo *pAlarmInfo,

                              int nAlmModeAction)

{

   _tprintf("\nNotifyAlmMod called\n");

}

// Called in dynamic mode when an alarm is deleted

void CAmvTest::NotifyAlmDel(struct testContext *pContext,

                              AlarmInfo *pAlarmInfo)

{

   _tprintf("\nNotifyAlmDel called\n");

}

// Variables used to test for user exiting program

static TCHAR szEXIT[] = _T("EXIT");

static int index = 0;

// Ready to exit when 4 characters of "EXIT" string have been matched

BOOL CAmvTest::ShallExit()

{

   return (index == 4);

}

// Watch for user request to exit (by typing "EXIT")

BOOL CAmvTest::Exit(BOOL NoWait=FALSE)

{

   TCHAR szTemp[2];

   szTemp[1] = _T('\0');

   // If no keyboard input waiting, return

   if (NoWait && !_kbhit())

      return FALSE;

   // Get first key from input buffer

   szTemp[0] = _getch();

   // If it matches next key in test string, increment index

   if (_tcsnicmp(szTemp, szEXIT+index, 1) == 0)

   {

      index++;

      if (index == 4)

          return TRUE;

      else

          return FALSE;

   }

   // If key does not match, restart search

   else

   {

      index = 0;

      return FALSE;

   }

}

//  "Main" program for test class

void CAmvTest::RunTest()

{

   TCHAR szProject[80];

   TCHAR szTemp[80];

   COR_STATUS status;

   // Get project name from the user

   _tprintf("Enter project name : ");

   _getts(szProject);

   //Dynamic Mode ?

   m_bDynamic = FALSE;

   _tprintf("Set Dynamic Mode ?(y/n) : ");

   _getts(szTemp);

   if (_tcsnicmp(szTemp,_T("Y"),1)==0)

      m_bDynamic = TRUE;

   //EXAMPLE : CAmvConn::FormConnection()

   m_pAmvConn->FormConnection(szProject, &status);

   if (status.status != COR_SUCCESS)

   {

      _tprintf("ERROR:%s\n", status.err_msg);

      return;

   }

   else

   {

      // wait For Connection

      _tprintf(_T("Waiting for connection..."));

      while ((!Exit(TRUE)) && (!m_bConnected))

      {

         // Empty

      }

      if (ShallExit())

      {

         _tprintf(_T("\nExiting ...\n"));

          return;

      }

      else

      {

         //EXAMPLE : CAmvConn::GetConnectedSystem()

          _tprintf(_T("Project %s connected\n"),

                   m_pAmvConn->GetConnectedSystem());

      }

   }

   // Print Class Filters

   PrintClassFilters();

   // Print Resource Filters

   PrintResourceFilters();

   // Get the list of current alarms from AMRP

   //EXAMPLE : CAmvConn::RequestAlarms()

   m_pAmvConn->RequestAlarms(&status);

   if (status.status!=COR_SUCCESS)

   {

      _tprintf("ERROR:%s\n",status.err_msg);

      return;

   }

   else

   {

      _tprintf("RequestAlarms Successful\n");

   }

   // Print alarms to the screen

   m_listAlarms.DrawList(m_pAmvConn);

   // if Dynamic/Static Mode

   if (m_bDynamic)

   {

      //EXAMPLE : CAmvConn::SetToDynamic()

      m_pAmvConn->SetToDynamic(&status);

      if (status.status!=COR_SUCCESS)

      {

         _tprintf("ERROR:%s\n",status.err_msg);

          return;

      }

      else

      {

         _tprintf("SetToDynamic Successful\n");

      }

   }

   else

   {

      //EXAMPLE : CAmvConn::SetToStatic()

      m_pAmvConn->SetToStatic(&status);

      if (status.status!=COR_SUCCESS)

      {

         _tprintf("ERROR:%s\n",status.err_msg);

          return;

      }

      else

      {

         _tprintf("SetToStatic Successful\n");

      }

      // In static mode Alarms will be retieved by RequestAlarms

      _tprintf("Calling RequestAlarm\n");

      m_pAmvConn->RequestAlarms(&status);

      // Print alarms to the screen

      m_listAlarms.DrawList(m_pAmvConn);

   }

   // Wait for user to type "EXIT". Alarm updates are handled

   // by a separate thread via callback functions.

   while (!Exit()) {

      // Empty

   }

   if (ShallExit())

   {

      _tprintf(_T("\nExiting ...\n"));

      return;

   }

}

// Print the class filters for this view

void CAmvTest::PrintClassFilters()

{

   COR_STATUS status;

   CAmvClassFilter *pClassFilter;

   TCHAR szID[80];

   _tprintf(_T("\n============== CLASS FILTERS ================\n\n"));

   for (pClassFilter=m_pAmvConn->ClassFilters->First(&status);

         pClassFilter!=NULL;

         pClassFilter=m_pAmvConn->ClassFilters->Next(pClassFilter,

                                                        &status))

   {

      if (pClassFilter->IsEnabled())

      {

         _tprintf(_T("ID : %s, Title : %s\n"),

                    pClassFilter->ID(szID),

                    pClassFilter->class_title);

      }

   }

   _tprintf(_T("\n===============================================\n\n"));

}

// Print the resource filters for this view

void CAmvTest::PrintResourceFilters()

{

   COR_STATUS status;

   CAmvResourceFilter *pResFilter;

   TCHAR szID[80];

   _tprintf(_T("\n============ RESOURCE FILTERS ===============\n\n"));

   for (pResFilter=m_pAmvConn->ResourceFilters->First(&status);

        pResFilter!=NULL;

        pResFilter=m_pAmvConn->ResourceFilters->Next(pResFilter,

                                                        &status))

   {

      if (pResFilter->IsEnabled())

      {

         _tprintf(_T("ID : %s\n"),pResFilter->ID(szID));

      }

   }

   _tprintf(_T("\n===============================================\n\n"));

}

///////////////////////////////////////////////////////////////////////

// CAlarmList

CAlarmList::~CAlarmList()

{

    DeleteAll();

}

void CAlarmList::DeleteAll()

{

   struct AlarmInfo *pAlarmInfo;

   while (!IsEmpty())

   {

      pAlarmInfo=(struct AlarmInfo *)RemoveTail();

      delete pAlarmInfo;

   }

}

// Print important fields for each alarm in the list

// Acknowledge each alarm as it is displayed

void CAlarmList::DrawList(CAmvConn *pAmvConn)

{

   COR_STATUS status;

   BOOL bUpdate=FALSE;

   struct AlarmInfo *pAlarmInfo;

   if (!m_bRedraw)

      return;

   POSITION pos;

   pos = GetHeadPosition();

   _tprintf(_T("\n======== ALARM LIST ========================\n\n"));

   while (pos != NULL)

   {

      // Print alarm, resource, and class IDs

      pAlarmInfo = (struct AlarmInfo *)GetNext(pos);

      _tprintf(_T(" Alarm ID : %s\t"),pAlarmInfo->alarmId);

      _tprintf(_T("Resource ID : %s\t"),pAlarmInfo->frId);

      _tprintf(_T("Class : %s\n"),pAlarmInfo->classId);

      // Print date and time alarm was generated

      int year,month,day;

      int hh,mm,ss,subsec;

      cor_stamp_get_components(

         &pAlarmInfo->genTime,

         &year,

         &month,

         &day,

         &hh,

         &mm,

         &ss,

         &subsec) ;

      //year=pAlarmInfo->genTime.yyyymmdd/10000;

      //month=(pAlarmInfo->genTime.yyyymmdd-year*10000)/100;

      //day=pAlarmInfo->genTime.yyyymmdd-year*10000-month*100;

      _tprintf(_T(" Date : %02d/%02d/%04d\t"),month,day,year);

      //hh=pAlarmInfo->genTime.hhmmsstt/1000000;

      //mm=(pAlarmInfo->genTime.hhmmsstt-hh*1000000)/10000;

      //ss = (pAlarmInfo->genTime.hhmmsstt-hh*1000000-mm*10000)/100;

      //tt= pAlarmInfo->genTime.hhmmsstt-hh*1000000-mm*10000-ss*100;

      _tprintf(_T("Time : %02d:%02d:%02d.%02d\n"),hh,mm,ss,subsec);

      // Print current state, acknowledgement state, and message

      _tprintf(_T(" State : %s\n"),pAlarmInfo->stateStr);

      _tprintf(_T("\tAck : %s\n"),pAlarmInfo->ackState);

      _tprintf(_T("\tMessage : %s\n"),pAlarmInfo->message);

      {

//      AM_COMMENT2_INFO comment_info[AM_MAX_ALARM_COMMENTS] ;

//      int numComments = AM_MAX_ALARM_COMMENTS ;

      int i ;

//      pAmvConn->fill_comment_info(pAlarmInfo, &comment_info[0]

//    , &numComments, &status) ;

      _tprintf(_T("NumComments: %d\n"), pAlarmInfo->numComments) ;

      for(i = 0 ; i < pAlarmInfo->numComments ; i++)

      {

         cor_stamp_get_components(

             &pAlarmInfo->pstacked_com[i].gentime,

             &year,

             &month,

             &day,

             &hh,

             &mm,

             &ss,

             &subsec) ;

         _tprintf(_T(" Comment Date : %02d/%02d/%04d\t"),month,day,year);

         _tprintf(_T(" Comment Time : %02d:%02d:%02d.%02d\n"),hh,mm,ss,subsec);

         _tprintf(_T(" \"%s\"\n"), pAlarmInfo->pstacked_com[i].alarm_comment) ;

      }

      }

      if (pos != NULL)

         _tprintf(_T("\n--------------------------------------\n\n"));

      // Send Ack

      if (_tcscmp(pAlarmInfo->ackState,"N") == 0)

      {

         bUpdate = TRUE;

         //EXAMPLE : CAmvConn::SetAction()

         pAmvConn->SetAction(pAlarmInfo,"A \0",&status);

         _tprintf(_T("\nSetAction Called\n"));

      }

   }

   _tprintf(_T("\n==============================================\n\n"));

   if (bUpdate)

   {

      //EXAMPLE : CAmvConn::UpdateList()

      pAmvConn->UpdateList(&status);

      _tprintf(_T("UpdateList Called\n"));

      }

}

More information

Alarm Viewer API Sample Program.