private void Save()
        {
            // TODO: show save file dialog
            // TODO: save
            var winSystem = new WpfWindowSystem();
            var filename  = winSystem.ShowSaveFileDialog("Экспорт настроек", "XML files|*.xml|All files|*.*");

            if (filename != null)
            {
                var doc         = new XDocument();
                var rootElement = new XElement("Settings");

                for (byte i = 0; i < 3; ++i)
                {
                    var curAinSettings = _ainSettingsStorageSettable.GetSettings(i);
                    if (curAinSettings != null)
                    {
                        var element = new XElement("AinSettings",
                                                   new XAttribute("Number", i),
                                                   new XElement("Reserved00", curAinSettings.Reserved00),
                                                   new XElement("AccDfDt", curAinSettings.AccDfDt.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Ain1LinkFault",
                                                                curAinSettings.Ain1LinkFault.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Ain2LinkFault",
                                                                curAinSettings.Ain2LinkFault.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Ain3LinkFault",
                                                                curAinSettings.Ain3LinkFault.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("DacCh", curAinSettings.DacCh.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("DecDfDt", curAinSettings.DecDfDt.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("DflLim", curAinSettings.DflLim.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("EmdecDfdt", curAinSettings.EmdecDfdt.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("FiMin", curAinSettings.FiMin.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("FiNom", curAinSettings.FiNom.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("FlMinMin", curAinSettings.FlMinMin.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Fmax", curAinSettings.Fmax.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Fmin", curAinSettings.Fmin.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Fnom", curAinSettings.Fnom.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Ia0", curAinSettings.Ia0.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Ib0", curAinSettings.Ib0.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Ic0", curAinSettings.Ic0.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("IdSetMax", curAinSettings.IdSetMax.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("IdSetMin", curAinSettings.IdSetMin.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Imax", curAinSettings.Imax.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Imcw", curAinSettings.Imcw.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("IoutMax", curAinSettings.IoutMax.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KiFi", curAinSettings.KiFi.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KiId", curAinSettings.KiId.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KiIq", curAinSettings.KiIq.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KiW", curAinSettings.KiW.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KpFi", curAinSettings.KpFi.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KpId", curAinSettings.KpId.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KpIq", curAinSettings.KpIq.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("KpW", curAinSettings.KpW.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Lm", curAinSettings.Lm.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Lrl", curAinSettings.Lrl.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Lsl", curAinSettings.Lsl.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Np", curAinSettings.Np.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("NimpFloorCode",
                                                                curAinSettings.NimpFloorCode.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("FanMode",
                                                                curAinSettings.FanMode.ToIoBits().ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("DirectCurrentMagnetization",
                                                                curAinSettings.DirectCurrentMagnetization.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Reserved24", curAinSettings.Reserved24),
                                                   new XElement("Reserved28", curAinSettings.Reserved28),
                                                   new XElement("Reserved32", curAinSettings.Reserved32),
                                                   new XElement("Reserved50", curAinSettings.Reserved50),
                                                   new XElement("Reserved51", curAinSettings.Reserved51),
                                                   new XElement("Rs", curAinSettings.Rs.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TauF", curAinSettings.TauF.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TauFSet", curAinSettings.TauFSet.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TauFi", curAinSettings.TauFi.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TauFlLim", curAinSettings.TauFlLim.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TauM", curAinSettings.TauM.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TauR", curAinSettings.TauR.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("TextMax", curAinSettings.TextMax.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("ToHl", curAinSettings.ToHl.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("UchMax", curAinSettings.UchMax),
                                                   new XElement("UchMin", curAinSettings.UchMin),
                                                   new XElement("Udc0", curAinSettings.Udc0.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("UdcMax", curAinSettings.UdcMax.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("UdcMin", curAinSettings.UdcMin.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("UmodThr", curAinSettings.UmodThr.ToString(CultureInfo.InvariantCulture)),
                                                   new XElement("Unom", curAinSettings.Unom.ToString(CultureInfo.InvariantCulture)));
                        rootElement.Add(element);
                    }
                }

                var engineSettings = _engineSettingsStorageSettable.EngineSettings;
                if (engineSettings != null)
                {
                    var engineSettingsElement = new XElement("EngineSettings",
                                                             new XElement("Inom", engineSettings.Inom.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Nnom", engineSettings.Nnom.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Nmax", engineSettings.Nmax.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Pnom", engineSettings.Pnom.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("CosFi", engineSettings.CosFi.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Eff", engineSettings.Eff.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Mass", engineSettings.Mass.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("MmM", engineSettings.MmM.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Height", engineSettings.Height.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("I2Tmax", engineSettings.I2Tmax.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("Icontinious", engineSettings.Icontinious.ToString(CultureInfo.InvariantCulture)),
                                                             new XElement("ZeroF", engineSettings.ZeroF.ToString(CultureInfo.InvariantCulture)));
                    rootElement.Add(engineSettingsElement);
                }

                doc.Add(rootElement);
                doc.Save(File.Open(filename, FileMode.Create, FileAccess.ReadWrite));
            }
        }
Пример #2
0
        public void ReadSettingsAsync(byte zeroBasedAinNumber, bool forceRead, Action <Exception, IAinSettings> callback)
        {
            if (forceRead == false)
            {
                // Берем настройки АИН из хранилища:
                var settings = _ainSettingsStorageSettable.GetSettings(zeroBasedAinNumber);
                if (settings != null)
                {
                    _notifyWorker.AddWork(() =>
                                          callback.Invoke(null,
                                                          settings)); // TODO: callback fail unknown, use try -> invoke, catch -> log
                    return;
                }
            }

            // Вычитываем настройки из порта:
            var sender = _commandSenderHost.Sender;

            if (sender == null)
            {
                throw new NullReferenceException("Порт передачи данных не открыт");
            }

            var readSettingsCmd = new ReadAinSettingsCommand(zeroBasedAinNumber);

            _logger.Log("Чтение настроек АИН " + (zeroBasedAinNumber + 1) + "...");
            _notifyWorker.AddWork(() => FireEventAinSettingsReadStarted(zeroBasedAinNumber));
            sender.SendCommandAsync(_targerAddressHost.TargetAddress, readSettingsCmd, _readSettingsTimeout, 2,
                                    (sendException, replyBytes) =>
            {
                if (sendException != null)
                {
                    var errorMessage = "Произошла ошибка во время чтения настрок АИН" + (zeroBasedAinNumber + 1);
                    _logger.Log(errorMessage);
                    try
                    {
                        var ex = new Exception(errorMessage, sendException);

                        _notifyWorker.AddWork(() => callback.Invoke(ex, null));
                        _notifyWorker.AddWork(() => FireEventAinSettingsReadComplete(zeroBasedAinNumber, ex, null));
                        _notifyWorker.AddWork(() =>
                                              _ainSettingsStorageSettable.SetSettings(zeroBasedAinNumber, null));
                    }
                    catch
                    {
                        _logger.Log(
                            "Не удалось совершить обратный вызов после неудачного чтения настроек (либо не удалось обnullить в хранилище) АИН" +
                            (zeroBasedAinNumber + 1));
                    }

                    return;
                }

                try
                {
                    var result = readSettingsCmd.GetResult(replyBytes);
                    if (zeroBasedAinNumber == 0 && result.Ain1LinkFault)
                    {
                        throw new Exception("Настройки АИН1 были считаны, однако флаг ошибки связи с АИН1 взведен");
                    }
                    if (zeroBasedAinNumber == 1 && result.Ain2LinkFault)
                    {
                        throw new Exception("Настройки АИН2 были считаны, однако флаг ошибки связи с АИН2 взведен");
                    }
                    if (zeroBasedAinNumber == 2 && result.Ain3LinkFault)
                    {
                        throw new Exception("Настройки АИН3 были считаны, однако флаг ошибки связи с АИН3 взведен");
                    }

                    try
                    {
                        _notifyWorker.AddWork(() => callback.Invoke(null, result));
                        _notifyWorker.AddWork(() =>
                                              FireEventAinSettingsReadComplete(zeroBasedAinNumber, null, result));
                        _notifyWorker.AddWork(() =>
                                              _ainSettingsStorageSettable.SetSettings(zeroBasedAinNumber, result));
                        _logger.Log("Настройки АИН " + (zeroBasedAinNumber + 1) + " успешно прочитаны");
                    }
                    catch
                    {
                        _logger.Log(
                            "Не удалось совершить обратный вызов после успешного чтения настроек (либо не удалось сохранить настройки в хранилище) АИН" +
                            (zeroBasedAinNumber + 1));
                    }
                }
                catch (Exception resultGetException)
                {
                    var errorMessage = "Ошибка во время разбора ответа на команду чтения настроек АИН" +
                                       (zeroBasedAinNumber + 1) + ": " + resultGetException.Message;
                    _logger.Log(errorMessage);
                    try
                    {
                        var ex = new Exception(errorMessage, resultGetException);
                        _notifyWorker.AddWork(() => callback.Invoke(ex, null));
                        _notifyWorker.AddWork(() => FireEventAinSettingsReadComplete(zeroBasedAinNumber, ex, null));
                        _notifyWorker.AddWork(() =>
                                              _ainSettingsStorageSettable.SetSettings(zeroBasedAinNumber, null));
                    }
                    catch
                    {
                        _logger.Log(
                            "Не удалось совершить обратный вызов после неудачного парсинга настроек (либо не удалось обnullить в хранилище) АИН " +
                            (zeroBasedAinNumber + 1));
                    }
                }
            });
        }