예제 #1
0
        /// <summary>
        /// Starts the refresh of weather data in a background thread.
        /// </summary>
        /// <param name="cityToRefresh">City which should be refreshed.</param>
        private async Task StartBackgroundRefresh(City cityToRefresh)
        {
            // Avoid additional refreshs while one is active
            if (!NetworkConnectionTracker.IsNetworkConnected)
            {
                ServiceRegistration.Get <ILogger>().Debug("WeatherModel: Background refresh - No Network connected");
                return;
            }
            ServiceRegistration.Get <ILogger>().Debug("WeatherModel: Background refresh");
            lock (_syncObj)
            {
                if (IsUpdating)
                {
                    ServiceRegistration.Get <ILogger>().Warn("WeatherModel: Background refresh re-entrance detected");
                    return;
                }
                IsUpdating = true;
            }
            _updateFinished.Reset();
            try
            {
                bool result = await ServiceRegistration.Get <IWeatherCatcher>().GetLocationData(cityToRefresh).ConfigureAwait(false);

                ServiceRegistration.Get <ILogger>().Info(result ?
                                                         "WeatherModel: Loaded weather data for {0}, {1}" : "WeatherModel: Failed to load weather data for {0}, {1}",
                                                         cityToRefresh.Name, cityToRefresh.Id);

                // Copy the data to the skin property...
                if (cityToRefresh.Id.Equals(_preferredLocationCode))
                {
                    CurrentLocation.Copy(cityToRefresh);
                }

                // ... and save the last update time to settings
                SetLastUpdateTime(DateTime.Now);

                ServiceRegistration.Get <ILogger>().Debug("WeatherModel: Background refresh end");
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Warn("WeatherModel: Error refreshing city '{0}'", e, cityToRefresh);
            }
            finally
            {
                _updateFinished.Set();
                IsUpdating = false;
            }
        }
예제 #2
0
        protected async Task ReadSettings(bool updateCityChanged)
        {
            // Add citys from settings to the locations list
            var settingsManager = ServiceRegistration.Get <ISettingsManager>();
            var settings        = settingsManager.Load <WeatherSettings>();

            if (settings.LocationsList == null || settings.LocationsList.Count == 0)
            {
                CurrentLocation.Copy(City.NoData);
                return;
            }

            _preferredLocationCode = settings.LocationCode;
            // Only do further updates if city was changed
            if (updateCityChanged && _preferredLocationCode == CurrentLocation.Id)
            {
                return;
            }

            _refreshIntervalSec = settings.RefreshInterval;
            SetLastUpdateTime(settings.LastUpdate);

            _locations.Clear();
            _locationsList.Clear();
            City cityToSet = null;

            foreach (CitySetupInfo loc in settings.LocationsList)
            {
                City city = AddCityToLocations(loc);
                if (loc.Id == _preferredLocationCode)
                {
                    cityToSet = city;
                }
            }

            // if there is no city selected until yet, choose the first one
            if (cityToSet == null && settings.LocationCode.Equals("<none>") && _locations.Count > 0)
            {
                // Fetch data in background
                _preferredLocationCode = _locations[0].Id;
                cityToSet = _locations[0];
            }

            await StartBackgroundRefresh(cityToSet);

            // we've added new citys, so update the locations collection
            _locationsList.FireChange();
        }
예제 #3
0
        protected void ReadSettings(bool updateCityChanged)
        {
            // Add citys from settings to the locations list
            if (_settings.Settings.LocationsList == null || _settings.Settings.LocationsList.Count == 0)
            {
                CurrentLocation.Copy(City.NoData);
                return;
            }

            _preferredLocationCode = _settings.Settings.LocationCode;
            // Only do further updates if city was changed
            if (updateCityChanged && _preferredLocationCode == CurrentLocation.Id)
            {
                return;
            }

            _refreshIntervalSec = _settings.Settings.RefreshInterval;
            SetLastUpdateTime(_settings.Settings.LastUpdate);

            _locations.Clear();
            _locationsList.Clear();
            foreach (CitySetupInfo loc in _settings.Settings.LocationsList)
            {
                City city = AddCityToLocations(loc);
                if (loc.Id == _preferredLocationCode)
                {
                    StartBackgroundRefresh(city);
                }
            }

            // if there is no city selected until yet, choose the first one
            if (_settings.Settings.LocationCode.Equals("<none>") && _locations.Count > 0)
            {
                // Fetch data in background
                _preferredLocationCode = _locations[0].Id;
                StartBackgroundRefresh(_locations[0]);
            }

            // we've added new citys, so update the locations collection
            _locationsList.FireChange();
        }
