Пример #1
0
        static async Task <int> ShowFreq(ShowFreqOptions opts)
        {
            var taskHelper = SystemTaskHelper.Instance;
            var dir        = new SystemDirectory(taskHelper, opts.DataDirectory);
            var reader     = new Freq.Reader(taskHelper, dir);
            var count      = opts.Count;
            var totalCount = (int)await reader.CountAsync();

            Console.WriteLine($"Grid Frequency count: {totalCount}");
            Console.WriteLine($"Latest {count} data readings:");
            var data = await reader.ReadAsync(totalCount - count, totalCount);

            data.ForEach(r => Console.WriteLine(r));
            return(0);
        }
Пример #2
0
        private async Task Start0(bool startImmediately, CancellationToken ct)
        {
            bool     wait         = !startImmediately;
            Duration getOfs       = Duration.Zero;
            Duration?waitOverride = null;

            while (true)
            {
                if (wait)
                {
                    await _scheduler.ScheduleOne(waitOverride ?? Duration.FromMinutes(2), Duration.FromSeconds(4), ct).ConfigureAwait(_taskHelper);
                }
                wait = true;
                var count = (int)await _reader.CountAsync(ct).ConfigureAwait(_taskHelper);

                Instant now = _time.GetCurrentInstant();
                Instant from;
                if (count > 0)
                {
                    var last = await(await _reader.ReadAsync(count - 1).ConfigureAwait(_taskHelper)).Last().ConfigureAwait(_taskHelper);
                    from = last.Update + getOfs;
                }
                else
                {
                    from = now - Duration.FromHours(1);
                }
                Instant to = from + Duration.FromHours(1);
                if (to > now)
                {
                    to = now;
                }
                var data = await _freq.GetAsync(from + Duration.FromSeconds(1), to, ct).ConfigureAwait(_taskHelper);

                if (data.Count == 0 && to < now - Duration.FromDays(1))
                {
                    // If no data, and we're at least a day in the past, then skip forward an hour.
                    // This is probably because data is missing.
                    getOfs      += Duration.FromHours(1);
                    waitOverride = Duration.FromSeconds(20);
                }
                else
                {
                    getOfs       = Duration.Zero;
                    waitOverride = null;
                }
                await _writer.AppendAsync(data, ct).ConfigureAwait(_taskHelper);
            }
        }
Пример #3
0
        public void Fetch()
        {
            TimeRunner.Run(async(time, th) =>
            {
                var downloader = new FakeElexonDownloader();
                var dir        = new FakeDirectory(th);
                var fetcher    = new FetchFreq(th, downloader, dir, time);

                var cts     = new CancellationTokenSource();
                Task unused = th.Run(async() =>
                {
                    await th.Delay(NodaTime.Duration.FromMinutes(3)).ConfigureAwait(th);
                    cts.Cancel();
                });
                await fetcher.Start(false, cts.Token).ConfigureAwaitHideCancel(th);

                var reader = new Freq.Reader(th, dir);
                Assert.Equal((60 + 2) * 4, await reader.CountAsync().ConfigureAwait(th));
                var data = await(await reader.ReadAsync().ConfigureAwait(th)).ToList().ConfigureAwait(th);
                Assert.Equal((60 + 2) * 4, data.Count);
                Assert.Equal(new Freq.Data(NodaTime.Instant.FromUtc(2017, 8, 2, 11, 0, 15), Frequency.FromHertz(49.910)), data.First());
                Assert.Equal(new Freq.Data(NodaTime.Instant.FromUtc(2017, 8, 2, 12, 2, 0), Frequency.FromHertz(50.040)), data.Last());
            }, startInstant: NodaTime.Instant.FromUtc(2017, 8, 2, 12, 0));
        }