コード例 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="QboxNextDataHandler"/> class.
        /// </summary>
        /// <param name="correlationId">The correlation identifier.</param>
        /// <param name="context">The context.</param>
        /// <param name="parserFactory">The parser factory.</param>
        /// <param name="counterService">The counter service.</param>
        /// <param name="stateStoreService">The state store service.</param>
        /// <param name="dateTimeService">The DateTimeService</param>
        /// <param name="logger">The logger.</param>
        public QboxNextDataHandler(
            [NotNull] string correlationId,
            [NotNull] QboxDataDumpContext context,
            [NotNull] IParserFactory parserFactory,
            [NotNull] ICounterStoreService counterService,
            [NotNull] IStateStoreService stateStoreService,
            [NotNull] IDateTimeService dateTimeService,
            [NotNull] ILogger <QboxNextDataHandler> logger)
        {
            Guard.IsNotNullOrEmpty(correlationId, nameof(correlationId));
            Guard.IsNotNull(context, nameof(context));
            Guard.IsNotNull(parserFactory, nameof(parserFactory));
            Guard.IsNotNull(counterService, nameof(counterService));
            Guard.IsNotNull(stateStoreService, nameof(stateStoreService));
            Guard.IsNotNull(logger, nameof(logger));
            Guard.IsNotNull(dateTimeService, nameof(dateTimeService));

            _correlationId     = correlationId;
            _context           = context;
            _parserFactory     = parserFactory;
            _counterService    = counterService;
            _stateStoreService = stateStoreService;
            _logger            = logger;
            _dateTimeService   = dateTimeService;
        }
コード例 #2
0
        private static async Task RunAsync(ILogger logger, ICounterStoreService service, string path)
        {
            int    roundAtMinutes = 5;
            string sn             = Path.GetFileNameWithoutExtension(path).Substring(0, 13);

            string[] counterIds = { "0181", "0182", "0281", "0282", "2421" };

            var list = new Dictionary <string, IList <CounterData> >();

            foreach (var counterId in counterIds)
            {
                var result = ReadFile(logger, path + counterId + ".qbx", sn, int.Parse(counterId));

                list.Add(counterId, result);
            }

            int batchValue = 12345;
            var values     = list.Values.SelectMany(v => v).ToList();
            var f          = values.First();
            var l          = values.Last();

            logger.LogInformation($"First: {f} and Last = {l}");

            var grouped = from v in values
                          group v by new
            {
                v.SerialNumber,
                v.CounterId,
                MeasureTimeRounded = v.MeasureTime.Truncate(TimeSpan.FromMinutes(roundAtMinutes))
            }
            into g
                select new CounterData
            {
                SerialNumber = g.Key.SerialNumber,
                CounterId    = g.Key.CounterId,
                MeasureTime  = g.Key.MeasureTimeRounded,
                PulseValue   = g.Max(s => s.PulseValue)
            };

            var sorted           = grouped.OrderBy(k => k.MeasureTime).ToList();
            var sortedAndGrouped = sorted.GroupBy(s => s.MeasureTime).ToList();

            Console.WriteLine($"values           Count = {values.Count}");
            Console.WriteLine($"sorted           Count = {sorted.Count}");
            Console.WriteLine($"sortedAndGrouped Count = {sortedAndGrouped.Count}");

            int i     = 1;
            var batch = new List <CounterData>();

            foreach (var grp in sortedAndGrouped)
            {
                string guid = Guid.NewGuid().ToString();
                foreach (var x in grp)
                {
                    if (batch.Count < batchValue)
                    {
                        batch.Add(x);
                    }
                    else
                    {
                        Console.WriteLine($"{(100.0 * i) / sortedAndGrouped.Count:F}%");

                        await service.StoreAsync(guid, batch);

                        batch.Clear();
                    }
                }

                i = i + 1;
            }
        }