Esempio n. 1
0
        public WtmSettingsObject()
        {
            this.DisplayCurrency          = "USD";
            this.UseYahooRates            = true;
            this.SwitchPeriodCount        = 1;
            this.SwitchPeriod             = "Days";
            this.Average24                = true;
            this.AutoSwitch               = false;
            this.ApplicationMode          = "Standalone";
            this.ServerSettingsAreUpdated = true;
            this.ProxyPassword            = new SecureString();
            this.DynamicRequestTrigger    = 50;
            this.DynamicRequestInterval   = 750;

            var defaultTime = new ScheduleTime {
                Hour = 0, Minute = 0
            };

            this.SwitchTimeFrom  = defaultTime;
            this.SwitchTimeTo    = defaultTime;
            this.HistoryTimeFrom = defaultTime;
            this.HistoryTimeTo   = defaultTime;

            this.TcpPort = 3535;
        }
Esempio n. 2
0
        private void ResetScheduledJob(JobType jobType, string time = "")
        {
            bool            settingsCondition = false;
            string          registryKey       = string.Empty;
            Func <Task>     job            = null;
            ScheduleDayTime settingsTime   = null;
            int             randomInterval = 0;
            string          period         = string.Empty;

            switch (jobType)
            {
            case JobType.Switch:
                settingsCondition = WtmSettings.AutoSwitch;
                registryKey       = Constants.SwitchRegistryKey;
                job    = (async() => await SwitchTaskWrapper().ConfigureAwait(false));
                period = WtmSettings.SwitchPeriod.TrimEnd('s');
                if (period == "Day")
                {
                    settingsTime = new ScheduleDayTime(WtmSettings.SwitchPeriodCount, WtmSettings.SwitchTimeFrom);
                }
                if (period == "Hour")
                {
                    settingsTime = new ScheduleDayTime(0, WtmSettings.SwitchPeriodCount, WtmSettings.SwitchTimeFrom.Minute);
                }
                randomInterval = ScheduleTime.Difference(WtmSettings.SwitchTimeTo, WtmSettings.SwitchTimeFrom);
                break;

            case JobType.UpdatePriceHistory:
                settingsCondition = WtmSettings.BackupHistoricalPrices;
                registryKey       = Constants.UpdatePriceHistoryRegistryKey;
                job            = (async() => await UpdatePriceHistoryTaskWrapper().ConfigureAwait(false));
                period         = "Day";
                settingsTime   = new ScheduleDayTime(1, WtmSettings.HistoryTimeFrom);
                randomInterval = ScheduleTime.Difference(WtmSettings.HistoryTimeTo, WtmSettings.HistoryTimeFrom);
                break;
            }
            JobManager.RemoveJob(jobType.ToString());
            if (settingsCondition)
            {
                DateTime scheduleTime = new DateTime();
                if (time != string.Empty) // Schedule job at given time. This branch is used when there's a pending job in registry after reboot.
                {
                    var pendingTime = Convert.ToDateTime(time, DateTimeFormatInfo.InvariantInfo).ToUniversalTime();
                    if (pendingTime > DateTime.UtcNow)
                    {
                        scheduleTime = pendingTime;
                    }
                    else // Over-protection, should not actually happen?
                    {
                        SetRegistryKeyValue(registryKey, "Schedule", string.Empty);
                        ResetScheduledJob(jobType);
                        return;
                    }
                }
                else // Schedule job from current WtmSettings.SwitchTimeFrom. This branch is used when applying new settings.
                {
                    using (RegistryKey regKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(registryKey, true))
                    {
                        DateTime now     = DateTime.Now;
                        DateTime newTime = new DateTime();
                        switch (period)
                        {
                        case "Day":
                            newTime = new DateTime(now.Year, now.Month, now.Day, settingsTime.Hour, settingsTime.Minute, 0, DateTimeKind.Local);
                            if (newTime < now)
                            {
                                newTime = newTime.AddDays(settingsTime.Day);
                            }
                            break;

                        case "Hour":
                            newTime = new DateTime(now.Year, now.Month, now.Day, now.Hour, settingsTime.Minute, 0, DateTimeKind.Local);
                            if (newTime < now)
                            {
                                newTime = newTime.AddHours(settingsTime.Hour);
                            }
                            break;
                        }

                        scheduleTime = newTime.ToUniversalTime();
                        if (randomInterval > 0)
                        {
                            scheduleTime = scheduleTime.AddMinutes(Helpers.Random.Next(randomInterval));
                        }
                        regKey.SetValue("Schedule", scheduleTime.ToString("o"), RegistryValueKind.String);
                        if (jobType == JobType.Switch)
                        {
                            SwitchSchedule = scheduleTime.ToLocalTime();
                        }
                        if (jobType == JobType.UpdatePriceHistory)
                        {
                            HistoricalPricesSchedule = scheduleTime.ToLocalTime();
                        }
                    }
                }

                JobManager.AddJob(
                    async() =>
                {
                    await job().ConfigureAwait(false);
                    ResetScheduledJob(jobType);
                    if (WtmSettings.RestartComputer && RestartComputerPending)
                    {
                        Helpers.RestartComputer(5);
                    }
                },
                    (sch) => sch.WithName(jobType.ToString()).ToRunOnceAt(scheduleTime));
            }
            UpdateNextJobStatus();
        }
