public void Add(IIdentifier psnLogInformationId, DateTime?beginTime, DateTime?endTime, DateTime?saveTime, PsnDataFragmentType psnDataType, bool isLastDeviceLog, IIdentifier deviceInformationId)
 {
     _relayOnStorage.Add(psnLogInformationId, beginTime, endTime, saveTime, psnDataType, isLastDeviceLog, deviceInformationId);
     _psnDataInformations.Add(
         new PsnDataInformationSimple(
             psnLogInformationId.IdentyString,
             beginTime,
             endTime,
             saveTime,
             psnDataType,
             isLastDeviceLog,
             deviceInformationId.IdentyString));
 }
Exemplo n.º 2
0
        // TODO: remove RPD logs

        public void SaveDataAsync(IRepository sourceRepo, IEnumerable <IPsnLog> psnLogs, IEnumerable <IFaultLog> rpdLogs, Action <OnCompleteEventArgs> onComplete, Action <OnProgressChangeEventArgs> onProgressChange)
        {
            // TODO: save rpdLogs
            if (!IsOpened)
            {
                _uiNotifier.Notify(() => onComplete(new OnCompleteEventArgs(OnCompleteEventArgs.CompleteResult.Error, "Сперва необходимо открыть репозиторий!")));
            }
            else
            {
                _backWorker.AddWork(
                    () => {
                    try {
                        var sourceSections = new List <ISection>();
                        foreach (var locomotive in sourceRepo.Locomotives)
                        {
                            sourceSections.AddRange(locomotive.Sections);
                        }

                        var pLogs = psnLogs.ToList();

                        int totalProceedCount       = 0;
                        double perLogPercent        = 0.0;
                        double lastInLogProgress    = 0.0;
                        double lastInLogProgressRaw = 0.0;
                        foreach (var psnLog in pLogs)
                        {
                            if (_psnDataInformtationStorage.PsnDataInformations.All(pdi => pdi.Id.ToString() != psnLog.Id.ToString()))
                            {
                                // TODO: плохо конечно, что я работаю с секциями и локомотивами репозитория-источника в фоновом потоке данного репозитория :)
                                // TODO: работать с ними в UI и ждать результата здесь!
                                var sourceSection    = sourceSections.First(s => s.Psns.Any(pl => pl.Id.ToString() == psnLog.Id.ToString()));
                                var sourceLocomotive = sourceRepo.Locomotives.First(l => l.Sections.Any(s => s == sourceSection));
                                //var devInfo = _deviceInformationStorage.DeviceInformations.First(di=>di.parentSection.DeviceInformationId);


                                // Пока у меня нет четкого (уникального) идентификатора устройства, поэтому всё определяет название локомотива и номер секции
                                if (!_deviceInformationStorage.DeviceInformations.Any(di => di.Name == sourceLocomotive.Name && di.Description == sourceSection.Name))
                                {
                                    // если нету описания устройства с такими секцией и локомотивом, то добавим:
                                    _deviceInformationStorage.Add(new IdentifierStringToLowerBased(sourceSection.DeviceInformationId.UnicString), sourceLocomotive.Name, sourceSection.Name);
                                }
                                // localDevInfo - необходим для правильного указания deviceInformationId у сохроняемого psnDataInformtation
                                var localDevInfo = _deviceInformationStorage.DeviceInformations.First(di => di.Name == sourceLocomotive.Name && di.Description == sourceSection.Name);

                                double percent1 = perLogPercent;
                                _psnDataStorage.Add(
                                    new IdentifierStringToLowerBased(psnLog.Id.UnicString),
                                    new StreamReadableDataBasedOnObject(psnLog),
                                    progressPercentage => {
                                    // still the same thread
                                    lastInLogProgressRaw = progressPercentage;
                                    var progress         = percent1 + progressPercentage / pLogs.Count;
                                    lastInLogProgress    = progress;
                                    _uiNotifier.Notify(() => onProgressChange(new OnProgressChangeEventArgs((int)progress)));
                                });

                                _psnDataInformtationStorage.Add(
                                    new IdentifierStringToLowerBased(psnLog.Id.UnicString),
                                    psnLog.BeginTime,
                                    psnLog.EndTime,
                                    DateTime.Now,
                                    new PsnDataFragmentTypeBuilderFromHighLevel(psnLog.LogType).Build(),
                                    psnLog.IsLastDeviceLog,
                                    localDevInfo.Id);

                                _psnDataCustomConfigurationsStorage.Add(
                                    new IdentifierStringToLowerBased(psnLog.Id.UnicString),
                                    new IdentifierStringToLowerBased(psnLog.PsnConfiguration.Id.ToString().ToLower()),
                                    psnLog.Name);
                            }
                            totalProceedCount++;
                            perLogPercent   = totalProceedCount * 100.0 / pLogs.Count;
                            double percent2 = perLogPercent;
                            _uiNotifier.Notify(() => onProgressChange(new OnProgressChangeEventArgs((int)percent2)));
                        }
                        UpdateLocomotivesIfAddedUnsafe();

                        _uiNotifier.Notify(() => onComplete(new OnCompleteEventArgs(OnCompleteEventArgs.CompleteResult.Ok, "Данные успешно сохранены")));
                    }
                    catch (Exception ex) {
                        var message = "Ошибка сохранения данных: " + ex.Message;
                        _uiNotifier.Notify(() => onComplete(new OnCompleteEventArgs(OnCompleteEventArgs.CompleteResult.Error, message)));
                    }
                });
            }
        }