Beispiel #1
0
        private void App_OnStartup(object sender, StartupEventArgs e)
        {
            var colorsForGraphics = new List <Color>
            {
                Colors.LawnGreen,
                Colors.Red,
                Colors.Cyan,
                Colors.Yellow,
                Colors.Coral,
                Colors.LightGreen,
                Colors.HotPink,
                Colors.DeepSkyBlue,
                Colors.Gold,
                Colors.Orange,
                Colors.Violet,
                Colors.White,
                Colors.Fuchsia,
                Colors.LightSkyBlue,
                Colors.LightGray,
                Colors.Khaki,
                Colors.SpringGreen,
                Colors.Tomato,
                Colors.LightCyan,
                Colors.Goldenrod,
                Colors.SlateBlue,
                Colors.Cornsilk,
                Colors.MediumPurple,
                Colors.RoyalBlue,
                Colors.MediumVioletRed,
                Colors.MediumTurquoise
            };

            _debugLogger = new RelayMultiLoggerWithStackTraceSimple(
                new RelayLoggerWithStackTrace(
                    new RelayActionLogger(s => { }),
                    new StackTraceFormatterWithNullSuport(" > ", "[NO STACK INFO]")),
                new RelayLoggerWithStackTrace(
                    new RelayLogger(
                        new ColoredConsoleLogger(ConsoleColor.Red, ConsoleColor.Black),
                        new ChainedFormatter(new List <ITextFormatter>
            {
                new ThreadFormatter(" > ", true, false, false),
                new DateTimeFormatter(" > ")
            })),
                    new StackTraceFormatterWithNullSuport(" > ", "[NO STACK INFO]")),
                new RelayLoggerWithStackTrace(
                    new RelayLogger(
                        new ColoredConsoleLogger(ConsoleColor.Yellow, ConsoleColor.Black),
                        new ChainedFormatter(new List <ITextFormatter>
            {
                new ThreadFormatter(" > ", true, false, false),
                new DateTimeFormatter(" > ")
            })),
                    new StackTraceFormatterWithNullSuport(" > ", "[NO STACK INFO]")),
                new RelayLoggerWithStackTrace(
                    new RelayLogger(
                        new ColoredConsoleLogger(ConsoleColor.DarkCyan, ConsoleColor.Black),
                        new ChainedFormatter(new List <ITextFormatter>
            {
                new ThreadFormatter(" > ", true, false, false),
                new DateTimeFormatter(" > ")
            })),
                    new StackTraceFormatterNothing()),
                new RelayLoggerWithStackTrace(
                    new RelayLogger(
                        new ColoredConsoleLogger(ConsoleColor.Cyan, ConsoleColor.Black),
                        new ChainedFormatter(new List <ITextFormatter>
            {
                new ThreadFormatter(" > ", true, false, false),
                new DateTimeFormatter(" > ")
            })),
                    new StackTraceFormatterNothing()),
                new RelayLoggerWithStackTrace(
                    new RelayLogger(
                        new ColoredConsoleLogger(ConsoleColor.Green, ConsoleColor.Black),
                        new ChainedFormatter(new List <ITextFormatter>
            {
                new ThreadFormatter(" > ", false, true, false), new DateTimeFormatter(" > ")
            })),
                    new StackTraceFormatterWithNullSuport(" > ", string.Empty)),
                new RelayLoggerWithStackTrace(
                    new RelayLogger(
                        new ColoredConsoleLogger(ConsoleColor.White, ConsoleColor.Black),
                        new ChainedFormatter(new List <ITextFormatter>
            {
                new ThreadFormatter(" > ", true, false, false), new DateTimeFormatter(" > ")
            })),
                    new StackTraceFormatterNothing()));

            var loggerAndRegPoint = new LoggerRegistrationPointThreadSafe();

            _commonLogger   = loggerAndRegPoint;
            _loggerRegPoint = loggerAndRegPoint;

            var paramLoggerAndRegPoint = new ParamLoggerRegistrationPointThreadSafe();

            _commonParamLogger   = paramLoggerAndRegPoint;
            _paramLoggerRegPoint = paramLoggerAndRegPoint;


            var cmdSenderHost = new CommandSenderHostThreadSafe();

            _cmdSenderHostSettable = cmdSenderHost;
            _cmdSenderHost         = cmdSenderHost;

            var targetAddressHost = new TargetAddressHostThreadSafe(1);

            _targetAddressHostSettable = targetAddressHost;
            _targetAddressHost         = targetAddressHost;

            var notifySendingEnabled = new NotifySendingEnabledThreadSafe(false);

            _notifySendingEnabledRaisable = notifySendingEnabled;
            _notifySendingEnabled         = notifySendingEnabled;

            var ainsCounter = new AinsCounterThreadSafe(1);

            _ainsCounterRaisable = ainsCounter;
            _ainsCounter         = ainsCounter;

            _cycleThreadHolder = new CycleThreadHolderThreadSafe();

            var ainSettingsStorage = new AinSettingsStorageThreadSafe(_ainsCounter, _notifySendingEnabled);

            _ainSettingsStorage              = ainSettingsStorage;
            _ainSettingsStorageSettable      = ainSettingsStorage;
            _ainSettingsStorageUpdatedNotify = ainSettingsStorage;

            var ainSettingsReader = new AinSettingsReader(_cmdSenderHost, _targetAddressHost, _commonLogger,
                                                          _ainSettingsStorageSettable, _debugLogger);

            _ainSettingsReader             = ainSettingsReader;
            _ainSettingsReadNotify         = ainSettingsReader;
            _ainSettingsReadNotifyRaisable = ainSettingsReader;

            _ainSettingsWriter = new AinSettingsWriter(_cmdSenderHost, _targetAddressHost, _ainsCounterRaisable,
                                                       _ainSettingsReader);
            _autoTimeSetter =
                new AutoTimeSetter(_cmdSenderHost, _notifySendingEnabled, _targetAddressHost, _commonLogger);


            var engineSettingsStorage = new EngineSettingsStorageThreadSafe();

            _engineSettingsStorage              = engineSettingsStorage;
            _engineSettingsStorageSettable      = engineSettingsStorage;
            _engineSettingsStorageUpdatedNotify = engineSettingsStorage;

            var engineSettingsReader = new EngineSettingsReader(_cmdSenderHost, _targetAddressHost, _commonLogger,
                                                                _engineSettingsStorageSettable, _debugLogger);

            _engineSettingsReader             = engineSettingsReader;
            _engineSettingsReadNotify         = engineSettingsReader;
            _engineSettingsReadNotifyRaisable = engineSettingsReader;

            _engineSettingsWriter = new EngineSettingsWriter(_cmdSenderHost, _targetAddressHost, _engineSettingsReader);


            _autoSettingsReader = new AutoSettingsReader(
                _notifySendingEnabled,
                _ainsCounterRaisable,
                _ainSettingsReader,
                _ainSettingsStorageSettable,
                _commonLogger,
                _engineSettingsReader,
                _engineSettingsStorageSettable);



            _bsEthernetLogsReadCycleModel = new ReadCycleModel(_cmdSenderHost, targetAddressHost, notifySendingEnabled);

            var uiRoot = new SimpleUiRoot(new WpfUiNotifierAsync(Dispatcher.CurrentDispatcher));

            var ainCommandAndCommonTelemetryVm = new AinCommandAndCommonTelemetryViewModel(
                new AinCommandAndMinimalCommonTelemetryViewModel(
                    _cmdSenderHost,
                    _targetAddressHost,
                    uiRoot,
                    _commonLogger,
                    _notifySendingEnabled,
                    0,
                    _ainSettingsStorage,
                    _ainSettingsStorageUpdatedNotify),
                new TelemetryCommonViewModel(),
                _cmdSenderHost,
                _targetAddressHost,
                uiRoot,
                _notifySendingEnabled);


            // TODO: Register something but not VM?
            _cycleThreadHolder.RegisterAsCyclePart(ainCommandAndCommonTelemetryVm);


            new CommandWindow {
                DataContext = new CommandWindowViewModel(ainCommandAndCommonTelemetryVm)
            }.Show();

            new BsEthernetLogsWindow {
                DataContext = new WindowViewModel(uiRoot, _bsEthernetLogsReadCycleModel)
            }.Show();



            var mainViewModel = new MainViewModel(
                new SimpleUiRoot(new WpfUiNotifierAsync(Dispatcher.CurrentDispatcher)),
                new WpfWindowSystem(),
                colorsForGraphics,
                _cmdSenderHostSettable,
                _targetAddressHost,
                _debugLogger,
                _loggerRegPoint,
                _notifySendingEnabledRaisable,
                _commonParamLogger,
                _ainsCounterRaisable,
                _cycleThreadHolder,
                _ainSettingsReader,
                _ainSettingsReadNotify,
                _ainSettingsReadNotifyRaisable,
                _ainSettingsWriter, _ainSettingsStorage, _ainSettingsStorageSettable,
                _ainSettingsStorageUpdatedNotify, _bsEthernetLogsReadCycleModel,
                _engineSettingsReader,
                _engineSettingsWriter,
                _engineSettingsReadNotify,
                _engineSettingsReadNotifyRaisable,
                _engineSettingsStorage,
                _engineSettingsStorageSettable,
                _engineSettingsStorageUpdatedNotify);

            new MainWindow {
                DataContext = mainViewModel
            }.Show();
        }
        public MainViewModel(IUserInterfaceRoot uiRoot, IWindowSystem windowSystem, List <Color> colors,
                             ICommandSenderHostSettable commandSenderHostSettable, ITargetAddressHost targetAddressHost,
                             IMultiLoggerWithStackTrace <int> debugLogger, ILoggerRegistrationPoint loggerRegistrationPoint,
                             INotifySendingEnabledRaisable notifySendingEnabled, IParameterLogger paramLogger,
                             IAinsCounterRaisable ainsCounterRaisable,
                             ICycleThreadHolder cycleThreadHolder,
                             IAinSettingsReader ainSettingsReader, IAinSettingsReadNotify ainSettingsReadNotify,
                             IAinSettingsReadNotifyRaisable ainSettingsReadNotifyRaisable, IAinSettingsWriter ainSettingsWriter,
                             IAinSettingsStorage ainSettingsStorage, IAinSettingsStorageSettable ainSettingsStorageSettable,
                             IAinSettingsStorageUpdatedNotify storageUpdatedNotify,
                             ReadCycleModel bsEthernetReadCycleModel,
                             IEngineSettingsReader engineSettingsReader,
                             IEngineSettingsWriter engineSettingsWriter,
                             IEngineSettingsReadNotify engineSettingsReadNotify,
                             IEngineSettingsReadNotifyRaisable engineSettingsReadNotifyRaisable,
                             IEngineSettingsStorage engineSettingsStorage,
                             IEngineSettingsStorageSettable engineSettingsStorageSettable,
                             IEngineSettingsStorageUpdatedNotify engineSettingsStorageUpdatedNotify)
        {
            _uiRoot = uiRoot;
            _colors = colors;

            _commandSenderHostSettable = commandSenderHostSettable;
            _commandSenderHost         = commandSenderHostSettable;
            _targetAddressHost         = targetAddressHost;

            _isPortOpened = false;

            // Лог программы:
            _debugLogger             = debugLogger;
            _loggerRegistrationPoint = loggerRegistrationPoint;

            // разрешение к отправке (COM-порт открыт/закрыт)
            _notifySendingEnabled = notifySendingEnabled;

            ProgramLogVm = new ProgramLogViewModel(_uiRoot, _debugLogger, new DateTimeFormatter(" > "));
            _logger      = new RelayLogger(ProgramLogVm);
            _loggerRegistrationPoint.RegisterLoggegr(_logger);

            GetPortsAvailable();

            OpenPortCommand          = new RelayCommand(OpenPort, () => !_isPortOpened);
            ClosePortCommand         = new RelayCommand(ClosePort, () => _isPortOpened);
            GetPortsAvailableCommand = new RelayCommand(GetPortsAvailable);

            _paramLogger = paramLogger;


            _ainsCounterRaisable   = ainsCounterRaisable;
            _cycleThreadHolder     = cycleThreadHolder;
            _ainSettingsReader     = ainSettingsReader;
            _ainSettingsReadNotify = ainSettingsReadNotify;
            _ainSettingsWriter     = ainSettingsWriter;

            // Блоки АИН в системе:
            AinsCountInSystem = new List <int> {
                1, 2, 3
            };
            SelectedAinsCount = AinsCountInSystem.First();

            var ainSettingsReadedWriter = new AinSettingsReaderWriter(_ainSettingsReader, _ainSettingsWriter);

            _engineSettingsReader               = engineSettingsReader;
            _engineSettingsWriter               = engineSettingsWriter;
            _engineSettingsReadNotify           = engineSettingsReadNotify;
            _engineSettingsReadNotifyRaisable   = engineSettingsReadNotifyRaisable;
            _engineSettingsStorage              = engineSettingsStorage;
            _engineSettingsStorageSettable      = engineSettingsStorageSettable;
            _engineSettingsStorageUpdatedNotify = engineSettingsStorageUpdatedNotify;


            AinCommandAndCommonTelemetryVm = new AinCommandAndCommonTelemetryViewModel(
                new AinCommandAndMinimalCommonTelemetryViewModel(_commandSenderHost, _targetAddressHost, _uiRoot,
                                                                 _logger, _notifySendingEnabled, 0, ainSettingsStorage, storageUpdatedNotify),
                new TelemetryCommonViewModel(), _commandSenderHost, _targetAddressHost, _uiRoot, _notifySendingEnabled);

            _cycleThreadHolder.RegisterAsCyclePart(AinCommandAndCommonTelemetryVm);

            TelemtryVm = new TelemetryViewModel(_uiRoot, _commandSenderHost, _targetAddressHost, _logger,
                                                _cycleThreadHolder, _ainsCounterRaisable, _paramLogger, _notifySendingEnabled);

            SettingsVm = new SettingsViewModel(_uiRoot, _logger,
                                               ainSettingsReadedWriter, _ainSettingsReadNotify, ainSettingsReadNotifyRaisable, ainSettingsStorage,
                                               ainSettingsStorageSettable, storageUpdatedNotify, _ainsCounterRaisable,
                                               _commandSenderHost, _targetAddressHost, _notifySendingEnabled,
                                               _engineSettingsReader,
                                               _engineSettingsWriter,
                                               _engineSettingsReadNotify,
                                               _engineSettingsReadNotifyRaisable,
                                               _engineSettingsStorage,
                                               _engineSettingsStorageSettable,
                                               _engineSettingsStorageUpdatedNotify,
                                               _debugLogger); // TODO: can be moved to app.xaml.cs if needed

            ArchiveVm = new ArchivesViewModel(
                new ArchiveViewModel(_commandSenderHost, _targetAddressHost, _uiRoot, _logger, _notifySendingEnabled,
                                     0),
                new ArchiveViewModel(_commandSenderHost, _targetAddressHost, _uiRoot, _logger, _notifySendingEnabled,
                                     1));

            MnemonicChemeVm =
                new MnemonicChemeViewModel(Path.Combine(Environment.CurrentDirectory, "mnemoniccheme.png"));
            OldLookVm = new OldLookViewModel(_uiRoot, windowSystem, _commandSenderHost, _targetAddressHost,
                                             _notifySendingEnabled, this, _logger, _debugLogger, _cycleThreadHolder, _ainsCounterRaisable,
                                             _paramLogger, ainSettingsStorage, storageUpdatedNotify);

            _ain1StateColor = Colors.Gray;
            _ain2StateColor = Colors.Gray;
            _ain3StateColor = Colors.Gray;

            _ain1IsUsed = true;
            _ain2IsUsed = false;
            _ain3IsUsed = false;

            _ainsCounterRaisable.AinsCountInSystemHasBeenChanged += ainsCount =>
            {
                switch (ainsCount)
                {
                case 1:
                    Ain1IsUsed = true;
                    Ain2IsUsed = false;
                    Ain3IsUsed = false;
                    break;

                case 2:
                    Ain1IsUsed = true;
                    Ain2IsUsed = true;
                    Ain3IsUsed = false;
                    break;

                case 3:
                    Ain1IsUsed = true;
                    Ain2IsUsed = true;
                    Ain3IsUsed = true;
                    break;

                default:
                    throw new Exception("Такое число АИН в системе не поддерживается");
                }
            };

            AinCommandAndCommonTelemetryVm.AinsLinkInformationHasBeenUpdated += (ain1Error, ain2Error, ain3Error) =>
            {
                Ain1StateColor = ain1Error.HasValue ? ain1Error.Value ? Colors.Red : Colors.YellowGreen : Colors.Gray;
                Ain2StateColor = ain2Error.HasValue ? ain2Error.Value ? Colors.Red : Colors.YellowGreen : Colors.Gray;
                Ain3StateColor = ain3Error.HasValue ? ain3Error.Value ? Colors.Red : Colors.YellowGreen : Colors.Gray;
            };

            _notifySendingEnabled.SendingEnabledChanged += isEnabled =>
            {
                // TODO: execution in ui thread
                Ain1StateColor = Colors.Gray;
                Ain2StateColor = Colors.Gray;
                Ain3StateColor = Colors.Gray;
            };

            EngineAutoSetupVm = new EngineAutoSetupViewModel(
                new TableViewModel("Начальные значения:", _logger),
                new TableViewModel("После тестирования:", _logger),
                _notifySendingEnabled, _ainSettingsReader, _ainSettingsReadNotify, _ainSettingsWriter, _uiRoot, _logger,
                _commandSenderHost, _targetAddressHost, bsEthernetReadCycleModel);

            _logger.Log("Программа загружена");
        }