public void Run()
        {
            // Running in a background thread, perform the web query which may take awhile.
            LogbookSettings settings = new LogbookSettings();

            settings.Load(logbook);

            if (settings.UserId.Length > 0 && settings.PublicKey.Length > 0)
            {
                IList <WithingsWebServiceProxy.MeasurementInfo> measurements = null;
                try
                {
                    measurements = WithingsWebServiceProxy.GetMeasurementsSinceLastUpdate(culture, settings.UserId, settings.PublicKey, settings.LastUpdate);
                }
                catch (Exception ex)
                {
                    measurements = null;
                    AddLogEntry(settings, "Could not read user measurements. " + ex.Message);
                }

                // Jump back to the UI thread to process the web response since it may cause data change events and UI updates.
                // TODO: Ugly way to get back on the UI thread via the ActiveView. Possibly a better solution in 3.0?
                IView activeView = Plugin.Instance.Application.ActiveView;
                while (activeView == null)
                {
                    Thread.Sleep(2000);
                    activeView = Plugin.Instance.Application.ActiveView;
                }
                activeView.CreatePageControl().BeginInvoke(new ProcessResponseCallback(ProcessResponse), new object[] { logbook, settings, measurements });
            }
        }
        public void Run()
        {
            // Running in a background thread, perform the web query which may take awhile.
            LogbookSettings settings = new LogbookSettings();
            settings.Load(logbook);

            if (settings.UserId.Length > 0 && settings.PublicKey.Length > 0)
            {
                IList<WithingsWebServiceProxy.MeasurementInfo> measurements = null;
                try
                {
                    measurements = WithingsWebServiceProxy.GetMeasurementsSinceLastUpdate(culture, settings.UserId, settings.PublicKey, settings.LastUpdate);
                }
                catch (Exception ex)
                {
                    measurements = null;
                    AddLogEntry(settings, "Could not read user measurements. " + ex.Message);
                }

                // Jump back to the UI thread to process the web response since it may cause data change events and UI updates.
                // TODO: Ugly way to get back on the UI thread via the ActiveView. Possibly a better solution in 3.0?
                IView activeView = Plugin.Instance.Application.ActiveView;
                while (activeView == null)
                {
                    Thread.Sleep(2000);
                    activeView = Plugin.Instance.Application.ActiveView;
                }
                activeView.CreatePageControl().BeginInvoke(new ProcessResponseCallback(ProcessResponse), new object[] { logbook, settings, measurements });
            }
        }
 private void AddLogEntry(LogbookSettings settings, string errortext)
 {
     settings.LastLogEntryDate = DateTime.Today;
     if (errortext.Length == 0)
     {
         settings.LastLogEntry = CommonResources.Text.ActionOk;
     }
     else
     {
         settings.LastLogEntry = errortext;
     }
 }
 private void ProcessResponse(ILogbook logbook, LogbookSettings settings, IList <WithingsWebServiceProxy.MeasurementInfo> measurements)
 {
     // Running in the UI thread.
     if (measurements != null)
     {
         MeasurementImporter.ImportMeasurements(logbook, settings, measurements);
         settings.LastUpdate = WithingsWebServiceProxy.GetNowEpoch();
         AddLogEntry(settings, "");
     }
     settings.Save(logbook);
     ExtendSettingsPages.RefreshSettings();
 }
        public ConfigureWizard()
        {
            InitializeComponent();

            Text = ResourceLookup.UI_ConfigureWizard_Title;

            logbook = Plugin.Instance.Application != null ? Plugin.Instance.Application.Logbook : null;
            settings = new LogbookSettings();
            if (logbook != null)
            {
                settings.Load(logbook);
            }

            accountInfoPage = new ConfigureWizard_Page_AccountInfo(this);
            selectUserPage = new ConfigureWizard_Page_SelectUser(this);
            importSettingsPage = new ConfigureWizard_Page_ImportSettings(this);
            importHistoryPage = new ConfigureWizard_Page_ImportHistory(this);

            Pages = new IWizardPage[] { accountInfoPage, selectUserPage, importSettingsPage, importHistoryPage };
        }
