コード例 #1
0
    /// <summary>
    /// Httpでサーバーに存在する指定のパスのファイルを取得して、DBに登録する
    /// </summary>
    /// <returns>The server datas to regist D.</returns>
    /// <param name="mono">Mono.</param>
    /// <param name="filePathList">File path list.</param>
    /// <param name="restoreCount">(復元再開時に使用)復元済みのデータ件数</param>
    static IEnumerator DownloadServerDatasToRegistDBByHttp(MonoBehaviour mono, List <string> filePathList, int restoreCount = 0)
    {
        Debug.Log("DownloadServerDatasToRegistDB");
        //スリープしないようにする
        Screen.sleepTimeout = SleepTimeout.NeverSleep;

        //指定されたファイルパスとDBとを照合してDBに未登録のデータを探し出す
        List <string> noDBRegistDataList = GetNoDBRegistDataList(filePathList);

        var sleepTable = MyDatabase.Instance.GetSleepTable();

        //進捗ダイアログ表示
        ProgressDialog.Show("データを復元しています。", noDBRegistDataList.Count + restoreCount, restoreCount);
        int downloadCompleteDataSum = restoreCount; //ダウンロード完了したデータ件数

        if (!HttpManager.IsInternetAvailable())
        {
            //サーバーとの接続に失敗すれば、ダウンロードに失敗したら、スリープ設定解除
            Screen.sleepTimeout = SleepTimeout.SystemSetting;

            //進捗ダイアログ終了
            ProgressDialog.Dismiss();

            //復元に失敗した事をユーザーに伝える
            yield return(mono.StartCoroutine(TellFailedRestoreData()));

            AskReDownloadData(mono, filePathList, downloadCompleteDataSum);

            yield break;
        }

        foreach (var filePath in noDBRegistDataList)
        {
            //filePathの例: /RD8001/Data/112233445566/202002/20180827092055_1.csv
            var date = Kaimin.Common.Utility.getDateFromDownloadCsvFilePath(filePath); //例:20180827092055

            var dataDirectoryPath = filePath.Replace("/RD8001/Data/", "");
            dataDirectoryPath = dataDirectoryPath.Substring(0, dataDirectoryPath.LastIndexOf('/') + 1); //例:112233445566/yyyyMMdd/
            //念のためん、先頭にスラッシュがついてれば、削除する
            if (dataDirectoryPath.IndexOf('/') == 0)
            {
                dataDirectoryPath = dataDirectoryPath.Substring(1);
            }

            var    dataPath     = dataDirectoryPath + date + ".csv";        //例:112233445566/yyyyMM/20180827092055.csv
            string saveFilePath = Kaimin.Common.Utility.GsDataPath() + dataPath;

            string downloadUrl  = HttpManager.API_DOWNLOAD_URL + "?file_path=" + filePath;
            var    downloadTask = HttpManager.DownloadFile(saveFilePath, downloadUrl);
            yield return(downloadTask.AsCoroutine());

            Debug.Log(downloadTask.Result ? "Download Success!" : "Download Failed...");

            //ダウンロードに成功すれば、ダウンロードしたデータをDBに登録する
            if (downloadTask.Result)
            {
                sleepTable.Update(new DbSleepData(date, dataPath, true));
                Debug.Log("Insert " + dataPath + " to DB.");

                //データが正常に保存されているか確認する
                Debug.Log("isExistFile:" + System.IO.File.Exists(saveFilePath));

                //進捗ダイアログ更新
                downloadCompleteDataSum++;
                //復元した件数を記録
                UserDataManager.State.SaveRestoreDataCount(downloadCompleteDataSum);
                ProgressDialog.UpdateProgress(downloadCompleteDataSum);
            }
            else
            {
                //ダウンロードに失敗したら

                //スリープ設定解除
                Screen.sleepTimeout = SleepTimeout.SystemSetting;
                //進捗ダイアログ終了
                ProgressDialog.Dismiss();

                //復元に失敗した事をユーザーに伝える
                yield return(mono.StartCoroutine(TellFailedRestoreData()));

                AskReDownloadData(mono, filePathList, downloadCompleteDataSum);

                yield break;
            }
        }

        //復元が成功した場合のみここまで到達
        Debug.Log("RestoreCompleted!!!!!");

        //DB確認
        Debug.Log("StartCheckDBData----------------------------");
        var dbFilePathList = Kaimin.Common.Utility.GetAllFiles(Kaimin.Common.Utility.GsDataPath(), "*.csv");

        foreach (var filePath in dbFilePathList)
        {
            Debug.Log("FilePath:" + filePath);
        }
        Debug.Log("EndCheckDBData----------------------------");

        //スリープ設定解除
        Screen.sleepTimeout = SleepTimeout.SystemSetting;
        //これ以降復元しないように設定
        UserDataManager.State.SaveNessesaryRestore(false);
        //進捗ダイアログ終了
        ProgressDialog.Dismiss();
        //復元完了した事をユーザーに伝える
        yield return(mono.StartCoroutine(TellCompleteDataRestore()));
    }