ERMsg CUISolutionMesonetDaily::Execute(CCallback& callback)
	{
		ERMsg msg;

		int firstYear = as<int>(FIRST_YEAR);
		int lastYear = as<int>(LAST_YEAR);
		size_t nbYears = lastYear - firstYear + 1;
		callback.PushTask(m_name + " : " + GetString(IDS_UPDATE_FILE), nbYears);
		callback.AddMessage(GetString(IDS_UPDATE_FILE));

		string workingDir = GetDir(WORKING_DIR);
		CTime today = CTime::GetCurrentTime();

		callback.AddMessage(GetString(IDS_UPDATE_DIR));
		callback.AddMessage(workingDir, 1);
		callback.AddMessage(GetString(IDS_UPDATE_FROM));
		callback.AddMessage(SERVER_NAME, 1);
		callback.AddMessage("");
		


		//open a connection on the server
		CInternetSessionPtr pSession;
		CFtpConnectionPtr pConnection;

		msg = GetFtpConnection(SERVER_NAME, pConnection, pSession, PRE_CONFIG_INTERNET_ACCESS, Get(USER_NAME), Get(PASSWORD));

		if (msg)
			msg = UpdateStationList(pConnection, callback);

		if (msg)
		{
			CFileInfoVector dirList;
			UtilWWW::FindDirectories(pConnection, SUB_DIR, dirList);

			pConnection->Close();
			pSession->Close();

			for (size_t i = 0; i < dirList.size() && msg; i++)
			{
				string dirName = GetLastDirName(dirList[i].m_filePath);
				int year = ToInt(dirName);
				if (year >= firstYear && year <= lastYear)
				{
					msg = GetFtpConnection(SERVER_NAME, pConnection, pSession, PRE_CONFIG_INTERNET_ACCESS, Get(USER_NAME), Get(PASSWORD));

					int nbDownload = 0;
					string yearPage = SUB_DIR + dirName;
					string outputPath = workingDir + dirName + "/";

					//Load files list
					callback.AddMessage(ToString(year), 1);
					callback.AddMessage(GetString(IDS_LOAD_FILE_LIST), 2);
					CFileInfoVector fileList;
					msg += UtilWWW::FindFiles(pConnection, (yearPage + "/*.zip"), fileList, callback);

					callback.AddMessage(GetString(IDS_NB_FILES_FOUND) + ToString(fileList.size()) + ")", 2);
					pConnection->Close();
					pSession->Close();

					//clean list
					for (CFileInfoVector::iterator it = fileList.begin(); it != fileList.end() && msg;)
					{
						string fileTitle = GetFileTitle(it->m_filePath);
						string filePathZip = outputPath + fileTitle + ".zip";
						string filePathWea = outputPath + fileTitle + ".wea";

						if (UtilWWW::IsFileUpToDate(*it, filePathWea, false))
							it = fileList.erase(it);
						else
							it++;

						msg += callback.StepIt(0);
					}

					callback.AddMessage(GetString(IDS_NB_FILES_CLEARED) + ToString(fileList.size()), 2);

					//download all files
					int nbRun = 0;
					int curI = 0;

					StringVector tmp(IDS_FTP_DIRECTION, "|;");
					callback.PushTask(tmp[0] + " " + dirName + " (" + ToString(fileList.size()) + " stations)", fileList.size());

					while (curI < fileList.size() && msg)
					{
						nbRun++;

						CreateMultipleDir(outputPath);

						//open a connection on the server
						msg = GetFtpConnection(SERVER_NAME, pConnection, pSession, PRE_CONFIG_INTERNET_ACCESS, Get(USER_NAME), Get(PASSWORD));
						for (CFileInfoVector::iterator it = fileList.begin(); it != fileList.end() && msg; it++)
						{
							string fileTitle = GetFileTitle(it->m_filePath);
							string filePathZip = outputPath + fileTitle + ".zip";
							string filePathWea = outputPath + fileTitle + ".wea";
							string stationPage = yearPage + "/" + fileTitle + ".zip";

							msg = UtilWWW::CopyFile(pConnection, stationPage.c_str(), filePathZip.c_str(), INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD | INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_DONT_CACHE);
							if (msg)
							{
								ASSERT(FileExists(filePathZip));
								string app = GetApplicationPath() + "External\\7z.exe";
								string param = " e \"" + filePathZip + "\" -y -o\"" + outputPath + "\""; ReplaceString(param, "\\", "/");
								string command = app + param;
								msg += WinExecWait(command.c_str());
								msg += RemoveFile(filePathZip);

								//update time stamp to the zip file
								boost::filesystem::path p(filePathWea);
								if (boost::filesystem::exists(p))
									boost::filesystem::last_write_time(p, fileList[i].m_time);//std::time(0)
								

								nbDownload++;
								curI++;
								msg += callback.StepIt();
							}
						}

						//if an error occur: try again
						if (!msg && !callback.GetUserCancel())
						{
							if (nbRun < 5)
							{
								callback.AddMessage(msg);
								msg.asgType(ERMsg::OK);

								callback.PushTask("Waiting 2 seconds for server...", 100);
								for (size_t i = 0; i < 40 && msg; i++)
								{
									Sleep(50);//wait 50 milisec
									msg += callback.StepIt();
								}
								callback.PopTask();

							}
						}

						callback.AddMessage(GetString(IDS_NB_FILES_DOWNLOADED) + ToString(nbDownload), 2);

						pConnection->Close();
						pSession->Close();

					}//while

					callback.PopTask();
					callback.StepIt();
				}//year is included
			}//for all years
		}//if msg

		callback.PopTask();
		return msg;
	}
