mirror of
https://github.com/vdemydiuk/mtapi.git
synced 2026-08-21 06:38:11 +00:00
Changed design of monitors for better extensibility
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
|
||||
namespace MtApi.Monitors.Triggers
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for triggers which can be used to trigger a <see cref="MtMonitorBase"/>.
|
||||
/// </summary>
|
||||
public interface IMonitorTrigger
|
||||
{
|
||||
/// <summary>
|
||||
/// Event will be called if the trigger raised.
|
||||
/// </summary>
|
||||
event EventHandler Raised;
|
||||
/// <summary>
|
||||
/// Returns whether the trigger is started
|
||||
/// </summary>
|
||||
bool IsStarted { get; }
|
||||
/// <summary>
|
||||
/// Stops the trigger and prevents further calls of the <see cref="Raised"/> event.
|
||||
/// </summary>
|
||||
void Stop();
|
||||
/// <summary>
|
||||
/// Starts the trigger.
|
||||
/// </summary>
|
||||
void Start();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
|
||||
namespace MtApi.Monitors.Triggers
|
||||
{
|
||||
/// <summary>
|
||||
/// Raises the <see cref="Raised"/> event if a bar is closed and a new one started.
|
||||
/// </summary>
|
||||
public class NewBarTrigger : IMonitorTrigger
|
||||
{
|
||||
#region Fields
|
||||
private volatile bool _isStarted;
|
||||
private readonly MtApiClient _apiClient;
|
||||
#endregion
|
||||
|
||||
public bool IsStarted => _isStarted;
|
||||
public event EventHandler Raised;
|
||||
public NewBarTrigger(MtApiClient apiClient)
|
||||
{
|
||||
_apiClient = apiClient;
|
||||
_apiClient.OnLastTimeBar += _apiClient_OnLastTimeBar;
|
||||
}
|
||||
|
||||
private void _apiClient_OnLastTimeBar(object sender, TimeBarArgs e)
|
||||
{
|
||||
if (_isStarted)
|
||||
Raised?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Start() => SetIsStarted(true);
|
||||
public void Stop() => SetIsStarted(false);
|
||||
private void SetIsStarted(bool value)
|
||||
{
|
||||
if (value != _isStarted)
|
||||
{
|
||||
_isStarted = value;
|
||||
if (value)
|
||||
_apiClient.OnLastTimeBar += _apiClient_OnLastTimeBar;
|
||||
else
|
||||
_apiClient.OnLastTimeBar -= _apiClient_OnLastTimeBar;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Timers;
|
||||
|
||||
namespace MtApi.Monitors.Triggers
|
||||
{
|
||||
public class TimeElapsedTrigger : IMonitorTrigger
|
||||
{
|
||||
readonly Timer _timer;
|
||||
|
||||
public event EventHandler Raised;
|
||||
/// <summary>
|
||||
/// Interval for raising the trigger
|
||||
/// </summary>
|
||||
public TimeSpan Interval
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(_timer.Interval);
|
||||
set => _timer.Interval = value.TotalMilliseconds;
|
||||
}
|
||||
public bool IsStarted => _timer.Enabled;
|
||||
|
||||
public bool AutoReset { get => _timer.AutoReset; set => _timer.AutoReset = value; }
|
||||
|
||||
public TimeElapsedTrigger(TimeSpan time, bool autoReset = true)
|
||||
{
|
||||
_timer = new Timer(time.TotalMilliseconds);
|
||||
_timer.Elapsed += _timer_Elapsed;
|
||||
AutoReset = autoReset;
|
||||
}
|
||||
|
||||
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
_timer.Elapsed -= _timer_Elapsed;
|
||||
Raised?.Invoke(this, EventArgs.Empty);
|
||||
_timer.Elapsed += _timer_Elapsed;
|
||||
}
|
||||
public void Stop() => _timer.Stop();
|
||||
|
||||
public void Start() => _timer.Start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user