Esempio n. 1
0
        /// <summary>
        /// Initializes the application.
        /// </summary>
        public static async Task Initialize()
        {
            throw new InvalidOperationException();
#endif

            Log.Debug("Initializing {0}", ApplicationInformation);

            RegisterAnalytics();

            Engine   = new Engine();
            Sensors  = SensorPack.Create(Engine);
            Recorder = new Recorder(Engine);
            Sync     = new SyncManager();

            await UserLog.Initialize().ConfigureAwait(false);

            UserLog.Add(string.Format(LogStrings.Started, Version));

            await FileNaming.InitializeFileStructure().ConfigureAwait(false);

            await DatabaseUtility.Initialize().ConfigureAwait(false);

            _initialized = true;
        }
Esempio n. 2
0
        /// <summary>
        /// Runs the synchronization attempt.
        /// Does not raise exceptions.
        /// </summary>
        public async Task <SyncResult> Synchronize(CancellationToken token, SyncPolicy policy = SyncPolicy.Default)
        {
            if (DateTime.UtcNow < NextUploadOpportunity && policy == SyncPolicy.Default)
            {
                Log.Debug("Can't sync: sync attempt too early, next upload scheduled after {0}", NextUploadOpportunity);

                return(new SyncResult(new InvalidOperationException("Sync attempt too early")));
            }

            Settings.LastUploadAttempt = DateTime.UtcNow;

            if (!CheckSyncConditions(policy))
            {
                return(new SyncResult(error: new InvalidOperationException("Sync conditions not met")));
            }

            Log.Debug("Sync attempt started (policy {0})", policy);

            IsSyncing = true;
            StatusChanged.Raise(this);

            try {
                var ret = await SynchronizeInner(token, policy);

                Log.Debug("Sync process terminated normally");
                Log.Event("Sync.terminate", new Dictionary <string, string>()
                {
                    { "policy", policy.ToString() }
                });

                if (ret.HasFailed)
                {
                    UserLog.Add(UserLog.Icon.Error, LogStrings.FileUploadFailure, ret.Error.Message);

                    SyncError.Raise(this, new SyncErrorEventArgs(ret.Error));
                }
                else
                {
                    if (ret.ChunksUploaded == 1)
                    {
                        UserLog.Add(LogStrings.FileUploadSummarySingular);
                    }
                    else
                    {
                        UserLog.Add(LogStrings.FileUploadSummaryPlural, ret.ChunksUploaded);
                    }
                }

                return(ret);
            }
            catch (TaskCanceledException) {
                Log.Debug("Sync process was canceled");
                return(new SyncResult(0, 0));
            }
            catch (Exception ex) {
                Log.Error(ex, "Sync process failed with unforeseen error");

                UserLog.Add(UserLog.Icon.Error, LogStrings.FileUploadFailure, ex.Message);

                return(new SyncResult(error: ex));
            }
            finally {
                IsSyncing = false;
                StatusChanged.Raise(this);
            }
        }