Exemplo n.º 2
0
BOOL CDOSProcess::executeTool(CProcessStatus& status)
{
	CString sCl = getCommandLine(status);

	/* FIND OUT WHERE THE EXECUTABLE IS */

	 CString sActualPath;
	 HINSTANCE hInstance = FindExecutable(getToolPath(), NULL, sActualPath.GetBuffer(MAX_PATH));
	 sActualPath.ReleaseBuffer();

	if((DWORD)hInstance == ERROR_FILE_NOT_FOUND)
	{
		CString s;
		s.Format("The executable %s could not be found.  Make sure it is part of the PATH statement in your AUTOEXEC.BAT file\n", getToolPath());
		throw(CProcessFailure(this, s));
	}
	else if((DWORD)hInstance == ERROR_PATH_NOT_FOUND)
	{
		CString s;
		s.Format("Windows gave a PATH NOT FOUND error while trying to  execute %s\n", getToolPath());
		throw(CProcessFailure(this, s));
	}
	else if((DWORD)hInstance == SE_ERR_ACCESSDENIED)
	{
		CString s;
		s.Format("Windows gave an ACCESS DENIED error while trying to  execute %s\n", getToolPath());
		throw(CProcessFailure(this, s));
	}
	else if((DWORD)hInstance <=32)
	{
		CString s;
		s.Format("Unknown problem trying to execute(find?) %s\n", getToolPath());
		throw(CProcessFailure(this, s));
	}


	CString sBatchPath = status.makeTempPath(getBatchName(status), ".bat");

	ofstream fout(sBatchPath);
	if(!fout.is_open())
	{
		MessageBox( NULL, "executeTool()", "Couldn't open batch file for writing", MB_ICONEXCLAMATION | MB_OK);
		return FALSE;
	}
	//TRACE("%s Command Line Length=%d\n", getToolPath(), strlen(sCl));
	fout << "REM created by CARLAStudio during a parse operation\n";
	fout << "\"" << sActualPath << "\" " << sCl << "\n";
	fout.close();

	CString s;
#ifdef Before1_04
	if(status.getVerbosity())
		s = sBatchPath;
	else
		s.Format("C:\\WINDOWS\\COMMAND.COM  /c \"%s\"", sBatchPath);
#else  // hab and drz 1.04 to make work with NT
	TCHAR lpszCommand[MAX_PATH];
	if (RunningNT())
	{
		GetSystemDirectory(lpszCommand, MAX_PATH);
		lstrcat(lpszCommand, "\\cmd.exe");
	}
	else
	{
		GetWindowsDirectory(lpszCommand, MAX_PATH);
		lstrcat(lpszCommand, "\\command.com");
	}
	s.Format("%s %s \"%s\"", lpszCommand,
						 (status.getVerbosity()& 0x01)  ? "/k" : "/c", //jdh 11/10/99
						 sBatchPath);
#endif // Before1_04

	return WinExecWait(s, TRUE); //status.getVerbosity());// false makes winoldapps

/*	 hInstance = ShellExecute(
		AfxGetMainWnd( )->m_hWnd,	// handle to parent window
		"open",	// pointer to string that specifies operation to perform
		sBatchPath,	// pointer to filename or folder name string
		NULL,	// pointer to string that specifies executable-file parameters
		// To Do: this is just a guess at the best place to put them
		//status.sRAWPath.getDirectory(),	// pointer to string that specifies default directory
		status.getTempDirectory(),
		status.getVerbosity()?SW_SHOW:SW_HIDE	// whether file is shown when opened
	   );

	return TRUE;
*/
}