< Summary - ReFlex - Library

Information
Class: Implementation.Components.TimerLoop
Assembly: ReFlex.Core.Implementation
File(s): D:\a\reflex\reflex\library\src\Core\Implementation\Components\TimerLoop.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 52
Coverable lines: 52
Total lines: 97
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 30
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
get_IntervalLength()100%210%
get_IsLoopRunning()0%620%
set_IsLoopRunning(...)0%620%
Run()0%620%
Stop()0%620%
OnTimerTick()0%506220%

File(s)

D:\a\reflex\reflex\library\src\Core\Implementation\Components\TimerLoop.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Timers;
 5using Implementation.Interfaces;
 6using ReFlex.Core.Common.Components;
 7using ReFlex.Core.Common.Util;
 8
 9namespace Implementation.Components
 10{
 11    public class TimerLoop : ITimerLoop
 12    {
 13        private readonly INetworkManager _networkManager;
 14        private readonly IInteractionManager _interactionManager;
 15        private readonly ICalibrationManager _calibrationManager;
 16        private readonly Timer _timer;
 17
 018        private bool _interactionsProcessing = false;
 19
 20        public event EventHandler<bool> IsLoopRunningChanged;
 21
 022        public int IntervalLength { get; set; } = 100;
 23
 24        public bool IsLoopRunning
 25        {
 026            get => _timer != null && _timer.Enabled;
 27            set
 028            {
 029                if (value)
 030                    Run();
 31                else
 032                    Stop();
 033            }
 34        }
 35
 036        public TimerLoop(INetworkManager networkManager, IInteractionManager interactionManager,
 037            ICalibrationManager calibrationManager)
 038        {
 039            _networkManager = networkManager;
 040            _interactionManager = interactionManager;
 041            _calibrationManager = calibrationManager;
 042            _timer = new Timer(IntervalLength);
 043            _interactionsProcessing = false;
 044        }
 45
 46        public void Run()
 047        {
 048            _interactionsProcessing = false;
 49
 050            if (_networkManager is null)
 051                return;
 52
 053            _timer.Interval = IntervalLength;
 054            _timer.AutoReset = true;
 055            _timer.Elapsed += OnTimerTick;
 056            _timer.Start();
 057        }
 58
 59        public void Stop()
 060        {
 061            _interactionsProcessing = false;
 062            if (_timer is null)
 063                return;
 64
 065            _timer.Stop();
 066            _timer.Elapsed -= OnTimerTick;
 067        }
 68
 69        private async void OnTimerTick(object sender, EventArgs args)
 070        {
 071            if (_interactionManager == null || _interactionsProcessing)
 072                return;
 73
 074            _interactionsProcessing = true;
 75
 076            var updateStatus = await _interactionManager.Update();
 077            _interactionsProcessing = false;
 078            if (updateStatus == ProcessServiceStatus.Error)
 079            {
 80
 081                Stop();
 082                IsLoopRunningChanged?.Invoke(this, IsLoopRunning);
 083            }
 84
 085            var stream = _interactionManager?.Interactions?.ToList();
 86
 87
 088            if (stream != null)
 089            {
 090                for (var i = 0; i < stream.Count; i++)
 091                    stream[i] = _calibrationManager?.Calibrate(stream[i]);
 092            }
 93
 094            _networkManager?.Broadcast(stream ?? new List<Interaction>());
 095        }
 96    }
 97}