string GetProcessStatus( string query, string id )
{
	// string programName = GetRequestProgramName( query );
	Program* program = ProgramManager::Instance()->GetRunningProgram( atoi( id.c_str() ) );
	string jsonDescr;

	if( program == NULL ) {
		// the program is not started
		jsonDescr += "{\"Status\":\"NotAvailable\"}";
		return jsonDescr;
	}

	string deviceStatus;
	string propertyName = "Value";
	DeviceParameter value;
	char deviceId[16];
	string status = "not set";
	char buffer[32];

	string deviceIp = WebServer::Instance()->Ip();
	int devicePort = WebServer::Instance()->Port();
	sprintf( buffer, "%d", devicePort );

	string processResource = "http://" + deviceIp + ":" + (string)buffer + "/Processes/" + id;
	string projectResource = "http://" + deviceIp + ":" + (string)buffer + "/Projects/" + id;
	string operation = GetProgramStatusOperation;
	jsonDescr = "{\"ResourceId\":\"" + processResource + "\",\"ProjectResourceId\":\"" + projectResource + "\",\"Devices\":[";
	string deviceUrl;
	string propertyUrl;

	list<BaseDevice*>::iterator it;

	for( it = program->Devices()->begin(); it != program->Devices()->end(); it++ )
	{
		value = ( *it )->GetParameter( propertyName );

		if ( value.IsInitialized() == true )
		{
			status = value.StringValue();
		}
		else
		{
			continue;
		}

		sprintf( deviceId, "%d", (*it)->Id() );
		// sprintf( status, "%d", statusValue );

		deviceUrl = processResource + "/Devices/" + (string)deviceId;
		deviceStatus = "{\"ResourceId\":\"" + deviceUrl + "\",\"Properties\":[{\"ResourceId\":\"" + deviceUrl + "/Properties/" + propertyName + "\",\"devkey\":\"" +
				propertyName + "\",\"devtype\":\"int\",\"devvalue\":\"" +  status + "\"}]},";
		jsonDescr += deviceStatus;
	}

	jsonDescr = jsonDescr.substr( 0, jsonDescr.length() - 1 ); // remove the last comma
 	jsonDescr += "],\"Status\":\"Running\"}";

	return jsonDescr;
}
string GetDevicePropery( string query )
{
	string result = "";

	int programId = atoi( strtok( (char*)query.c_str(), "." ) );
	int deviceId = atoi( strtok( NULL, "." ) );
	char* propertyName = strtok( NULL, "." );

	string propertyValue = "";

	Program* program = ProgramManager::Instance()->GetRunningProgram( programId );
	if ( program == NULL )
	{
		cout<< "Program with id: "<<programId<<" is not started.\n";
		EventLogger::Instance()->WriteVerbose( "Program with id: %d is not started.", programId );

		return "";
	}

	list<BaseDevice*>::iterator it;

	for ( it = program->Devices()->begin(); it != program->Devices()->end(); it++ )
	{
		if ( ( *it )->Id() == deviceId )
		{
			//we have found the device we want -> set the Property we need
			DeviceParameter value = ( *it )->GetParameter( propertyName );

			if( value.IsInitialized() == true )
			{
				propertyValue = Convert::ToString( value.Value(), value.AgilartType() );
			}
			else
			{
				propertyValue = "0";
			}

			if ( result != "" )
				result += "-";

			result += propertyValue;

			break;
		}
	}

	return result;
}
void SetScreenDeviceStatus( string query )
{
	char *key = strtok( (char*)query.c_str(), "=" );
	char *value = strtok( NULL, ";" );

	int programId = atoi( strtok( key, "." ) );
	int deviceId = atoi( strtok( NULL, "." ) );
	char* propertyName = strtok( NULL, "." );

	bool isFound = false;

	Program* program = ProgramManager::Instance()->GetRunningProgram( programId );
	if ( program == NULL )
	{
		cout<< "Program with id: "<<programId<<" is not started.\n";
		EventLogger::Instance()->WriteVerbose("Program with id: %d is not started.", programId );

		return;
	}

	list<BaseDevice*>::iterator it;

	for ( it = program->Devices()->begin(); it != program->Devices()->end(); it++ )
	{
		if ( ( *it )->Id() == deviceId )
		{
			isFound = true;
			//we have found the device we want -> set the Property we need
			void* statusValue = new string( value );
			( *it )->SetParameter( propertyName, statusValue );

			break;
		}
	}

	if ( isFound == false )
	{
		cout<< "Device with id: "<<deviceId<<" is not available in program with id: "<<programId<<endl;
		EventLogger::Instance()->WriteVerbose("Device with id: %d is not available in program with id: %d", deviceId, programId );
	}
}