Exemple #1
0
    private IEnumerator CollectAllData(DataType type, DateTime fromDate, DateTime toDate)
    {
        float[] results = currentDatas[type];
        for (var i = 0; i < results.Length; ++i)
        {
            results[i] = -2f;
        }
        float    lon            = 0f;
        float    lat            = 0f;
        DateTime requestDate    = fromDate;
        DateTime endDate        = toDate;
        int      retrievedDatas = 0;

        while (requestDate <= endDate)
        {
            // キャッシュファイルの場所
            var currentDataFilePath =
                type.ToString() + requestDate.ToString("yyyy-MM-dd")
                + "x" + dataScale.ToString() + ".cache";
            if (dataSource == DataSource.SERVER)
            {
                // キャッシュファイルから情報を復元
                if (fileSystem.CopyArrayFromFile(currentDataFilePath, ref results))
                {
                    int loadedCount = 0;
                    foreach (float v in results)
                    {
                        if (v != -2)
                        {
                            ++loadedCount;
//                        print(v);
                        }
                    }
                    print("Loaded " + loadedCount.ToString() + " of " + type.ToString() + " on " +
                          requestDate.ToString("yyyy-MM-dd") + " from cache.");
                }
            }

            // データ保存用のメソッド。コールバックから呼ばれる
            Action <float, float, float> StoreData = (float _lon, float _lat, float val) => {
                pendingHttpRequests--;
                retrievedDatas++;

                int index = Mathf.FloorToInt(_lat * totalLongitudes + _lon);
                results[index] = val;

                if (retrievedDatas % 32 == 0)
                {
                    SaveCache(currentDataFilePath, results);
                }
            };

            print("Retrieving " + type.ToString() + " on " + requestDate.ToString("yyyy-MM-dd"));
            lon = 0f;
            lat = 0f;
            while (lon <= totalLongitudes)
            {
                // 取得を中止しているとき
                if (abortCollection)
                {
                    if (pendingHttpRequests <= 0)
                    {
                        print("Retrieve aborted");
                        SaveCache(currentDataFilePath, results);
                        return(false);
                    }
                    yield return(new WaitForSeconds(3f));

                    continue;
                }

                // すでに初期値以外のデータが入っていた場合
                if (results[Mathf.FloorToInt(lat * totalLongitudes + lon)] != -2f)
                {
                    retrievedDatas++;
                }
                else
                {
                    if (pendingHttpRequests >= MAX_PARALLEL_REQUEST)
                    {
                        yield return(new WaitForSeconds(5f));

                        continue;
                    }

                    // HTTPRequestWrapperにデータの取得を依頼する
                    pendingHttpRequests++;
                    RetrieveData(type, requestDate, lon, lat,
                                 (float data, float longitude, float latitude) => {
                        StoreData(longitude, latitude, data);
                    },
                                 (string responceText, float longitude, float latitude) => {
                        StoreData(longitude, latitude, -1f);
                    });

                    // 取得間隔調整用
                    if (pendingHttpRequests >= 8)
                    {
                        yield return(new WaitForSeconds(2.5f));
                    }
                    else if (pendingHttpRequests > 0)
                    {
                        yield return(new WaitForSeconds(0.2f));
                    }
                }

                // ビューがデータの更新を検知するためのイベント
                if (onDataUpdate != null)
                {
                    onDataUpdate(type, results);
                }

                InformationMonitor.SetLabel("Progress (" + type.ToString() + "): ",
                                            (retrievedDatas * 100 / results.Length).ToString() + "% (" + retrievedDatas.ToString() + "/" +
                                            results.Length.ToString() + ")");

                lat++;
                if (lat >= totalLatitudes)
                {
                    lon++;
                    lat = 0;
                    yield return(null);
                }
            }
            SaveCache(currentDataFilePath, results);
            requestDate = requestDate.AddDays(1);
            yield return(null);
        }
        if (onDataComplete != null)
        {
            onDataComplete(type, results);
        }
    }