/*
* Function used to initialize the IBM Watson IoT client using the config file which is generated when you register your device
*
* @param client - Reference to the ManagedDevice
*
* @param configFilePath - File path to the configuration file
*
* @return int return code
* error codes
* CONFIG_FILE_ERROR -3 - Config file not present or not in right format
*/
int initialize_configfile_dm(ManagedDevice *client, char *configFilePath)
{
	int rc = -1;

	rc = initialize_configfile(&deviceClient, configFilePath);
	
	return rc;
}
/*
* Function used to initialize the IBM Watson IoT client using the config file which is generated when you register your device
*
* @param configFilePath - File path to the configuration file
*
* @return int return code
* error codes
* CONFIG_FILE_ERROR -3 - Config file not present or not in right format
*/
int initialize_configfile_dm(char *configFilePath)
{
	int rc = -1;
	rc = initialize_configfile(&dmClient.deviceClient, configFilePath);
	return rc;
}
int main(int argc, char const *argv[])
{
	int rc = -1;

	iotfclient  client;

	//catch interrupt signal
	signal(SIGINT, sigHandler);
	signal(SIGTERM, sigHandler);

	char *configFilePath = NULL;

	if(isEMBDCHomeDefined()){

	    getSamplesPath(&configFilePath);
	    configFilePath = realloc(configFilePath,strlen(configFilePath)+15);
	    strcat(configFilePath,"gateway.cfg");
        }
	else{
	    printf("IOT_EMBDC_HOME is not defined\n");
	    printf("Define IOT_EMBDC_HOME to client library path to execute samples\n");
	    return -1;
        }

	rc = initialize_configfile(&client, configFilePath,1);
	free(configFilePath);

	if(rc != SUCCESS){
		printf("initialize failed and returned rc = %d.\n Quitting..", rc);
		return 0;
	}

	setKeepAliveInterval(59);

	rc = connectiotf(&client);

	if(rc != SUCCESS){
		printf("Connection failed and returned rc = %d.\n Quitting..", rc);
		return 0;
	}

	//Registering the function "myCallback" as the command handler.
	setGatewayCommandHandler(&client, myCallback);
	// providing "+" will subscribe to all the command of all formats.
	subscribeToDeviceCommands(&client, "elevator", "elevator-1", "+", "+", 0);

	while(!interrupt)
	{
		printf("Publishing the event stat with rc ");
		//publishing gateway events
		rc= publishGatewayEvent(&client, "elevatorDevices","elevatorGateway", "{\"d\" : {\"temp\" : 34 }}", QOS0);
		//publishing device events on behalf of a device
		rc= publishDeviceEvent(&client, "elevator","elevator-1","status","json", "{\"d\" : {\"temp\" : 34 }}", QOS0);

		printf(" %d\n", rc);
		//Yield for receiving commands.
		yield(&client, 1000);
		sleep(1);
	}

	printf("Quitting!!\n");

	//Be sure to disconnect the gateway at exit
	disconnectGateway(&client);

	return 0;
}
int main(int argc, char const *argv[])
{
	int rc = -1;
	int x=0;
	ManagedDevice client;


	char *configFilePath = "./device.cfg";

	rc = initialize_configfile(&deviceClient, configFilePath);
	if(rc != SUCCESS){
		printf("initialize failed and returned rc = %d.\n Quitting..", rc);
		scanf("%d",&x);
		return 0;
	}

	rc = connectiotf_dm(&client);
	if(rc != SUCCESS){
		printf("Connection; failed and returned rc = %d.\n Quitting..", rc);
		scanf("%d",&x);
		return 0;
	}

	setCommandHandler_dm(&client, myCallback);
	setManagedHandler_dm(&client,managedCallBack );
	subscribeCommands_dm(&client);
	populateMgmtConfig(&client);
	int exit = 0;
	char reqId[40];
	while (!exit){
		printOptions();
		int val;
		scanf("%d",&val);
		switch (val) {
			case 1:
				printf("\n publish manage ..\n");
				publishManageEvent(&client,4000,1,1, reqId);
				printf("\n Manage Event Exited: %s",reqId);
				break;
			case 2:
				printf("\n publish unmanaged..\n");
				publishUnManageEvent(&client, reqId);
				printf("\nunmanaged Request Exit : %s",reqId);
				break;
			case 3:
				printf("\n publish addErrorCode ..\n");
				addErrorCode(&client, 121 , reqId);
				printf("\n addErrorCode Request Exit : %s",reqId);
				break;
			case 4:
				printf("\n publish clearErrorCodes ..\n");
				clearErrorCodes(&client,  reqId);
				printf("\n clearErrorCodes Request Exit :");// %s",reqId);
				break;
			case 5:
				printf("\n publish addLog ..\n");
				addLog(&client, "test","",1, reqId);
				printf("\n addLog Request Exit : %s",reqId);
				break;
			case 6:
				printf("\n publish clearLogs ..\n");
				clearLogs(&client,reqId);
				printf("\n clearLogs Request Exit : %s",reqId);
				break;
			case 7:
				printf("\n publish updateLocation ..\n");
				time_t t = time(NULL);
				char updatedDateTime[50];//"2016-03-01T07:07:56.323Z"
				strftime(updatedDateTime, sizeof(updatedDateTime), "%Y-%m-%dT%TZ", localtime(&t));
				updateLocation(&client, 77.5667,12.9667, 0,updatedDateTime, 0, reqId) ;
				printf("updateLocation Request Exit : %s",reqId);
				break;
			case 8:
				printf("\n publish Event To WIoTP ..\n");
				publishEventToIot(&client);
				break;
			default:
				disconnect_dm(&client);
				printf("\n Quitting!!\n");
				exit = 1;
				break;
		}
	}

	return 0;
}