using System; using System.Collections.Generic; using System.Linq; using DTS.Common.Interface.DASFactory; using DTS.Common.Interface.DASFactory.Download; using DTS.Common.Utilities.Logging; using DTS.DASLib.Service; namespace DTS.Common.SerializationPlus { public class EventInfoAggregate : IEventInfoAggregate { public string EventId { get; set; } public string EventDescription { get; set; } public double DurationSeconds { get; set; } public string GUID { get; set; } public bool HasBeenDownloaded { get; set; } public bool WasTriggered { get; set; } public int NumberOfChannels { get; set; } public ulong NumberOfSamples { get; set; } public ulong NumberOfBytes { get; set; } public bool Faulted { get; set; } public int EventNumber { get; set; } private readonly Dictionary _eventIndices = new Dictionary(); private List _das = null; private Slice.Control.Event _event; public Slice.Control.Event GetEvent(bool bClear) { if (bClear) { _event = null; } return GetEvent(); } public Slice.Control.Event GetEvent() { if (null == _event) { _event = new Slice.Control.Event(GetDasList(), this); } return _event; } public int GetEventIndex(IDASCommunication idas) { if (!_eventIndices.ContainsKey(idas)) { APILogger.Log(string.Format("ERROR in GetEventIndex for {0}", idas.SerialNumber)); return -1; } return _eventIndices[idas]; } public List GetDasList() { if (null == _das) { _das = new List(); _das.AddRange(_eventIndices.Keys.ToArray()); } return _das; } public EventInfoAggregate(DownloadReport.EventInfo newEvent) { EventId = newEvent.TestID; EventDescription = newEvent.Description; //if newEvent doesn't have modules this will throw an exception, we don't want that //so populate the event here with no samples, no duration //I discovered this issue while working on //15575 No notification if unplug comm cable during downloading on G5 //if units do come back after disconnecting in the download step their module information may not be populated //and would previously crash if (newEvent.Modules.Any()) { DurationSeconds = newEvent.Modules[0].PreTriggerSeconds + newEvent.Modules[0].PostTriggerSeconds; } else { DurationSeconds = 0; } GUID = newEvent.TestGUID.ToString(); HasBeenDownloaded = newEvent.HasBeenDownloaded; WasTriggered = newEvent.WasTriggered; NumberOfChannels = newEvent.Modules.Sum(s => s.Channels.Length); if (newEvent.Modules.Any()) { NumberOfSamples = newEvent.Modules[0].NumberOfSamples; } else { NumberOfSamples = 0; } NumberOfBytes = NumberOfSamples * (ulong)NumberOfChannels * 2; Faulted = IsFaulted(newEvent); EventNumber = newEvent.EventNumber; if (newEvent.Modules.Any()) { _eventIndices.Add(((DASModule)newEvent.Modules[0]).OwningDAS, newEvent.EventNumber); } } /// /// returns true if faulted /// private bool IsFaulted(IEventInfo info) { return 0 != info.FaultFlags || 0 != info.FaultFlagsEx; } public void Add(IEventInfo newEvent) { if (newEvent.TestID != EventId) { APILogger.Log("Warning, expected matching test ids: ", newEvent.TestID, EventId); } if (newEvent.Description != EventDescription) { APILogger.Log("Warning, expected matching test descriptions: ", newEvent.Description); } var duration = newEvent.Modules[0].PreTriggerSeconds + newEvent.Modules[0].PostTriggerSeconds; if (duration != DurationSeconds) { APILogger.Log("Warning, durations don't match: ", duration, DurationSeconds); DurationSeconds = Math.Min(DurationSeconds, duration); } if (newEvent.TestGUID.ToString() != GUID) { APILogger.Log("Warning, expected test GUIDs to match,", GUID, newEvent.TestGUID.ToString()); } if (newEvent.HasBeenDownloaded != HasBeenDownloaded) { APILogger.Log("Warning, event has been downloaded from one unit, but not another"); } if (newEvent.WasTriggered != WasTriggered) { APILogger.Log("Warning, one unit has triggered, but another has not"); } var faulted = IsFaulted(newEvent); Faulted = Faulted || faulted; if (newEvent.Modules[0].NumberOfSamples != NumberOfSamples) { APILogger.Log("Warning, expected number of samples to match", NumberOfSamples, newEvent.Modules[0].NumberOfSamples); NumberOfSamples = Math.Min(NumberOfSamples, newEvent.Modules[0].NumberOfSamples); } if (newEvent.EventNumber != EventNumber) { APILogger.Log("Warning, expected event numbers to match", EventNumber, newEvent.EventNumber); } NumberOfChannels += newEvent.Modules.Sum(s => s.Channels.Length); NumberOfBytes = 2 * (ulong)NumberOfChannels * NumberOfSamples; //rather than using .Add we should use index assignment in case there are duplicate keys //I've eliminated the duplicate key that happened because of //http://manuscript.dts.local/f/cases/44357/TSR-AIR-GO-given-UDP-auto-discovery-is-disabled-manually-entered-ip-address-failed-matching-the-DAS-and-can-t-ARM //but this is better practice _eventIndices[((DASModule)newEvent.Modules[0]).OwningDAS] = newEvent.EventNumber; } } }