Esempio n. 6
0
        public ConfigureWizard()
        {
            InitializeComponent();

            Text = ResourceLookup.UI_ConfigureWizard_Title;

            logbook  = Plugin.Instance.Application != null ? Plugin.Instance.Application.Logbook : null;
            settings = new LogbookSettings();
            if (logbook != null)
            {
                settings.Load(logbook);
            }

            accountInfoPage    = new ConfigureWizard_Page_AccountInfo(this);
            selectUserPage     = new ConfigureWizard_Page_SelectUser(this);
            importSettingsPage = new ConfigureWizard_Page_ImportSettings(this);
            importHistoryPage  = new ConfigureWizard_Page_ImportHistory(this);

            Pages = new IWizardPage[] { accountInfoPage, selectUserPage, importSettingsPage, importHistoryPage };
        }
        public void RefreshControls()
        {
            settings = new LogbookSettings();
            ILogbook logbook = Plugin.Instance.Application != null ? Plugin.Instance.Application.Logbook : null;

            if (logbook != null)
            {
                settings.Load(logbook);
            }

            if (settings.PublicKey.Length == 0)
            {
                configureButton.Visible = true;
                tableLayoutPanel.RowStyles[0].Height = 60;
                settingsPanel.Visible = false;
            }
            else
            {
                configureButton.Visible = false;
                tableLayoutPanel.RowStyles[0].Height = 0;
                settingsPanel.SetSettings(logbook, settings);
                settingsPanel.Visible = true;
            }
        }
 public void SetSettings(ILogbook logbook, LogbookSettings settings)
 {
     this.logbook  = logbook;
     this.settings = settings;
     RefreshControls();
 }
 private void AddLogEntry(LogbookSettings settings, string errortext)
 {
     settings.LastLogEntryDate = DateTime.Today;
     if (errortext.Length == 0)
     {
         settings.LastLogEntry = CommonResources.Text.ActionOk;
     }
     else
     {
         settings.LastLogEntry = errortext;
     }
 }
 private void ProcessResponse(ILogbook logbook, LogbookSettings settings, IList<WithingsWebServiceProxy.MeasurementInfo> measurements)
 {
     // Running in the UI thread.
     if (measurements != null)
     {
         MeasurementImporter.ImportMeasurements(logbook, settings, measurements);
         settings.LastUpdate = WithingsWebServiceProxy.GetNowEpoch();
         AddLogEntry(settings, "");
     }
     settings.Save(logbook);
     ExtendSettingsPages.RefreshSettings();
 }
