View active alarms in user defined ListView
System requirements:
- CodeGear C++Builder 2009;
- TwinCAT v2.11 B2228 or higher;
- Import the library TcEventLogger.exe (TCEVENTLOGGERLib_OCX.h);
The sample shows the active alarms in a user-defined C++Builder list view (TListView-Control).
The following steps are needed to create the sample:
- Create a new VCL form application
- Move the TTcEventLog and TStatusBar activeX components from the component palette to the form.
- Implement the methods: FormCreate(), FormClose(). With FormCreate() the connection to EventLogger is configurated / created. With FormClose() the connection is separated;
- Implement the event routines: OnNewEvent, OnResetEvent. The example project supports thereby alarms without acknowledgement. To receive alarms with achnowlegde you have to implement further event routines: OnSignalEvent() and OnConfirmEvent();
- Implement the event routine OnShutdown(). This routine is called if the last instamce of the Eventloggers (e.g. at shutdown of the system) is discharged completely;
- Implement the help methods: AddNewEvent() and DisplayActiveAlarms(). These methods are used to read the active messages and to show them in the list;
- Implement the help method: GetEventPosByID(). These method searchs in the list view for a specific message with the help of the SourceId and ID and supplies in case of success the rowe number (itemIndex). You can delete the message from the list with the help of the row number;
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "TCEVENTLOGGERLib_OCX"
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
const char ColH[6][10] = { "SrcId", "Id", "Class", "Source", "Date/Time", "Message" };
//---------------------------------------------------------------------------
// This method is called when the form is loaded
void __fastcall TForm2::FormCreate(TObject *Sender)
{
bool bRemote = true;
if (bRemote) {
TcEventLog1->ConnectKind = ckRemote;
TcEventLog1->RemoteMachineName = "172.17.60.234";
TcEventLog1->AutoConnect = true;
}
langID = 1033;// language id // initialize ListView
ListView1->ViewStyle = vsReport;
ListView1->GridLines = true;
ListView1->RowSelect = true;
// init/add column header
ListView1->Columns->Clear();
for (int i = 0; i < 6; i++)
{
TListColumn *pCol = ListView1->Columns->Add();
if (pCol)
{
pCol->Caption = ColH[i];
pCol->AutoSize = true;
}
}
long v = 0, r = 0, b = 0;
TcEventLog1->GetVersion(&v, &r, &b);
StatusBar1->SimplePanel = true;
StatusBar1->SimpleText = Format( "Machine name: \"%s\", version: %d.%d.%d", ARRAYOFCONST((TcEventLog1->RemoteMachineName, v, r, b)));
DisplayActiveAlarms();
}
//---------------------------------------------------------------------------
// Display all active alarms
void TForm2::DisplayActiveAlarms()
{
// Clear the ListView
ListView1->Items->Clear();
// Get collection of active events
ITcEnumEventsExPtr spEnumEvts = TcEventLog1->EnumActiveEventsEx();
if (spEnumEvts)
{
if (spEnumEvts->Count > 0)
{
for (long i = 0; i < spEnumEvts->Count; i++)
{
// Here we get one event from the collection
ITcEventPtr spEvent;
HRESULT hr = spEnumEvts->Item(i, &spEvent);
if (SUCCEEDED(hr))
{
// Add event to the ListView
AddNewEvent( spEvent );
}
}// for (long i = 0 ...
}// if (spEnumEvts->Count ...
}// if (SUCCEEDED(hr))
}
//---------------------------------------------------------------------------
// This method adds new event to the list
void TForm2::AddNewEvent( ITcEventPtr spEvent )
{
TListItem *pItem = ListView1->Items->Add();
if (pItem) {
// Save Id to Data property for later use
pItem->Data = (void*)spEvent->Id;
// Get event SrcId
pItem->Caption = spEvent->SrcId;
// Get event Id
pItem->SubItems->Add( spEvent->Id );
// Get event Class
pItem->SubItems->Add( spEvent->Class );
// Get source SourceName
BSTR srcName = 0;
HRESULT hr = spEvent->get_SourceName( langID, &srcName );
if (SUCCEEDED(hr)) {
pItem->SubItems->Add( srcName );
SysFreeString( srcName );
}
// Get event creation date/time
DATE dtCreated = 0;
hr = spEvent->get_Date(&dtCreated);
if (SUCCEEDED(hr)) {
pItem->SubItems->Add( FormatDateTime( L"dddd, mmmm d, yyyy ' at ' hh:mm:ss", dtCreated ) );
}
// Get the event message text
BSTR msgString = 0;
hr = spEvent->GetMsgString( langID, &msgString );
if (SUCCEEDED(hr)) {
pItem->SubItems->Add( msgString );
SysFreeString( msgString );
}
}// if (pItem) ...
}
//---------------------------------------------------------------------------
// This method returns ListView event index
// Return value: true => Success, false => event entry not found in the list
bool TForm2::GetEventPosByID( long SrcId, long Id, int &itemIndex )
{
itemIndex = 0;
TListItem *pItem;
int startIndex = 0;
do{
// search for the event in the list (by SrcId + Id)
pItem = ListView1->FindCaption(startIndex, SrcId, true, true, true );
if (pItem) {
startIndex = pItem->Index + 1;
if ( Id == (long)pItem->Data )
{
itemIndex = pItem->Index;
return true;
}
}
}while (pItem);
return false;
}
//---------------------------------------------------------------------------
// This method is called when a new alarm is issued
void __fastcall TForm2::TcEventLog1NewEvent(TObject *Sender, LPDISPATCH srcObj)
{
AddNewEvent( srcObj );
}
//---------------------------------------------------------------------------
// This method is called when a alarm is reset
void __fastcall TForm2::TcEventLog1ResetEvent(TObject *Sender, LPDISPATCH srcObj)
{
ITcEventPtr spEvent = srcObj;
int itemIndex = 0;
if( GetEventPosByID( spEvent->SrcId, spEvent->Id, itemIndex ) )
ListView1->Items->Delete(itemIndex);//removes event from the list
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormClose(TObject *Sender, TCloseAction &Action)
{
TcEventLog1->Disconnect();
}
//---------------------------------------------------------------------------
void __fastcall TForm2::TcEventLog1Shutdown(TObject *Sender, Variant shutdownParm)
{
TcEventLog1->Disconnect();
}
Language / IDE |
Source code |
---|---|
CodeGear C++Builder 2009 |