| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Timers; |
| | | 5 | | using Implementation.Interfaces; |
| | | 6 | | using ReFlex.Core.Common.Components; |
| | | 7 | | using ReFlex.Core.Common.Util; |
| | | 8 | | |
| | | 9 | | namespace 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 | | |
| | 0 | 18 | | private bool _interactionsProcessing = false; |
| | | 19 | | |
| | | 20 | | public event EventHandler<bool> IsLoopRunningChanged; |
| | | 21 | | |
| | 0 | 22 | | public int IntervalLength { get; set; } = 100; |
| | | 23 | | |
| | | 24 | | public bool IsLoopRunning |
| | | 25 | | { |
| | 0 | 26 | | get => _timer != null && _timer.Enabled; |
| | | 27 | | set |
| | 0 | 28 | | { |
| | 0 | 29 | | if (value) |
| | 0 | 30 | | Run(); |
| | | 31 | | else |
| | 0 | 32 | | Stop(); |
| | 0 | 33 | | } |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | public TimerLoop(INetworkManager networkManager, IInteractionManager interactionManager, |
| | 0 | 37 | | ICalibrationManager calibrationManager) |
| | 0 | 38 | | { |
| | 0 | 39 | | _networkManager = networkManager; |
| | 0 | 40 | | _interactionManager = interactionManager; |
| | 0 | 41 | | _calibrationManager = calibrationManager; |
| | 0 | 42 | | _timer = new Timer(IntervalLength); |
| | 0 | 43 | | _interactionsProcessing = false; |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | public void Run() |
| | 0 | 47 | | { |
| | 0 | 48 | | _interactionsProcessing = false; |
| | | 49 | | |
| | 0 | 50 | | if (_networkManager is null) |
| | 0 | 51 | | return; |
| | | 52 | | |
| | 0 | 53 | | _timer.Interval = IntervalLength; |
| | 0 | 54 | | _timer.AutoReset = true; |
| | 0 | 55 | | _timer.Elapsed += OnTimerTick; |
| | 0 | 56 | | _timer.Start(); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | public void Stop() |
| | 0 | 60 | | { |
| | 0 | 61 | | _interactionsProcessing = false; |
| | 0 | 62 | | if (_timer is null) |
| | 0 | 63 | | return; |
| | | 64 | | |
| | 0 | 65 | | _timer.Stop(); |
| | 0 | 66 | | _timer.Elapsed -= OnTimerTick; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | private async void OnTimerTick(object sender, EventArgs args) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (_interactionManager == null || _interactionsProcessing) |
| | 0 | 72 | | return; |
| | | 73 | | |
| | 0 | 74 | | _interactionsProcessing = true; |
| | | 75 | | |
| | 0 | 76 | | var updateStatus = await _interactionManager.Update(); |
| | 0 | 77 | | _interactionsProcessing = false; |
| | 0 | 78 | | if (updateStatus == ProcessServiceStatus.Error) |
| | 0 | 79 | | { |
| | | 80 | | |
| | 0 | 81 | | Stop(); |
| | 0 | 82 | | IsLoopRunningChanged?.Invoke(this, IsLoopRunning); |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | var stream = _interactionManager?.Interactions?.ToList(); |
| | | 86 | | |
| | | 87 | | |
| | 0 | 88 | | if (stream != null) |
| | 0 | 89 | | { |
| | 0 | 90 | | for (var i = 0; i < stream.Count; i++) |
| | 0 | 91 | | stream[i] = _calibrationManager?.Calibrate(stream[i]); |
| | 0 | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | _networkManager?.Broadcast(stream ?? new List<Interaction>()); |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |