public GetEnergyControl(IList <ListPoint> source, Action closeed, Action canceled = null, Action <Exception> faulted = null) : this()
        {
            if (source == null || closeed == null)
            {
                throw new ArgumentNullException();
            }

            _onClosed   = closeed;
            _onCanceled = canceled;
            _onFaulted  = faulted;

            List <ListPointWithResult> list = null;

            Dialogs.IDialog dialog = _window.DialogWaitingScreen("Подготовка ...");
            dialog.Show();
            System.Threading.ThreadPool.QueueUserWorkItem(o =>
            {
                var _s = source
                         .Flatten(i => i.Items)
                         .Where(i => i.TypeCode == "SUBSTATION")
                         .OrderBy(i => i.ParentName)
                         .ThenBy(i => i.Name)
                         .ToList <ListPoint>();

                list = new List <ListPointWithResult>();
                foreach (var point in _s)
                {
                    ListPointWithResult pwr = new ListPointWithResult(point)
                    {
                        Items = null
                    };
                    var items = point.Items
                                .Flatten(i => i.Items)
                                .Where(i =>
                                       (i.ParentTypeCode == "AUXILIARY" && i.TypeCode == "ELECTRICITY" && i.EсpName == "Свои нужды") ||
                                       (i.ParentTypeCode == "SECTIONBUS" && (i.ParentName.Contains("10кВ") || i.ParentName.Contains("6кВ")) &&
                                        i.TypeCode == "ELECTRICITY" && i.EсpName == "Трансформаторы"))
                                .Select(i => new ListPointWithResult(i)
                    {
                        ParentName = pwr.ParentName, ParentTypeCode = pwr.Name
                    })
                                .ToList();

                    pwr.Items = items;

                    list.Add(pwr);
                    foreach (var item in items)
                    {
                        list.Add(item);
                    }
                }

                if (list == null || list.Count == 0)
                {
                    _window.ShowDialogWarning(Strings.EmptyList);
                    dialog.Close();
                    _onClosed();
                }
                List = new ObservableCollection <ListPointWithResult>(list);
                dialog.Close();
            });
        }
        private void InitCommands()
        {
            App.Log("Иницилизация команд главного окна");
            SettingsCommand = new DelegateCommand(o =>
            {
                App.Log("Просмотр настроек");
                Dialogs.IDialog dialog = null;
                Action updateUI        = () =>
                                         dialog.Close();
                var control = new SettingsControl(updateUI);
                dialog      = this.DialogCustom(control);
                dialog.Show();
            });

            UpdateCommand = new DelegateCommand(o =>
            {
                cts = new CancellationTokenSource();

                Dialogs.IDialog dialog = this.DialogInfo(Strings.UpdatingInProgress, null, MessageBoxImage.None, TMPApplication.WpfDialogs.DialogMode.Cancel);
                dialog.CloseBehavior   = TMPApplication.WpfDialogs.DialogCloseBehavior.ExplicitClose;
                dialog.Cancel          = () =>
                {
                    cts.Cancel();
                    dialog.CanCancel = false;
                };
                dialog.Show();

                if (CheckAvailability() == false)
                {
                    this.ShowDialogWarning(Strings.MessageServerNotAvailability, null, () => dialog.Close());
                    return;
                }
                try
                {
                    this.ShowDialogInfo(Strings.OnUpdatingPointsList, null, () =>
                    {
                        App.Log("Получение списка точек от сервиса");
                        _rootEmcosGroup = null;
                        try
                        {
                            _rootEmcosGroup = Utils.Base64StringToObject <ListPoint>(Properties.Settings.Default.RootPoint);
                        }
                        catch
                        { }
                        if (_rootEmcosGroup == null)
                        {
                            _rootEmcosGroup = DEFAULT_ROOT_EMCOS_GROUP;
                        }

                        _backgroudTask = Task.Factory.StartNew(async() =>
                        {
                            _index = 0;

                            var source = await FillPointsTree(_rootEmcosGroup);
                            if (source.Count == 0 && String.IsNullOrEmpty(ServiceHelper.ErrorMessage) == false)
                            {
                                this.ShowDialogWarning(ServiceHelper.ErrorMessage);
                                dialog.Close();
                                return;
                            }
                            InitModel(source);
                            IsReadyToGetData = true;
                            SaveList(source);
                            dialog.Close();
                        }, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
                    });
                }
                catch (Exception ex)
                {
                    App.Log("Получение списка точек от сервиса - ошибка");
                    dialog.Close();
                    this.ShowDialogError(String.Format(Strings.Error, App.GetExceptionDetails(ex)));
                }
            });
            GetReportCommand = new DelegateCommand(o =>
            {
                this.ShowDialogInfo(Strings.OnGettingData, null, () =>
                {
                    App.Log("Получение отчёта по точкам");

                    if (CheckAvailability() == false)
                    {
                        this.ShowDialogWarning(Strings.MessageServerNotAvailability);
                        return;
                    }

                    var substations = _treeModel.Point.Items
                                      .Flatten(i => i.Items)
                                      .Where(i => (i.TypeCode == "SUBSTATION" || i.TypeCode == "VOLTAGE") && i.IsChecked)
                                      .OrderBy(i => i.ParentName)
                                      .ThenBy(i => i.Name)
                                      .ToList <ListPoint>();
                    if (substations == null || substations.Count == 0)
                    {
                        this.ShowDialogWarning(Strings.EmptyList);
                        return;
                    }

                    Dialogs.IDialog dialog = null;

                    btnPanel.IsEnabled = false;
                    try
                    {
                        App.Current.MainWindow.TaskbarItemInfo = new System.Windows.Shell.TaskbarItemInfo()
                        {
                            Description   = Strings.GetDataHeader,
                            ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal,
                            ProgressValue = 0.01
                        };
                        Action updateUI = () =>
                        {
                            btnPanel.IsEnabled = true;
                            dialog.Close();
                        };
                        Action completed = () =>
                        {
                            App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
                            updateUI();
                        };
                        Action canceled = () =>
                        {
                            App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Paused;
                            this.ShowDialogInfo(Strings.Canceled);
                            updateUI();
                        };
                        Action <Exception> faulted = (Exception e) =>
                        {
                            App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Error;
                            this.ShowDialogError(App.GetExceptionDetails(e));
                            updateUI();
                        };

                        dialog = this.DialogCustom(new GetDataControl(substations, completed, canceled, faulted));
                        dialog.Show();
                    }
                    catch (Exception ex)
                    {
                        App.Log("Получение отчётов - ошибка");
                        this.ShowDialogError(App.GetExceptionDetails(ex), null,
                                             () => TMPApplication.DispatcherExtensions.InUi(() => btnPanel.IsEnabled = true));
                        dialog.Close();
                    }
                });
            },
                                                   o => IsReadyToGetData);
            GetEnergyCommand = new DelegateCommand(o =>
            {
                this.ShowDialogInfo(Strings.OnGettingData, null, () =>
                {
                    App.Log("Получение суточных значений");
                    if (CheckAvailability() == false)
                    {
                        this.ShowDialogWarning(Strings.MessageServerNotAvailability);
                        return;
                    }

                    Dialogs.IDialog dialog = null;

                    btnPanel.IsEnabled = false;
                    try
                    {
                        App.Current.MainWindow.TaskbarItemInfo               = new System.Windows.Shell.TaskbarItemInfo();
                        App.Current.MainWindow.TaskbarItemInfo.Description   = Strings.GetDataHeader;
                        App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Normal;
                        App.Current.MainWindow.TaskbarItemInfo.ProgressValue = 0.01;

                        Action updateUI = () =>
                        {
                            TMPApplication.DispatcherExtensions.InUi(() => btnPanel.IsEnabled = true);
                            dialog.Close();
                        };
                        Action completed = () =>
                        {
                            App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
                            updateUI();
                        };
                        Action canceled = () =>
                        {
                            App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Paused;
                            this.ShowDialogInfo(Strings.Canceled, Strings.Message, null);
                            updateUI();
                        };
                        Action <Exception> faulted = (Exception e) =>
                        {
                            App.Current.MainWindow.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.Error;

                            this.ShowDialogError(App.GetExceptionDetails(e));
                            updateUI();
                        };

                        dialog = this.DialogCustom(new GetEnergyControl(_treeModel.Point.Items, completed, canceled, faulted));
                        dialog.Show();
                    }
                    catch (Exception ex)
                    {
                        App.Log("Получение суточных значений - ошибка");
                        this.ShowDialogError(App.GetExceptionDetails(ex), null,
                                             () => TMPApplication.DispatcherExtensions.InUi(() => btnPanel.IsEnabled = true));
                        dialog.Close();
                    }
                });
            },
                                                   o => IsReadyToGetData);

            TreeCheckOrUncheckItemsCommand = new DelegateCommand(
                o =>
            {
                ICSharpCode.TreeView.SharpTreeView tree = o as ICSharpCode.TreeView.SharpTreeView;
                if (tree != null)
                {
                    ListPoint point = (ListPoint)tree.SelectedItem;
                    if (point != null && point.Items != null)
                    {
                        foreach (var child in point.Items)
                        {
                            if (child.TypeCode == "FES" || child.TypeCode == "RES" || child.TypeCode == "SUBSTATION" || child.TypeCode == "VOLTAGE")
                            {
                                child.IsChecked = !child.IsChecked;
                            }
                        }
                    }
                }
            },
                o => _treeModel != null && _treeModel.Children != null && _treeModel.Children.Count > 0);
            TreeUnselectAllCommand = new DelegateCommand(
                o =>
            {
                if (_treeModel != null)
                {
                    foreach (var item in _treeModel.Point.Items)
                    {
                        ForEachPointInTree(item, p => true, p => p.IsChecked = false);
                    }
                }
            },
                o => _treeModel != null && _treeModel.Children != null && _treeModel.Children.Count > 0);
            TreeSelectAllCommand = new DelegateCommand(
                o =>
            {
                if (_treeModel != null)
                {
                    foreach (var item in _treeModel.Point.Items)
                    {
                        ForEachPointInTree(item, p => p.TypeCode == "SUBSTATION" || p.TypeCode == "VOLTAGE", p => p.IsChecked = true);
                    }
                }
            },
                o => _treeModel != null && _treeModel.Children != null && _treeModel.Children.Count > 0);
        }