Esempio n. 11
0
        public static void ImportMeasurements(ILogbook logbook, LogbookSettings settings, IList <WithingsWebServiceProxy.MeasurementInfo> measurements)
        {
            List <WithingsWebServiceProxy.MeasurementInfo> orderedMeasurements = new List <WithingsWebServiceProxy.MeasurementInfo>(measurements);

            if (settings.ImportMultipleEntries == LogbookSettings.MultipleEntriesUpdateStyles.Latest)
            {
                orderedMeasurements.Sort(new OldestToNewestComparer());
            }
            else
            {
                orderedMeasurements.Sort(new NewestToOldestComparer());
            }

            foreach (WithingsWebServiceProxy.MeasurementInfo measurement in orderedMeasurements)
            {
                // Skip ambiguous readings unless setting is selected.
                if (!settings.ImportAmbiguousEntries)
                {
                    if (measurement.Source == WithingsWebServiceProxy.MeasurementInfo.SourceType.AmbiguousScaleReading)
                    {
                        continue;
                    }
                }

                // Always import height if settings is selected (even if manual).
                if (settings.ImportHeightEntries && !float.IsNaN(measurement.HeightMeters))
                {
                    logbook.Athlete.HeightCentimeters = measurement.HeightMeters * 100;
                }

                // Skip manual entries unless settings is selected.
                if (!settings.ImportManualEntries)
                {
                    if (measurement.Source == WithingsWebServiceProxy.MeasurementInfo.SourceType.ManualEntry ||
                        measurement.Source == WithingsWebServiceProxy.MeasurementInfo.SourceType.ProfileCreation)
                    {
                        continue;
                    }
                }

                IAthleteInfoEntry athleteInfoEntry = logbook.Athlete.InfoEntries.EntryForDate(measurement.Time.Date);
                try
                {
                    // Import weight
                    if (!float.IsNaN(measurement.WeightKilograms) && measurement.WeightKilograms > 0)
                    {
                        //System.Diagnostics.Trace.WriteLine("Weight entry on " + measurement.Time.ToLocalTime().ToShortDateString() + " " + measurement.Time.ToLocalTime().ToShortTimeString() + "=" + measurement.WeightKilograms);
                        float actualKilograms  = measurement.WeightKilograms;
                        float roundedKilograms = actualKilograms;
                        switch (Plugin.Instance.Application.SystemPreferences.WeightUnits)
                        {
                        case Weight.Units.Kilogram:
                            roundedKilograms = (float)Math.Round(actualKilograms + 0.005, 1);
                            break;

                        case Weight.Units.Pound:
                        case Weight.Units.Stone:
                            double pounds = Weight.Convert(actualKilograms + 0.005, Weight.Units.Kilogram, Weight.Units.Pound);
                            pounds           = Math.Round(pounds, 1);
                            roundedKilograms = (float)Weight.Convert(pounds, Weight.Units.Pound, Weight.Units.Kilogram);
                            break;
                        }

                        bool update = false;
                        if (float.IsNaN(athleteInfoEntry.WeightKilograms))
                        {
                            update = true;
                        }
                        else
                        {
                            switch (settings.ImportMultipleEntries)
                            {
                            case LogbookSettings.MultipleEntriesUpdateStyles.Earliest:
                            case LogbookSettings.MultipleEntriesUpdateStyles.Latest:
                                update = true;
                                break;

                            case LogbookSettings.MultipleEntriesUpdateStyles.Lowest:
                                if (roundedKilograms < athleteInfoEntry.WeightKilograms)
                                {
                                    update = true;
                                }
                                break;

                            case LogbookSettings.MultipleEntriesUpdateStyles.Highest:
                                if (roundedKilograms > athleteInfoEntry.WeightKilograms)
                                {
                                    update = true;
                                }
                                break;
                            }
                        }

                        if (update)
                        {
                            //System.Diagnostics.Trace.WriteLine("Weight updated to " + roundedKilograms + " kilograms. actual=" + actualKilograms);
                            athleteInfoEntry.WeightKilograms = roundedKilograms;

                            if (settings.UpdateBMI)
                            {
                                if (!float.IsNaN(logbook.Athlete.HeightCentimeters) && logbook.Athlete.HeightCentimeters > 0)
                                {
                                    float meters = logbook.Athlete.HeightCentimeters / 100;
                                    athleteInfoEntry.BMI = (float)Math.Round(actualKilograms / (meters * meters), 1);
                                    //System.Diagnostics.Trace.WriteLine("BMI updated to " + athleteInfoEntry.BMI);
                                }
                            }
                        }
                    }

                    // Import percent fat
                    if (!float.IsNaN(measurement.PercentFat) && measurement.PercentFat > 0 && measurement.PercentFat < 100)
                    {
                        //System.Diagnostics.Trace.WriteLine("Percent Fat entry on " + measurement.Time.ToLocalTime().ToShortDateString() + " " + measurement.Time.ToLocalTime().ToShortTimeString() + "=" + measurement.PercentFat);
                        float percent = measurement.PercentFat;
                        bool  update  = false;
                        if (float.IsNaN(athleteInfoEntry.BodyFatPercentage))
                        {
                            update = true;
                        }
                        else
                        {
                            switch (settings.ImportMultipleEntries)
                            {
                            case LogbookSettings.MultipleEntriesUpdateStyles.Earliest:
                            case LogbookSettings.MultipleEntriesUpdateStyles.Latest:
                                update = true;
                                break;

                            case LogbookSettings.MultipleEntriesUpdateStyles.Lowest:
                                if (percent < athleteInfoEntry.BodyFatPercentage)
                                {
                                    update = true;
                                }
                                break;

                            case LogbookSettings.MultipleEntriesUpdateStyles.Highest:
                                if (percent > athleteInfoEntry.BodyFatPercentage)
                                {
                                    update = true;
                                }
                                break;
                            }
                        }
                        if (update)
                        {
                            percent = (float)Math.Round(percent + 0.005, 1);
                            //System.Diagnostics.Trace.WriteLine("Body fat % updated to " + percent + "%");
                            athleteInfoEntry.BodyFatPercentage = percent;
                        }
                    }
                }
                catch { }
            }
        }
        public void RefreshControls()
        {
            settings = new LogbookSettings();
            ILogbook logbook = Plugin.Instance.Application != null ? Plugin.Instance.Application.Logbook : null;
            if (logbook != null)
            {
                settings.Load(logbook);
            }

            if (settings.PublicKey.Length == 0)
            {
                configureButton.Visible = true;
                tableLayoutPanel.RowStyles[0].Height = 60;
                settingsPanel.Visible = false;
            }
            else
            {
                configureButton.Visible = false;
                tableLayoutPanel.RowStyles[0].Height = 0;
                settingsPanel.SetSettings(logbook, settings);
                settingsPanel.Visible = true;
            }
        }