using RealTimeGraphX.DataPoints; using RealTimeGraphX.WPF; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; using Tango.BL.Enumerations; using Tango.FSE.Common.Diagnostics; using Tango.FSE.Diagnostics.Project.Widgets.RealTimeGraph; namespace Tango.FSE.Diagnostics.Project.Widgets.RealTimeGraphMultiChannel { [Description("Multi Series Graph")] public class RealTimeGraphMultiChannelWidget : RealTimeGraphWidgetBase { public RealTimeGraphMultiChannelWidget() { Monitor = TechMonitors.DispensersPressure; } public async override Task Init() { await base.Init(); if (!TechMonitor.MultiChannel) { throw new InvalidOperationException($"Cannot initialize a real-time graph multi channel widget using the single channel monitor '{Monitor}'."); } Controller.DataSeriesCollection.Clear(); for (int i = 0; i < TechMonitor.ChannelCount; i++) { Controller.DataSeriesCollection.Add(new WpfGraphDataSeries() { Name = $"{TechMonitor.Name.First()}{i + 1}", Stroke = Colors.DimGray, }); } MachineProvider.MachineConnected -= MachineProvider_MachineConnected; MachineProvider.MachineConnected += MachineProvider_MachineConnected; ConfigureByConnectedMachine(); } private void ConfigureByConnectedMachine() { if (MachineProvider.IsConnected && MachineProvider.Machine != null) { try { Controller.DataSeriesCollection.ToList().ForEach(x => x.IsVisible = false); foreach (var pack in MachineProvider.Machine.Configuration.NoneEmptyIdsPacks.OrderBy(x => x.PackIndex).ToList()) { Color color = Colors.DimGray; if (pack.LiquidType.LiquidTypeColor == Colors.Black) { color = Colors.Gray; } else if (pack.LiquidType.LiquidTypeColor == Colors.Transparent) { color = Colors.White; } else { color = pack.LiquidType.LiquidTypeColor; } Controller.DataSeriesCollection[pack.PackIndex].Name = pack.LiquidType.Name; Controller.DataSeriesCollection[pack.PackIndex].Stroke = color; Controller.DataSeriesCollection[pack.PackIndex].IsVisible = true; } } catch (Exception ex) { LogManager.Log(ex, $"Error initializing colors for real-time multi channel graph data series collection '{Monitor}'."); } } } public override FrameworkElement GetView() { return new RealTimeGraphMultiChannelWidgetView(); } private void MachineProvider_MachineConnected(object sender, Common.Connection.MachineConnectedEventArgs e) { ConfigureByConnectedMachine(); } public override void OnDiagnosticsData(DiagnosticsPackage package) { var matrix = package.GetMonitorMatrix(Monitor); if (matrix.Count > 0) { var firstPoints = matrix.First(); if (matrix.Any(x => x.Count > 0)) { List dates = new List(); DateTime dayStart = new DateTime(package.Frame.DiagnosticsTime.Year, package.Frame.DiagnosticsTime.Month, package.Frame.DiagnosticsTime.Day); TimeSpan offset = package.Frame.DiagnosticsTime - dayStart; for (int i = 0; i < firstPoints.Count; i++) { dates.Add( offset .Add(TimeSpan.FromMilliseconds(-package.Frame.Delta.TotalMilliseconds)) .Add(TimeSpan.FromMilliseconds(package.Frame.Delta.TotalMilliseconds * (i / (double)firstPoints.Count)))); } List> datesMatrix = new List>(); List> pointsMatrix = new List>(); foreach (var points in matrix) { datesMatrix.Add(dates); pointsMatrix.Add(points.Select(x => new DoubleDataPoint(x)).ToList()); } Controller.PushData(datesMatrix, pointsMatrix); } } } public override List> Components { get { return Services.TechComponentsService.Monitors.FindAll().Result.Where(x => x.MultiChannel).Select(x => new DiagnosticsWidgetComponent() { DisplayName = x.Description, Object = (TechMonitors)x.Code, }).OrderByAlphaNumeric(x => x.DisplayName).ToList(); } } } }