Esempio n. 3
0
        private async void WtmSettings_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Debug.WriteLine("222 WtmSettings_PropertyChanged " + e.PropertyName + " " + WtmSettings.SwitchTimeFrom.Hour + ":" + WtmSettings.SwitchTimeFrom.Minute + " " + +WtmSettings.SwitchTimeTo.Hour + ":" + WtmSettings.SwitchTimeTo.Minute);
            Debug.WriteLine("222 SwitchTimeEdit " + WtmSettings.SwitchTimeEdit);
            if (e.PropertyName == "SwitchPeriod")
            {
                SwitchTimeIsUpdated = false;
            }
            if (e.PropertyName == "SwitchTimeEdit")
            {
                if (WtmSettings.SwitchTimeEdit && SwitchTimeIsUpdated)
                {
                    SwitchTimeIsUpdated = false;
                }

                if (WtmSettings.SwitchTimeEdit == false)
                {
                    SwitchTimeUpdateIsInProgress = false;
                    return;
                }

                if (WtmSettings.SwitchTimeEdit && SwitchTimeUpdateIsInProgress)
                {
                    return;
                }

                if (WtmSettings.SwitchTimeEdit && SwitchTimeUpdateIsInProgress == false)
                {
                    Debug.WriteLine("!!! WtmSettings_PropertyChanged SaveUndoRedo" + " " + WtmSettings.SwitchTimeEdit + " Property: " + e.PropertyName);
                    Debug.WriteLine("SwitchTimeUpdateIsInProgress: " + SwitchTimeUpdateIsInProgress + " SwitchTimeIsUpdated: " + SwitchTimeIsUpdated);
                    SaveUndoRedo("WtmSettings");
                    SwitchTimeUpdateIsInProgress = true;
                    return;
                }
            }
            if (e.PropertyName == "HistoryTimeEdit")
            {
                if (WtmSettings.HistoryTimeEdit && HistoryTimeIsUpdated)
                {
                    HistoryTimeIsUpdated = false;
                }

                if (WtmSettings.HistoryTimeEdit == false)
                {
                    HistoryTimeUpdateIsInProgress = false;
                    return;
                }

                if (WtmSettings.HistoryTimeEdit && HistoryTimeUpdateIsInProgress)
                {
                    return;
                }

                if (WtmSettings.HistoryTimeEdit && HistoryTimeUpdateIsInProgress == false)
                {
                    Debug.WriteLine("!!! WtmSettings_PropertyChanged SaveUndoRedo" + " " + WtmSettings.HistoryTimeEdit);
                    Debug.WriteLine("HistoryTimeUpdateIsInProgress: " + HistoryTimeUpdateIsInProgress + " HistoryTimeIsUpdated: " + HistoryTimeIsUpdated);
                    SaveUndoRedo("WtmSettings");
                    HistoryTimeUpdateIsInProgress = true;
                    return;
                }
            }

            if (e.PropertyName == "StartWithWindows")
            {
                if (WtmSettings.StartWithWindows)
                {
                    string path = Helpers.ApplicationPath();
                    SetRegistryKeyValue(Constants.RunRegistryKey, Constants.AppName, path);
                }
                else
                {
                    DeleteRegistryKeyValue(Constants.RunRegistryKey, Constants.AppName);
                }
            }
            if (e.PropertyName == "AutoSwitch")
            {
                if (WtmSettings.AutoSwitch == false)
                {
                    ClearJob(JobType.Switch);
                    SwitchSchedule = new DateTime();
                    UpdateNextJobStatus();
                    WtmSwitchTimeFromStored = WtmSettings.SwitchTimeFrom.Clone();
                    WtmSwitchTimeToStored   = WtmSettings.SwitchTimeTo.Clone();
                    SwitchTimeIsUpdated     = true;
                }
                else
                {
                    WtmSwitchTimeFromStored = new ScheduleTime(-1, -1);
                    WtmSwitchTimeToStored   = new ScheduleTime(-1, -1);
                    SwitchTimeIsUpdated     = true;
                }
            }
            if (e.PropertyName == "BackupHistoricalPrices")
            {
                if (WtmSettings.BackupHistoricalPrices == false)
                {
                    ClearJob(JobType.UpdatePriceHistory);
                    HistoricalPricesSchedule = new DateTime();
                    UpdateNextJobStatus();
                    WtmHistoryTimeFromStored = WtmSettings.HistoryTimeFrom.Clone();
                    WtmHistoryTimeFromStored = WtmSettings.HistoryTimeTo.Clone();
                    HistoryTimeIsUpdated     = true;
                }
                else
                {
                    WtmHistoryTimeFromStored = new ScheduleTime(-1, -1);
                    WtmHistoryTimeFromStored = new ScheduleTime(-1, -1);
                    HistoryTimeIsUpdated     = true;
                }
            }
            if (e.PropertyName == "ProxyPassword")
            {
                WtmSettings.ProxyPasswordEncrypted = WtmSettings.ProxyPassword.Encrypt();
            }
            if (e.PropertyName == "Proxy" ||
                e.PropertyName == "UseProxy" ||
                e.PropertyName == "ProxyUserName" ||
                e.PropertyName == "ProxyPassword" ||
                e.PropertyName == "AnonymousProxy")
            {
                UpdateWtmHttpClient();
            }
            if (e.PropertyName == "ApplicationMode")
            {
                TestConnection.RaiseCanExecuteChanged();
                if (WtmSettings.ApplicationMode == "Standalone")
                {
                    WtmSettings.ServerSettingsAreUpdated = true;
                    if (WtmProxyServerServiceHost != null)
                    {
                        WtmProxyServerServiceHost.Close();
                    }
                }
                ApplyWtmSettingsAndSave();
            }
            if (e.PropertyName == "WtmRequestInterval")
            {
                WhatToMine.RequestInterval = WtmSettings.WtmRequestInterval;
            }
            if (e.PropertyName == "UseYahooRates")
            {
                SaveUndoIsEnabled = false;
                if (WtmSettings.UseYahooRates)
                {
                    await WtmSettings.GetYahooRates(true);
                }
                else
                {
                    WtmSettings.DefaulDisplayCurrencyList();
                }
                SaveUndoIsEnabled = true;
            }
        }
Esempio n. 4
0
 public ScheduleDayTime(int day, ScheduleTime scheduleTime)
 {
     this.Day    = day;
     this.Hour   = scheduleTime.Hour;
     this.Minute = scheduleTime.Minute;
 }