예제 #4
0
        /// <summary>
        /// Updates the given location with new data.
        /// </summary>
        /// <param name="threadArgument">City which should be refreshed.</param>
        private void BackgroundRefresh(object threadArgument)
        {
            if (!NetworkConnectionTracker.IsNetworkConnected)
            {
                ServiceRegistration.Get <ILogger>().Debug("WeatherModel: Background refresh - No Network connected");
                return;
            }
            ServiceRegistration.Get <ILogger>().Debug("WeatherModel: Background refresh");
            _updateFinished.Reset();
            IncUpdateCount();
            try
            {
                City cityToRefresh = (City)threadArgument;

                bool result = ServiceRegistration.Get <IWeatherCatcher>().GetLocationData(cityToRefresh);

                ServiceRegistration.Get <ILogger>().Info(result ?
                                                         "WeatherModel: Loaded weather data for {0}, {1}" : "WeatherModel: Failed to load weather data for {0}, {1}",
                                                         cityToRefresh.Name, cityToRefresh.Id);

                // Copy the data to the skin property...
                if (cityToRefresh.Id.Equals(_preferredLocationCode))
                {
                    CurrentLocation.Copy(cityToRefresh);
                }

                // ... and save the last update time to settings
                SetLastUpdateTime(DateTime.Now);

                ServiceRegistration.Get <ILogger>().Debug("WeatherModel: Background refresh end");
            }
            catch (Exception e)
            {
                ServiceRegistration.Get <ILogger>().Warn("WeatherModel: Error refreshing city '{0}'", e, threadArgument);
            }
            finally
            {
                DecUpdateCount();
                _updateFinished.Set();
            }
        }
예제 #5
0
        private void UpdateUI(EDEvent edEvent)
        {
            //if (checkBoxSaveTelemetryFolder.Checked)
            //    SaveToFile(edEvent);

            if (checkBoxUpload.Checked)
            {
                UploadToServer(edEvent);
            }

            if (edEvent.Flags > 0)
            {
                _formFlagsWatcher?.UpdateFlags(edEvent.Flags);
            }

            if ((edEvent.PlanetRadius > 0) && (FormLocator.PlanetaryRadius != edEvent.PlanetRadius))
            {
                FormLocator.PlanetaryRadius = edEvent.PlanetRadius;
            }

            // Update the UI with the event data
            Action action;

            if (!_lastUpdateTime.Equals(edEvent.TimeStamp.ToString("HH:MM:ss")))
            {
                _lastUpdateTime = edEvent.TimeStamp.ToString("HH:MM:ss");
                action          = new Action(() => { labelLastUpdateTime.Text = _lastUpdateTime; });
                if (labelLastUpdateTime.InvokeRequired)
                {
                    labelLastUpdateTime.Invoke(action);
                }
                else
                {
                    action();
                }
            }

            _vehicleTelemetry?.ProcessEvent(edEvent, !checkBoxCaptureSRVTelemetry.Checked);
            if (edEvent.HasCoordinates())
            {
                if (checkBoxUseDirectionOfTravelAsHeading.Checked && (!checkBoxUseSmartHeadingOnlyWhenInSRV.Checked || edEvent.isInSRV()) && (!checkBoxUseSmartHeadingOnlyWhenOver.Checked || (SpeedInMS > (int)numericUpDownUseSmartHeadingOnlyWhenOver.Value)))
                {
                    // We ignore the heading given by E: D, as that is direction we are facing, not travelling
                    // We calculate our direction based on previous location
                    CurrentHeading = _vehicleTelemetry.CurrentHeading;
                }
                else
                {
                    CurrentHeading = edEvent.Heading;
                }

                PreviousLocation          = CurrentLocation.Copy();
                CurrentLocation.Latitude  = edEvent.Latitude;
                CurrentLocation.Longitude = edEvent.Longitude;
                CurrentLocation.Altitude  = edEvent.Altitude;
                if (!String.IsNullOrEmpty(edEvent.BodyName))
                {
                    CurrentLocation.PlanetName = edEvent.BodyName;
                }
                if (edEvent.PlanetRadius > 0)
                {
                    CurrentLocation.PlanetaryRadius = edEvent.PlanetRadius;
                }

                CommanderLocationChanged?.Invoke(null, null);
            }

            action = new Action(() => { labelLastUpdateTime.Text = DateTime.UtcNow.ToString("HH:mm:ss"); });
            if (labelLastUpdateTime.InvokeRequired)
            {
                labelLastUpdateTime.Invoke(action);
            }
            else
            {
                action();
            }
        }