#include "agnt_def.h"
#include "agnt_rfc.h"
#include "agnt_mon.h"

#include "saprfc.h"
#include "sapitab.h"

/*=======================================================================
  (C) Copyright SAP AG, 2002

         
 Author  : Dostert/SAP
 Project : Get data from R/3 using XMB
 Using   : RFCSDK 
 ========================================================================*/

void usage(char *progname) {
	#define BL fprintf(stdout,   

	 BL"Agent\n\n");	   
	 BL"Syntax: %s  <Login_Template>  \n",  progname     );
}


int main(int argc, char **argv) {
	int i,j;

	RFC_HANDLE rfc_handle;
	AlMoniHandle_T *MoniHandle; 
	int   NrOfMoniTempl;
	
	AlMoniNode_T *nodes;
        unsigned int NrOfNodes;	
	
	if (argc < 2) {
		usage(argv[0]);
		return AG_INVALID_ARGUMENTS;
	}


	if ( sapmon_init(&MoniHandle, &NrOfMoniTempl) >=  AG_FIRST_ERROR) {
		printf("Could not initialize INI-File\n");
		return AG_CALL_INVALID;
	}

	if( sapmon_connect(argv[1], &rfc_handle) >=  AG_FIRST_ERROR) {
		printf("Could not log on\n");
		return AG_CALL_INVALID;
	}
	
	for (i = 0; i <  NrOfMoniTempl; i++) {
		sapmon_request(&rfc_handle, &(MoniHandle[i]), 1 );
		sapmon_request_tree(MoniHandle[i], &nodes, &NrOfNodes);	

		//printf("\nDescription %s\n",  sapmon_get_template_description(MoniHandle[i]));

		//printf("%i: Anz NrOfNodes %i", i, NrOfNodes);	
		for (j = 0; j < NrOfNodes; j++) {
			printf("MO: %s, MA: %s, Value %s\n", nodes[j].Objectname, nodes[j].Mtnameshrt, nodes[j].currentValue);
		}
	}

	while(1) {
	       for (i = 0; i <  NrOfMoniTempl; i++) {
			sapmon_refresh( &rfc_handle, MoniHandle, NrOfMoniTempl );
        	        sapmon_request_tree(MoniHandle[i], &nodes, &NrOfNodes);


	                for (j = 0; j < NrOfNodes; j++) {
        	                printf("MO: %s, MA: %s, Value %s\n", nodes[j].Objectname, nodes[j].Mtnameshrt, nodes[j].currentValue);
                	}
		}
		sleep(10);
	}

	/*
	sapmon_request(&rfc_handle, MoniHandle, NrOfMoniTempl );
	while(1) {
		sapmon_refresh( &rfc_handle, MoniHandle, NrOfMoniTempl );
		sleep(30);
	}
	*/
	// Display

	//Log off
	sapmon_disconnect(&rfc_handle);	
	
	return SAP_OK;
}
