ERMsg CUIEnvCanRadar::ExecuteHistorical(CCallback& callback)
	{
		ERMsg msg;
		string workingDir = GetDir(WORKING_DIR);
		CreateMultipleDir(workingDir);

		CInternetSessionPtr pSession;
		CHttpConnectionPtr pConnection;

		msg = GetHttpConnection(SERVER_NAME[as<size_t>(TYPE)], pConnection, pSession);
		if (!msg)
			return msg;


		callback.AddMessage(GetString(IDS_UPDATE_DIR));
		callback.AddMessage(workingDir, 1);
		callback.AddMessage(GetString(IDS_UPDATE_FROM));
		callback.AddMessage(SERVER_NAME[as<size_t>(TYPE)], 1);
		callback.AddMessage("");


		//Get remote station list
		StringVector imageList;
		if (msg)
			msg = GetRadarList(imageList, callback);

		if (msg)
			msg = CleanRadarList(imageList, callback);

		if (!msg)
			return msg;


		callback.PushTask("Download historical radar images (" + ToString(imageList.size())+ ")", imageList.size());
		//callback.SetNbStep(imageList.size());


		int nbRun = 0;
		int curI = 0;

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

			CInternetSessionPtr pSession;
			CHttpConnectionPtr pConnection;

			msg = GetHttpConnection(SERVER_NAME[as<size_t>(TYPE)], pConnection, pSession);

			if (msg)
			{
				TRY
				{
					for (int i = curI; i<imageList.size() && msg; i++)
					{
						string filePath = GetOutputFilePath(as<size_t>(TYPE), imageList[i]);
						msg += CreateMultipleDir(GetPath(filePath));
						//msg += CopyFile(pConnection, imageList[i], filePath, INTERNET_FLAG_TRANSFER_BINARY | WININET_API_FLAG_SYNC);

						CString URL(imageList[i].c_str());
						CHttpFile* pURLFile = pConnection->OpenRequest(_T("GET"), URL, NULL, 1, NULL, NULL, INTERNET_FLAG_EXISTING_CONNECT | INTERNET_FLAG_TRANSFER_BINARY | WININET_API_FLAG_SYNC | INTERNET_FLAG_NEED_FILE);

						//CStdioFile* pURLFile = pSession->OpenURL(UtilWin::Convert(imageList[i]), 0, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_EXISTING_CONNECT);

						bool bRep = false;

						if (pURLFile != NULL)
						{
							if (pURLFile->SendRequest() != 0)
							{
								CArray<char> source;
								int len = 0;
								CArray<char> tmp;
								tmp.SetSize(50);

								DWORD dwStatusCode = 0;
								pURLFile->QueryInfoStatusCode(dwStatusCode);

								ULONGLONG length = pURLFile->GetLength();
								while (length > 0)
								{
									pURLFile->Read(tmp.GetData(), 50);
									source.Append(tmp);

									length = pURLFile->GetLength();
								}

								pURLFile->QueryInfoStatusCode(dwStatusCode);
								pURLFile->Close();

								ofStream file;

								msg = file.open(filePath, ios::out | ios::binary);
								if (msg)
								{
									if (!source.IsEmpty())
										file.write(source.GetData(), (UINT)source.GetSize());

									file.close();

									//convert image to GeoTiff
									//ConvertToGeotiff(filePath, CCanadianRadar(CCanadianRadar::coord));
								}
							}
						}

						if (msg)
						{
							curI++;
							nbRun = 0;
							msg += callback.StepIt();
						}
					}
				}
				CATCH_ALL(e)
				{
					msg = UtilWin::SYGetMessage(*e);
				}
				END_CATCH_ALL

					//if an error occur: try again
					if (!msg && !callback.GetUserCancel())
					{
						//callback.AddTask(1);//one step more

						if (nbRun < 5)
						{
							callback.AddMessage(msg);
							msg.asgType(ERMsg::OK);
							Sleep(1000);//wait 1 sec
						}
					}

				//clean connection
				pConnection->Close();
				pSession->Close();
			}
		}


		callback.AddMessage(GetString(IDS_NB_FILES_DOWNLOADED) + ToString(curI));
		callback.PopTask();


		return msg;
	}
Пример #2
0
UINT CAccountInfo::ProcessResponse(CHttpFile& pHttpFile)
{
#ifdef	ARTSTORE
	return 1;
#else	
	//Take the HttpFile and parse it.
	UINT nFileLength = pHttpFile.GetLength();
	
	CString strBuffer;
	CString strSearch;
	CString strList;
	LPSTR b = strBuffer.GetBuffer(nFileLength);
	pHttpFile.Read(b, nFileLength);
	strBuffer.ReleaseBuffer();

	CBasicRequestInfo::ProcessResponse(strBuffer);

	UINT uStatusCode = atoi((LPCTSTR)GetStatusCode());
	if(uStatusCode == 0 || uStatusCode == 15) //success or in use
	{

		CString strSearch;
		
		strSearch = "AccountID=";
		CString strTemp = "";
		ParseFileForValue(strBuffer, strSearch, strTemp);
		m_strServerSuggestedAccountID = strTemp;

		if(m_strRequestedAccountID == strTemp){
			
			SetAccountID(strTemp);

			CString strResult;
			CString strNotify;
			strNotify.LoadString(IDS_PGS_ACCOUNTID_NOTIFICATION);
			strResult.Format(strNotify, GetAccountID());
			AfxMessageBox(strResult, MB_ICONINFORMATION);

			//at this point, the server has created our accountID (if new)

			CUUEncoder UUEncoder;

			GET_PMWAPP()->WriteProfileString(SECTION_ServerAccount,
														ENTRY_ServerAccountName,
														UUEncoder.EncodeString(GetAccountID()));
			GET_PMWAPP()->WriteProfileString(SECTION_ServerAccount,
														ENTRY_ServerPassword,
														UUEncoder.EncodeString(GetPassword()));
		}else{
			//try to establish account again
			return 100;
		}

	}

	if(!GetStatusCode())
	  return 1;
	else
	  return 0;
#endif
}