예제 #1
0
        private async Task DownloadDBFiles()
        {
            Stopwatch sw = new Stopwatch();

            Core.Globals.GetFileNameResult      = GetFileNameResultType.None;
            Core.Globals.DownloadMasterDBResult = DownloadDBResultType.None;
            Core.Globals.DownloadAuditDBResult  = DownloadDBResultType.None;

            string UserName = Core.Globals.LoginUsername;

            DateTime Today = DateTime.Now;

            sw.Start();
            bool filenameRetrieved = await serviceManager.GetFileName(UserName, Core.Globals.AppID, Core.Globals.OSType).ConfigureAwait(false);

            sw.Stop();

            int elapsed = sw.Elapsed.Milliseconds;

            if (filenameRetrieved && Core.Globals.GetFileNameResult == GetFileNameResultType.Success)
            {
                // Successful
                Console.WriteLine("GetFileName call success");

                // Store DB Information into the App Preferences
                var appPreferences = new AppPreferences(this.Activity.ApplicationContext);
                appPreferences.SaveMasterDBInfoFileName(Core.Globals.MasterDBInfo.FileName);
                appPreferences.SaveMasterDBInfoFileSize(Core.Globals.MasterDBInfo.FileSize);
                appPreferences.SaveMasterDBInfoFileType(Core.Globals.MasterDBInfo.FileType);
                appPreferences.SaveMasterDBInfoIsComplete(Core.Globals.MasterDBInfo.IsComplete);
                appPreferences.SaveMasterDBInfoPathHttp(Core.Globals.MasterDBInfo.PathHTTP);
                appPreferences.SaveMasterDBInfoVersion(Core.Globals.MasterDBInfo.Version);

                // Clean database
                string masterDBPath = new FileUtil().GetMasterDBPath();
                if (File.Exists(masterDBPath))
                {
                    File.Delete(masterDBPath);
                }

                GlobalsAndroid.masterDB = null;

                GC.Collect();
                GC.WaitForPendingFinalizers();

                // Start downloading databases
                sw.Start();
                bool downloaded = await serviceManager.DownloadFile().ConfigureAwait(false);

                sw.Stop();

                elapsed += sw.Elapsed.Milliseconds;

                if (downloaded)
                {
                    // Successful
                    Console.WriteLine("DownloadFile call success");

                    // Add Download Log
                    var DownloadDate = Today.ToString("yyyy-MM-dd HH:mm:ss.fff");
                    var LoginID      = Core.Globals.LoginUsername;
                    var Status       = "OK";
                    if (Core.Globals.DownloadMasterDBResult == DownloadDBResultType.Success)
                    {
                        Status = "OK";
                    }
                    else if (Core.Globals.DownloadMasterDBResult == DownloadDBResultType.NotFoundError || Core.Globals.DownloadMasterDBResult == DownloadDBResultType.BadRequestError)
                    {
                        Status = "Error 404 File Not Found";
                    }
                    var FilePath   = Core.Globals.MasterDBInfo.PathHTTP + Core.Globals.MasterDBInfo.FileName;
                    var TotalTime  = sw.Elapsed.TotalSeconds.ToString("F3");
                    var AppVersion = Core.Globals.AppVersion;
                    var OSType     = Core.Globals.OSType;
                    var OSVersion  = Core.Globals.OSVersion;
                    var MacAddr    = Core.Globals.MACAddress;

                    Task.Run(() => serviceManager.AddDownloadLog(DownloadDate, LoginID, Status, FilePath, TotalTime, AppVersion, OSType, OSVersion, MacAddr));
                }
                else
                {
                    // There might be some error with the internet connection.
                    // Unexpected error
                    Console.WriteLine("DownloadFile call failure");
                }
            }
            else
            {
                // There might be some error with the internet connection.
                // Unexpected error
                Console.WriteLine("GetFileName call failure");
            }


            if (elapsed < timeLimit)
            {
                downloadCheckCallObserver = new Timer(this.CheckDownloadResult, null, timeLimit * 1000 - elapsed, System.Threading.Timeout.Infinite);
            }
            else
            {
                CheckDownloadResult(null);
            }
        }