using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core;
using Tango.Core.Threading;
using Tango.FSE.Common;
using Tango.FSE.Common.Connection;
using Tango.FSE.Common.Diagnostics;
using Tango.PMR.Diagnostics;
using Tango.Settings;
namespace Tango.FSE.UI.Diagnostics
{
///
/// Represents the default implementation.
///
///
///
public class DefaultDiagnosticsProvider : ExtendedObject, IDiagnosticsProvider
{
private IntervalMessageDispatcher _frameDispatcher;
private IMachineProvider _machineProvider;
#region Events
///
/// Occurs when a diagnostics frame is available.
///
public event EventHandler FrameReceived;
#endregion
#region Properties
///
/// Gets the current diagnostics frame.
///
public DiagnosticsFrame CurrentFrame { get; private set; }
private int _frameRate;
///
/// Gets the current frame rate.
///
public int FrameRate
{
get { return _frameRate; }
set { _frameRate = value; RaisePropertyChangedAuto(); }
}
///
/// Gets or sets the diagnostics frames throttling mode.
///
public DiagnosticsThrottlingMode ThrottlingMode { get; set; }
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// The machine provider.
public DefaultDiagnosticsProvider(IMachineProvider machineProvider)
{
_machineProvider = machineProvider;
ThrottlingMode = DiagnosticsThrottlingMode.Direct; //SettingsManager.Default.GetOrCreate().DiagnosticsThrottlingMode;
machineProvider.MachineOperator.DiagnosticsDataAvailable += MachineOperator_DiagnosticsDataAvailable;
_frameDispatcher = new IntervalMessageDispatcher(PostDiagnostics);
_frameDispatcher.DriftCompensationInterval = (int)TimeSpan.FromSeconds(30).TotalMilliseconds;
_frameDispatcher.Start();
}
#endregion
#region Private Methods
private void MachineOperator_DiagnosticsDataAvailable(object sender, StartDiagnosticsResponse diagnostics)
{
if (ThrottlingMode == DiagnosticsThrottlingMode.Direct)
{
PostDiagnostics(diagnostics);
}
else
{
_frameDispatcher.Interval = (int)diagnostics.ElapsedMilli;
_frameDispatcher.Push(diagnostics);
}
}
private void PostDiagnostics(StartDiagnosticsResponse data)
{
if (data.Monitors == null || _machineProvider.MachineOperator.DeviceInformation == null) return;
FrameRate = (int)data.ElapsedMilli;
uint interval = _machineProvider.MachineOperator.DeviceInformation.DiagnosticsInterval;
DateTime diagnosticsTime = DateTime.ParseExact(data.DateTime, "MM/dd/yyyy HH:mm:ss.fff", null);
TimeSpan delta = TimeSpan.FromMilliseconds(data.ElapsedMilli);
var frame = new DiagnosticsFrame()
{
Data = data,
DiagnosticsTime = diagnosticsTime,
Delta = delta,
Interval = interval
};
FrameReceived?.Invoke(this, new DiagnosticsFrameReceivedEventArgs()
{
Frame = frame,
});
CurrentFrame = frame;
}
#endregion
}
}