コード例 #1
0
        public async Task LoadProfilesAsync(bool isLatest)
        {
            lock (_locker)
            {
                if (IsLoading.Value)
                {
                    return;
                }

                IsLoading.TurnOn();
            }

            Debug.WriteLine("Load start!");

            try
            {
                var oldProfileIndices = Enumerable.Range(0, Profiles.Count).Reverse().ToList();                 // Reverse method is to start removing from the tail.
                var newProfiles       = new List <ProfileItem>();

                foreach (var newProfile in await _worker.GetProfilesAsync(isLatest, _loadingTimeoutDuration))
                {
                    var isExisting = false;

                    for (int index = 0; (index < Profiles.Count) && !isExisting; index++)
                    {
                        var oldProfile = Profiles[index];
                        if (!oldProfile.Id.Equals(newProfile.Id, StringComparison.Ordinal))
                        {
                            continue;
                        }

                        // Copy changeable values.
                        oldProfile.Position    = newProfile.Position;
                        oldProfile.IsAutomatic = newProfile.IsAutomatic;
                        oldProfile.Signal      = newProfile.Signal;
                        oldProfile.IsConnected = newProfile.IsConnected;

                        oldProfileIndices.Remove(index);
                        isExisting = true;
                    }

                    if (!isExisting)
                    {
                        newProfiles.Add(newProfile);
                    }
                }

                oldProfileIndices.ForEach(x => Profiles.RemoveAt(x));
                newProfiles.ForEach(x => Profiles.Add(x));

                // Calculate count of positions for each interface.
                Profiles
                .GroupBy(x => x.InterfaceId)
                .ToList()
                .ForEach(profilesGroup =>
                {
                    var count = profilesGroup.Count();

                    foreach (var profile in profilesGroup)
                    {
                        profile.PositionCount = count;
                    }
                });

                Debug.WriteLine(Profiles.Any()
                                        ? Profiles
                                .Select(x => $"Profile {x.Name} -> Position: {x.Position}, AutoConnection {x.IsAutomatic}, Signal: {x.Signal}, IsConnected {x.IsConnected}")
                                .Aggregate((work, next) => work + Environment.NewLine + next)
                                        : "No Profile");
            }
            finally
            {
                IsLoading.TurnOff();
            }
        }
コード例 #2
0
ファイル: MainController.cs プロジェクト: whidbey/Wifinian
        private async Task LoadProfilesBaseAsync()
        {
            var oldProfileIndices = Enumerable.Range(0, Profiles.Count).ToList();
            var newProfiles       = new List <ProfileItem>();

            foreach (var newProfile in await _worker.GetProfilesAsync())
            {
                var isExisting = false;

                foreach (int index in oldProfileIndices)
                {
                    var oldProfile = Profiles[index];
                    if (string.Equals(oldProfile.Id, newProfile.Id, StringComparison.Ordinal))
                    {
                        isExisting = true;
                        oldProfile.Copy(newProfile);
                        oldProfileIndices.Remove(index);
                        break;
                    }
                }

                if (!isExisting)
                {
                    newProfiles.Add(newProfile);
                }
            }

            if ((oldProfileIndices.Count > 0) || (newProfiles.Count > 0))
            {
                lock (_profilesLock)
                {
                    oldProfileIndices.Reverse();                     // Reverse indices to start removing from the tail.
                    oldProfileIndices.ForEach(x => Profiles.RemoveAt(x));
                    newProfiles.ForEach(x => Profiles.Add(x));
                }

                // Calculate count of positions for each interface.
                Profiles
                .GroupBy(x => x.InterfaceId)
                .ToList()
                .ForEach(subProfiles =>
                {
                    var count = subProfiles.Count();

                    foreach (var profile in subProfiles)
                    {
                        profile.PositionCount = count;
                    }
                });
            }

            Debug.WriteLine(Profiles.Any()
                                ? Profiles
                            .Select(x => $"Profile {x.Name} -> AutoConnect: {x.IsAutoConnectEnabled}, AutoSwitch: {x.IsAutoSwitchEnabled}, Position: {x.Position}, IsRadioOn: {x.IsRadioOn}, Signal: {x.Signal}, IsConnected {x.IsConnected}")
                            .Aggregate((work, next) => work + Environment.NewLine + next)
                                : "No Profile");

            if (EngagesPriority.Value)
            {
                var targetProfiles = Profiles
                                     .GroupBy(x => x.InterfaceId)
                                     .Where(subProfiles => subProfiles.All(x => !x.IsConnected || x.IsAutoSwitchEnabled))
                                     .Select(subProfiles => subProfiles
                                             .Where(x => x.IsAutoSwitchEnabled && (Settings.Current.SignalThreshold <= x.Signal))
                                             .OrderBy(x => x.Position)
                                             .FirstOrDefault())
                                     .Where(x => (x != null) && !x.IsConnected)
                                     .ToArray();

                if (targetProfiles.Length > 0)
                {
                    await Task.WhenAll(targetProfiles.Select(x => ConnectNetworkAsync(x, false)));
                }
            }
        }