/*
 * SetupMeasurement.c
 * 
 * Gocator 2000 Sample
 * Copyright (C) 2011 by LMI Technologies Inc.
 * 
 * Licensed under The MIT License.
 * Redistributions of files must retain the above copyright notice.
 *
 * Purpose: Connect to Gocator system, setup measurement data under Profile Mode.
 * Please refer to ReceiveMeasurement.c for receiving of the measurement data. 
 *
 * NOTE! This code requires the sensor to not have any conflicting measuremetn setup already. 
 *
 */
#include <GoSdk/GoSdk.h>
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#define SENSOR_IP			    "192.168.1.10"						

void main(int argc, char **argv)
{
	kAssembly api = kNULL;
	kStatus status;	
	GoSystem system = kNULL;
	GoSensor sensor = kNULL;
	GoProfilePositionX positionX = kNULL;
	kIpAddress ipAddress;
	GoSetup setup = kNULL;
	GoTools tools = kNULL;
	GoProfilePosition profilePositionTool = kNULL;
	GoProfilePositionZ profileMeasurementTopZ =kNULL;
	GoProfileFeature profileFeatureTop = kNULL;
	GoProfileRegion regionTop = kNULL;
	GoOutput outputModule = kNULL;
	GoEthernet ethernetOutput = kNULL;

	// construct Gocator API Library
	if ((status = GoSdk_Construct(&api)) != kOK)
	{
		printf("Error: GoSdk_Construct:%d\n", status);
		return;
	}

	// construct GoSystem object
	if ((status = GoSystem_Construct(&system, kNULL)) != kOK)
	{
		printf("Error: GoSystem_Construct:%d\n", status);
		return;
	}

	// Parse IP address into address data structure
	kIpAddress_Parse(&ipAddress, SENSOR_IP);

	// obtain GoSensor object by sensor IP address
	if ((status = GoSystem_FindSensorByIpAddress(system, &ipAddress, &sensor)) != kOK)
	{
		printf("Error: GoSystem_FindSensorByIpAddress:%d\n", status);
		return;
	}

	// create connection to GoSensor object
	if ((status = GoSensor_Connect(sensor)) != kOK)
	{
		printf("Error: GoSensor_Connect:%d\n", status);
		return;
	}

	// enable sensor data channel
	if ((status = GoSystem_EnableData(system, kTRUE)) != kOK)
	{
		printf("Error: GoSensor_EnableData:%d\n", status);
		return;
	}
	
	// retrieve setup handle
	if ((setup = GoSensor_Setup(sensor)) == kNULL)
	{
		printf("Error: GoSensor_Setup: Invalid Handle\n");
	}	

	// retreive tools handle
	if ((tools = GoSensor_Tools(sensor)) == kNULL)
	{
		printf("Error: GoSensor_Tools: Invalid Handle\n");
	}

	// add ProfilePosition tool, retreive tool handle
	if ((status = GoTools_AddTool(tools, GO_TOOL_PROFILE_POSITION, &profilePositionTool)) != kOK)
	{
		printf("Error: GoTools_AddTool:%d\n", status);
		return;
	}

	if ((status = GoTool_SetName(profilePositionTool, "Profile position Test")) != kOK)
	{
		printf("Error: GoTools_AddTool: Invalid Handle\n");
		return;
	}

	// add Z measurement for ProfilePosition tool
	if ((profileMeasurementTopZ = GoProfilePosition_ZMeasurement(profilePositionTool)) == kNULL)
	{
		printf("Error: GoProfilePosition_ZMeasurement: Invalid Handle\n");
		return;
	}

	// enable zprofileMeasurementTop
	if ((status = GoMeasurement_Enable(profileMeasurementTopZ, kTRUE)) != kOK)
	{
		printf("Error: GoMeasurement_Enable:%d\n", status);
		return;
	}

	// set the measurement ID for zprofileMeasurementTop
	if ((status = GoMeasurement_SetId(profileMeasurementTopZ, 0)) != kOK)
	{
		printf("Error: GoMeasurement_SetId:%d\n", status);
		return;
	}

	// set the name for zprofileMeasuermentTop
	if ((status = GoMeasurement_SetName(profileMeasurementTopZ, "Profile Measurement Z")) != kOK)
	{
		printf("Error: GoMeasurement_SetName:%d\n", status);
	}

	// set ProfilePosition feature to top
	if ((profileFeatureTop = GoProfilePosition_Feature(profilePositionTool)) == kNULL)
	{
		printf("Error: GoProfilePosition_Feature: Invalid Handle\n");
	}

	if ((status = GoProfileFeature_SetType(profileFeatureTop, GO_PROFILE_FEATURE_TYPE_MAX_Z)) != kOK)
	{
		printf("Error: GoProfileFeature_SetType:%d\n", status);
	}

	// set the ROI to fill the entire active area
	if ((regionTop = GoProfileFeature_Region(profileFeatureTop)) == kNULL)
	{
		printf("Error: GoProfileFeature_Region: Invalid Handle\n");
	}
	if ((status = GoProfileRegion_SetX(regionTop, GoSetup_TransformedDataRegionX(setup, GO_ROLE_MAIN))) != kOK)
	{
		printf("Error: GoProfileRegion_SetX:%d\n", status);
	}
	if ((status = GoProfileRegion_SetZ(regionTop, GoSetup_TransformedDataRegionZ(setup, GO_ROLE_MAIN))) != kOK)
	{
		printf("Error: GoProfileRegion_SetZ:%d\n", status);
	}
	if ((status = GoProfileRegion_SetHeight(regionTop, GoSetup_TransformedDataRegionHeight(setup, GO_ROLE_MAIN))) != kOK)
	{
		printf("Error: GoProfileRegion_SetHeight:%d\n", status);
	}
	if ((status = GoProfileRegion_SetWidth(regionTop, GoSetup_TransformedDataRegionWidth(setup, GO_ROLE_MAIN))) != kOK)
	{
		printf("Error: GoProfileRegion_SetWidth:%d\n", status);
	}

	// enable Ethernet output for measurement tool	

	if ((outputModule = GoSensor_Output(sensor)) == kNULL)
	{
		printf("Error: GoSensor_Output: Invalid Handle\n");
	}
	if ((ethernetOutput = GoOutput_Ethernet(outputModule)) == kNULL)
	{
		printf("Error: GoOutput_Ethernet: Invalid Handle\n");
	}
	if ((status = GoEthernet_ClearAllSources(ethernetOutput)) != kOK)
	{
		printf("Error: GoEthernet_ClearAllSources:%d\n", status);
	}
	if ((status = GoEthernet_AddSource(ethernetOutput, GO_OUTPUT_SOURCE_MEASUREMENT, 0)) != kOK)
	{
		printf("Error: GoEthernet_AddSource:%d\n", status);
	}

	// refer to ReceiveMeasurement.c for receiving of the measurement data.

	// destroy handles
	GoDestroy(system);
	GoDestroy(api);

	printf("Press any key to continue...\n");
	getchar();
	return;
}